Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 88 additions & 99 deletions functions/compose-inference-cluster/function/fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
# Composed resource key for the backend XR.
BACKEND_RESOURCE_KEY = "serving-stack"

# Composed resource key for the ClusterProviderConfig that reaches the cluster.
CLUSTER_PROVIDER_CONFIG_RESOURCE_KEY = "cluster-provider-config-kubernetes"

# Composed resource key for the ClusterUsage that blocks the InferenceCluster's
# deletion while anything composes objects onto it.
#
Expand Down Expand Up @@ -358,6 +361,8 @@ def compose(self) -> None:
source = cluster.source
if not self.check_devices_supported(source):
return
if not self.check_pools_provisionable(source):
return

if source == CLUSTER_SOURCE_GKE:
self.compose_gke(cluster.gke)
Expand Down Expand Up @@ -779,6 +784,37 @@ def check_devices_supported(self, source: Cloud) -> bool:
response.fatal(self.rsp, msg)
return False

def check_pools_provisionable(self, source: Cloud) -> bool:
"""Gate on a node pool whose class has no provisioning block for the cloud.

The cloud cluster XR can't build a pool without one. Gating is a fatal
result for the same reason as check_devices_supported: a running
cluster whose shared class loses its block holds its resources rather
than having Crossplane collect them. Every offending class is named.
Existing never gates, because it provisions nothing.
"""
if source == CLUSTER_SOURCE_EXISTING:
return True
offending = [
f"InferenceClass {name} has no {source} provisioning block"
for name in sorted({p.className for p in self.xr.spec.nodePools or []})
if getattr(self.classes[name].spec.provisioning, source.lower(), None) is None
]
if not offending:
return True
msg = "; ".join(offending)
response.set_conditions(
self.rsp,
resource.Condition(
typ=CONDITION_TYPE_CLUSTER_READY,
status="False",
reason=CONDITION_REASON_INVALID_NODE_POOL,
message=msg,
),
)
response.fatal(self.rsp, msg)
return False

def compose_gke(self, gke: v1alpha1.Gke | None) -> None:
"""Compose an InferenceCluster backed by a Modelplane-provisioned
GKE cluster. Composes the GKECluster XR, waits for it to be ready,
Expand All @@ -797,7 +833,7 @@ def compose_gke(self, gke: v1alpha1.Gke | None) -> None:
sa_key = self.observed_gke_secret(_IDENTITY_TYPE_GCP)
backend_exists = BACKEND_RESOURCE_KEY in self.req.observed.resources

if gke_ready and kubeconfig_secret:
if kubeconfig_secret and self.keep_cluster_provider_config(cluster_ready=gke_ready):
self.compose_cluster_provider_config(
kubeconfig_secret.name,
kubeconfig_secret.key,
Expand Down Expand Up @@ -843,7 +879,7 @@ def compose_eks(self, eks: v1alpha1.Eks | None) -> None:
kubeconfig = self.observed_eks_secret(_SECRET_TYPE_KUBECONFIG)
backend_exists = BACKEND_RESOURCE_KEY in self.req.observed.resources

if eks_ready and kubeconfig:
if kubeconfig and self.keep_cluster_provider_config(cluster_ready=eks_ready):
self.compose_cluster_provider_config(kubeconfig.name, kubeconfig.key)

backend_secrets = self.resolve_eks_backend_secrets(eks_ready=eks_ready, backend_exists=backend_exists)
Expand Down Expand Up @@ -882,7 +918,7 @@ def compose_aks(self, aks: v1alpha1.Aks | None) -> None:
kubeconfig = self.observed_aks_secret(_SECRET_TYPE_KUBECONFIG)
backend_exists = BACKEND_RESOURCE_KEY in self.req.observed.resources

if aks_ready and kubeconfig:
if kubeconfig and self.keep_cluster_provider_config(cluster_ready=aks_ready):
self.compose_cluster_provider_config(kubeconfig.name, kubeconfig.key)

backend_secrets = self.resolve_aks_backend_secrets(aks_ready=aks_ready, backend_exists=backend_exists)
Expand Down Expand Up @@ -926,7 +962,7 @@ def compose_nebius(self, nebius: v1alpha1.Nebius | None) -> None:
credentials = self.observed_nebius_secret(_IDENTITY_TYPE_NEBIUS)
backend_exists = BACKEND_RESOURCE_KEY in self.req.observed.resources

if nebius_ready and kubeconfig:
if kubeconfig and self.keep_cluster_provider_config(cluster_ready=nebius_ready):
self.compose_cluster_provider_config(
kubeconfig.name,
kubeconfig.key,
Expand Down Expand Up @@ -970,7 +1006,7 @@ def compose_vultr(self, vultr: v1alpha1.Vultr | None) -> None:
kubeconfig = self.observed_vultr_secret(_SECRET_TYPE_KUBECONFIG)
backend_exists = BACKEND_RESOURCE_KEY in self.req.observed.resources

if vultr_ready and kubeconfig:
if kubeconfig and self.keep_cluster_provider_config(cluster_ready=vultr_ready):
self.compose_cluster_provider_config(kubeconfig.name, kubeconfig.key)

backend_secrets = self.resolve_vultr_backend_secrets(vultr_ready=vultr_ready, backend_exists=backend_exists)
Expand Down Expand Up @@ -1009,7 +1045,7 @@ def compose_civo(self, civo: v1alpha1.Civo | None) -> None:
kubeconfig = self.observed_civo_secret(_SECRET_TYPE_KUBECONFIG)
backend_exists = BACKEND_RESOURCE_KEY in self.req.observed.resources

if civo_ready and kubeconfig:
if kubeconfig and self.keep_cluster_provider_config(cluster_ready=civo_ready):
self.compose_cluster_provider_config(kubeconfig.name, kubeconfig.key)

backend_secrets = self.resolve_civo_backend_secrets(civo_ready=civo_ready, backend_exists=backend_exists)
Expand Down Expand Up @@ -1109,6 +1145,18 @@ def compose_serving_stack(
),
)

def keep_cluster_provider_config(self, *, cluster_ready: bool) -> bool:
"""Whether to compose the ClusterProviderConfig reaching a provisioned cluster.

It's first composed once the cloud cluster XR is Ready, then kept while
it's observed. The cloud cluster XR goes unready whenever any resource it
composes does, and every Object on the cluster - its namespaces,
PoolObservations, and every ModelReplica's and ModelRoute's - reaches it
through this ProviderConfig, so dropping it then would have Crossplane
delete a ProviderConfig they all still use.
"""
return cluster_ready or CLUSTER_PROVIDER_CONFIG_RESOURCE_KEY in self.req.observed.resources

def compose_cluster_provider_config(
self,
kubeconfig_name: str,
Expand Down Expand Up @@ -1151,10 +1199,10 @@ def compose_cluster_provider_config(
),
)
resource.update(
self.rsp.desired.resources["cluster-provider-config-kubernetes"],
self.rsp.desired.resources[CLUSTER_PROVIDER_CONFIG_RESOURCE_KEY],
cpc,
)
self.rsp.desired.resources["cluster-provider-config-kubernetes"].ready = fnv1.READY_TRUE
self.rsp.desired.resources[CLUSTER_PROVIDER_CONFIG_RESOURCE_KEY].ready = fnv1.READY_TRUE

def write_status(self, schedulable_pools: list[v1alpha1.SchedulablePool]) -> None:
"""Write the InferenceCluster status."""
Expand Down Expand Up @@ -1248,21 +1296,12 @@ def compose_gke_cluster(self, gke: v1alpha1.Gke) -> None:
gke_node_pools: list[gkev1alpha1.NodePool] = []

for pool in self.xr.spec.nodePools or []:
cls = self.classes.get(pool.className)
if not cls or not cls.spec.provisioning or not cls.spec.provisioning.gke:
msg = f"InferenceClass {pool.className} has no GKE provisioning block"
response.set_conditions(
self.rsp,
resource.Condition(
typ=CONDITION_TYPE_CLUSTER_READY,
status="False",
reason=CONDITION_REASON_INVALID_NODE_POOL,
message=msg,
),
)
response.warning(self.rsp, msg)
return
prov = cls.spec.provisioning.gke
provisioning = self.classes[pool.className].spec.provisioning
# Unreachable: check_pools_provisionable gates on every pool's
# class having this block.
if not provisioning or not provisioning.gke:
continue
prov = provisioning.gke
# A class with no accelerator describes a CPU pool: a standard
# node image, no GPU attachment, and the modelplane.ai/cpu
# label and taint composed by compose-gke-cluster.
Expand Down Expand Up @@ -1319,21 +1358,11 @@ def compose_eks_cluster(self, eks: v1alpha1.Eks) -> None:
eks_node_pools: list[eksv1alpha1.NodePool] = []

for pool in self.xr.spec.nodePools or []:
cls = self.classes.get(pool.className)
if not cls or not cls.spec.provisioning or not cls.spec.provisioning.eks:
msg = f"InferenceClass {pool.className} has no EKS provisioning block"
response.set_conditions(
self.rsp,
resource.Condition(
typ=CONDITION_TYPE_CLUSTER_READY,
status="False",
reason=CONDITION_REASON_INVALID_NODE_POOL,
message=msg,
),
)
response.warning(self.rsp, msg)
return
prov = cls.spec.provisioning.eks
provisioning = self.classes[pool.className].spec.provisioning
# Unreachable (see the GKE builder above).
if not provisioning or not provisioning.eks:
continue
prov = provisioning.eks
# A class with no accelerator describes a CPU pool (see the GKE
# builder above).
node_pool = eksv1alpha1.NodePool(
Expand Down Expand Up @@ -1398,21 +1427,11 @@ def compose_aks_cluster(self, aks: v1alpha1.Aks) -> None:
aks_node_pools: list[aksv1alpha1.NodePool] = []

for pool in self.xr.spec.nodePools or []:
cls = self.classes.get(pool.className)
if not cls or not cls.spec.provisioning or not cls.spec.provisioning.aks:
msg = f"InferenceClass {pool.className} has no AKS provisioning block"
response.set_conditions(
self.rsp,
resource.Condition(
typ=CONDITION_TYPE_CLUSTER_READY,
status="False",
reason=CONDITION_REASON_INVALID_NODE_POOL,
message=msg,
),
)
response.warning(self.rsp, msg)
return
prov = cls.spec.provisioning.aks
provisioning = self.classes[pool.className].spec.provisioning
# Unreachable (see the GKE builder above).
if not provisioning or not provisioning.aks:
continue
prov = provisioning.aks
# A class with no accelerator describes a CPU pool (see the GKE
# builder above).
node_pool = aksv1alpha1.NodePool(
Expand Down Expand Up @@ -1472,21 +1491,11 @@ def compose_nebius_cluster(self, nebius: v1alpha1.Nebius) -> None:
nebius_node_pools: list[nebiusv1alpha1.NodePool] = []

for pool in self.xr.spec.nodePools or []:
cls = self.classes.get(pool.className)
if not cls or not cls.spec.provisioning or not cls.spec.provisioning.nebius:
msg = f"InferenceClass {pool.className} has no Nebius provisioning block"
response.set_conditions(
self.rsp,
resource.Condition(
typ=CONDITION_TYPE_CLUSTER_READY,
status="False",
reason=CONDITION_REASON_INVALID_NODE_POOL,
message=msg,
),
)
response.warning(self.rsp, msg)
return
prov = cls.spec.provisioning.nebius
provisioning = self.classes[pool.className].spec.provisioning
# Unreachable (see the GKE builder above).
if not provisioning or not provisioning.nebius:
continue
prov = provisioning.nebius
# A class with no accelerator describes a CPU pool (see the GKE
# builder above). driversPreset rides the gpu block: a CPU
# platform preinstalls no NVIDIA stack.
Expand Down Expand Up @@ -1633,21 +1642,11 @@ def compose_vultr_cluster(self, vultr: v1alpha1.Vultr) -> None:
vultr_node_pools: list[vultrv1alpha1.NodePool] = []

for pool in self.xr.spec.nodePools or []:
cls = self.classes.get(pool.className)
if not cls or not cls.spec.provisioning or not cls.spec.provisioning.vultr:
msg = f"InferenceClass {pool.className} has no Vultr provisioning block"
response.set_conditions(
self.rsp,
resource.Condition(
typ=CONDITION_TYPE_CLUSTER_READY,
status="False",
reason=CONDITION_REASON_INVALID_NODE_POOL,
message=msg,
),
)
response.warning(self.rsp, msg)
return
prov = cls.spec.provisioning.vultr
provisioning = self.classes[pool.className].spec.provisioning
# Unreachable (see the GKE builder above).
if not provisioning or not provisioning.vultr:
continue
prov = provisioning.vultr
# A class with no accelerator describes a CPU pool (see the GKE
# builder above).
node_pool = vultrv1alpha1.NodePool(
Expand Down Expand Up @@ -1762,7 +1761,7 @@ def civo_nvlink_disabled_pools(self) -> list[str]:
setting to each listed pool through a dedicated NVIDIADriver, so
multi-GPU pools in the same cluster keep NVLink between their
GPUs. Pools whose class lacks a Civo provisioning block are
skipped here and reported by compose_civo_cluster."""
skipped here; check_pools_provisionable gates on them."""
pools: list[str] = []
for pool in self.xr.spec.nodePools or []:
cls = self.classes.get(pool.className)
Expand All @@ -1783,21 +1782,11 @@ def compose_civo_cluster(self, civo: v1alpha1.Civo) -> None:
civo_node_pools: list[civov1alpha1.NodePool] = []

for pool in self.xr.spec.nodePools or []:
cls = self.classes.get(pool.className)
if not cls or not cls.spec.provisioning or not cls.spec.provisioning.civo:
msg = f"InferenceClass {pool.className} has no Civo provisioning block"
response.set_conditions(
self.rsp,
resource.Condition(
typ=CONDITION_TYPE_CLUSTER_READY,
status="False",
reason=CONDITION_REASON_INVALID_NODE_POOL,
message=msg,
),
)
response.warning(self.rsp, msg)
return
prov = cls.spec.provisioning.civo
provisioning = self.classes[pool.className].spec.provisioning
# Unreachable (see the GKE builder above).
if not provisioning or not provisioning.civo:
continue
prov = provisioning.civo
# A class with no accelerator describes a CPU pool (see the GKE
# builder above).
node_pool = civov1alpha1.NodePool(
Expand Down
Loading
Loading