From cb4789185c5aef4624174feaf11ffcbff8d1cd43 Mon Sep 17 00:00:00 2001 From: Aleksei Sviridkin Date: Tue, 3 Nov 2026 06:25:56 +0300 Subject: [PATCH] feat(cozystack-controller): serve the GPU webhooks with GPU enabled A replica must not receive fail-closed admission traffic until its webhook server serves. Otherwise startup and rollout can block GPU VM creation. GPU resource names requested only by pods cannot always be inferred from a class or the KubeVirt permitted-device table. The hoster needs a way to identify them for admission. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin --- cmd/cozystack-controller/main.go | 29 ++++++++++++++++++++++++++- cmd/cozystack-controller/main_test.go | 10 +++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/cmd/cozystack-controller/main.go b/cmd/cozystack-controller/main.go index d49f7c37b..1a2a72882 100644 --- a/cmd/cozystack-controller/main.go +++ b/cmd/cozystack-controller/main.go @@ -20,6 +20,7 @@ import ( "crypto/tls" "flag" "os" + "strings" "time" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) @@ -52,6 +53,7 @@ import ( "github.com/cozystack/cozystack/internal/controller/tenantquota" "github.com/cozystack/cozystack/internal/controller/wildcardsecret" gpucontroller "github.com/cozystack/cozystack/internal/gpu/controller" + gpuwebhook "github.com/cozystack/cozystack/internal/gpu/webhook" "github.com/cozystack/cozystack/internal/telemetry" cmv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1" @@ -98,6 +100,7 @@ func main() { var cozyProxyContractImplemented bool var endpointAttachmentNodePorts bool var enableGPU bool + var gpuResourceNames string var tlsOpts []func(*tls.Config) flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+ "Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.") @@ -127,7 +130,9 @@ func main() { flag.BoolVar(&endpointAttachmentNodePorts, "endpoint-attachment-allocate-node-ports", false, "Allocate node ports on the LoadBalancer Services EndpointAttachments render. Off by default, matching the platform convention; needed only by load balancers that forward through node ports (Hetzner RobotLB).") flag.BoolVar(&enableGPU, "enable-gpu", false, - "Compute GPUClass status and copy vGPU licenses into entitled namespaces. Without it, existing license copies are deleted.") + "Compute GPUClass status, copy vGPU licenses into entitled namespaces and serve the GPU admission webhooks. Without it, existing license copies are deleted.") + flag.StringVar(&gpuResourceNames, "gpu-resource-names", "", + "Comma-separated GPU resource names besides those of GPUClasses and the KubeVirt permittedHostDevices table, such as nvidia.com/gpu.") opts := zap.Options{ Development: false, } @@ -390,6 +395,10 @@ func main() { os.Exit(1) } + if enableGPU { + gpuwebhook.Register(mgr, splitNames(gpuResourceNames)) + } + // +kubebuilder:scaffold:builder if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { @@ -400,6 +409,14 @@ func main() { setupLog.Error(err, "unable to set up ready check") os.Exit(1) } + // The GPU webhooks fail closed, so a replica takes traffic only once its + // webhook server serves. + if enableGPU { + if err := mgr.AddReadyzCheck("webhook", mgr.GetWebhookServer().StartedChecker()); err != nil { + setupLog.Error(err, "unable to set up webhook ready check") + os.Exit(1) + } + } // Initialize telemetry collector collector, err := telemetry.NewCollector(mgr.GetClient(), &telemetryConfig, mgr.GetConfig()) @@ -420,3 +437,13 @@ func main() { os.Exit(1) } } + +func splitNames(s string) []string { + var out []string + for _, n := range strings.Split(s, ",") { + if n = strings.TrimSpace(n); n != "" { + out = append(out, n) + } + } + return out +} diff --git a/cmd/cozystack-controller/main_test.go b/cmd/cozystack-controller/main_test.go index 491048d7d..4e6d01197 100644 --- a/cmd/cozystack-controller/main_test.go +++ b/cmd/cozystack-controller/main_test.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "slices" "testing" "k8s.io/apimachinery/pkg/runtime" @@ -41,3 +42,12 @@ func TestSchemeRegistersReconciledKinds(t *testing.T) { } } } + +func TestSplitNames(t *testing.T) { + if got := splitNames(" nvidia.com/gpu, ,amd.com/gpu,"); !slices.Equal(got, []string{"nvidia.com/gpu", "amd.com/gpu"}) { + t.Errorf("splitNames = %v", got) + } + if got := splitNames(""); got != nil { + t.Errorf("splitNames of an empty flag = %v", got) + } +}