Skip to content
Open
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
118 changes: 111 additions & 7 deletions functions/compose-inference-gateway/function/fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
CONDITION_REASON_GATEWAY_PROGRAMMED = "GatewayProgrammed"
CONDITION_REASON_WAITING_FOR_CLUSTER = "WaitingForCluster"
CONDITION_REASON_WAITING_FOR_GATEWAY = "WaitingForGateway"
CONDITION_REASON_WAITING_FOR_AI_GATEWAY_ROUTE = "WaitingForAIGatewayRoute"
CONDITION_REASON_WAITING_FOR_ENVOY_DEPLOYMENT = "WaitingForEnvoyDeployment"
CONDITION_REASON_CLUSTER_TAKEN = "ClusterAlreadyHasGateway"
CONDITION_REASON_SECRETS_MISSING = "SecretsMissing"
CONDITION_REASON_AUTH_NOT_ACCEPTED = "CallerAuthNotAccepted"
Expand Down Expand Up @@ -77,6 +79,13 @@
_FAILOVER_NAME = "inference-gateway-failover"
_CLIENT_TRAFFIC_NAME = "inference-gateway-client-traffic"
_REDIRECT_NAME = "inference-gateway-redirect"
_AI_GATEWAY_ANCHOR_NAME = "inference-gateway-anchor"
_AI_GATEWAY_ANCHOR_HEADER = "x-modelplane-gateway-anchor"
_AI_GATEWAY_ANCHOR_HEADER_VALUE = "true"
_ENVOY_GATEWAY_NAMESPACE = "envoy-gateway-system"
# Pin the name so readiness can observe the Deployment without depending on
# Envoy Gateway's generated hash.
_ENVOY_DEPLOYMENT_NAME = "envoy-modelplane-system-inference-gateway"

# The Gateway's listener names. When the gateway terminates TLS, inference binds
# to the HTTPS listener alone (compose-model-route sets the AIGatewayRoute's
Expand Down Expand Up @@ -151,6 +160,26 @@
"a.conditions.exists(c, c.type == 'Accepted' && c.status == 'True'))"
)

_AI_GATEWAY_ROUTE_ACCEPTED_CEL = (
"has(object.status) && has(object.status.conditions) && "
"object.status.conditions.exists(c, c.type == 'Accepted' && c.status == 'True')"
)

# The AI Gateway stamps the pod template when it has processed an active route.
# Require every existing pod to be updated, but allow a replica to be unavailable.
_ENVOY_DEPLOYMENT_ROLLOUT_READY_CEL = (
"has(object.metadata) && has(object.metadata.generation) && "
"has(object.spec) && has(object.spec.replicas) && has(object.spec.template) && "
"has(object.spec.template.metadata) && has(object.spec.template.metadata.annotations) && "
"'aigateway.envoyproxy.io/extproc-config-hash' in object.spec.template.metadata.annotations && "
"object.spec.template.metadata.annotations['aigateway.envoyproxy.io/extproc-config-hash'] != '' && "
"has(object.status) && "
"has(object.status.observedGeneration) && object.status.observedGeneration >= object.metadata.generation && "
"has(object.status.updatedReplicas) && has(object.status.replicas) && "
"object.status.updatedReplicas == object.status.replicas && "
"has(object.status.availableReplicas) && object.status.availableReplicas > 0"
)

# Kubernetes injects ndots:5 into every pod, which tells the resolver to try each
# search domain before a name with fewer than five dots. Every hostname this
# gateway resolves has fewer: a provider like api.together.xyz has two, a cluster
Expand Down Expand Up @@ -320,7 +349,9 @@ def compose(self) -> None:
self.resolve_secrets()
self.compose_secrets()
self.compose_envoy_proxy()
self.compose_envoy_deployment()
self.compose_gateway()
self.compose_ai_gateway_anchor()
self.compose_client_pki()
self.compose_cluster_names()
self.compose_caller_auth()
Expand Down Expand Up @@ -560,6 +591,7 @@ def compose_envoy_proxy(self) -> None:
"kubernetes": {
"envoyService": {"externalTrafficPolicy": "Cluster"},
"envoyDeployment": {
"name": _ENVOY_DEPLOYMENT_NAME,
"replicas": _PROXY_REPLICAS,
"patch": {"type": "StrategicMerge", "value": _NDOTS_PATCH},
"pod": {
Expand Down Expand Up @@ -596,6 +628,21 @@ def compose_envoy_proxy(self) -> None:
),
)

def compose_envoy_deployment(self) -> None:
"""Observe Envoy's Deployment so readiness includes its full rollout."""
obj = _k8s_object(
self.pc,
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {"name": _ENVOY_DEPLOYMENT_NAME, "namespace": _ENVOY_GATEWAY_NAMESPACE},
},
ready_when=_ENVOY_DEPLOYMENT_ROLLOUT_READY_CEL,
)
obj.spec.managementPolicies = ["Observe"]
obj.spec.watch = True
resource.update(self.rsp.desired.resources["envoy-deployment"], obj)

def compose_gateway(self) -> None:
"""The Gateway callers connect to.

Expand Down Expand Up @@ -667,6 +714,48 @@ def compose_gateway(self) -> None:
),
)

def compose_ai_gateway_anchor(self) -> None:
"""Keep AI Gateway's ext-proc installed before any model route exists."""
listener = _LISTENER_HTTPS if self.xr.spec.tls else _LISTENER_HTTP
resource.update(
self.rsp.desired.resources["ai-gateway-anchor"],
_k8s_object(
self.pc,
{
"apiVersion": "aigateway.envoyproxy.io/v1beta1",
"kind": "AIGatewayRoute",
"metadata": {"name": _AI_GATEWAY_ANCHOR_NAME, "namespace": REMOTE_NAMESPACE},
"spec": {
"parentRefs": [
{
"group": "gateway.networking.k8s.io",
"kind": "Gateway",
"name": _GATEWAY_NAME,
"namespace": REMOTE_NAMESPACE,
"sectionName": listener,
}
],
"rules": [
{
"matches": [
{
"headers": [
{
"type": "Exact",
"name": _AI_GATEWAY_ANCHOR_HEADER,
"value": _AI_GATEWAY_ANCHOR_HEADER_VALUE,
}
]
}
]
}
],
},
},
ready_when=_AI_GATEWAY_ROUTE_ACCEPTED_CEL,
),
)

def compose_client_pki(self) -> None:
"""Compose the CA whose certificates a cluster gateway trusts.

Expand Down Expand Up @@ -1330,16 +1419,16 @@ def shared_caller_key(self) -> tuple[str, str] | None:
return None

def derive_conditions(self) -> None:
"""GatewayReady tracks the Gateway being programmed and, where the XR
asks for auth, its caller policy being accepted.
"""GatewayReady tracks the Gateway, Envoy rollout and AI Gateway route,
and, where the XR asks for auth, its caller policy being accepted.

Both, because a gateway whose policy was rejected answers every request
with a 500 while its Gateway still has an address.

A missing input Secret is reported here (see resolve_secrets). The
Gateway's own readiness only tracks having an address, so neither a
fail-closed auth policy nor an unprogrammed HTTPS listener would
otherwise show.
Gateway's own readiness only tracks having an address, so an unaccepted
anchor route, fail-closed auth policy, or unprogrammed HTTPS listener
would otherwise show late or not at all.
"""
if self.xr.spec.auth and not self.caller_secrets:
if "caller-secrets" not in self.req.required_resources:
Expand All @@ -1366,9 +1455,12 @@ def derive_conditions(self) -> None:
"authentication policy and every request is refused",
)
return
readiness_keys = ["gateway", "ai-gateway-anchor", "envoy-deployment"]
if self.xr.spec.auth:
readiness_keys.append("caller-auth")
waiting = [
key
for key in (["gateway", "caller-auth"] if self.xr.spec.auth else ["gateway"])
for key in readiness_keys
if resource.get_condition(self.req.observed.resources.get(key), "Ready").status != "True"
]
if not waiting:
Expand All @@ -1381,12 +1473,24 @@ def derive_conditions(self) -> None:
),
)
return
if waiting == ["caller-auth"]:
if "caller-auth" in waiting and "gateway" not in waiting:
self.not_ready(
CONDITION_REASON_AUTH_NOT_ACCEPTED,
"The gateway's caller authentication policy has not been accepted, so every request is refused",
)
return
if ("ai-gateway-anchor" in waiting or "envoy-deployment" in waiting) and "gateway" not in waiting:
if "ai-gateway-anchor" in waiting:
self.not_ready(
CONDITION_REASON_WAITING_FOR_AI_GATEWAY_ROUTE,
f"Waiting for the AI Gateway anchor route on cluster {self.xr.spec.clusterName} to be accepted",
)
else:
self.not_ready(
CONDITION_REASON_WAITING_FOR_ENVOY_DEPLOYMENT,
f"Waiting for the Envoy Deployment on cluster {self.xr.spec.clusterName} to finish rolling out",
)
return
self.not_ready(
CONDITION_REASON_WAITING_FOR_GATEWAY,
f"Waiting for the Gateway on cluster {self.xr.spec.clusterName} to be programmed",
Expand Down
Loading