From 1a5ce01fd5e36959ddc860de43d80eb9d42df03b Mon Sep 17 00:00:00 2001 From: Aleksei Sviridkin Date: Tue, 3 Nov 2026 06:26:59 +0300 Subject: [PATCH] feat(cozystack-controller): register the GPU webhooks with GPU enabled Fail-closed GPU admission must remain available during controller rollouts and node disruptions. Replicas sharing one node would fail together, and an unready replica must not receive admission traffic. The availability dependency must be limited to VMs using class entries. Other VMs and pods should not wait for a GPU webhook they do not need. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin --- .../templates/certmanager.yaml | 49 ++++ .../templates/deployment.yaml | 41 ++- .../cozystack-controller/templates/pdb.yaml | 11 + .../templates/service.yaml | 17 ++ .../templates/webhooks.yaml | 62 +++++ .../tests/gpu_webhooks_test.yaml | 243 ++++++++++++++++++ .../system/cozystack-controller/values.yaml | 3 + 7 files changed, 425 insertions(+), 1 deletion(-) create mode 100644 packages/system/cozystack-controller/templates/certmanager.yaml create mode 100644 packages/system/cozystack-controller/templates/pdb.yaml create mode 100644 packages/system/cozystack-controller/templates/service.yaml create mode 100644 packages/system/cozystack-controller/templates/webhooks.yaml create mode 100644 packages/system/cozystack-controller/tests/gpu_webhooks_test.yaml diff --git a/packages/system/cozystack-controller/templates/certmanager.yaml b/packages/system/cozystack-controller/templates/certmanager.yaml new file mode 100644 index 000000000..012082817 --- /dev/null +++ b/packages/system/cozystack-controller/templates/certmanager.yaml @@ -0,0 +1,49 @@ +{{- if .Values.cozystackController.gpu.enabled }} +apiVersion: cert-manager.io/v1 +kind: Issuer +metadata: + name: cozystack-controller-webhook-selfsigned + namespace: {{ .Release.Namespace }} +spec: + selfSigned: {} +--- +apiVersion: cert-manager.io/v1 +kind: Certificate +metadata: + name: cozystack-controller-webhook-ca + namespace: {{ .Release.Namespace }} +spec: + secretName: cozystack-controller-webhook-ca + duration: 43800h # 5 years + commonName: cozystack-controller-webhook-ca + issuerRef: + name: cozystack-controller-webhook-selfsigned + isCA: true + privateKey: + rotationPolicy: Never +--- +apiVersion: cert-manager.io/v1 +kind: Issuer +metadata: + name: cozystack-controller-webhook-ca + namespace: {{ .Release.Namespace }} +spec: + ca: + secretName: cozystack-controller-webhook-ca +--- +apiVersion: cert-manager.io/v1 +kind: Certificate +metadata: + name: cozystack-controller-webhook + namespace: {{ .Release.Namespace }} +spec: + secretName: cozystack-controller-webhook-cert + duration: 8760h + renewBefore: 720h + issuerRef: + name: cozystack-controller-webhook-ca + commonName: cozystack-controller-webhook + dnsNames: + - cozystack-controller-webhook + - cozystack-controller-webhook.{{ .Release.Namespace }}.svc +{{- end }} diff --git a/packages/system/cozystack-controller/templates/deployment.yaml b/packages/system/cozystack-controller/templates/deployment.yaml index 342d0ce12..edbae7108 100644 --- a/packages/system/cozystack-controller/templates/deployment.yaml +++ b/packages/system/cozystack-controller/templates/deployment.yaml @@ -1,3 +1,4 @@ +{{- $gpu := .Values.cozystackController.gpu }} apiVersion: apps/v1 kind: Deployment metadata: @@ -5,7 +6,11 @@ metadata: labels: app: cozystack-controller spec: + {{- if $gpu.enabled }} + replicas: {{ max 2 .Values.cozystackController.replicas }} + {{- else }} replicas: {{ .Values.cozystackController.replicas }} + {{- end }} selector: matchLabels: app: cozystack-controller @@ -15,6 +20,17 @@ spec: app: cozystack-controller spec: serviceAccountName: cozystack-controller + {{- if $gpu.enabled }} + affinity: + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - weight: 100 + podAffinityTerm: + topologyKey: kubernetes.io/hostname + labelSelector: + matchLabels: + app: cozystack-controller + {{- end }} containers: - name: cozystack-controller image: "{{ .Values.cozystackController.image }}" @@ -37,10 +53,33 @@ spec: {{- if .Values.cozystackController.endpointAttachments.allocateLoadBalancerNodePorts }} - --endpoint-attachment-allocate-node-ports {{- end }} - {{- if .Values.cozystackController.gpu.enabled }} + {{- if $gpu.enabled }} - --enable-gpu + {{- with $gpu.resourceNames }} + - --gpu-resource-names={{ join "," . }} + {{- end }} + ports: + - name: webhook + containerPort: 9443 + - name: health + containerPort: 8081 + readinessProbe: + httpGet: + path: /readyz + port: health + volumeMounts: + - name: webhook-certs + mountPath: /tmp/k8s-webhook-server/serving-certs + readOnly: true {{- end }} {{- with .Values.cozystackController.resources }} resources: {{- toYaml . | nindent 10 }} {{- end }} + {{- if $gpu.enabled }} + volumes: + - name: webhook-certs + secret: + secretName: cozystack-controller-webhook-cert + defaultMode: 0400 + {{- end }} diff --git a/packages/system/cozystack-controller/templates/pdb.yaml b/packages/system/cozystack-controller/templates/pdb.yaml new file mode 100644 index 000000000..f77581d1e --- /dev/null +++ b/packages/system/cozystack-controller/templates/pdb.yaml @@ -0,0 +1,11 @@ +{{- if .Values.cozystackController.gpu.enabled }} +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: cozystack-controller +spec: + maxUnavailable: 1 + selector: + matchLabels: + app: cozystack-controller +{{- end }} diff --git a/packages/system/cozystack-controller/templates/service.yaml b/packages/system/cozystack-controller/templates/service.yaml new file mode 100644 index 000000000..c21051cee --- /dev/null +++ b/packages/system/cozystack-controller/templates/service.yaml @@ -0,0 +1,17 @@ +{{- if .Values.cozystackController.gpu.enabled }} +apiVersion: v1 +kind: Service +metadata: + name: cozystack-controller-webhook + labels: + app: cozystack-controller +spec: + type: ClusterIP + ports: + - name: webhook + port: 443 + targetPort: webhook + protocol: TCP + selector: + app: cozystack-controller +{{- end }} diff --git a/packages/system/cozystack-controller/templates/webhooks.yaml b/packages/system/cozystack-controller/templates/webhooks.yaml new file mode 100644 index 000000000..e7c1a21ea --- /dev/null +++ b/packages/system/cozystack-controller/templates/webhooks.yaml @@ -0,0 +1,62 @@ +{{- if .Values.cozystackController.gpu.enabled }} +{{- $namespaceSelector := dict "matchExpressions" (list + (dict "key" "cozystack.io/system" "operator" "NotIn" "values" (list "true")) + (dict "key" "kubernetes.io/metadata.name" "operator" "NotIn" "values" (list "kube-system" "default"))) }} +# Both webhooks select only objects carrying the reference label, so an +# outage of the controller blocks only the start of VMs with class entries. +# The name sorts before virt-api-mutator, so KubeVirt's own mutator sees the +# gpus entries too. +apiVersion: admissionregistration.k8s.io/v1 +kind: MutatingWebhookConfiguration +metadata: + name: cozystack-gpu + annotations: + cert-manager.io/inject-ca-from: {{ .Release.Namespace }}/cozystack-controller-webhook +webhooks: +- name: gpu-vmi.cozystack.io + admissionReviewVersions: ["v1"] + sideEffects: None + timeoutSeconds: 10 + failurePolicy: Fail + reinvocationPolicy: IfNeeded + clientConfig: + service: + name: cozystack-controller-webhook + namespace: {{ .Release.Namespace }} + path: /gpu/mutate-vmi + rules: + - operations: [CREATE] + apiGroups: [kubevirt.io] + apiVersions: [v1] + resources: [virtualmachineinstances] + namespaceSelector: + {{- toYaml $namespaceSelector | nindent 4 }} + objectSelector: + matchLabels: + cozystack.io/gpu-reference: "true" +# Migration targets and the pod that binds WaitForFirstConsumer volumes are +# launcher pods too, and need the tolerations as much as the first one. +- name: gpu-launcher.cozystack.io + admissionReviewVersions: ["v1"] + sideEffects: None + timeoutSeconds: 10 + failurePolicy: Fail + # A later mutator may drop a toleration; the call adds it again. + reinvocationPolicy: IfNeeded + clientConfig: + service: + name: cozystack-controller-webhook + namespace: {{ .Release.Namespace }} + path: /gpu/mutate-virt-launcher + rules: + - operations: [CREATE] + apiGroups: [""] + apiVersions: [v1] + resources: [pods] + namespaceSelector: + {{- toYaml $namespaceSelector | nindent 4 }} + objectSelector: + matchLabels: + cozystack.io/gpu-reference: "true" + kubevirt.io: virt-launcher +{{- end }} diff --git a/packages/system/cozystack-controller/tests/gpu_webhooks_test.yaml b/packages/system/cozystack-controller/tests/gpu_webhooks_test.yaml new file mode 100644 index 000000000..0622ba6b7 --- /dev/null +++ b/packages/system/cozystack-controller/tests/gpu_webhooks_test.yaml @@ -0,0 +1,243 @@ +suite: cozystack-controller GPU webhooks +templates: + - templates/deployment.yaml + - templates/service.yaml + - templates/certmanager.yaml + - templates/webhooks.yaml + - templates/pdb.yaml + +release: + name: cozystack-controller + namespace: cozy-system + +tests: + - it: renders no webhook object by default + templates: + - templates/service.yaml + - templates/certmanager.yaml + - templates/webhooks.yaml + - templates/pdb.yaml + asserts: + - hasDocuments: + count: 0 + + - it: leaves the default Deployment as it was + template: templates/deployment.yaml + asserts: + - equal: + path: spec.replicas + value: 1 + - equal: + path: spec.template.spec.containers[0].args + value: [--leader-elect, --zap-log-level=info] + - notExists: + path: spec.template.spec.volumes + - notExists: + path: spec.template.spec.containers[0].ports + - notExists: + path: spec.template.spec.containers[0].readinessProbe + - notExists: + path: spec.template.spec.affinity + + - it: serves the webhooks with GPU enabled + template: templates/deployment.yaml + set: + cozystackController.gpu: + enabled: true + resourceNames: [nvidia.com/gpu, amd.com/gpu] + asserts: + - contains: + path: spec.template.spec.containers[0].args + content: --gpu-resource-names=nvidia.com/gpu,amd.com/gpu + - equal: + path: spec.template.spec.containers[0].ports + value: + - name: webhook + containerPort: 9443 + - name: health + containerPort: 8081 + - equal: + path: spec.template.spec.containers[0].readinessProbe + value: + httpGet: + path: /readyz + port: health + - equal: + path: spec.template.spec.containers[0].volumeMounts + value: + - name: webhook-certs + mountPath: /tmp/k8s-webhook-server/serving-certs + readOnly: true + - equal: + path: spec.template.spec.volumes + value: + - name: webhook-certs + secret: + secretName: cozystack-controller-webhook-cert + defaultMode: 0400 + + - it: passes no resource-name flag for an empty hoster list + template: templates/deployment.yaml + set: + cozystackController.gpu.enabled: true + asserts: + - notContains: + path: spec.template.spec.containers[0].args + content: --gpu-resource-names= + any: true + + # The webhooks fail closed: one replica would block GPU VM starts for every + # controller rollout, and two on one node would go down with it. + - it: runs two replicas spread over nodes with GPU enabled + template: templates/deployment.yaml + set: + cozystackController.gpu.enabled: true + asserts: + - equal: + path: spec.replicas + value: 2 + - equal: + path: spec.template.spec.affinity + value: + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - weight: 100 + podAffinityTerm: + topologyKey: kubernetes.io/hostname + labelSelector: + matchLabels: + app: cozystack-controller + + - it: keeps a higher replica count with GPU enabled + template: templates/deployment.yaml + set: + cozystackController.gpu.enabled: true + cozystackController.replicas: 3 + asserts: + - equal: + path: spec.replicas + value: 3 + + - it: keeps one replica down at most + template: templates/pdb.yaml + set: + cozystackController.gpu.enabled: true + asserts: + - equal: + path: spec + value: + maxUnavailable: 1 + selector: + matchLabels: + app: cozystack-controller + + - it: serves the webhook certificate for the webhook Service + template: templates/certmanager.yaml + set: + cozystackController.gpu.enabled: true + documentSelector: + path: metadata.name + value: cozystack-controller-webhook + asserts: + - equal: + path: spec.dnsNames + value: + - cozystack-controller-webhook + - cozystack-controller-webhook.cozy-system.svc + - equal: + path: spec.secretName + value: cozystack-controller-webhook-cert + + - it: routes the webhook Service to the webhook port + template: templates/service.yaml + set: + cozystackController.gpu.enabled: true + asserts: + - equal: + path: spec.ports + value: + - name: webhook + port: 443 + targetPort: webhook + protocol: TCP + + # Only objects carrying the reference label wait on these webhooks, so an + # outage blocks the start of VMs with class entries and no other VM. + - it: renders the VMI webhook + template: templates/webhooks.yaml + set: + cozystackController.gpu.enabled: true + asserts: + - equal: + path: metadata.annotations["cert-manager.io/inject-ca-from"] + value: cozy-system/cozystack-controller-webhook + - equal: + path: webhooks[0].name + value: gpu-vmi.cozystack.io + - equal: + path: webhooks[0].clientConfig.service.path + value: /gpu/mutate-vmi + - equal: + path: webhooks[0].failurePolicy + value: Fail + - equal: + path: webhooks[0].sideEffects + value: None + - equal: + path: webhooks[0].reinvocationPolicy + value: IfNeeded + - equal: + path: webhooks[0].rules + value: + - operations: [CREATE] + apiGroups: [kubevirt.io] + apiVersions: [v1] + resources: [virtualmachineinstances] + - equal: + path: webhooks[0].objectSelector + value: + matchLabels: + cozystack.io/gpu-reference: "true" + + - it: renders the launcher pod webhook for every virt-launcher pod with class entries + template: templates/webhooks.yaml + set: + cozystackController.gpu.enabled: true + asserts: + - equal: + path: webhooks[1].name + value: gpu-launcher.cozystack.io + - equal: + path: webhooks[1].clientConfig.service.path + value: /gpu/mutate-virt-launcher + - equal: + path: webhooks[1].failurePolicy + value: Fail + - equal: + path: webhooks[1].sideEffects + value: None + - equal: + path: webhooks[1].rules + value: + - operations: [CREATE] + apiGroups: [""] + apiVersions: [v1] + resources: [pods] + - equal: + path: webhooks[1].objectSelector + value: + matchLabels: + cozystack.io/gpu-reference: "true" + kubevirt.io: virt-launcher + + - it: keeps both webhooks within the API server's timeout + template: templates/webhooks.yaml + set: + cozystackController.gpu.enabled: true + asserts: + - equal: + path: webhooks[0].timeoutSeconds + value: 10 + - equal: + path: webhooks[1].timeoutSeconds + value: 10 diff --git a/packages/system/cozystack-controller/values.yaml b/packages/system/cozystack-controller/values.yaml index b7d671753..88ab95a77 100644 --- a/packages/system/cozystack-controller/values.yaml +++ b/packages/system/cozystack-controller/values.yaml @@ -32,3 +32,6 @@ cozystackController: # off, the controller deletes every license copy. gpu: enabled: false + # Hoster's gpu.resourceNames: GPU resource names besides those of + # GPUClasses and the KubeVirt permittedHostDevices table. + resourceNames: []