Skip to content

Commit ac3f8eb

Browse files
authored
prometheus calls should be made with explicitly generated token (#353)
* prometheus calls should be made with explicitly generated token * address review comments * addressed review comments * updates
1 parent a0a2a53 commit ac3f8eb

File tree

1 file changed

+7
-30
lines changed

1 file changed

+7
-30
lines changed

ocp_utilities/monitoring.py

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import requests
88
from kubernetes.dynamic import DynamicClient
99
from ocp_resources.route import Route
10-
from ocp_resources.secret import Secret
11-
from ocp_resources.service_account import ServiceAccount
1210
from timeout_sampler import TimeoutExpiredError, TimeoutSampler
1311
from simple_logger.logger import get_logger
1412

@@ -36,61 +34,40 @@ class Prometheus(object):
3634

3735
def __init__(
3836
self,
37+
bearer_token: str,
3938
namespace: str = "openshift-monitoring",
4039
resource_name: str = "prometheus-k8s",
4140
client: DynamicClient = None,
4241
verify_ssl: bool = True,
43-
bearer_token: str = "",
4442
) -> None:
4543
"""
4644
Args:
45+
bearer_token (str, Required): Used for query OAuth with API endpoint, this needs to be created via oc
46+
create token command
47+
Example to create prometheus token: oc create token prometheus-k8s -n openshift-monitoring --duration=600s
48+
This would create a token for prometheus calls, that would expire in 600 seconds
4749
namespace (str): Prometheus API resource namespace
4850
resource_name (str): Prometheus API resource name
4951
client (DynamicClient): Admin client resource
5052
verify_ssl (bool): Perform SSL verification on query
51-
bearer_token (str): Used for query OAuth with API endpoint
5253
"""
5354
self.namespace = namespace
5455
self.resource_name = resource_name
5556
self.client = client or get_client()
5657
self.api_v1 = "/api/v1"
5758
self.verify_ssl = verify_ssl
5859
self.bearer_token = bearer_token
60+
5961
self.api_url = self._get_route()
60-
self.headers = self._get_headers()
62+
self.headers = {"Authorization": f"Bearer {self.bearer_token}"}
6163
self.scrape_interval = self.get_scrape_interval()
6264

6365
def _get_route(self) -> str:
6466
# get route to prometheus HTTP api
6567
LOGGER.info("Prometheus: Obtaining route")
6668
route = Route(namespace=self.namespace, name=self.resource_name, client=self.client).instance.spec.host
67-
6869
return f"https://{route}"
6970

70-
def _get_headers(self) -> Dict[str, str]:
71-
"""Uses the Prometheus serviceaccount to get an access token for OAuth if not given"""
72-
LOGGER.info("Setting Prometheus headers and Obtaining OAuth token")
73-
74-
if not self.bearer_token:
75-
secret = self._get_resource_secret()
76-
self.bearer_token = secret.instance.metadata.annotations["openshift.io/token-secret.value"]
77-
78-
return {"Authorization": f"Bearer {self.bearer_token}"}
79-
80-
def _get_service_account(self) -> ServiceAccount:
81-
"""get service account for the given namespace and resource"""
82-
83-
return ServiceAccount(namespace=self.namespace, name=self.resource_name, client=self.client)
84-
85-
def _get_resource_secret(self) -> Secret:
86-
"""secret for the service account extracted"""
87-
resource_sa = self._get_service_account()
88-
return Secret(
89-
namespace=self.namespace,
90-
name=resource_sa.instance.imagePullSecrets[0].name,
91-
client=self.client,
92-
)
93-
9471
def _get_response(self, query: str) -> Dict[str, Any]:
9572
response = requests.get(f"{self.api_url}{query}", headers=self.headers, verify=self.verify_ssl)
9673

0 commit comments

Comments
 (0)