From cd08d7ad53922d881161862ad1f9c19c415447da Mon Sep 17 00:00:00 2001 From: Moritz Clasmeier Date: Fri, 17 Apr 2026 12:51:40 +0200 Subject: [PATCH] Properly support overwriting clusterName --- internal/deployer/crs.go | 5 +++-- internal/deployer/deploy_via_operator.go | 24 ++++++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/internal/deployer/crs.go b/internal/deployer/crs.go index 02639aa..c31549a 100644 --- a/internal/deployer/crs.go +++ b/internal/deployer/crs.go @@ -9,7 +9,8 @@ import ( // generateCRS generates the Central Resource Secret using roxctl func (d *Deployer) generateCRS(ctx context.Context, clusterName string) (string, error) { - d.logger.Info("Generating CRS with roxctl...") + crsName := fmt.Sprintf("%s-crs", clusterName) + d.logger.Infof("Generating CRS named %q with roxctl...", crsName) result, err := d.runRoxctl(ctx, RoxctlOptions{ Args: []string{ @@ -17,7 +18,7 @@ func (d *Deployer) generateCRS(ctx context.Context, clusterName string) (string, "central", "crs", "generate", - clusterName, + crsName, "--output=-", // Output to stdout }, UseAuthentication: true, diff --git a/internal/deployer/deploy_via_operator.go b/internal/deployer/deploy_via_operator.go index 4fe2134..6187419 100644 --- a/internal/deployer/deploy_via_operator.go +++ b/internal/deployer/deploy_via_operator.go @@ -12,6 +12,7 @@ import ( "github.com/stackrox/roxie/internal/env" "github.com/stackrox/roxie/internal/helpers" "gopkg.in/yaml.v3" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" ) var ( @@ -623,7 +624,19 @@ func (d *Deployer) deploySecuredClusterOperator(ctx context.Context, resources s return fmt.Errorf("failed to prepare namespace: %w", err) } - clusterName := generateClusterName() + securedClusterCR, err := d.createSecuredClusterCR(resources) + if err != nil { + return fmt.Errorf("failed to create SecuredCluster CR: %w", err) + } + + clusterName, found, err := unstructured.NestedString(securedClusterCR, "spec", "clusterName") + if err != nil { + return fmt.Errorf("failed to get cluster name from SecuredCluster CR: %w", err) + } + if !found || clusterName == "" { + return fmt.Errorf("cluster name not found in SecuredCluster CR") + } + d.logger.Infof("Using cluster name: %s", clusterName) crsContent, err := d.generateCRS(ctx, clusterName) if err != nil { @@ -634,11 +647,6 @@ func (d *Deployer) deploySecuredClusterOperator(ctx context.Context, resources s return fmt.Errorf("failed to apply CRS: %w", err) } - securedClusterCR, err := d.createSecuredClusterCR(clusterName, resources) - if err != nil { - return fmt.Errorf("failed to create SecuredCluster CR: %w", err) - } - if err := d.applySecuredClusterCR(ctx, securedClusterCR); err != nil { return fmt.Errorf("failed to apply SecuredCluster CR: %w", err) } @@ -656,7 +664,7 @@ func (d *Deployer) deploySecuredClusterOperator(ctx context.Context, resources s } // createSecuredClusterCR creates the SecuredCluster custom resource. -func (d *Deployer) createSecuredClusterCR(clusterName, resources string) (map[string]interface{}, error) { +func (d *Deployer) createSecuredClusterCR(resources string) (map[string]interface{}, error) { base := map[string]interface{}{ "apiVersion": "platform.stackrox.io/v1alpha1", "kind": "SecuredCluster", @@ -668,7 +676,7 @@ func (d *Deployer) createSecuredClusterCR(clusterName, resources string) (map[st }, }, "spec": map[string]interface{}{ - "clusterName": clusterName, + "clusterName": generateClusterName(), // Just a default, can be overwritten. "centralEndpoint": internalCentralEndpoint(d.centralNamespace), "imagePullSecrets": []map[string]string{ {"name": "stackrox"},