Skip to content

Commit

Permalink
update: kubernetes host is inferred from KUBERNETES_SERVICE_HOST and …
Browse files Browse the repository at this point in the history
…KUBERNETES_SERVICE_PORT.
  • Loading branch information
ltbringer committed Nov 4, 2024
1 parent 62d7f4b commit d555a58
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions python/ray/autoscaler/_private/kuberay/node_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@

RAY_HEAD_POD_NAME = os.getenv("RAY_HEAD_POD_NAME")

# https://kubernetes.io/docs/tasks/run-application/access-api-from-pod/#directly-accessing-the-rest-api
# While running in a Pod, your container can create an HTTPS URL for the Kubernetes API server by fetching the
# KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT_HTTPS environment variables.
KUBERNETES_SERVICE_HOST = os.getenv("KUBERNETES_SERVICE_HOST", "https://kubernetes.default")
KUBERNETES_SERVICE_PORT = os.getenv("KUBERNETES_SERVICE_PORT", "443")
KUBERNETES_HOST = f"{KUBERNETES_SERVICE_HOST}:{KUBERNETES_SERVICE_PORT}"
# Key for GKE label that identifies which multi-host replica a pod belongs to
REPLICA_INDEX_KEY = "replicaIndex"

Expand Down Expand Up @@ -177,23 +183,36 @@ def load_k8s_secrets() -> Tuple[Dict[str, str], str]:


def url_from_resource(
namespace: str, path: str, kuberay_crd_version: str = KUBERAY_CRD_VER
namespace: str, path: str, kuberay_crd_version: str = KUBERAY_CRD_VER, kubernetes_host: str = KUBERNETES_HOST
) -> str:
"""Convert resource path to REST URL for Kubernetes API server.
Args:
namespace: The K8s namespace of the resource
path: The part of the resource path that starts with the resource type.
Supported resource types are "pods" and "rayclusters".
kuberay_crd_version: The API version of the KubeRay CRD. Looks like "v1alpha1", "v1".
kubernetes_host: The host of the Kubernetes API server. Uses $KUBERNETES_SERVICE_HOST and
$KUBERNETES_SERVICE_PORT to construct the kubernetes_host if not provided.
When set by Kubernetes,
$KUBERNETES_SERVICE_HOST could be an IP address. That's why the https scheme is
added here.
Defaults to "https://kubernetes.default:443".
"""
if kubernetes_host.startswith("http://"):
raise ValueError("Kubernetes host must be accessed over HTTPS.")
if not kubernetes_host.startswith("https://"):
kubernetes_host = "https://" + kubernetes_host
if path.startswith("pods"):
api_group = "/api/v1"
elif path.startswith("rayclusters"):
api_group = "/apis/ray.io/" + kuberay_crd_version
else:
raise NotImplementedError("Tried to access unknown entity at {}".format(path))
return (
"https://kubernetes.default:443"
kubernetes_host
+ api_group
+ "/namespaces/"
+ namespace
Expand Down Expand Up @@ -520,4 +539,4 @@ def _get(self, path: str) -> Dict[str, Any]:

def _patch(self, path: str, payload: List[Dict[str, Any]]) -> Dict[str, Any]:
"""Wrapper for REST PATCH of resource with proper headers."""
return self.k8s_api_client.patch(path, payload)
return self.k8s_api_client.patch(path, payload)

0 comments on commit d555a58

Please sign in to comment.