@@ -100,6 +100,7 @@ func TestUIPluginUninstallCleanup(t *testing.T) {
100100 },
101101 Spec : sub .Spec .DeepCopy (),
102102 }
103+ savedSub .Spec .InstallPlanApproval = olmv1alpha1 .ApprovalAutomatic
103104 t .Cleanup (func () {
104105 if delay := * postponeRestoration ; delay > 0 {
105106 t .Logf ("Cleanup: Waiting %v before restoring operator (inspect the cluster now)" , delay )
@@ -109,14 +110,29 @@ func TestUIPluginUninstallCleanup(t *testing.T) {
109110 forceDeleteAllUIPlugins (t , context .Background ())
110111 if err := f .K8sClient .Create (context .Background (), savedSub ); err != nil {
111112 if apierrors .IsAlreadyExists (err ) {
112- t .Log ("Cleanup: Subscription already exists, skipping" )
113+ t .Log ("Cleanup: Subscription already exists, skipping create" )
114+ } else {
115+ t .Logf ("Cleanup: WARNING — failed to recreate Subscription: %v" , err )
116+ t .Log ("Cleanup: Reinstall manually with: oc apply -f <subscription.yaml>" )
113117 return
114118 }
115- t .Logf ("Cleanup: WARNING — failed to recreate Subscription: %v" , err )
116- t .Log ("Cleanup: Reinstall manually with: oc apply -f <subscription.yaml>" )
117- return
119+ } else {
120+ t .Log ("Cleanup: Subscription recreated, OLM will reinstall the operator" )
118121 }
119- t .Log ("Cleanup: Subscription recreated, OLM will reinstall the operator" )
122+
123+ t .Log ("Cleanup: Waiting for CSV to reach Succeeded phase..." )
124+ if err := waitForCSVSucceeded (t , ns , 5 * time .Minute ); err != nil {
125+ t .Logf ("Cleanup: WARNING — CSV did not reach Succeeded: %v" , err )
126+ } else if f .IsOpenshiftCluster {
127+ t .Log ("Cleanup: Re-enabling OpenShift mode on reinstalled CSV..." )
128+ if err := patchCSVOpenShiftEnabled (t , ns ); err != nil {
129+ t .Logf ("Cleanup: WARNING — failed to patch CSV: %v" , err )
130+ }
131+ }
132+
133+ t .Log ("Cleanup: Waiting for operator deployment to become ready..." )
134+ f .AssertDeploymentReady ("observability-operator" , ns , framework .WithTimeout (5 * time .Minute ))(t )
135+ t .Log ("Cleanup: Operator is ready" )
120136 })
121137 }
122138
@@ -371,3 +387,74 @@ func assertNoManagedPodsRemain(t *testing.T, ctx context.Context, namespace stri
371387 t .Fatalf ("managed pods not cleaned up after UIPlugin deletion: %v" , lastSeen )
372388 }
373389}
390+
391+ // waitForCSVSucceeded polls until the observability-operator CSV reaches the Succeeded phase.
392+ func waitForCSVSucceeded (t * testing.T , namespace string , timeout time.Duration ) error {
393+ t .Helper ()
394+ return wait .PollUntilContextTimeout (context .Background (), 10 * time .Second , timeout , true , func (ctx context.Context ) (bool , error ) {
395+ csvs := & olmv1alpha1.ClusterServiceVersionList {}
396+ if err := f .K8sClient .List (ctx , csvs , & client.ListOptions {Namespace : namespace }); err != nil {
397+ return false , nil
398+ }
399+ for i := range csvs .Items {
400+ if strings .Contains (csvs .Items [i ].Name , "observability-operator" ) &&
401+ csvs .Items [i ].Status .Phase == olmv1alpha1 .CSVPhaseSucceeded {
402+ t .Logf ("Cleanup: CSV %s is Succeeded" , csvs .Items [i ].Name )
403+ return true , nil
404+ }
405+ }
406+ return false , nil
407+ })
408+ }
409+
410+ // patchCSVOpenShiftEnabled patches the reinstalled CSV to add --openshift.enabled=true
411+ // to the operator container args. This is needed because operator-sdk run bundle +
412+ // enable_openshift() only patches the initial CSV; a reinstalled CSV loses the flag.
413+ func patchCSVOpenShiftEnabled (t * testing.T , namespace string ) error {
414+ t .Helper ()
415+ ctx := context .Background ()
416+
417+ csvs := & olmv1alpha1.ClusterServiceVersionList {}
418+ if err := f .K8sClient .List (ctx , csvs , & client.ListOptions {Namespace : namespace }); err != nil {
419+ return fmt .Errorf ("listing CSVs: %w" , err )
420+ }
421+
422+ for i := range csvs .Items {
423+ csv := & csvs .Items [i ]
424+ if ! strings .Contains (csv .Name , "observability-operator" ) {
425+ continue
426+ }
427+
428+ modified := false
429+ for di := range csv .Spec .InstallStrategy .StrategySpec .DeploymentSpecs {
430+ ds := & csv .Spec .InstallStrategy .StrategySpec .DeploymentSpecs [di ]
431+ if ds .Name != "observability-operator" {
432+ continue
433+ }
434+ for ci := range ds .Spec .Template .Spec .Containers {
435+ c := & ds .Spec .Template .Spec .Containers [ci ]
436+ if c .Name != "operator" {
437+ continue
438+ }
439+ for _ , arg := range c .Args {
440+ if arg == "--openshift.enabled=true" {
441+ t .Log ("Cleanup: CSV already has --openshift.enabled=true" )
442+ return nil
443+ }
444+ }
445+ c .Args = append (c .Args , "--openshift.enabled=true" )
446+ modified = true
447+ }
448+ }
449+
450+ if modified {
451+ if err := f .K8sClient .Update (ctx , csv ); err != nil {
452+ return fmt .Errorf ("updating CSV: %w" , err )
453+ }
454+ t .Logf ("Cleanup: Patched CSV %s with --openshift.enabled=true" , csv .Name )
455+ }
456+ return nil
457+ }
458+
459+ return fmt .Errorf ("no observability-operator CSV found" )
460+ }
0 commit comments