From e78c3d96ce41597d5622e8ca5bc13ec48be0d8f6 Mon Sep 17 00:00:00 2001 From: Rick Rackow Date: Tue, 22 Sep 2026 09:04:29 +0200 Subject: [PATCH 1/2] feat: support namespaced hosted control plane deployments Signed-off-by: Rick Rackow Signed-off-by: Jan Larwig --- cloud/services/server/reconcile_test.go | 162 ++++++++++--------- cmd/manager/main.go | 41 ++++- cmd/manager/main_test.go | 140 ++++++++++++++++ controller/stackitcluster_controller.go | 7 + controller/stackitcluster_controller_test.go | 34 ++++ controller/stackitmachine_controller_test.go | 16 ++ docs/src/SUMMARY.md | 1 + docs/src/topics/hosted-control-planes.md | 80 +++++++++ util/bootstrap.go | 8 +- 9 files changed, 406 insertions(+), 83 deletions(-) create mode 100644 cmd/manager/main_test.go create mode 100644 docs/src/topics/hosted-control-planes.md diff --git a/cloud/services/server/reconcile_test.go b/cloud/services/server/reconcile_test.go index 90a676a..1721530 100644 --- a/cloud/services/server/reconcile_test.go +++ b/cloud/services/server/reconcile_test.go @@ -12,6 +12,7 @@ package server_test import ( "context" + "encoding/base64" "encoding/json" "errors" "net/http" @@ -168,88 +169,95 @@ func lookup(m map[string]any, key string) any { func TestServiceReconcile(t *testing.T) { t.Run("creates server with bootVolume and expected payload when not found", func(t *testing.T) { - var ( - createPayload map[string]any - recordedServer *server.Server - wasCreated bool - ) - spec := server.ServerSpec{ - Name: "node-0", - MachineType: "c2i.2", - ImageID: testImageID, - AvailabilityZone: "eu01-1", - SSHKeyName: "default", - NetworkID: testNetworkID, - SecurityGroups: []string{testSecurityGroupID}, - UserData: []byte("#cloud-config\n"), - RootVolume: server.RootVolume{ - SizeGiB: 50, - PerformanceClass: "storage_premium_perf6", - DeleteOnTermination: true, - }, - Tags: map[string]string{"cluster": "test"}, - SetServer: func(srv *server.Server, created bool) { - recordedServer = srv - wasCreated = created - }, - } + for name, userData := range map[string][]byte{ + "cloud-init": []byte("#cloud-config\n"), + "ignition": []byte("{\n \"ignition\": {\"version\": \"3.2.0\"}\n}\n"), + } { + t.Run(name, func(t *testing.T) { + var ( + createPayload map[string]any + recordedServer *server.Server + wasCreated bool + ) + spec := server.ServerSpec{ + Name: "node-0", + MachineType: "c2i.2", + ImageID: testImageID, + AvailabilityZone: "eu01-1", + SSHKeyName: "default", + NetworkID: testNetworkID, + SecurityGroups: []string{testSecurityGroupID}, + UserData: userData, + RootVolume: server.RootVolume{ + SizeGiB: 50, + PerformanceClass: "storage_premium_perf6", + DeleteOnTermination: true, + }, + Tags: map[string]string{"cluster": "test"}, + SetServer: func(srv *server.Server, created bool) { + recordedServer = srv + wasCreated = created + }, + } - svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - switch { - case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers"): - if got := r.URL.Query().Get("label_selector"); got != "cluster=test" { - t.Fatalf("label_selector = %q, want cluster=test", got) + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers"): + if got := r.URL.Query().Get("label_selector"); got != "cluster=test" { + t.Fatalf("label_selector = %q, want cluster=test", got) + } + if got := r.URL.Query().Get("details"); got != testBoolTrue { + t.Fatalf("details = %q, want true", got) + } + writeJSON(t, w, map[string]any{"items": []any{}}) + case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/servers"): + createPayload = readJSON(t, r) + writeJSON(t, w, map[string]any{ + "id": testServerID, + "machineType": "c2i.2", + "name": "node-0", + "status": "ACTIVE", + "availabilityZone": "eu01-1", + }) + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID+"/nics"): + writeJSON(t, w, map[string]any{ + "items": []any{ + map[string]any{"ipv4": "10.0.0.10", "ipv6": "fd00::10"}, + }, + }) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) } - if got := r.URL.Query().Get("details"); got != testBoolTrue { - t.Fatalf("details = %q, want true", got) + if !wasCreated { + t.Fatal("SetServer created = false, want true") + } + if recordedServer == nil || recordedServer.ID != testServerID || recordedServer.Name != "node-0" || recordedServer.State != "ACTIVE" { + t.Fatalf("recordedServer = %#v", recordedServer) + } + if len(recordedServer.Addresses) != 2 { + t.Fatalf("recordedServer.Addresses = %#v, want 2 addresses", recordedServer.Addresses) } - writeJSON(t, w, map[string]any{"items": []any{}}) - case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/servers"): - createPayload = readJSON(t, r) - writeJSON(t, w, map[string]any{ - "id": testServerID, - "machineType": "c2i.2", - "name": "node-0", - "status": "ACTIVE", - "availabilityZone": "eu01-1", - }) - case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID+"/nics"): - writeJSON(t, w, map[string]any{ - "items": []any{ - map[string]any{"ipv4": "10.0.0.10", "ipv6": "fd00::10"}, - }, - }) - default: - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - } - })) - if err := svc.Reconcile(context.Background()); err != nil { - t.Fatalf("Reconcile() error = %v", err) - } - if !wasCreated { - t.Fatal("SetServer created = false, want true") - } - if recordedServer == nil || recordedServer.ID != testServerID || recordedServer.Name != "node-0" || recordedServer.State != "ACTIVE" { - t.Fatalf("recordedServer = %#v", recordedServer) + assertStringField(t, createPayload, "name", "node-0") + assertStringField(t, createPayload, "machineType", "c2i.2") + assertFieldAbsent(t, createPayload, "imageId") + assertStringField(t, createPayload, "availabilityZone", "eu01-1") + assertStringField(t, createPayload, "keypairName", "default") + assertStringField(t, createPayload, "userData", base64.StdEncoding.EncodeToString(userData)) + assertBoolField(t, createPayload, "configDrive", true) + assertNestedStringField(t, createPayload, []string{"labels", "cluster"}, "test") + assertNestedStringField(t, createPayload, []string{"networking", "networkId"}, testNetworkID) + assertNestedStringField(t, createPayload, []string{"bootVolume", "performanceClass"}, "storage_premium_perf6") + assertNestedStringField(t, createPayload, []string{"bootVolume", "source", "id"}, testImageID) + assertNestedStringField(t, createPayload, []string{"bootVolume", "source", "type"}, "image") + assertNestedNumberField(t, createPayload, []string{"bootVolume", "size"}, 50) + }) } - if len(recordedServer.Addresses) != 2 { - t.Fatalf("recordedServer.Addresses = %#v, want 2 addresses", recordedServer.Addresses) - } - - assertStringField(t, createPayload, "name", "node-0") - assertStringField(t, createPayload, "machineType", "c2i.2") - assertFieldAbsent(t, createPayload, "imageId") - assertStringField(t, createPayload, "availabilityZone", "eu01-1") - assertStringField(t, createPayload, "keypairName", "default") - assertStringField(t, createPayload, "userData", "I2Nsb3VkLWNvbmZpZwo=") - assertBoolField(t, createPayload, "configDrive", true) - assertNestedStringField(t, createPayload, []string{"labels", "cluster"}, "test") - assertNestedStringField(t, createPayload, []string{"networking", "networkId"}, testNetworkID) - assertNestedStringField(t, createPayload, []string{"bootVolume", "performanceClass"}, "storage_premium_perf6") - assertNestedStringField(t, createPayload, []string{"bootVolume", "source", "id"}, testImageID) - assertNestedStringField(t, createPayload, []string{"bootVolume", "source", "type"}, "image") - assertNestedNumberField(t, createPayload, []string{"bootVolume", "size"}, 50) }) t.Run("creates server with imageId when rootVolume size and performanceClass are unset", func(t *testing.T) { diff --git a/cmd/manager/main.go b/cmd/manager/main.go index 682eb40..3dad71e 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -19,7 +19,9 @@ package main import ( "crypto/tls" "flag" + "fmt" "os" + "strings" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. @@ -27,10 +29,12 @@ import ( "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" + "k8s.io/apimachinery/pkg/util/validation" clientgoscheme "k8s.io/client-go/kubernetes/scheme" clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" "sigs.k8s.io/cluster-api/util/record" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/metrics/filters" @@ -63,6 +67,7 @@ func main() { var metricsCertPath, metricsCertName, metricsCertKey string var webhookCertPath, webhookCertName, webhookCertKey string var enableLeaderElection bool + var namespace, leaderElectionNamespace string var probeAddr string var secureMetrics bool var enableHTTP2 bool @@ -73,6 +78,9 @@ func main() { flag.BoolVar(&enableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") + flag.StringVar(&namespace, "namespace", "", "Namespace to watch. Leave empty to watch all namespaces.") + flag.StringVar(&leaderElectionNamespace, "leader-election-namespace", "", + "Namespace for leader election leases. Defaults to --namespace when set, otherwise the pod namespace.") flag.BoolVar(&secureMetrics, "metrics-secure", true, "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.") flag.StringVar(&webhookCertPath, "webhook-cert-path", "", "The directory that contains the webhook certificate.") @@ -159,7 +167,7 @@ func main() { metricsServerOptions.KeyName = metricsCertKey } - mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ + managerOptions := ctrl.Options{ Scheme: scheme, Metrics: metricsServerOptions, WebhookServer: webhookServer, @@ -177,7 +185,12 @@ func main() { // if you are doing or is intended to do any operation such as perform cleanups // after the manager stops then its usage might be unsafe. // LeaderElectionReleaseOnCancel: true, - }) + } + if err := configureNamespaces(&managerOptions, namespace, leaderElectionNamespace); err != nil { + setupLog.Error(err, "Invalid namespace configuration") + os.Exit(1) + } + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), managerOptions) if err != nil { setupLog.Error(err, "Failed to start manager") os.Exit(1) @@ -237,3 +250,27 @@ func main() { os.Exit(1) } } + +// configureNamespaces keeps the cached reads and watches in the requested +// namespace. The default lease namespace follows the watch namespace so that +// multiple namespaced managers can each elect a leader using namespaced RBAC. +func configureNamespaces(options *ctrl.Options, namespace, leaderElectionNamespace string) error { + for name, value := range map[string]string{ + "namespace": namespace, "leader-election-namespace": leaderElectionNamespace, + } { + if value == "" { + continue + } + if problems := validation.IsDNS1123Label(value); len(problems) > 0 { + return fmt.Errorf("--%s must be a valid namespace: %s", name, strings.Join(problems, "; ")) + } + } + if namespace != "" { + options.Cache.DefaultNamespaces = map[string]cache.Config{namespace: {}} + if leaderElectionNamespace == "" { + leaderElectionNamespace = namespace + } + } + options.LeaderElectionNamespace = leaderElectionNamespace + return nil +} diff --git a/cmd/manager/main_test.go b/cmd/manager/main_test.go new file mode 100644 index 0000000..e878eff --- /dev/null +++ b/cmd/manager/main_test.go @@ -0,0 +1,140 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package main + +import ( + "context" + "strings" + "testing" + "time" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/cache" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/envtest" +) + +func TestConfigureNamespaces(t *testing.T) { + for _, tt := range []struct { + name string + namespace string + leaderElectionNamespace string + wantLeaseNamespace string + wantError string + }{ + {name: "cluster wide defaults"}, + {name: "namespaced lease defaults", namespace: "clusters-example", wantLeaseNamespace: "clusters-example"}, + { + name: "explicit lease namespace", namespace: "clusters-example", + leaderElectionNamespace: "provider-system", wantLeaseNamespace: "provider-system", + }, + { + name: "cluster wide with explicit lease namespace", + leaderElectionNamespace: "provider-system", wantLeaseNamespace: "provider-system", + }, + {name: "reject multiple namespaces", namespace: "one,two", wantError: "--namespace"}, + {name: "reject whitespace", namespace: " ", wantError: "--namespace"}, + {name: "reject uppercase", namespace: "HostedCluster", wantError: "--namespace"}, + {name: "reject invalid lease namespace", leaderElectionNamespace: "one/two", wantError: "--leader-election-namespace"}, + } { + t.Run(tt.name, func(t *testing.T) { + options := ctrl.Options{} + err := configureNamespaces(&options, tt.namespace, tt.leaderElectionNamespace) + if tt.wantError != "" { + if err == nil || !strings.Contains(err.Error(), tt.wantError) { + t.Fatalf("configureNamespaces() error = %v, want %q", err, tt.wantError) + } + return + } + if err != nil { + t.Fatalf("configureNamespaces() error = %v", err) + } + if options.LeaderElectionNamespace != tt.wantLeaseNamespace { + t.Fatalf("lease namespace = %q, want %q", options.LeaderElectionNamespace, tt.wantLeaseNamespace) + } + if tt.namespace == "" { + if options.Cache.DefaultNamespaces != nil { + t.Fatal("cluster-wide manager unexpectedly restricts namespaces") + } + } else if _, ok := options.Cache.DefaultNamespaces[tt.namespace]; !ok || len(options.Cache.DefaultNamespaces) != 1 { + t.Fatalf("cache namespaces = %v, want only %q", options.Cache.DefaultNamespaces, tt.namespace) + } + }) + } +} + +// Exercise the actual controller-runtime cache: a namespaced deployment must +// neither observe nor read another hosted cluster's bootstrap or cloud secrets. +func TestNamespaceCacheIsolation(t *testing.T) { + testEnv := &envtest.Environment{} + config, err := testEnv.Start() + if err != nil { + t.Fatalf("start envtest: %v", err) + } + t.Cleanup(func() { + if err := testEnv.Stop(); err != nil { + t.Errorf("stop envtest: %v", err) + } + }) + liveClient, err := client.New(config, client.Options{Scheme: scheme}) + if err != nil { + t.Fatalf("create live client: %v", err) + } + ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second) + defer cancel() + for _, namespace := range []string{"hosted-one", "hosted-two"} { + if err := liveClient.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}); err != nil { + t.Fatalf("create namespace: %v", err) + } + if err := liveClient.Create(ctx, &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: "credentials"}, + Data: map[string][]byte{"value": []byte(namespace)}, + }); err != nil { + t.Fatalf("create secret: %v", err) + } + } + options := ctrl.Options{} + if err := configureNamespaces(&options, "hosted-one", ""); err != nil { + t.Fatal(err) + } + options.Cache.Scheme = scheme + scopedCache, err := cache.New(config, options.Cache) + if err != nil { + t.Fatalf("create namespace cache: %v", err) + } + cacheErrors := make(chan error, 1) + go func() { cacheErrors <- scopedCache.Start(ctx) }() + t.Cleanup(func() { + cancel() + if err := <-cacheErrors; err != nil { + t.Errorf("run cache: %v", err) + } + }) + secret := &corev1.Secret{} + if err := scopedCache.Get(ctx, client.ObjectKey{Namespace: "hosted-one", Name: "credentials"}, secret); err != nil { + t.Fatalf("get watched secret: %v", err) + } + if string(secret.Data["value"]) != "hosted-one" { + t.Fatalf("read wrong namespace's secret: %s", secret.Namespace) + } + if err := scopedCache.Get(ctx, client.ObjectKey{Namespace: "hosted-two", Name: "credentials"}, &corev1.Secret{}); err == nil { + t.Fatal("cache permitted a secret read outside its namespace") + } + secrets := &corev1.SecretList{} + if err := scopedCache.List(ctx, secrets); err != nil { + t.Fatalf("list watched secrets: %v", err) + } + if len(secrets.Items) != 1 || secrets.Items[0].Namespace != "hosted-one" { + t.Fatalf("cache list escaped namespace scope: %v", secrets.Items) + } +} diff --git a/controller/stackitcluster_controller.go b/controller/stackitcluster_controller.go index 5db54df..5b26709 100644 --- a/controller/stackitcluster_controller.go +++ b/controller/stackitcluster_controller.go @@ -32,6 +32,7 @@ import ( "k8s.io/apimachinery/pkg/types" clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" clusterutil "sigs.k8s.io/cluster-api/util" + "sigs.k8s.io/cluster-api/util/annotations" "sigs.k8s.io/cluster-api/util/collections" "sigs.k8s.io/cluster-api/util/record" ctrl "sigs.k8s.io/controller-runtime" @@ -100,6 +101,12 @@ func (r *StackitClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reque } return ctrl.Result{}, err } + // An external controller owns this infrastructure object's lifecycle and + // status. Do not add finalizers, patch conditions, or access cloud resources. + if annotations.IsExternallyManaged(stackitCluster) { + log.V(1).Info("Skipping externally managed StackitCluster") + return ctrl.Result{}, nil + } cluster, err := clusterutil.GetOwnerCluster(ctx, r.Client, stackitCluster.ObjectMeta) if err != nil { diff --git a/controller/stackitcluster_controller_test.go b/controller/stackitcluster_controller_test.go index 26a533c..6578706 100644 --- a/controller/stackitcluster_controller_test.go +++ b/controller/stackitcluster_controller_test.go @@ -224,6 +224,40 @@ var _ = Describe("StackitCluster Controller", func() { expectCondition(got.Status.Conditions, infrav1.ClusterLoadBalancerReadyCondition, metav1.ConditionTrue, "Skipped") }) + DescribeTable("leaves externally managed infrastructure untouched", func(managedBy string, deleting bool) { + got := &infrav1.StackitCluster{} + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + got.Annotations = map[string]string{clusterv1.ManagedByAnnotation: managedBy} + if deleting { + // Keep the object observable while deletion is in progress. The + // external controller is responsible for its own finalizers. + got.Finalizers = []string{"external.example.com/cleanup"} + } + Expect(k8sClient.Update(ctx, got)).To(Succeed()) + got.Status.Ready = true + got.Status.Initialization.Provisioned = true + Expect(k8sClient.Status().Update(ctx, got)).To(Succeed()) + if deleting { + Expect(k8sClient.Delete(ctx, got)).To(Succeed()) + } + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + before := got.DeepCopy() + reconciler.CloudClientFactory = func(context.Context, cloud.Credentials) (cloud.Client, error) { + Fail("externally managed clusters must not access cloud resources") + return nil, nil + } + + result, err := reconciler.Reconcile(ctx, request) + Expect(err).NotTo(HaveOccurred()) + Expect(result).To(Equal(reconcile.Result{})) + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + Expect(got).To(Equal(before)) + }, + Entry("during normal reconciliation", "external.example.com/controller", false), + Entry("when the annotation value is empty", "", false), + Entry("during deletion", "external.example.com/controller", true), + ) + It("creates the bastion and publishes its public IP when enabled", func() { got := &infrav1.StackitCluster{} Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) diff --git a/controller/stackitmachine_controller_test.go b/controller/stackitmachine_controller_test.go index 084b725..4761f72 100644 --- a/controller/stackitmachine_controller_test.go +++ b/controller/stackitmachine_controller_test.go @@ -140,6 +140,22 @@ var _ = Describe("StackitMachine Controller", func() { expectCondition(got.Status.Conditions, infrav1.MachineInstanceReadyCondition, metav1.ConditionTrue, "Available") }) + It("passes Ignition bootstrap data to the server without modification", func() { + ignition := []byte("{\n \"ignition\": {\"version\": \"3.2.0\", \"config\": {\"merge\": [{\"source\": \"https://ignition.example.com/worker\"}]}}\n}\n") + secret := &corev1.Secret{} + Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: bootstrapName}, secret)).To(Succeed()) + secret.Data = map[string][]byte{"value": ignition, "format": []byte("ignition")} + Expect(k8sClient.Update(ctx, secret)).To(Succeed()) + + result, err := reconciler.Reconcile(ctx, request) + Expect(err).NotTo(HaveOccurred()) + Expect(result).To(Equal(reconcile.Result{})) + got := &infrav1.StackitMachine{} + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + Expect(got.Status.Ready).To(BeTrue()) + Expect(fakeCloud.ServerUserData(got.Status.InstanceID)).To(Equal(ignition)) + }) + It("does not silently recreate the server of an already-provisioned machine", func() { // Recreating it would replay bootstrap data pinned to the previous // identity: the replacement either never rejoins or rejoins while Machine diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 7dcfa29..bd27e6d 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -14,6 +14,7 @@ - [OS Images](topics/images.md) - [Accessing VM instances](topics/accessing-vm-instances.md) - [HA, Availability Zones & Failure Domains](topics/failure-domains.md) + - [Hosted control planes](topics/hosted-control-planes.md) - [IAM Permissions Used](topics/iam-permissions.md) - [Developer Guide](development/index.md) - [Architecture](development/architecture.md) diff --git a/docs/src/topics/hosted-control-planes.md b/docs/src/topics/hosted-control-planes.md new file mode 100644 index 0000000..88e82e3 --- /dev/null +++ b/docs/src/topics/hosted-control-planes.md @@ -0,0 +1,80 @@ +# Hosted control planes + +CAPSTK can provision worker machines for an externally hosted control plane. +The hosting controller supplies the Cluster API `Cluster`, `Machine` resources, +and bootstrap Secrets. CAPSTK uses the Cluster API **v1beta2** contract; the +STACKIT infrastructure resources retain their **v1alpha1** API version. + +## Run a manager per namespace + +Use `--namespace` to limit every controller cache and watch, including Secrets, +to the hosted cluster's management namespace. Run the manager with a service +account whose Role and RoleBinding grant access only in that namespace. Cache +scoping complements Kubernetes RBAC; it does not replace RBAC. + +```yaml +containers: +- name: manager + image: + args: + - --namespace=$(POD_NAMESPACE) + - --leader-elect + - --health-probe-bind-address=:8081 + - --metrics-bind-address=0 + env: + - name: POD_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + - name: ENABLE_WEBHOOKS + value: "false" +``` + +With `--namespace` set, leader election leases default to that namespace. Use +`--leader-election-namespace` to override the lease namespace if needed. The +health and readiness endpoints are `/healthz` and `/readyz` on the configured +probe address. Without `--namespace`, existing cluster-wide behavior remains. + +Disabling webhooks prevents this manager from starting an admission server or +requiring serving certificates. Install the CRDs separately. Admission webhook +configurations, if installed, must still point to a running admission server; +do not point them at a manager with `ENABLE_WEBHOOKS=false`. + +Credentials and bootstrap Secrets must be in the watched namespace. Set +`StackitCluster.spec.credentialsSecretRef.namespace` to that namespace or omit +it. The credentials Secret requires `serviceaccount.json`; its optional +`project-id` key must match `StackitCluster.spec.projectID` when supplied. + +## Reuse the hosted API endpoint + +Configure a `StackitCluster` with the existing worker network, hosted API +endpoint, and `spec.apiServerLoadBalancer.enabled: false`. Leave +`spec.bastion.enabled` false when no CAPSTK-managed bastion is required. CAPSTK +validates the network and credentials and publishes infrastructure readiness +without creating an API server load balancer or control-plane machines. + +An external infrastructure controller can instead own the `StackitCluster` +lifecycle using the `cluster.x-k8s.io/managed-by` annotation. In that case CAPSTK +does not change the cluster, including its finalizers or status, or manage its +cloud resources. The external controller must populate `status.ready` and +`status.initialization.provisioned` for worker provisioning and CAPI readiness, +and keep the cluster and credentials available until its machines are deleted. +Do not annotate an existing CAPSTK-managed cluster without also transferring +responsibility for its cloud resources and finalizers. + +## Bootstrap worker machines + +Point each `Machine.spec.bootstrap.dataSecretName` at a Secret containing the +bootstrap document in `data.value` (or `data.userData` for legacy consumers). +CAPSTK treats the bytes as opaque: cloud-init and Ignition JSON are forwarded +unchanged, then base64-encoded once for the STACKIT API. CAPSTK does not render, +merge, or inject configuration into the document. + +Select an image that supports the bootstrap format and STACKIT's metadata +service or config drive. A native `stackit` CoreOS image also needs the STACKIT +providers in Ignition and Afterburn; changing the image's platform ID does not +add those providers. Track image availability in the +[Fedora CoreOS platform request](https://github.com/coreos/fedora-coreos-tracker/issues/2175). +The control-plane integration remains responsible for generating the matching +bootstrap data, configuring the node's cloud provider and networking, and +validating the chosen image with an end-to-end worker lifecycle test. diff --git a/util/bootstrap.go b/util/bootstrap.go index 179ebe9..5034819 100644 --- a/util/bootstrap.go +++ b/util/bootstrap.go @@ -26,10 +26,10 @@ const ( // does not contain one of the expected keys. var ErrBootstrapDataInvalid = errors.New("bootstrap data invalid: secret has no \"value\" or \"userData\" key") -// ExtractBootstrapData returns the bootstrap payload from a Secret produced -// by CABPK/KubeadmConfig. Per spec section 13.2 it checks "value" first, then -// "userData". It returns ErrBootstrapDataInvalid when neither key is present -// or both are empty. +// ExtractBootstrapData returns an opaque bootstrap payload from a Secret. +// Both cloud-init and Ignition documents are passed through unchanged. It +// checks "value" first, then "userData", and returns ErrBootstrapDataInvalid +// when neither key is present or both are empty. func ExtractBootstrapData(secret *corev1.Secret) ([]byte, error) { if data, ok := secret.Data["value"]; ok && len(data) > 0 { return data, nil From 0cf5d536ff347d681ec38489734c0f14b870c6d7 Mon Sep 17 00:00:00 2001 From: Rick Rackow Date: Tue, 22 Sep 2026 09:04:40 +0200 Subject: [PATCH 2/2] fix: retain infrastructure until credential recovery Signed-off-by: Rick Rackow Signed-off-by: Jan Larwig --- controller/controller_test_helpers_test.go | 6 +- controller/stackitcluster_controller.go | 14 +--- controller/stackitcluster_controller_test.go | 77 ++++++++++++-------- controller/stackitmachine_controller.go | 25 ++----- controller/stackitmachine_controller_test.go | 45 +++++++++++- docs/src/usage/cleanup.md | 14 ++++ 6 files changed, 118 insertions(+), 63 deletions(-) diff --git a/controller/controller_test_helpers_test.go b/controller/controller_test_helpers_test.go index 8407de9..06f4faf 100644 --- a/controller/controller_test_helpers_test.go +++ b/controller/controller_test_helpers_test.go @@ -32,11 +32,11 @@ const ( testImageID = "33333333-3333-3333-3333-333333333333" ) -func createCredentialsSecret(ctx context.Context, name, namespace, projectID string) { +func createCredentialsSecret(ctx context.Context, name string) { secret := &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace}, + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "default"}, Data: map[string][]byte{ - "project-id": []byte(projectID), + "project-id": []byte(testProjectID), "serviceaccount.json": []byte("{}"), }, } diff --git a/controller/stackitcluster_controller.go b/controller/stackitcluster_controller.go index 5b26709..650a8b7 100644 --- a/controller/stackitcluster_controller.go +++ b/controller/stackitcluster_controller.go @@ -485,21 +485,11 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS // tolerate NotFound. cloudClient, err := util.BuildCloudClient(ctx, r.Client, r.CloudClientFactory, stackitCluster) if err != nil { - // A missing credentials Secret cannot be recovered from and commonly - // disappears first during namespace teardown, so finalize and make the - // possible leak loud rather than stranding the cluster in Terminating. - // Every other credentials problem is fixable and keeps retrying. - if apierrors.IsNotFound(err) { - log.Error(err, "Credentials secret is gone; finalizing without cloud cleanup", "cluster", stackitCluster.Name) - record.Warnf(stackitCluster, "CleanupSkipped", - "Credentials Secret is gone; finalizing without cloud cleanup. "+ - "Any remaining STACKIT resources for this cluster must be removed manually: %v", err) - controllerutil.RemoveFinalizer(stackitCluster, infrav1.ClusterFinalizer) - return ctrl.Result{}, nil - } log.Error(err, "Failed to build cloud client for cluster deletion", "cluster", stackitCluster.Name) record.Warnf(stackitCluster, "InvalidCredentials", "Failed to build cloud client for deletion: %v", err) + // A missing or invalid Secret can be restored. Retain the finalizer + // until the cloud confirms that provider-owned resources are removed. return clusterScope.CredentialFailureResult( err, credentialsRetryRequeueAfter, diff --git a/controller/stackitcluster_controller_test.go b/controller/stackitcluster_controller_test.go index 6578706..a7ee8b2 100644 --- a/controller/stackitcluster_controller_test.go +++ b/controller/stackitcluster_controller_test.go @@ -64,7 +64,7 @@ var _ = Describe("StackitCluster Controller", func() { }, } - createCredentialsSecret(ctx, credentials, namespace, testProjectID) + createCredentialsSecret(ctx, credentials) createOwnerCluster(ctx, clusterName) stackitClust = newStackitCluster(clusterName, namespace, true) stackitClust.Spec.CredentialsSecretRef.Name = credentials @@ -436,40 +436,59 @@ var _ = Describe("StackitCluster Controller", func() { Expect(fakeCloud.SecurityGroupCount()).To(Equal(0)) }) - It("finalizes deletion when the credentials Secret is already gone", func() { - // The Secret commonly disappears first during namespace teardown, and - // without it no cloud client can be built at all. - createOwnerCluster(ctx, clusterName+"-nocreds") - defer deleteIfExists(ctx, &clusterv1.Cluster{ - ObjectMeta: metav1.ObjectMeta{Name: clusterName + "-nocreds", Namespace: namespace}, - }) - orphaned := newStackitCluster(clusterName+"-nocreds", namespace, false) - orphaned.Spec.CredentialsSecretRef.Name = credentials - orphaned.Spec.Bastion = validBastionSpec() - Expect(k8sClient.Create(ctx, orphaned)).To(Succeed()) - defer deleteIfExists(ctx, orphaned) - - key := types.NamespacedName{Namespace: namespace, Name: orphaned.Name} - req := reconcile.Request{NamespacedName: key} - _, err := reconciler.Reconcile(ctx, req) + DescribeTable("retains cluster resources until credentials are restored", func(missing bool) { + got := &infrav1.StackitCluster{} + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + got.Spec.Bastion = validBastionSpec() + Expect(k8sClient.Update(ctx, got)).To(Succeed()) + _, err := reconciler.Reconcile(ctx, request) Expect(err).NotTo(HaveOccurred()) - By("removing the credentials Secret, as namespace teardown would") - Expect(k8sClient.Delete(ctx, &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{Name: credentials, Namespace: namespace}, - })).To(Succeed()) - - got := &infrav1.StackitCluster{} - Expect(k8sClient.Get(ctx, key, got)).To(Succeed()) + secret := &corev1.Secret{} + Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: credentials}, secret)).To(Succeed()) + if missing { + Expect(k8sClient.Delete(ctx, secret)).To(Succeed()) + } else { + secret.Data = nil + Expect(k8sClient.Update(ctx, secret)).To(Succeed()) + } + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) Expect(k8sClient.Delete(ctx, got)).To(Succeed()) - _, err = reconciler.Reconcile(ctx, req) - Expect(err).NotTo(HaveOccurred(), "deletion must not block on a Secret that can never come back") + result, err := reconciler.Reconcile(ctx, request) + if missing { + Expect(apierrors.IsNotFound(err)).To(BeTrue()) + } else { + Expect(err).NotTo(HaveOccurred()) + Expect(result.RequeueAfter).To(Equal(credentialsRetryRequeueAfter)) + } + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + Expect(got.Finalizers).To(ContainElement(infrav1.ClusterFinalizer)) + expectCondition(got.Status.Conditions, infrav1.ClusterCredentialsReadyCondition, metav1.ConditionFalse, "CredentialsInvalid") + Expect(fakeCloud.LoadBalancerCount()).To(Equal(1)) + Expect(fakeCloud.ServerCount()).To(Equal(1)) + Expect(fakeCloud.PublicIPCount()).To(Equal(1)) + Expect(fakeCloud.SecurityGroupCount()).To(Equal(2)) + + By("restoring credentials and completing cleanup") + deleteIfExists(ctx, secret) + createCredentialsSecret(ctx, credentials) + _, err = reconciler.Reconcile(ctx, request) + Expect(err).NotTo(HaveOccurred()) + Expect(fakeCloud.LoadBalancerCount()).To(Equal(0)) + Expect(fakeCloud.ServerCount()).To(Equal(0)) + Expect(fakeCloud.PublicIPCount()).To(Equal(0)) + Expect(fakeCloud.SecurityGroupCount()).To(Equal(0)) + _, err = fakeCloud.GetNetwork(ctx, testNetworkID) + Expect(err).NotTo(HaveOccurred(), "cluster cleanup must preserve the user-owned network") Eventually(func() bool { - return apierrors.IsNotFound(k8sClient.Get(ctx, key, &infrav1.StackitCluster{})) - }).Should(BeTrue(), "cluster stayed in Terminating because the finalizer was never removed") - }) + return apierrors.IsNotFound(k8sClient.Get(ctx, stackitKey, &infrav1.StackitCluster{})) + }).Should(BeTrue()) + }, + Entry("when the Secret is missing", true), + Entry("when the Secret is invalid", false), + ) It("requeues on invalid credentials during deletion and keeps the finalizer until corrected", func() { _, err := reconciler.Reconcile(ctx, request) diff --git a/controller/stackitmachine_controller.go b/controller/stackitmachine_controller.go index b8b5de4..e746f67 100644 --- a/controller/stackitmachine_controller.go +++ b/controller/stackitmachine_controller.go @@ -132,7 +132,7 @@ func (r *StackitMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque machineScope.SetPausedCondition(false, "") if !stackitMachine.DeletionTimestamp.IsZero() { - return ctrl.Result{}, r.reconcileDelete(ctx, machineScope) + return r.reconcileDelete(ctx, machineScope) } return r.reconcileNormal(ctx, machineScope) } @@ -294,7 +294,7 @@ func validateMachineAvailabilityZone(machineScope *scope.MachineScope) error { return nil } -func (r *StackitMachineReconciler) reconcileDelete(ctx context.Context, machineScope *scope.MachineScope) error { +func (r *StackitMachineReconciler) reconcileDelete(ctx context.Context, machineScope *scope.MachineScope) (ctrl.Result, error) { log := logf.FromContext(ctx) stackitMachine := machineScope.StackitMachine @@ -302,27 +302,16 @@ func (r *StackitMachineReconciler) reconcileDelete(ctx context.Context, machineS // deletion asks the cloud before dropping the finalizer. cloudClient, err := util.BuildCloudClient(ctx, r.Client, r.CloudClientFactory, machineScope.StackitCluster) if err != nil { - // A missing credentials Secret cannot be recovered from and commonly - // disappears first during namespace teardown, so finalize and make the - // possible leak loud rather than stranding the Machine in Terminating. - // Every other credentials problem is fixable and keeps retrying. - if apierrors.IsNotFound(err) { - log.Error(err, "Credentials secret is gone; finalizing without cloud cleanup", "machine", stackitMachine.Name) - record.Warnf(stackitMachine, "CleanupSkipped", - "Credentials Secret is gone; finalizing without cloud cleanup. "+ - "Any remaining STACKIT server for this machine must be removed manually: %v", err) - controllerutil.RemoveFinalizer(stackitMachine, infrav1.MachineFinalizer) - return nil - } log.Error(err, "Failed to build cloud client for machine deletion", "machine", stackitMachine.Name) record.Warnf(stackitMachine, "InvalidCredentials", "Failed to build cloud client for deletion: %v", err) - _, resultErr := machineScope.CredentialFailureResult( + // Credentials can be restored. Keep the finalizer until cloud cleanup + // succeeds, and propagate timed retries for invalid credentials too. + return machineScope.CredentialFailureResult( err, credentialsRetryRequeueAfter, infrav1.MachineCredentialsReadyCondition, ) - return resultErr } machineScope.SetCloudClient(cloudClient) @@ -331,7 +320,7 @@ func (r *StackitMachineReconciler) reconcileDelete(ctx context.Context, machineS log.Error(err, "Failed to delete server", "machine", stackitMachine.Name, "instanceID", instanceID) record.Warnf(stackitMachine, "FailedDeleteInstance", "Failed to delete server %s: %v", instanceID, err) - return err + return ctrl.Result{}, err } if instanceID != "" { record.Eventf( @@ -339,7 +328,7 @@ func (r *StackitMachineReconciler) reconcileDelete(ctx context.Context, machineS ) } controllerutil.RemoveFinalizer(stackitMachine, infrav1.MachineFinalizer) - return nil + return ctrl.Result{}, nil } func (r *StackitMachineReconciler) fetchBootstrapData(ctx context.Context, machine *clusterv1.Machine) ([]byte, metav1.ConditionStatus, string, string) { diff --git a/controller/stackitmachine_controller_test.go b/controller/stackitmachine_controller_test.go index 4761f72..24a4611 100644 --- a/controller/stackitmachine_controller_test.go +++ b/controller/stackitmachine_controller_test.go @@ -70,7 +70,7 @@ var _ = Describe("StackitMachine Controller", func() { }, } - createCredentialsSecret(ctx, credentials, namespace, testProjectID) + createCredentialsSecret(ctx, credentials) createOwnerCluster(ctx, clusterName) createReadyStackitCluster(ctx, clusterName, namespace, credentials) createOwnerMachine(ctx, machineName, clusterName, stackitName) @@ -638,6 +638,49 @@ var _ = Describe("StackitMachine Controller", func() { }).Should(BeTrue()) }) + DescribeTable("retains the server until credentials are restored", func(missing bool) { + _, err := reconciler.Reconcile(ctx, request) + Expect(err).NotTo(HaveOccurred()) + secret := &corev1.Secret{} + Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: credentials}, secret)).To(Succeed()) + if missing { + Expect(k8sClient.Delete(ctx, secret)).To(Succeed()) + } else { + secret.Data = nil + Expect(k8sClient.Update(ctx, secret)).To(Succeed()) + } + got := &infrav1.StackitMachine{} + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + Expect(k8sClient.Delete(ctx, got)).To(Succeed()) + + result, err := reconciler.Reconcile(ctx, request) + if missing { + Expect(apierrors.IsNotFound(err)).To(BeTrue()) + } else { + Expect(err).NotTo(HaveOccurred()) + Expect(result.RequeueAfter).To(Equal(credentialsRetryRequeueAfter)) + } + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + Expect(got.Finalizers).To(ContainElement(infrav1.MachineFinalizer)) + expectCondition(got.Status.Conditions, infrav1.MachineCredentialsReadyCondition, metav1.ConditionFalse, "CredentialsInvalid") + Expect(fakeCloud.ServerCount()).To(Equal(1)) + + By("restoring credentials and completing server cleanup") + deleteIfExists(ctx, secret) + createCredentialsSecret(ctx, credentials) + _, err = reconciler.Reconcile(ctx, request) + Expect(err).NotTo(HaveOccurred()) + Expect(fakeCloud.ServerCount()).To(Equal(0)) + _, err = fakeCloud.GetNetwork(ctx, testNetworkID) + Expect(err).NotTo(HaveOccurred(), "machine cleanup must preserve the user-owned network") + Eventually(func() bool { + return apierrors.IsNotFound(k8sClient.Get(ctx, stackitKey, &infrav1.StackitMachine{})) + }).Should(BeTrue()) + }, + Entry("when the Secret is missing", true), + Entry("when the Secret is invalid", false), + ) + It("keeps the finalizer when the server deletion fails", func() { _, err := reconciler.Reconcile(ctx, request) Expect(err).NotTo(HaveOccurred()) diff --git a/docs/src/usage/cleanup.md b/docs/src/usage/cleanup.md index 51c78d6..67f1fb6 100644 --- a/docs/src/usage/cleanup.md +++ b/docs/src/usage/cleanup.md @@ -14,6 +14,20 @@ kubectl get machine,stackitmachine,stackitcluster \ -l "cluster.x-k8s.io/cluster-name=${CLUSTER_NAME}" ``` +Keep the credentials Secret until all `StackitMachine` and `StackitCluster` +resources have finished deleting. Missing or invalid credentials leave these +resources in `Terminating` with their finalizers intact and a +`CredentialsReady=False` condition. The controllers retry cleanup once valid +credentials are restored; they do not abandon running cloud resources by +removing finalizers after a credential error. + +Delete the Cluster before deleting its management namespace. Kubernetes does +not allow creating a replacement Secret in a terminating namespace, so restore +credentials before namespace deletion. If recovery requires manual cleanup, +verify that all provider-owned resources have been removed before explicitly +removing their finalizers. Existing networks supplied to CAPSTK are user-owned +and are preserved during cluster deletion. + Real e2e resources are labeled so they can be cleaned up directly through the STACKIT API if Kubernetes cleanup fails: