@@ -10,6 +10,7 @@ import (
10
10
appsv1 "k8s.io/api/apps/v1"
11
11
"k8s.io/apimachinery/pkg/api/errors"
12
12
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
+ "k8s.io/apimachinery/pkg/labels"
13
14
"k8s.io/apimachinery/pkg/types"
14
15
"k8s.io/apimachinery/pkg/util/wait"
15
16
"k8s.io/client-go/util/retry"
@@ -88,36 +89,84 @@ func (c ImageRegistryConditions) String() string {
88
89
)
89
90
}
90
91
91
- func ensureImageRegistryToBeRemoved (te TestEnv ) {
92
+ func removeImageRegistry (te TestEnv ) {
93
+ te .Logf ("uninstalling the image registry..." )
94
+
95
+ operatorDeployment , err := te .Client ().Deployments (OperatorDeploymentNamespace ).Get (
96
+ context .Background (), OperatorDeploymentName , metav1.GetOptions {},
97
+ )
98
+ if err != nil {
99
+ te .Fatalf ("unable to get the operator deployment: %s" , err )
100
+ }
101
+
102
+ if ! isDeploymentRolledOut (operatorDeployment ) {
103
+ te .Errorf ("unexepected state: the operator is not rolled out before removing the image registry" )
104
+ }
105
+
106
+ if operatorDeployment .Spec .Replicas != nil && * operatorDeployment .Spec .Replicas == 0 {
107
+ config , err := te .Client ().Configs ().Get (
108
+ context .Background (), defaults .ImageRegistryResourceName , metav1.GetOptions {},
109
+ )
110
+ if errors .IsNotFound (err ) {
111
+ return
112
+ } else if err != nil {
113
+ te .Fatalf ("unable to get the image registry config: %s" , err )
114
+ }
115
+ conds := GetImageRegistryConditions (config )
116
+ if ! conds .Removed .IsTrue () {
117
+ te .Fatalf ("unable to uninstall the image registry: the operator is shutted down, but the image registry is not removed: %s" , config .Spec .ManagementState , conds )
118
+ }
119
+ return
120
+ }
121
+
122
+ err = wait .PollImmediate (2 * time .Second , 30 * time .Second , func () (stop bool , err error ) {
123
+ cr , err := te .Client ().Configs ().Get (
124
+ context .Background (), defaults .ImageRegistryResourceName , metav1.GetOptions {},
125
+ )
126
+ if err != nil {
127
+ te .Logf ("the image registry config is not found: %s" , err )
128
+ return false , nil
129
+ }
130
+ if cr .DeletionTimestamp != nil {
131
+ te .Logf ("the image registry config is being deleted: %s" , cr .DeletionTimestamp )
132
+ return false , nil
133
+ }
134
+ return true , nil
135
+ })
136
+ if err != nil {
137
+ te .Fatalf ("failed to wait until the operator creates the config object: %s" , err )
138
+ }
139
+
92
140
if _ , err := te .Client ().Configs ().Patch (
93
141
context .Background (),
94
142
defaults .ImageRegistryResourceName ,
95
143
types .MergePatchType ,
96
144
[]byte (`{"spec": {"managementState": "Removed"}}` ),
97
145
metav1.PatchOptions {},
98
146
); err != nil {
99
- if errors .IsNotFound (err ) {
100
- // That's not exactly what we are asked for. And few seconds later
101
- // the operator may bootstrap it. However, if the operator is
102
- // disabled, it means the registry is not installed and we're
103
- // already in the desired state.
104
- return
105
- }
106
147
te .Fatalf ("unable to uninstall the image registry: %s" , err )
107
148
}
108
149
109
150
var cr * imageregistryapiv1.Config
110
- err : = wait .Poll (5 * time .Second , AsyncOperationTimeout , func () (stop bool , err error ) {
151
+ err = wait .Poll (5 * time .Second , AsyncOperationTimeout , func () (stop bool , err error ) {
111
152
cr , err = te .Client ().Configs ().Get (
112
153
context .Background (), defaults .ImageRegistryResourceName , metav1.GetOptions {},
113
154
)
114
155
if errors .IsNotFound (err ) {
156
+ te .Logf ("waiting for the registry to be removed: the config object does not exist?!" )
115
157
cr = nil
116
158
return true , nil
117
159
} else if err != nil {
160
+ te .Logf ("waiting for the registry to be removed: %s" , err )
118
161
return false , err
119
162
}
120
163
164
+ if cr .Spec .ManagementState != "Removed" {
165
+ DumpYAML (te , "unexpected management state in the config object" , cr )
166
+ DumpOperatorLogs (te )
167
+ te .Fatalf ("unexpected management state: got %s, want Removed" , cr .Spec .ManagementState )
168
+ }
169
+
121
170
conds := GetImageRegistryConditions (cr )
122
171
te .Logf ("waiting for the registry to be removed: %s" , conds )
123
172
return conds .Progressing .IsFalse () && conds .Removed .IsTrue (), nil
@@ -130,6 +179,7 @@ func ensureImageRegistryToBeRemoved(te TestEnv) {
130
179
}
131
180
132
181
func deleteImageRegistryConfig (te TestEnv ) {
182
+ te .Logf ("deleting the image registry config..." )
133
183
// TODO(dmage): the finalizer should be removed by the operator
134
184
if err := retry .RetryOnConflict (retry .DefaultBackoff , func () error {
135
185
cr , err := te .Client ().Configs ().Get (
@@ -165,6 +215,19 @@ func deleteImageRegistryConfig(te TestEnv) {
165
215
}
166
216
}
167
217
218
+ func deleteLeaderElectionConfigMap (te TestEnv , name string ) {
219
+ err := te .Client ().ConfigMaps (OperatorDeploymentNamespace ).Delete (
220
+ context .Background (),
221
+ name ,
222
+ metav1.DeleteOptions {},
223
+ )
224
+ if err == nil {
225
+ te .Logf ("leader election configmap %s deleted" , name )
226
+ } else if ! errors .IsNotFound (err ) {
227
+ te .Errorf ("unable to delete leader election configmap %s: %s" , name , err )
228
+ }
229
+ }
230
+
168
231
func deleteNodeCADaemonSet (te TestEnv ) {
169
232
ds , err := te .Client ().DaemonSets (defaults .ImageRegistryOperatorNamespace ).Get (
170
233
context .Background (),
@@ -224,18 +287,16 @@ func deleteImageRegistryCertificates(te TestEnv) {
224
287
}
225
288
226
289
func deleteImageRegistryAlwaysPresentResources (te TestEnv ) {
290
+ te .Logf ("deleting always-present resources..." )
227
291
defer deleteImageRegistryCertificates (te )
228
292
defer deleteNodeCADaemonSet (te )
293
+ defer deleteLeaderElectionConfigMap (te , "openshift-master-controllers" )
229
294
}
230
295
231
296
func RemoveImageRegistry (te TestEnv ) {
232
- te .Logf ("uninstalling the image registry..." )
233
- ensureImageRegistryToBeRemoved (te )
234
- te .Logf ("stopping the operator..." )
297
+ removeImageRegistry (te )
235
298
StopDeployment (te , OperatorDeploymentNamespace , OperatorDeploymentName )
236
- te .Logf ("deleting the image registry config..." )
237
299
deleteImageRegistryConfig (te )
238
- te .Logf ("deleting always-present resources..." )
239
300
deleteImageRegistryAlwaysPresentResources (te )
240
301
}
241
302
@@ -255,7 +316,6 @@ func DeployImageRegistry(te TestEnv, spec *imageregistryapiv1.ImageRegistrySpec)
255
316
}
256
317
}
257
318
258
- te .Logf ("starting the operator..." )
259
319
startOperator (te )
260
320
}
261
321
@@ -505,10 +565,10 @@ func SetupAvailableImageRegistry(t *testing.T, spec *imageregistryapiv1.ImageReg
505
565
}
506
566
507
567
func TeardownImageRegistry (te TestEnv ) {
508
- defer func () {
509
- RemoveImageRegistry (te )
510
- EnsureClusterOperatorsAreHealthy (te , 10 * time . Second , AsyncOperationTimeout )
511
- }( )
568
+ defer WaitUntilClusterOperatorsAreHealthy ( te , 10 * time . Second , AsyncOperationTimeout )
569
+ defer CheckAbsenceOfOperatorPods (te )
570
+ defer RemoveImageRegistry (te )
571
+ defer CheckPodsAreNotRestarted ( te , labels . Everything () )
512
572
if te .Failed () {
513
573
DumpImageRegistryResource (te )
514
574
DumpOperatorDeployment (te )
0 commit comments