|
| 1 | +# prototype/mohawk_operator.py (NEW) - K8s operator for deployment |
| 2 | +import kubernetes as k8s |
| 3 | +from kubernetes.client import AppsV1Api, CoreV1Api |
| 4 | +import time |
| 5 | + |
| 6 | +class MohawkOperator: |
| 7 | + """Kubernetes operator for Mohawk Inference Engine deployment.""" |
| 8 | + |
| 9 | + def __init__(self, namespace: str = "mohawk"): |
| 10 | + self.apps_v1 = AppsV1Api() |
| 11 | + self.core_v1 = CoreV1Api() |
| 12 | + self.namespace = namespace |
| 13 | + |
| 14 | + def deploy_worker_statefulset( |
| 15 | + self, |
| 16 | + replicas: int, |
| 17 | + model_id: str, |
| 18 | + worker_image: str = "rwilliamspbg-ops/mohawk-worker:latest" |
| 19 | + ): |
| 20 | + """Deploy worker StatefulSet with GPU scheduling.""" |
| 21 | + |
| 22 | + stateful_set = { |
| 23 | + "apiVersion": "apps/v1", |
| 24 | + "kind": "StatefulSet", |
| 25 | + "metadata": { |
| 26 | + "name": "mohawk-worker", |
| 27 | + "namespace": self.namespace, |
| 28 | + "labels": {"app": "mohawk-worker"} |
| 29 | + }, |
| 30 | + "spec": { |
| 31 | + "serviceName": "mohawk-workers", |
| 32 | + "replicas": replicas, |
| 33 | + "selector": { |
| 34 | + "matchLabels": {"app": "mohawk-worker"} |
| 35 | + }, |
| 36 | + "template": { |
| 37 | + "metadata": { |
| 38 | + "labels": {"app": "mohawk-worker"} |
| 39 | + }, |
| 40 | + "spec": { |
| 41 | + "containers": [{ |
| 42 | + "name": "worker", |
| 43 | + "image": worker_image, |
| 44 | + "ports": [{"containerPort": 8003}], |
| 45 | + "resources": { |
| 46 | + "requests": {"cpu": "4", "memory": "16Gi"}, |
| 47 | + "limits": {"cpu": "8", "memory": "24Gi"} |
| 48 | + }, |
| 49 | + "env": [ |
| 50 | + {"name": "WORKER_PORT", "value": "8003"}, |
| 51 | + {"name": "ENABLE_PQC", "value": "true"}, |
| 52 | + {"name": "MODEL_ID", "value": model_id} |
| 53 | + ], |
| 54 | + "volumeMounts": [{ |
| 55 | + "name": "model-weights", |
| 56 | + "mountPath": "/app/weights", |
| 57 | + "readOnly": True |
| 58 | + }] |
| 59 | + }], |
| 60 | + "volumes": [{ |
| 61 | + "name": "model-weights", |
| 62 | + "persistentVolumeClaim": { |
| 63 | + "claimName": "mohawk-model-weights-pvc" |
| 64 | + } |
| 65 | + }] |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + k8s_api = k8s.client.ApiClient() |
| 72 | + stateful_set_obj = k8s.client.V1StatefulSet.from_dict(stateful_set) |
| 73 | + self.apps_v1.create_namespaced_stateful_set( |
| 74 | + namespace=self.namespace, |
| 75 | + body=stateful_set_obj |
| 76 | + ) |
| 77 | + |
| 78 | + def deploy_controller_deployment(self, replicas: int): |
| 79 | + """Deploy controller Deployment.""" |
| 80 | + |
| 81 | + deployment = { |
| 82 | + "apiVersion": "apps/v1", |
| 83 | + "kind": "Deployment", |
| 84 | + "metadata": { |
| 85 | + "name": "mohawk-controller", |
| 86 | + "namespace": self.namespace |
| 87 | + }, |
| 88 | + "spec": { |
| 89 | + "replicas": replicas, |
| 90 | + "selector": { |
| 91 | + "matchLabels": {"app": "mohawk-controller"} |
| 92 | + }, |
| 93 | + "template": { |
| 94 | + "metadata": { |
| 95 | + "labels": {"app": "mohawk-controller"} |
| 96 | + }, |
| 97 | + "spec": { |
| 98 | + "containers": [{ |
| 99 | + "name": "controller", |
| 100 | + "image": "rwilliamspbg-ops/mohawk-controller:latest", |
| 101 | + "ports": [{"containerPort": 9000}], |
| 102 | + "resources": { |
| 103 | + "requests": {"cpu": "2", "memory": "8Gi"}, |
| 104 | + "limits": {"cpu": "4", "memory": "16Gi"} |
| 105 | + } |
| 106 | + }] |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + deployment_obj = k8s.client.V1Deployment.from_dict(deployment) |
| 113 | + self.apps_v1.create_namespaced_deployment( |
| 114 | + namespace=self.namespace, |
| 115 | + body=deployment_obj |
| 116 | + ) |
| 117 | + |
| 118 | + def deploy_prometheus_monitoring(self): |
| 119 | + """Deploy Prometheus for monitoring.""" |
| 120 | + # Create ServiceMonitor for Mohawk workers |
| 121 | + prometheus_crds = [ |
| 122 | + { |
| 123 | + "apiVersion": "monitoring.coreos.com/v1", |
| 124 | + "kind": "ServiceMonitor", |
| 125 | + "metadata": { |
| 126 | + "name": "mohawk-worker-monitor", |
| 127 | + "namespace": self.namespace |
| 128 | + }, |
| 129 | + "spec": { |
| 130 | + "selector": { |
| 131 | + "matchLabels": {"app": "mohawk-worker"} |
| 132 | + }, |
| 133 | + "endpoints": [{ |
| 134 | + "port": "http-metrics", |
| 135 | + "path": "/metrics", |
| 136 | + "interval": "15s" |
| 137 | + }] |
| 138 | + } |
| 139 | + } |
| 140 | + ] |
| 141 | + |
| 142 | + for crd in prometheus_crds: |
| 143 | + core_api = k8s.client.ApiClient() |
| 144 | + # Create CRD objects |
| 145 | + pass |
| 146 | + |
| 147 | + def scale_workers(self, target_replicas: int): |
| 148 | + """Scale workers using HorizontalPodAutoscaler.""" |
| 149 | + hpa = { |
| 150 | + "apiVersion": "autoscaling/v2", |
| 151 | + "kind": "HorizontalPodAutoscaler", |
| 152 | + "metadata": { |
| 153 | + "name": "mohawk-worker-hpa", |
| 154 | + "namespace": self.namespace |
| 155 | + }, |
| 156 | + "spec": { |
| 157 | + "scaleTargetRef": { |
| 158 | + "apiVersion": "apps/v1", |
| 159 | + "kind": "StatefulSet", |
| 160 | + "name": "mohawk-worker" |
| 161 | + }, |
| 162 | + "minReplicas": 3, |
| 163 | + "maxReplicas": 12, |
| 164 | + "metrics": [{ |
| 165 | + "type": "Resource", |
| 166 | + "resource": { |
| 167 | + "name": "cpu", |
| 168 | + "target": { |
| 169 | + "type": "Utilization", |
| 170 | + "averageUtilization": 70 |
| 171 | + } |
| 172 | + } |
| 173 | + }] |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + hpa_obj = k8s.client.V1HorizontalPodAutoscaler.from_dict(hpa) |
| 178 | + self.apps_v1.create_namespaced_horizontal_pod_autoscaler( |
| 179 | + namespace=self.namespace, |
| 180 | + body=hpa_obj |
| 181 | + ) |
0 commit comments