|
| 1 | +# Copyright © 2025 Apple Inc. |
| 2 | + |
| 3 | +"""k8s HealthCheckPolicy module for gke_gateway_route feature.""" |
| 4 | + |
| 5 | +import copy |
| 6 | +import logging |
| 7 | +from typing import Any, Optional |
| 8 | + |
| 9 | +import kubernetes as k8s |
| 10 | +from absl import flags |
| 11 | + |
| 12 | +from axlearn.cloud.common.utils import FlagConfigurable, generate_job_name |
| 13 | +from axlearn.cloud.gcp.config import default_project |
| 14 | +from axlearn.cloud.gcp.pathways_utils import NOTARY_PROXY_HTTP_PORT |
| 15 | +from axlearn.common.config import REQUIRED, Required, config_class |
| 16 | +from axlearn.common.utils import Nested |
| 17 | + |
| 18 | + |
| 19 | +class LWSHealthCheckPolicy(FlagConfigurable): |
| 20 | + """LWS HealthCheckPolicy for gke_gateway_route feature. |
| 21 | +
|
| 22 | + Creates a HealthCheckPolicy K8s object that configures health checks |
| 23 | + for the LWS service when using GKE Gateway routing. |
| 24 | + """ |
| 25 | + |
| 26 | + @config_class |
| 27 | + class Config(FlagConfigurable.Config): |
| 28 | + """Configures LWSHealthCheckPolicy. |
| 29 | +
|
| 30 | + Attributes: |
| 31 | + name: The name of the LWS resource. |
| 32 | + project: The GCP project. |
| 33 | + namespace: The namespace of the service. |
| 34 | + check_interval_sec: Interval between health checks in seconds. |
| 35 | + timeout_sec: Timeout for each health check in seconds. |
| 36 | + healthy_threshold: Number of consecutive successes to mark healthy. |
| 37 | + unhealthy_threshold: Number of consecutive failures to mark unhealthy. |
| 38 | + health_check_port: The port to use for TCP health check. |
| 39 | + """ |
| 40 | + |
| 41 | + name: Required[str] = REQUIRED |
| 42 | + project: Required[str] = REQUIRED |
| 43 | + namespace: str = "default" |
| 44 | + check_interval_sec: int = 10 |
| 45 | + timeout_sec: int = 5 |
| 46 | + healthy_threshold: int = 1 |
| 47 | + unhealthy_threshold: int = 3 |
| 48 | + health_check_port: Optional[int] = None |
| 49 | + |
| 50 | + @classmethod |
| 51 | + def define_flags(cls, fv: flags.FlagValues): |
| 52 | + common_kwargs = dict(flag_values=fv, allow_override=True) |
| 53 | + flags.DEFINE_string("name", None, "Name of the HealthCheckPolicy.", **common_kwargs) |
| 54 | + flags.DEFINE_string("project", None, "The GCP project name.", **common_kwargs) |
| 55 | + flags.DEFINE_integer( |
| 56 | + "health_check_interval_sec", |
| 57 | + 10, |
| 58 | + "Interval between health checks in seconds.", |
| 59 | + **common_kwargs, |
| 60 | + ) |
| 61 | + flags.DEFINE_integer( |
| 62 | + "health_check_timeout_sec", |
| 63 | + 5, |
| 64 | + "Timeout for each health check in seconds.", |
| 65 | + **common_kwargs, |
| 66 | + ) |
| 67 | + flags.DEFINE_integer( |
| 68 | + "health_check_healthy_threshold", |
| 69 | + 1, |
| 70 | + "Number of consecutive successes to mark healthy.", |
| 71 | + **common_kwargs, |
| 72 | + ) |
| 73 | + flags.DEFINE_integer( |
| 74 | + "health_check_unhealthy_threshold", |
| 75 | + 3, |
| 76 | + "Number of consecutive failures to mark unhealthy.", |
| 77 | + **common_kwargs, |
| 78 | + ) |
| 79 | + flags.DEFINE_integer( |
| 80 | + "health_check_port", |
| 81 | + None, |
| 82 | + "Port to use for TCP health check. Defaults to NOTARY_PROXY_HTTP_PORT.", |
| 83 | + **common_kwargs, |
| 84 | + ) |
| 85 | + |
| 86 | + @classmethod |
| 87 | + def set_defaults(cls, fv: flags.FlagValues): |
| 88 | + fv.set_default("name", fv.name or generate_job_name()) |
| 89 | + fv.set_default("project", default_project()) |
| 90 | + fv.set_default("health_check_interval_sec", fv.health_check_interval_sec or 10) |
| 91 | + fv.set_default("health_check_timeout_sec", fv.health_check_timeout_sec or 5) |
| 92 | + fv.set_default("health_check_healthy_threshold", fv.health_check_healthy_threshold or 1) |
| 93 | + fv.set_default("health_check_unhealthy_threshold", fv.health_check_unhealthy_threshold or 3) |
| 94 | + |
| 95 | + @classmethod |
| 96 | + def from_flags(cls, fv: flags.FlagValues, **kwargs): |
| 97 | + cfg: LWSHealthCheckPolicy.Config = super().from_flags(fv, **kwargs) |
| 98 | + cfg.name = fv.name |
| 99 | + cfg.check_interval_sec = fv.health_check_interval_sec |
| 100 | + cfg.timeout_sec = fv.health_check_timeout_sec |
| 101 | + cfg.healthy_threshold = fv.health_check_healthy_threshold |
| 102 | + cfg.unhealthy_threshold = fv.health_check_unhealthy_threshold |
| 103 | + if hasattr(fv, "health_check_port") and fv.health_check_port: |
| 104 | + cfg.health_check_port = fv.health_check_port |
| 105 | + return cfg |
| 106 | + |
| 107 | + @classmethod |
| 108 | + def default_config(cls): |
| 109 | + return super().default_config() |
| 110 | + |
| 111 | + def __init__(self, cfg: Config): |
| 112 | + super().__init__(cfg) |
| 113 | + logging.info("LWSHealthCheckPolicy class init") |
| 114 | + self._config = copy.deepcopy(cfg) |
| 115 | + self.name = cfg.name |
| 116 | + self.service_name = f"{cfg.name}-service" |
| 117 | + # Default to NOTARY_PROXY_HTTP_PORT if not specified |
| 118 | + self.health_check_port = cfg.health_check_port or NOTARY_PROXY_HTTP_PORT |
| 119 | + |
| 120 | + def _build_health_check_policy(self) -> Nested[Any]: |
| 121 | + """Builds a config for a HealthCheckPolicy. |
| 122 | +
|
| 123 | + Returns: |
| 124 | + A nested dict corresponding to a K8s HealthCheckPolicy config. |
| 125 | + """ |
| 126 | + cfg = self.config |
| 127 | + logging.info("LWSHealthCheckPolicy class build") |
| 128 | + logging.info(str(self.config)) |
| 129 | + |
| 130 | + # Import utils here to avoid circular dependency |
| 131 | + from axlearn.cloud.gcp.utils import ( # pylint: disable=import-outside-toplevel |
| 132 | + custom_leaderworkerset_kwargs, |
| 133 | + ) |
| 134 | + |
| 135 | + # Fetch the LeaderWorkerSet to get its UID for owner reference |
| 136 | + api_kwargs = custom_leaderworkerset_kwargs() |
| 137 | + custom_api = k8s.client.CustomObjectsApi() |
| 138 | + lws = custom_api.get_namespaced_custom_object( |
| 139 | + group=api_kwargs["group"], |
| 140 | + version=api_kwargs["version"], |
| 141 | + namespace=cfg.namespace, |
| 142 | + plural=api_kwargs["plural"], |
| 143 | + name=self.name, |
| 144 | + ) |
| 145 | + |
| 146 | + # Build the HealthCheckPolicy spec |
| 147 | + health_check_policy = { |
| 148 | + "apiVersion": "networking.gke.io/v1", |
| 149 | + "kind": "HealthCheckPolicy", |
| 150 | + "metadata": { |
| 151 | + "name": f"{self.name}-health-check", |
| 152 | + "namespace": cfg.namespace, |
| 153 | + "ownerReferences": [ |
| 154 | + { |
| 155 | + "apiVersion": f'{api_kwargs["group"]}/{api_kwargs["version"]}', |
| 156 | + "kind": "LeaderWorkerSet", |
| 157 | + "name": self.name, |
| 158 | + "uid": lws["metadata"]["uid"], |
| 159 | + "controller": True, |
| 160 | + "blockOwnerDeletion": True, |
| 161 | + } |
| 162 | + ], |
| 163 | + }, |
| 164 | + "spec": { |
| 165 | + "default": { |
| 166 | + "checkIntervalSec": cfg.check_interval_sec, |
| 167 | + "timeoutSec": cfg.timeout_sec, |
| 168 | + "healthyThreshold": cfg.healthy_threshold, |
| 169 | + "unhealthyThreshold": cfg.unhealthy_threshold, |
| 170 | + "config": { |
| 171 | + "type": "TCP", |
| 172 | + "tcpHealthCheck": { |
| 173 | + "port": self.health_check_port, |
| 174 | + }, |
| 175 | + }, |
| 176 | + }, |
| 177 | + "targetRef": { |
| 178 | + "group": "", |
| 179 | + "kind": "Service", |
| 180 | + "name": self.service_name, |
| 181 | + }, |
| 182 | + }, |
| 183 | + } |
| 184 | + |
| 185 | + return health_check_policy |
| 186 | + |
| 187 | + def execute(self): |
| 188 | + """Creates the HealthCheckPolicy in the cluster.""" |
| 189 | + logging.info("LWSHealthCheckPolicy class execute") |
| 190 | + health_check_policy = self._build_health_check_policy() |
| 191 | + logging.info("Submitting LWSHealthCheckPolicy body=%s", health_check_policy) |
| 192 | + |
| 193 | + return k8s.client.CustomObjectsApi().create_namespaced_custom_object( |
| 194 | + group="networking.gke.io", |
| 195 | + version="v1", |
| 196 | + namespace=self.config.namespace, |
| 197 | + plural="healthcheckpolicies", |
| 198 | + body=health_check_policy, |
| 199 | + ) |
0 commit comments