From 44f83714bf2d5381a60daebc7f7f6ada6372c1e4 Mon Sep 17 00:00:00 2001 From: "v.oleynikov" Date: Mon, 16 Nov 2026 07:36:44 +0300 Subject: [PATCH] feat(controller): let the module controller serve its admission webhooks With webhookEnabled the module controller pod runs two Go processes: the controller and a "webhooks" container that serves a few validating handlers. On masters that is a container request per controller replica for handlers the controller binary can serve itself. Add webhooksInController (default false, used only with webhookEnabled). With it the webhooks container and its VPA policy are not rendered, and the controller container gets what the webhooks container had: - the "https" port (webhooksPort), which the webhook Service targets by name, rendered after the metrics port and before additionalControllerPorts; - the webhook certificate Secret mounted read-only at webhooksCertMountPath; - WEBHOOKS_BIND_ADDRESS=: and WEBHOOKS_CERT_DIR=, which the webhookserver package of sds-common-lib reads. The pod keeps the checksum/ca annotation and the webhook-certs volume. The controller has to serve the handlers there over TLS and report ready only once the listener is up, since pod readiness is what puts the pod behind the webhook Service. Modules that do not set the option render as before. Signed-off-by: v.oleynikov --- charts/helm_lib/Chart.yaml | 2 +- .../helm_lib/templates/_module_controller.tpl | 37 ++- ..._lib_module_controller_manifests_test.yaml | 296 ++++++++++++++++++ 3 files changed, 330 insertions(+), 5 deletions(-) diff --git a/charts/helm_lib/Chart.yaml b/charts/helm_lib/Chart.yaml index e87c9a2..7f08587 100644 --- a/charts/helm_lib/Chart.yaml +++ b/charts/helm_lib/Chart.yaml @@ -1,5 +1,5 @@ apiVersion: v2 type: library name: deckhouse_lib_helm -version: 1.72.37 +version: 1.72.38 description: "Helm utils template definitions for Deckhouse modules." diff --git a/charts/helm_lib/templates/_module_controller.tpl b/charts/helm_lib/templates/_module_controller.tpl index 318e634..58052d9 100644 --- a/charts/helm_lib/templates/_module_controller.tpl +++ b/charts/helm_lib/templates/_module_controller.tpl @@ -58,6 +58,15 @@ strategy: the metrics port (default: none) - additionalPodAnnotations: additional annotations for the pod template, e.g. checksums of mounted certificates (default: none) + - webhooksInController: with webhookEnabled, serve the admission webhooks from the controller + process instead of a separate "webhooks" container (default: false). The webhooks container + and its VPA policy are not rendered; the controller container gets the "https" port + (webhooksPort), the webhook certificate Secret mounted at webhooksCertMountPath, and + WEBHOOKS_BIND_ADDRESS=: and WEBHOOKS_CERT_DIR=. The + controller must serve the webhook handlers there over TLS with tls.crt and tls.key from that + directory, and must not report ready before the listener is up: the readiness of the pod is + what puts it behind the webhook Service. webhooksImageName and webhooksCommand are not used + in this mode. - controllerImage: custom controller image (default: uses helm_lib_module_image) - controllerImageName: image name for helm_lib_module_image (default: "controller") - webhooksImageName: image name for webhooks (default: "webhooks") @@ -96,6 +105,8 @@ strategy: {{- $fullname := $config.fullname | default "controller" }} {{- $valuesKey := $config.valuesKey | required "$config.valuesKey is required" }} {{- $webhookEnabled := dig "webhookEnabled" false $config }} + {{- $webhooksInController := and $webhookEnabled (dig "webhooksInController" false $config) }} + {{- $webhooksContainer := and $webhookEnabled (not $webhooksInController) }} {{- $webhookCertPath := $config.webhookCertPath | default "internal.customWebhookCert" }} {{- $onMasterNode := dig "onMasterNode" true $config }} {{- $priorityClass := $config.priorityClass | default "system-cluster-critical" }} @@ -158,7 +169,7 @@ spec: maxAllowed: cpu: {{ $controllerMaxCpu }} memory: {{ $controllerMaxMemory }} - {{- if $webhookEnabled }} + {{- if $webhooksContainer }} - containerName: "webhooks" minAllowed: {{- include "helm_lib_module_webhooks_resources" $context | nindent 8 }} @@ -264,7 +275,7 @@ spec: periodSeconds: 1 failureThreshold: 3 {{- $plainMetricsPort := and $controllerMetricsPort (not $controllerMetricsProxyPort) }} - {{- if or $controllerMetricsInProcessAuthorization $plainMetricsPort $additionalControllerPorts }} + {{- if or $controllerMetricsInProcessAuthorization $plainMetricsPort $webhooksInController $additionalControllerPorts }} ports: {{- if $controllerMetricsInProcessAuthorization }} - name: https-metrics @@ -275,6 +286,11 @@ spec: containerPort: {{ $controllerMetricsPort }} protocol: TCP {{- end }} + {{- if $webhooksInController }} + - name: https + containerPort: {{ $webhooksPort }} + protocol: TCP + {{- end }} {{- if $additionalControllerPorts }} {{- $additionalControllerPorts | toYaml | nindent 12 }} {{- end }} @@ -311,14 +327,27 @@ spec: value: ":{{ $controllerMetricsPort }}" {{- end }} {{- end }} + {{- if $webhooksInController }} + - name: WEBHOOKS_BIND_ADDRESS + value: ":{{ $webhooksPort }}" + - name: WEBHOOKS_CERT_DIR + value: {{ $webhooksCertMountPath | quote }} + {{- end }} {{- if $additionalControllerEnvs }} {{- $additionalControllerEnvs | toYaml | nindent 12 }} {{- end }} - {{- if $additionalControllerVolumeMounts }} + {{- if or $webhooksInController $additionalControllerVolumeMounts }} volumeMounts: + {{- if $webhooksInController }} + - name: webhook-certs + mountPath: {{ $webhooksCertMountPath }} + readOnly: true + {{- end }} + {{- if $additionalControllerVolumeMounts }} {{- $additionalControllerVolumeMounts | toYaml | nindent 12 }} + {{- end }} {{- end }} - {{- if $webhookEnabled }} + {{- if $webhooksContainer }} - name: webhooks {{- include "helm_lib_module_container_security_context_pss_restricted_flexible" (dict "ro" true "seccompProfile" true) | nindent 10 }} {{- if $webhooksCommand }} diff --git a/tests/tests/helm_lib_module_controller_manifests_test.yaml b/tests/tests/helm_lib_module_controller_manifests_test.yaml index 2137b1b..a56b6d8 100644 --- a/tests/tests/helm_lib_module_controller_manifests_test.yaml +++ b/tests/tests/helm_lib_module_controller_manifests_test.yaml @@ -1177,3 +1177,299 @@ tests: documentSelector: path: kind value: Deployment + + - it: serves the webhooks from the controller container with webhooksInController + set: + global: + enabledModules: ["vertical-pod-autoscaler-crd"] + modules: + placement: {} + modulesImages: + registry: + base: "deckhouse.io/deckhouse/ce" + digests: + testModule: + controller: sha256:abc123 + webhooks: sha256:def456 + common: + kubeRbacProxy: sha256:krp789 + discovery: + d8SpecificNodeCountByRole: + master: 3 + testModule: + logLevel: INFO + internal: + customWebhookCert: + ca: test-ca-cert + crt: test-crt + key: test-key + _testvalues: + valuesKey: testModule + webhookEnabled: true + webhooksInController: true + asserts: + - lengthEqual: + path: spec.template.spec.containers + count: 1 + documentSelector: + path: kind + value: Deployment + - equal: + path: spec.template.spec.containers[0].name + value: controller + documentSelector: + path: kind + value: Deployment + - contains: + path: spec.template.spec.containers[0].ports + content: + name: https + containerPort: 8443 + protocol: TCP + documentSelector: + path: kind + value: Deployment + - contains: + path: spec.template.spec.containers[0].env + content: + name: WEBHOOKS_BIND_ADDRESS + value: ":8443" + documentSelector: + path: kind + value: Deployment + - contains: + path: spec.template.spec.containers[0].env + content: + name: WEBHOOKS_CERT_DIR + value: /etc/webhook/certs + documentSelector: + path: kind + value: Deployment + - contains: + path: spec.template.spec.containers[0].volumeMounts + content: + name: webhook-certs + mountPath: /etc/webhook/certs + readOnly: true + documentSelector: + path: kind + value: Deployment + - contains: + path: spec.template.spec.volumes + content: + name: webhook-certs + secret: + secretName: webhooks-https-certs + documentSelector: + path: kind + value: Deployment + - matchRegex: + path: spec.template.metadata.annotations["checksum/ca"] + pattern: "^[0-9a-f]{64}$" + documentSelector: + path: kind + value: Deployment + - lengthEqual: + path: spec.resourcePolicy.containerPolicies + count: 1 + documentSelector: + path: kind + value: VerticalPodAutoscaler + - equal: + path: spec.resourcePolicy.containerPolicies[0].containerName + value: controller + documentSelector: + path: kind + value: VerticalPodAutoscaler + + - it: uses webhooksPort and webhooksCertMountPath with webhooksInController + set: + global: + enabledModules: ["vertical-pod-autoscaler-crd"] + modules: + placement: {} + modulesImages: + registry: + base: "deckhouse.io/deckhouse/ce" + digests: + testModule: + controller: sha256:abc123 + webhooks: sha256:def456 + common: + kubeRbacProxy: sha256:krp789 + discovery: + d8SpecificNodeCountByRole: + master: 3 + testModule: + logLevel: INFO + internal: + customWebhookCert: + ca: test-ca-cert + crt: test-crt + key: test-key + _testvalues: + valuesKey: testModule + webhookEnabled: true + webhooksInController: true + webhooksPort: 9443 + webhooksCertMountPath: /certs + additionalControllerPorts: + - name: health + containerPort: 8081 + protocol: TCP + additionalControllerVolumeMounts: + - name: extra + mountPath: /extra + asserts: + - equal: + path: spec.template.spec.containers[0].ports + value: + - name: https + containerPort: 9443 + protocol: TCP + - name: health + containerPort: 8081 + protocol: TCP + documentSelector: + path: kind + value: Deployment + - equal: + path: spec.template.spec.containers[0].volumeMounts + value: + - name: webhook-certs + mountPath: /certs + readOnly: true + - name: extra + mountPath: /extra + documentSelector: + path: kind + value: Deployment + - contains: + path: spec.template.spec.containers[0].env + content: + name: WEBHOOKS_BIND_ADDRESS + value: ":9443" + documentSelector: + path: kind + value: Deployment + - contains: + path: spec.template.spec.containers[0].env + content: + name: WEBHOOKS_CERT_DIR + value: /certs + documentSelector: + path: kind + value: Deployment + + - it: ignores webhooksInController without webhookEnabled + set: + global: + enabledModules: ["vertical-pod-autoscaler-crd"] + modules: + placement: {} + modulesImages: + registry: + base: "deckhouse.io/deckhouse/ce" + digests: + testModule: + controller: sha256:abc123 + webhooks: sha256:def456 + common: + kubeRbacProxy: sha256:krp789 + discovery: + d8SpecificNodeCountByRole: + master: 3 + testModule: + logLevel: INFO + internal: + customWebhookCert: + ca: test-ca-cert + crt: test-crt + key: test-key + _testvalues: + valuesKey: testModule + webhookEnabled: false + webhooksInController: true + asserts: + - lengthEqual: + path: spec.template.spec.containers + count: 1 + documentSelector: + path: kind + value: Deployment + - notExists: + path: spec.template.spec.containers[0].ports + documentSelector: + path: kind + value: Deployment + - notExists: + path: spec.template.spec.containers[0].volumeMounts + documentSelector: + path: kind + value: Deployment + - notExists: + path: spec.template.spec.volumes + documentSelector: + path: kind + value: Deployment + - notContains: + path: spec.template.spec.containers[0].env + content: + name: WEBHOOKS_BIND_ADDRESS + value: ":8443" + documentSelector: + path: kind + value: Deployment + + - it: keeps the webhooks container when webhooksInController is not set + set: + global: + enabledModules: ["vertical-pod-autoscaler-crd"] + modules: + placement: {} + modulesImages: + registry: + base: "deckhouse.io/deckhouse/ce" + digests: + testModule: + controller: sha256:abc123 + webhooks: sha256:def456 + common: + kubeRbacProxy: sha256:krp789 + discovery: + d8SpecificNodeCountByRole: + master: 3 + testModule: + logLevel: INFO + internal: + customWebhookCert: + ca: test-ca-cert + crt: test-crt + key: test-key + _testvalues: + valuesKey: testModule + webhookEnabled: true + asserts: + - lengthEqual: + path: spec.template.spec.containers + count: 2 + documentSelector: + path: kind + value: Deployment + - equal: + path: spec.template.spec.containers[1].name + value: webhooks + documentSelector: + path: kind + value: Deployment + - notExists: + path: spec.template.spec.containers[0].volumeMounts + documentSelector: + path: kind + value: Deployment + - lengthEqual: + path: spec.resourcePolicy.containerPolicies + count: 2 + documentSelector: + path: kind + value: VerticalPodAutoscaler