Skip to content

Commit b4991fe

Browse files
mclasmeierMoritz Clasmeier
andauthored
Ensure pull secrets exist for operator when deploying in Konflux mode (#171)
Co-authored-by: Moritz Clasmeier <mclasmeier@redhat.com>
1 parent 15fac92 commit b4991fe

6 files changed

Lines changed: 23 additions & 28 deletions

File tree

internal/deployer/deploy_via_operator.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ func (d *Deployer) ensureOperatorDeployed(ctx context.Context) error {
117117
func (d *Deployer) deployCentralOperator(ctx context.Context, resources, exposure string) error {
118118
d.logger.Info("🚀 Deploying Central via Operator...")
119119

120-
if err := d.prepareNamespace(ctx, d.centralNamespace); err != nil {
120+
needPullSecrets := env.GetCurrentClusterType() != env.InfraOpenShift4
121+
if err := d.prepareNamespace(ctx, d.centralNamespace, needPullSecrets); err != nil {
121122
return fmt.Errorf("failed to prepare namespace: %w", err)
122123
}
123124

@@ -185,14 +186,14 @@ func (d *Deployer) getDeployedOperatorImage(ctx context.Context) (string, error)
185186
}
186187

187188
// prepareNamespace creates pull secrets in the namespace if needed
188-
func (d *Deployer) prepareNamespace(ctx context.Context, namespace string) error {
189+
func (d *Deployer) prepareNamespace(ctx context.Context, namespace string, needPullSecrets bool) error {
189190
d.logger.Infof("Preparing namespace %s", namespace)
190191

191192
if err := d.ensureNamespaceExists(namespace); err != nil {
192193
return err
193194
}
194195

195-
if env.GetCurrentClusterType() != env.InfraOpenShift4 {
196+
if needPullSecrets {
196197
if err := d.ensurePullSecretExists(ctx, namespace); err != nil {
197198
return fmt.Errorf("ensuring image pull secret exists: %w", err)
198199
}
@@ -202,9 +203,11 @@ func (d *Deployer) prepareNamespace(ctx context.Context, namespace string) error
202203
}
203204

204205
func (d *Deployer) ensurePullSecretExists(ctx context.Context, namespace string) error {
205-
// Assemble pull secret YAML from pre-verified credentials
206-
pullSecretYAML := d.dockerAuth.CreatePullSecretYAMLFromCredentials(d.dockerCreds, namespace)
206+
if d.dockerCreds == nil {
207+
return errors.New("no pull secrets available to set up on the cluster")
208+
}
207209

210+
pullSecretYAML := d.dockerAuth.CreatePullSecretYAMLFromCredentials(*d.dockerCreds, namespace)
208211
_, err := d.runKubectl(ctx, k8s.KubectlOptions{
209212
Args: []string{"apply", "-f", "-"},
210213
Stdin: strings.NewReader(pullSecretYAML),
@@ -647,7 +650,8 @@ func (d *Deployer) configureCentralEndpoint(ctx context.Context, exposure string
647650
func (d *Deployer) deploySecuredClusterOperator(ctx context.Context, resources string) error {
648651
d.logger.Info("🚀 Deploying SecuredCluster via Operator...")
649652

650-
if err := d.prepareNamespace(ctx, d.sensorNamespace); err != nil {
653+
needPullSecrets := env.GetCurrentClusterType() != env.InfraOpenShift4
654+
if err := d.prepareNamespace(ctx, d.sensorNamespace, needPullSecrets); err != nil {
651655
return fmt.Errorf("failed to prepare namespace: %w", err)
652656
}
653657

internal/deployer/deployer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type Deployer struct {
7777
dockerCreds *dockerauth.Credentials
7878
clusterResourceKinds map[string]struct{}
7979
tempDir string
80+
useOperatorPullSecrets bool
8081
}
8182

8283
type ResourceToDelete struct {

internal/deployer/operator.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,14 @@ func (d *Deployer) deployOperatorFromCSV(ctx context.Context, bundleDir string)
319319
}
320320

321321
serviceAccountName := deploymentSpec["service_account"].(string)
322+
d.useOperatorPullSecrets = d.useKonflux && env.GetCurrentClusterType() != env.InfraOpenShift4
322323

323324
d.logger.Info("📋 Operator deployment plan:")
324325
d.logger.Dim(fmt.Sprintf(" • Namespace: %s", operatorNamespace))
325326
d.logger.Dim(fmt.Sprintf(" • ServiceAccount: %s", serviceAccountName))
327+
d.logger.Dim(fmt.Sprintf(" • Setting up pull secrets: %v", d.useOperatorPullSecrets))
326328

327-
if err := d.createOperatorNamespace(ctx); err != nil {
329+
if err := d.prepareNamespace(ctx, operatorNamespace, d.useOperatorPullSecrets); err != nil {
328330
return err
329331
}
330332

@@ -392,24 +394,6 @@ func (d *Deployer) parseCSVDeploymentSpec(csvFile string) (map[string]interface{
392394
return deploymentSpec, nil
393395
}
394396

395-
// createOperatorNamespace creates the operator namespace
396-
func (d *Deployer) createOperatorNamespace(ctx context.Context) error {
397-
nsYAML := fmt.Sprintf(`apiVersion: v1
398-
kind: Namespace
399-
metadata:
400-
name: %s
401-
labels:
402-
name: %s
403-
app.kubernetes.io/managed-by: roxie
404-
`, operatorNamespace, operatorNamespace)
405-
406-
_, err := d.runKubectl(ctx, k8s.KubectlOptions{
407-
Args: []string{"apply", "-f", "-"},
408-
Stdin: strings.NewReader(nsYAML),
409-
})
410-
return err
411-
}
412-
413397
// createServiceAccount creates a service account
414398
func (d *Deployer) createServiceAccount(ctx context.Context, namespace, name string) error {
415399
sa := map[string]interface{}{
@@ -422,6 +406,12 @@ func (d *Deployer) createServiceAccount(ctx context.Context, namespace, name str
422406
},
423407
}
424408

409+
if d.useOperatorPullSecrets {
410+
sa["imagePullSecrets"] = []map[string]string{
411+
{"name": "stackrox"},
412+
}
413+
}
414+
425415
yamlData, err := yaml.Marshal(sa)
426416
if err != nil {
427417
return fmt.Errorf("failed to marshal ServiceAccount '%s/%s': %w", namespace, name, err)

internal/deployer/operator_olm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (d *Deployer) deployOperatorViaOLM(ctx context.Context) error {
4040
indexImage := d.getOperatorIndexImage()
4141
d.logger.Infof("Index image: %s", indexImage)
4242

43-
if err := d.createOperatorNamespace(ctx); err != nil {
43+
if err := d.prepareNamespace(ctx, operatorNamespace, false); err != nil {
4444
return err
4545
}
4646

internal/dockerauth/dockerauth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (d *DockerAuth) VerifyCredentials(username, password string) error {
216216
}
217217

218218
// CreatePullSecretYAMLFromCredentials creates Kubernetes pull secret YAML from verified credentials.
219-
func (d *DockerAuth) CreatePullSecretYAMLFromCredentials(creds *Credentials, namespace string) string {
219+
func (d *DockerAuth) CreatePullSecretYAMLFromCredentials(creds Credentials, namespace string) string {
220220
// Create auth string
221221
authString := fmt.Sprintf("%s:%s", creds.Username, creds.Password)
222222
encodedAuth := base64.StdEncoding.EncodeToString([]byte(authString))

internal/dockerauth/dockerauth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestGetAndVerifyCredentialsFromEnv(t *testing.T) {
3131
}
3232

3333
// Test creating YAML from credentials
34-
yamlText := da.CreatePullSecretYAMLFromCredentials(creds, "ns")
34+
yamlText := da.CreatePullSecretYAMLFromCredentials(*creds, "ns")
3535

3636
// Verify YAML structure
3737
if !strings.Contains(yamlText, "apiVersion: v1") {

0 commit comments

Comments
 (0)