-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoperator.go
More file actions
655 lines (550 loc) · 20.7 KB
/
Copy pathoperator.go
File metadata and controls
655 lines (550 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
package deployer
import (
"bytes"
"context"
"crypto/rand"
"errors"
"fmt"
"math/big"
"os"
"path/filepath"
"strings"
"time"
"gopkg.in/yaml.v3"
"github.com/stackrox/roxie/internal/env"
"github.com/stackrox/roxie/internal/k8s"
"github.com/stackrox/roxie/internal/ocihelper"
)
const (
adminPasswordSecretName = "admin-password"
operatorNamespace = "rhacs-operator-system"
operatorDeploymentName = "rhacs-operator-controller-manager"
operatorBundleImageRepo = "quay.io/rhacs-eng/stackrox-operator-bundle"
operatorBundleImageReleaseRepo = "quay.io/rhacs-eng/release-operator-bundle"
)
var requiredCRDs = []string{
"centrals.platform.stackrox.io",
"securedclusters.platform.stackrox.io",
"securitypolicies.config.stackrox.io",
}
// deployOperatorNonOLM deploys the RHACS operator without OLM
func (d *Deployer) deployOperatorNonOLM(ctx context.Context) error {
d.logger.Infof("Operator tag: %s", d.operatorTag)
if d.useKonflux {
if err := d.ensureKonfluxImageRewriting(ctx); err != nil {
return fmt.Errorf("failed to configure Konflux image rewriting: %w", err)
}
} else {
if err := d.removeKonfluxImageRewriting(ctx); err != nil {
return fmt.Errorf("failed to remove Konflux ImageContentSourcePolicy: %v", err)
}
}
bundleImage := d.getOperatorBundleImage()
bundleDir, err := d.downloadAndExtractOperatorBundle(ctx, bundleImage)
if err != nil {
return fmt.Errorf("failed to download operator bundle: %w", err)
}
defer d.cleanupTempDir(bundleDir, "operator bundle directory")
d.logger.Infof("Bundle image: %s", bundleImage)
crdFiles, err := d.identifyCRDFileNames(bundleDir)
if err != nil {
return err
}
if err := d.applyCRDsToCluster(ctx, crdFiles); err != nil {
return err
}
if err := d.deployOperatorFromCSV(ctx, bundleDir); err != nil {
return err
}
return nil
}
// downloadAndExtractOperatorBundle downloads and extracts the operator bundle
func (d *Deployer) downloadAndExtractOperatorBundle(ctx context.Context, bundleImage string) (string, error) {
bundleDir, err := os.MkdirTemp(d.tempDir, "stackrox-operator-bundle-")
if err != nil {
return "", fmt.Errorf("failed to create temp dir: %w", err)
}
d.logger.Dimf("Created temporary directory: %s", bundleDir)
d.logger.Info("Pulling and extracting operator bundle image...")
// The bundle images only contain platform-agnostic YAML files.
if err := ocihelper.ExtractManifestsFromImage(ctx, d.logger, bundleImage, bundleDir); err != nil {
os.RemoveAll(bundleDir)
return "", fmt.Errorf("failed to copy bundle contents: %w", err)
}
d.logger.Successf("✓ Bundle extracted to: %s", bundleDir)
return bundleDir, nil
}
// identifyCRDFileNames identifies CRD files in the bundle directory.
// Returns list of CRD files found in the bundle.
func (d *Deployer) identifyCRDFileNames(bundleDir string) ([]string, error) {
var crdFiles []string
err := filepath.Walk(bundleDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(info.Name(), ".yaml") && !strings.HasSuffix(info.Name(), ".yml") {
return nil
}
content, err := os.ReadFile(path)
if err != nil {
d.logger.Warningf("Failed to read file %q from extracted bundle: %v", path, err)
return nil
}
var meta struct {
Kind string `yaml:"kind"`
}
if err := yaml.Unmarshal(content, &meta); err != nil {
d.logger.Warningf("Failed to unmarshal file %q from extracted bundle: %v", path, err)
return nil
}
if meta.Kind == "CustomResourceDefinition" {
crdFiles = append(crdFiles, path)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to walk bundle directory: %w", err)
}
return crdFiles, nil
}
// applyCRDsToCluster applies CRD files to the cluster
func (d *Deployer) applyCRDsToCluster(ctx context.Context, crdFiles []string) error {
d.logger.Infof("Applying %d CRD(s) to cluster", len(crdFiles))
for _, crdFile := range crdFiles {
result, err := d.runKubectl(ctx, k8s.KubectlOptions{
Args: []string{"apply", "-f", crdFile},
})
if err != nil {
d.logger.Errorf("kubectl stderr: %s", result.Stderr)
return fmt.Errorf("failed to apply CRD %s: %w\nStderr: %s", crdFile, err, result.Stderr)
}
basename := filepath.Base(crdFile)
d.logger.Successf("✓ Successfully applied CRD %s", basename)
}
return nil
}
// ensureCRDsInstalled ensures required CRDs exist
func (d *Deployer) ensureCRDsInstalled(ctx context.Context) error {
var missing []string
for _, crd := range requiredCRDs {
_, err := d.runKubectl(ctx, k8s.KubectlOptions{
Args: []string{"get", "crd", crd},
})
if err != nil {
missing = append(missing, crd)
}
}
if len(missing) > 0 {
bundleImage := d.getOperatorBundleImage()
d.logger.Warningf("Missing CRDs detected (%s)", strings.Join(missing, ", "))
d.logger.Warningf("Fetching bundle %s", bundleImage)
bundleDir, err := d.downloadAndExtractOperatorBundle(ctx, bundleImage)
if err != nil {
return err
}
defer d.cleanupTempDir(bundleDir, "CRD bundle directory")
crdFiles, err := d.identifyCRDFileNames(bundleDir)
if err != nil {
return err
}
return d.applyCRDsToCluster(ctx, crdFiles)
}
return nil
}
func (d *Deployer) getOperatorBundleImage() string {
if d.useKonflux {
d.logger.Infof("Using Konflux-built operator bundle image")
return fmt.Sprintf(operatorBundleImageReleaseRepo+":v%s", d.operatorTag)
}
return fmt.Sprintf(operatorBundleImageRepo+":v%s", d.operatorTag)
}
// ensureKonfluxImageRewriting configures image rewriting for Konflux images
func (d *Deployer) ensureKonfluxImageRewriting(ctx context.Context) error {
if !env.GetCurrentClusterType().IsOpenShift() {
return errors.New("image rewriting for Konflux is only supported on OpenShift4 clusters")
}
d.logger.Info("Configuring ImageContentSourcePolicy for Konflux images on OpenShift4...")
return d.applyImageContentSourcePolicy(ctx)
}
// applyImageContentSourcePolicy creates the ImageContentSourcePolicy for Konflux image mirrors
func (d *Deployer) applyImageContentSourcePolicy(ctx context.Context) error {
// Define repository digest mirrors as Go data structures
rewrite := func(from, to string) map[string]interface{} {
source := fmt.Sprintf("registry.redhat.io/advanced-cluster-security/%s", from)
mirror := fmt.Sprintf("quay.io/rhacs-eng/%s", to)
if d.verbose {
d.logger.Dimf("Image rewriting rule: %s -> %s", source, mirror)
}
return map[string]interface{}{
"source": source,
"mirrors": []string{mirror},
}
}
repositoryDigestMirrors := []map[string]interface{}{
rewrite("rhacs-operator-bundle", "release-operator-bundle"),
rewrite("rhacs-rhel8-operator", "release-operator"),
rewrite("rhacs-main-rhel8", "release-main"),
rewrite("rhacs-scanner-rhel8", "release-scanner"),
rewrite("rhacs-scanner-slim-rhel8", "release-scanner-slim"),
rewrite("rhacs-scanner-db-rhel8", "release-scanner-db"),
rewrite("rhacs-scanner-db-slim-rhel8", "release-scanner-db-slim"),
rewrite("rhacs-collector-slim-rhel8", "release-collector-slim"),
rewrite("rhacs-collector-rhel8", "release-collector"),
rewrite("rhacs-fact-rhel8", "release-fact"),
rewrite("rhacs-roxctl-rhel8", "release-roxctl"),
rewrite("rhacs-central-db-rhel8", "release-central-db"),
rewrite("rhacs-scanner-v4-db-rhel8", "release-scanner-v4-db"),
rewrite("rhacs-scanner-v4-rhel8", "release-scanner-v4"),
// Also support downstream image rewriting for upcoming UBI9/rhel9 images.
rewrite("rhacs-operator-bundle-rhel9", "release-operator-bundle"),
rewrite("rhacs-rhel9-operator", "release-operator"),
rewrite("rhacs-main-rhel9", "release-main"),
rewrite("rhacs-scanner-rhel9", "release-scanner"),
rewrite("rhacs-scanner-slim-rhel9", "release-scanner-slim"),
rewrite("rhacs-scanner-db-rhel9", "release-scanner-db"),
rewrite("rhacs-scanner-db-slim-rhel9", "release-scanner-db-slim"),
rewrite("rhacs-collector-slim-rhel9", "release-collector-slim"),
rewrite("rhacs-collector-rhel9", "release-collector"),
rewrite("rhacs-fact-rhel9", "release-fact"),
rewrite("rhacs-roxctl-rhel9", "release-roxctl"),
rewrite("rhacs-central-db-rhel9", "release-central-db"),
rewrite("rhacs-scanner-v4-db-rhel9", "release-scanner-v4-db"),
rewrite("rhacs-scanner-v4-rhel9", "release-scanner-v4"),
}
icsp := map[string]interface{}{
"apiVersion": "operator.openshift.io/v1alpha1",
"kind": "ImageContentSourcePolicy",
"metadata": map[string]interface{}{
"name": "acs-konflux-builds",
},
"spec": map[string]interface{}{
"repositoryDigestMirrors": repositoryDigestMirrors,
},
}
yamlData, err := yaml.Marshal(icsp)
if err != nil {
return fmt.Errorf("failed to marshal ImageContentSourcePolicy: %w", err)
}
d.logger.Dim("Applying ImageContentSourcePolicy...")
_, err = d.runKubectl(ctx, k8s.KubectlOptions{
Args: []string{"apply", "-f", "-"},
Stdin: bytes.NewReader(yamlData),
})
if err != nil {
return fmt.Errorf("failed to apply ImageContentSourcePolicy: %w", err)
}
d.logger.Successf("✓ ImageContentSourcePolicy 'acs-konflux-builds' applied")
d.logger.Info("Note: OpenShift nodes may need to restart to apply the image mirroring configuration")
return nil
}
// removeKonfluxImageRewriting removes the ImageContentSourcePolicy for Konflux images if it exists
func (d *Deployer) removeKonfluxImageRewriting(ctx context.Context) error {
if !env.GetCurrentClusterType().IsOpenShift() {
return nil
}
d.logger.Dim("Removing Konflux ImageContentSourcePolicy if present...")
_, err := d.runKubectl(ctx, k8s.KubectlOptions{
Args: []string{"delete", "imagecontentsourcepolicy", "acs-konflux-builds", "--ignore-not-found=true"},
})
if err != nil {
return fmt.Errorf("failed to delete ImageContentSourcePolicy: %w", err)
}
return nil
}
// deployOperatorFromCSV deploys the operator from CSV
func (d *Deployer) deployOperatorFromCSV(ctx context.Context, bundleDir string) error {
csvFile := filepath.Join(bundleDir, "rhacs-operator.clusterserviceversion.yaml")
if _, err := os.Stat(csvFile); os.IsNotExist(err) {
return errors.New("ClusterServiceVersion file not found in bundle")
}
d.logger.Info("🔍 Parsing ClusterServiceVersion deployment specification")
deploymentSpec, err := d.parseCSVDeploymentSpec(csvFile)
if err != nil {
return err
}
serviceAccountName := deploymentSpec["service_account"].(string)
d.logger.Info("📋 Operator deployment plan:")
d.logger.Dim(fmt.Sprintf(" • Namespace: %s", operatorNamespace))
d.logger.Dim(fmt.Sprintf(" • ServiceAccount: %s", serviceAccountName))
if err := d.createOperatorNamespace(ctx); err != nil {
return err
}
if err := d.createServiceAccount(ctx, operatorNamespace, serviceAccountName); err != nil {
return err
}
if err := d.createClusterRoleFromCSV(ctx, deploymentSpec); err != nil {
return err
}
if err := d.createClusterRoleBinding(ctx, operatorNamespace, serviceAccountName); err != nil {
return err
}
if err := d.createDeploymentFromCSV(ctx, operatorNamespace, deploymentSpec); err != nil {
return err
}
// Apply bundle service resources (if they exist)
_ = d.applyBundleServiceResources(ctx, bundleDir, operatorNamespace)
if err := d.waitForOperatorReady(ctx, operatorNamespace, operatorDeploymentName, 300); err != nil {
return err
}
d.logger.Success("🎉 Operator deployment completed successfully!")
return nil
}
// parseCSVDeploymentSpec parses the CSV file
func (d *Deployer) parseCSVDeploymentSpec(csvFile string) (map[string]interface{}, error) {
content, err := os.ReadFile(csvFile)
if err != nil {
return nil, fmt.Errorf("failed to read CSV file: %w", err)
}
var csvContent map[string]interface{}
if err := yaml.Unmarshal(content, &csvContent); err != nil {
return nil, fmt.Errorf("failed to parse CSV: %w", err)
}
spec := csvContent["spec"].(map[string]interface{})
installSpec := spec["install"].(map[string]interface{})["spec"].(map[string]interface{})
deployments := installSpec["deployments"].([]interface{})
clusterPermissions := installSpec["clusterPermissions"].([]interface{})
metadata := csvContent["metadata"].(map[string]interface{})
deploymentSpec := map[string]interface{}{
"name": metadata["name"],
"deployments": deployments,
"cluster_permissions": clusterPermissions,
"service_account": "rhacs-operator-controller-manager",
}
if len(clusterPermissions) > 0 {
firstPerm := clusterPermissions[0].(map[string]interface{})
if sa, ok := firstPerm["serviceAccountName"]; ok {
deploymentSpec["service_account"] = sa
}
}
return deploymentSpec, nil
}
// createOperatorNamespace creates the operator namespace
func (d *Deployer) createOperatorNamespace(ctx context.Context) error {
nsYAML := fmt.Sprintf(`apiVersion: v1
kind: Namespace
metadata:
name: %s
labels:
name: %s
app.kubernetes.io/managed-by: roxie
`, operatorNamespace, operatorNamespace)
_, err := d.runKubectl(ctx, k8s.KubectlOptions{
Args: []string{"apply", "-f", "-"},
Stdin: strings.NewReader(nsYAML),
})
return err
}
// createServiceAccount creates a service account
func (d *Deployer) createServiceAccount(ctx context.Context, namespace, name string) error {
sa := map[string]interface{}{
"apiVersion": "v1",
"kind": "ServiceAccount",
"metadata": map[string]interface{}{
"name": name,
"namespace": namespace,
"labels": map[string]string{"app": "rhacs-operator"},
},
}
yamlData, err := yaml.Marshal(sa)
if err != nil {
return fmt.Errorf("failed to marshal ServiceAccount '%s/%s': %w", namespace, name, err)
}
_, err = d.runKubectl(ctx, k8s.KubectlOptions{
Args: []string{"apply", "-f", "-"},
Stdin: bytes.NewReader(yamlData),
})
if err != nil {
return fmt.Errorf("failed to create ServiceAccount '%s/%s': %w", namespace, name, err)
}
return nil
}
// createClusterRoleFromCSV creates ClusterRole from CSV
func (d *Deployer) createClusterRoleFromCSV(ctx context.Context, deploymentSpec map[string]interface{}) error {
clusterPermissions := deploymentSpec["cluster_permissions"].([]interface{})
if len(clusterPermissions) == 0 {
d.logger.Warning("No cluster permissions found in CSV")
return nil
}
firstPerm := clusterPermissions[0].(map[string]interface{})
rules := firstPerm["rules"]
clusterRole := map[string]interface{}{
"apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "ClusterRole",
"metadata": map[string]interface{}{
"name": "rhacs-operator-manager-role",
"labels": map[string]string{"app": "rhacs-operator"},
},
"rules": rules,
}
yamlData, err := yaml.Marshal(clusterRole)
if err != nil {
return fmt.Errorf("failed to marshal ClusterRole 'rhacs-operator-manager-role': %w", err)
}
_, err = d.runKubectl(ctx, k8s.KubectlOptions{
Args: []string{"apply", "-f", "-"},
Stdin: bytes.NewReader(yamlData),
})
if err != nil {
return fmt.Errorf("failed to create ClusterRole 'rhacs-operator-manager-role': %w", err)
}
return nil
}
// createClusterRoleBinding creates ClusterRoleBinding
func (d *Deployer) createClusterRoleBinding(ctx context.Context, namespace, serviceAccountName string) error {
crb := map[string]interface{}{
"apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "ClusterRoleBinding",
"metadata": map[string]interface{}{
"name": "rhacs-operator-manager-rolebinding",
"labels": map[string]string{"app": "rhacs-operator"},
},
"roleRef": map[string]interface{}{
"apiGroup": "rbac.authorization.k8s.io",
"kind": "ClusterRole",
"name": "rhacs-operator-manager-role",
},
"subjects": []interface{}{
map[string]interface{}{
"kind": "ServiceAccount",
"name": serviceAccountName,
"namespace": namespace,
},
},
}
yamlData, err := yaml.Marshal(crb)
if err != nil {
return fmt.Errorf("failed to marshal ClusterRoleBinding 'rhacs-operator-manager-rolebinding' for ServiceAccount '%s/%s': %w", namespace, serviceAccountName, err)
}
_, err = d.runKubectl(ctx, k8s.KubectlOptions{
Args: []string{"apply", "-f", "-"},
Stdin: bytes.NewReader(yamlData),
})
if err != nil {
return fmt.Errorf("failed to create ClusterRoleBinding 'rhacs-operator-manager-rolebinding': %w", err)
}
return nil
}
// createDeploymentFromCSV creates Deployment from CSV
func (d *Deployer) createDeploymentFromCSV(ctx context.Context, namespace string, deploymentSpec map[string]interface{}) error {
deployments := deploymentSpec["deployments"].([]interface{})
if len(deployments) == 0 {
return errors.New("no deployments found in CSV")
}
csvDeployment := deployments[0].(map[string]interface{})
deploymentName, _ := csvDeployment["name"].(string)
if deploymentName == "" {
deploymentName = operatorDeploymentName
}
deploymentTemplate := csvDeployment["spec"].(map[string]interface{})
deployment := map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": deploymentName,
"namespace": namespace,
"labels": csvDeployment["label"],
},
"spec": deploymentTemplate,
}
spec := deployment["spec"].(map[string]interface{})
if template, ok := spec["template"].(map[string]interface{}); ok {
if podSpec, ok := template["spec"].(map[string]interface{}); ok {
podSpec["serviceAccountName"] = deploymentSpec["service_account"]
}
}
yamlData, err := yaml.Marshal(deployment)
if err != nil {
return fmt.Errorf("failed to marshal Deployment '%s/%s': %w", namespace, deploymentName, err)
}
_, err = d.runKubectl(ctx, k8s.KubectlOptions{
Args: []string{"apply", "-f", "-"},
Stdin: bytes.NewReader(yamlData),
})
if err != nil {
return fmt.Errorf("failed to create Deployment '%s/%s': %w", namespace, deploymentName, err)
}
return nil
}
func (d *Deployer) applyBundleServiceResources(ctx context.Context, bundleDir, namespace string) error {
serviceFile := filepath.Join(bundleDir, "rhacs-operator-controller-manager-metrics-service_v1_service.yaml")
if _, err := os.Stat(serviceFile); err == nil {
d.runKubectl(ctx, k8s.KubectlOptions{
Args: []string{"apply", "-n", namespace, "-f", serviceFile},
IgnoreErrors: true,
})
}
clusterRoleFile := filepath.Join(bundleDir, "rhacs-operator-metrics-reader_rbac.authorization.k8s.io_v1_clusterrole.yaml")
if _, err := os.Stat(clusterRoleFile); err == nil {
d.runKubectl(ctx, k8s.KubectlOptions{
Args: []string{"apply", "-f", clusterRoleFile},
IgnoreErrors: true,
})
}
return nil
}
// waitForOperatorReady waits for operator deployment to be ready
func (d *Deployer) waitForOperatorReady(ctx context.Context, namespace, deploymentName string, timeout int) error {
d.logger.Info("⏳ Waiting for operator deployment to become ready...")
start := time.Now()
for time.Since(start) < time.Duration(timeout)*time.Second {
result, err := d.runKubectl(ctx, k8s.KubectlOptions{
Args: []string{"get", "deployment", deploymentName, "-n", namespace, "-o", "jsonpath={.status.readyReplicas}"},
})
if err == nil && result.Stdout != "" {
replicas := strings.TrimSpace(result.Stdout)
if replicas != "0" && replicas != "" {
d.logger.Successf("✓ Operator deployment is ready (%s replicas)", replicas)
return nil
}
}
time.Sleep(2 * time.Second)
}
return errors.New("timeout waiting for operator deployment to become ready")
}
func generateClusterName() string {
n, _ := rand.Int(rand.Reader, big.NewInt(9000))
return fmt.Sprintf("sensor-%d", n.Int64()+1000)
}
// teardownOperatorNonOLM removes the operator when installed without OLM.
func (d *Deployer) teardownOperatorNonOLM(ctx context.Context) error {
d.logger.Info("🧹 Tearing down operator deployed without OLM...")
// Delete operator namespace.
d.runKubectl(ctx, k8s.KubectlOptions{
Args: []string{"delete", "namespace", operatorNamespace, "--wait=false"},
IgnoreErrors: true,
})
// Delete cluster-scoped resources created by non-OLM flow.
clusterResources := []struct {
name string
kind string
}{
{name: "rhacs-operator-manager-rolebinding", kind: "clusterrolebinding"},
{name: "rhacs-operator-manager-role", kind: "clusterrole"},
}
for _, resource := range clusterResources {
d.runKubectl(ctx, k8s.KubectlOptions{
Args: []string{"delete", resource.kind, resource.name, "--ignore-not-found=true"},
IgnoreErrors: true,
})
}
if err := d.waitForNamespaceDeletion(operatorNamespace); err != nil {
d.logger.Warningf("Namespace %s deletion incomplete: %v", operatorNamespace, err)
}
d.logger.Success("✓ Non-OLM operator resources removed")
return nil
}
// teardownOperator removes the operator if it exists, detecting the deployment mode automatically.
func (d *Deployer) teardownOperator(ctx context.Context) error {
operatorExists, operatorMode := d.detectOperatorDeploymentMode(ctx)
if !operatorExists {
d.logger.Dim("No operator deployment found, skipping operator teardown")
return nil
}
if operatorMode == OperatorModeOLM {
return d.teardownOperatorOLM(ctx)
}
return d.teardownOperatorNonOLM(ctx)
}