@@ -14,33 +14,76 @@ import (
1414 "gopkg.in/yaml.v3"
1515)
1616
17- // deployCentralOperator deploys Central using the operator
18- func (d * Deployer ) deployCentralOperator (ctx context.Context , resources , exposure string ) error {
19- d .logger .Info ("🚀 Deploying Central via Operator..." )
20-
17+ // ensureOperatorDeployed ensures the operator is deployed with the correct version and mode
18+ func (d * Deployer ) ensureOperatorDeployed (ctx context.Context ) error {
2119 if err := d .ensureCRDsInstalled (ctx ); err != nil {
2220 return fmt .Errorf ("failed to ensure CRDs installed: %w" , err )
2321 }
2422
25- operatorDeployed := d .isOperatorDeployed (ctx )
26- needsDeployment := ! operatorDeployed
27-
28- if operatorDeployed {
29- // Operator exists, check if version is correct
23+ // Detect current operator deployment mode
24+ operatorExists , currentMode := d .detectOperatorDeploymentMode (ctx )
25+ needsDeployment := false
26+ needsTeardown := false
27+
28+ if ! operatorExists {
29+ needsDeployment = true
30+ } else if d .useOLM && currentMode == OperatorModeNonOLM {
31+ // Switching from non-OLM to OLM
32+ d .logger .Info ("🔄 Switching operator from non-OLM to OLM mode..." )
33+ needsTeardown = true
34+ needsDeployment = true
35+ } else if ! d .useOLM && currentMode == OperatorModeOLM {
36+ // Switching from OLM to non-OLM
37+ d .logger .Info ("🔄 Switching operator from OLM to non-OLM mode..." )
38+ needsTeardown = true
39+ needsDeployment = true
40+ } else {
41+ // Same mode, check version
3042 if d .isOperatorVersionCorrect (ctx ) {
3143 d .logger .Info ("✓ Operator already deployed with correct version" )
3244 } else {
3345 d .logger .Info ("🔄 Operator version mismatch, redeploying..." )
46+ needsTeardown = true
3447 needsDeployment = true
3548 }
3649 }
3750
51+ if needsTeardown {
52+ // Perform teardown for the current mode
53+ if currentMode == OperatorModeOLM {
54+ if err := d .teardownOperatorOLM (ctx ); err != nil {
55+ return fmt .Errorf ("failed to teardown OLM operator: %w" , err )
56+ }
57+ } else {
58+ if err := d .teardownOperatorNonOLM (ctx ); err != nil {
59+ return fmt .Errorf ("failed to teardown non-OLM operator: %w" , err )
60+ }
61+ }
62+ }
63+
3864 if needsDeployment {
39- if err := d .deployOperator (ctx ); err != nil {
40- return fmt .Errorf ("failed to deploy operator: %w" , err )
65+ if d .useOLM {
66+ if err := d .deployOperatorViaOLM (ctx ); err != nil {
67+ return fmt .Errorf ("failed to deploy operator via OLM: %w" , err )
68+ }
69+ } else {
70+ if err := d .deployOperator (ctx ); err != nil {
71+ return fmt .Errorf ("failed to deploy operator: %w" , err )
72+ }
4173 }
4274 }
4375
76+ return nil
77+ }
78+
79+ // deployCentralOperator deploys Central using the operator
80+ func (d * Deployer ) deployCentralOperator (ctx context.Context , resources , exposure string ) error {
81+ d .logger .Info ("🚀 Deploying Central via Operator..." )
82+
83+ if err := d .ensureOperatorDeployed (ctx ); err != nil {
84+ return err
85+ }
86+
4487 if err := d .prepareNamespace (ctx , d .centralNamespace ); err != nil {
4588 return fmt .Errorf ("failed to prepare namespace: %w" , err )
4689 }
@@ -65,14 +108,6 @@ func (d *Deployer) deployCentralOperator(ctx context.Context, resources, exposur
65108 return d .configureCentralEndpoint (ctx , exposure )
66109}
67110
68- // isOperatorDeployed checks if the operator is already deployed
69- func (d * Deployer ) isOperatorDeployed (ctx context.Context ) bool {
70- _ , err := d .runKubectl (ctx , KubectlOptions {
71- Args : []string {"get" , "deployment" , operatorDeploymentName , "-n" , operatorNamespace },
72- })
73- return err == nil
74- }
75-
76111// isOperatorVersionCorrect checks if the deployed operator matches the desired version
77112func (d * Deployer ) isOperatorVersionCorrect (ctx context.Context ) bool {
78113 currentImage , err := d .getDeployedOperatorImage (ctx )
@@ -520,31 +555,12 @@ func (d *Deployer) configureCentralEndpoint(ctx context.Context, exposure string
520555 return nil
521556}
522557
523- // deploySecuredClusterOperator deploys SecuredCluster using the operator
558+ // deploySecuredClusterOperator deploys SecuredCluster using the operator.
524559func (d * Deployer ) deploySecuredClusterOperator (ctx context.Context , resources string ) error {
525560 d .logger .Info ("🚀 Deploying SecuredCluster via Operator..." )
526561
527- if err := d .ensureCRDsInstalled (ctx ); err != nil {
528- return fmt .Errorf ("failed to ensure CRDs installed: %w" , err )
529- }
530-
531- operatorDeployed := d .isOperatorDeployed (ctx )
532- needsDeployment := ! operatorDeployed
533-
534- if operatorDeployed {
535- // Operator exists, check if version is correct
536- if d .isOperatorVersionCorrect (ctx ) {
537- d .logger .Info ("✓ Operator already deployed with correct version" )
538- } else {
539- d .logger .Info ("🔄 Operator version mismatch, redeploying..." )
540- needsDeployment = true
541- }
542- }
543-
544- if needsDeployment {
545- if err := d .deployOperator (ctx ); err != nil {
546- return fmt .Errorf ("failed to deploy operator: %w" , err )
547- }
562+ if err := d .ensureOperatorDeployed (ctx ); err != nil {
563+ return err
548564 }
549565
550566 if err := d .prepareNamespace (ctx , d .sensorNamespace ); err != nil {
@@ -579,7 +595,7 @@ func (d *Deployer) deploySecuredClusterOperator(ctx context.Context, resources s
579595 return nil
580596}
581597
582- // createSecuredClusterCR creates the SecuredCluster custom resource
598+ // createSecuredClusterCR creates the SecuredCluster custom resource.
583599func (d * Deployer ) createSecuredClusterCR (clusterName , resources string ) (map [string ]interface {}, error ) {
584600 base := map [string ]interface {}{
585601 "apiVersion" : "platform.stackrox.io/v1alpha1" ,
0 commit comments