-
Notifications
You must be signed in to change notification settings - Fork 290
Support proxy-based Prometheus discovery for Holmes #1977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aantn
wants to merge
1
commit into
master
Choose a base branch
from
codex/linear-mention-rob-71-holmes-extend-prometheus-toolset-di
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| import logging | ||
| import os | ||
| from dataclasses import dataclass | ||
| from typing import Dict, Optional | ||
|
|
||
| from kubernetes import client | ||
| from kubernetes.client import V1ServiceList | ||
|
|
@@ -7,9 +10,63 @@ | |
| from robusta.core.model.env_vars import CLUSTER_DOMAIN | ||
|
|
||
|
|
||
| @dataclass | ||
| class DiscoveredServiceUrl: | ||
| url: str | ||
| headers: Optional[Dict[str, str]] = None | ||
|
|
||
| def __str__(self) -> str: | ||
| return self.url | ||
|
|
||
|
|
||
| def _should_use_proxy() -> bool: | ||
| return not os.getenv("KUBERNETES_SERVICE_HOST") | ||
|
|
||
|
|
||
| def _derive_service_scheme(port) -> str: | ||
| port_name = (port.name or "").lower() | ||
| app_protocol = getattr(port, "app_protocol", None) or "" | ||
| app_protocol = app_protocol.lower() | ||
|
|
||
| if "https" in port_name or "https" in app_protocol or port.port == 443: | ||
| return "https" | ||
| return "http" | ||
|
|
||
|
|
||
| def _get_kube_proxy_headers() -> Optional[Dict[str, str]]: | ||
| try: | ||
| api_client = client.ApiClient() | ||
| auth_header = api_client.configuration.get_api_key_with_prefix("authorization") | ||
| headers: Dict[str, str] = {} | ||
| if auth_header: | ||
| headers["Authorization"] = auth_header | ||
| if api_client.configuration.default_headers: | ||
| headers.update(api_client.configuration.default_headers) | ||
| return headers or None | ||
| except Exception as e: | ||
| logging.debug(f"Unable to build Kubernetes proxy headers: {e}") | ||
| return None | ||
|
|
||
|
|
||
| def _build_proxy_url(name: str, namespace: str, port: int, scheme: str) -> Optional[str]: | ||
| try: | ||
| configuration = client.Configuration.get_default_copy() | ||
| except Exception as e: | ||
| logging.debug(f"Unable to load Kubernetes configuration for proxy url: {e}") | ||
| return None | ||
|
|
||
| host = (configuration.host or "").rstrip("/") | ||
| if not host: | ||
| return None | ||
|
|
||
| path = f"/api/v1/namespaces/{namespace}/services/{scheme}:{name}:{port}/proxy" | ||
| return f"{host}{path}" | ||
|
|
||
|
|
||
| def find_service_url(label_selector): | ||
| """ | ||
| Get the url of an in-cluster service with a specific label | ||
| Get the url of a service with a specific label. When running outside the cluster, | ||
| prefer the Kubernetes API proxy using the current user's credentials. | ||
| """ | ||
| # we do it this way because there is a weird issue with hikaru's ServiceList.listServiceForAllNamespaces() | ||
| v1 = client.CoreV1Api() | ||
|
|
@@ -19,7 +76,17 @@ def find_service_url(label_selector): | |
| svc: V1Service = svc_list.items[0] | ||
| name = svc.metadata.name | ||
| namespace = svc.metadata.namespace | ||
| port = svc.spec.ports[0].port | ||
| url = f"http://{name}.{namespace}.svc.{CLUSTER_DOMAIN}:{port}" | ||
| logging.info(f"discovered service with label-selector: `{label_selector}` at url: `{url}`") | ||
| return url | ||
| port_obj = svc.spec.ports[0] | ||
| port = port_obj.port | ||
|
Comment on lines
+79
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential IndexError if service has no ports. If 🔎 Proposed fix name = svc.metadata.name
namespace = svc.metadata.namespace
+ if not svc.spec.ports:
+ logging.warning(f"Service {name} in {namespace} has no ports defined")
+ return None
port_obj = svc.spec.ports[0]
port = port_obj.port
🤖 Prompt for AI Agents |
||
| scheme = _derive_service_scheme(port_obj) | ||
|
|
||
| cluster_local_url = f"{scheme}://{name}.{namespace}.svc.{CLUSTER_DOMAIN}:{port}" | ||
| proxy_url = _build_proxy_url(name, namespace, port, scheme) if _should_use_proxy() else None | ||
| headers = _get_kube_proxy_headers() if proxy_url else None | ||
| final_url = proxy_url or cluster_local_url | ||
|
|
||
| logging.info( | ||
| f"discovered service with label-selector: `{label_selector}` at url: `{final_url}` " | ||
| f"(proxy_used={proxy_url is not None})" | ||
| ) | ||
| return DiscoveredServiceUrl(url=final_url, headers=headers) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
Kubernetes API proxy URL format services /api/v1/namespaces services proxy💡 Result:
Kubernetes API server proxy URL patterns (service-level)
Base form:
/api/v1/namespaces/{namespace}/services/{service-ident}/proxy{/path...}.[1][2]
{service-ident} supported forms:
Examples:
You can append additional path/query after /proxy, e.g. /proxy/_search?q=foo or /proxy/api/v1/items. [1][3]
Notes:
Sources: Kubernetes API reference and docs; examples/discussion. [1] Kubernetes API reference — Proxy operations. [2] kubectl cluster-info / proxy examples and formats. [3] kubectl proxy docs. [4] community examples/Q&A showing https: prefix.
Fix proxy path format for HTTP services.
The proxy URL format is incorrect for HTTP services. Per Kubernetes API documentation, the service identifier should be:
{name}:{port}(no scheme prefix)https:{name}:{port}(with scheme prefix)The current code uses
{scheme}:{name}:{port}for all schemes, which generates invalid URLs for HTTP (e.g.,http:my-service:8080instead ofmy-service:8080). Conditional logic is needed: omit the scheme prefix for HTTP, include it only for HTTPS.🧰 Tools
🪛 Ruff (0.14.10)
54-54: Do not catch blind exception:
Exception(BLE001)
🤖 Prompt for AI Agents