Skip to content

Commit ebef866

Browse files
committed
removeImageRegistry should be able to remove the registry during bootstrap
1 parent e12d50c commit ebef866

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed

pkg/operator/controller.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (c *Controller) sync() error {
278278
}
279279
c.syncStatus(cr, deploy, routes, applyError)
280280

281-
metadataChanged := strategy.Metadata(&prevCR.ObjectMeta, &cr.ObjectMeta)
281+
metadataChanged := strategy.Metadata(prevCR.ObjectMeta.DeepCopy(), &cr.ObjectMeta)
282282
specChanged := !reflect.DeepEqual(prevCR.Spec, cr.Spec)
283283
if metadataChanged || specChanged {
284284
difference, err := object.DiffString(prevCR, cr)
@@ -299,7 +299,12 @@ func (c *Controller) sync() error {
299299
updatedCR.ObjectMeta = cr.ObjectMeta
300300
}
301301
if specChanged {
302+
// FIXME: Here be dragons. The operator can
303+
// accidentally lose user-provided
304+
// configuration.
305+
managementState := updatedCR.Spec.ManagementState
302306
updatedCR.Spec = cr.Spec
307+
updatedCR.Spec.ManagementState = managementState
303308
}
304309

305310
updatedCR, err = c.clients.RegOp.ImageregistryV1().Configs().Update(
@@ -313,6 +318,10 @@ func (c *Controller) sync() error {
313318
// If we updated the Status field too, we'll make one more call and we
314319
// want it to succeed.
315320
cr.ResourceVersion = updatedCR.ResourceVersion
321+
322+
// Update prevCR to make diff accurate.
323+
prevCR.ObjectMeta = updatedCR.ObjectMeta
324+
prevCR.Spec = updatedCR.Spec
316325
}
317326

318327
cr.Status.ObservedGeneration = cr.Generation

test/e2e/aws_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -760,12 +760,6 @@ func TestAWSFinalizerDeleteS3Bucket(t *testing.T) {
760760
if exists {
761761
t.Errorf("s3 bucket should have been deleted, but it wasn't: %s", err)
762762
}
763-
764-
// Once the config object is deleted, the operator will create a new one.
765-
// The testing framework does not expect creation of a new config object
766-
// when it is in the teardown stage, so let's wait until the operator
767-
// stabilizes again.
768-
framework.WaitUntilImageRegistryIsAvailable(te)
769763
}
770764

771765
// createAWSConfigFile creates an AWS credentials config based on the contents of the Secret

test/framework/imageregistry.go

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,35 +91,82 @@ func (c ImageRegistryConditions) String() string {
9191

9292
func removeImageRegistry(te TestEnv) {
9393
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+
94140
if _, err := te.Client().Configs().Patch(
95141
context.Background(),
96142
defaults.ImageRegistryResourceName,
97143
types.MergePatchType,
98144
[]byte(`{"spec": {"managementState": "Removed"}}`),
99145
metav1.PatchOptions{},
100146
); err != nil {
101-
if errors.IsNotFound(err) {
102-
// That's not exactly what we are asked for. And few seconds later
103-
// the operator may bootstrap it. However, if the operator is
104-
// disabled, it means the registry is not installed and we're
105-
// already in the desired state.
106-
return
107-
}
108147
te.Fatalf("unable to uninstall the image registry: %s", err)
109148
}
110149

111150
var cr *imageregistryapiv1.Config
112-
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) {
113152
cr, err = te.Client().Configs().Get(
114153
context.Background(), defaults.ImageRegistryResourceName, metav1.GetOptions{},
115154
)
116155
if errors.IsNotFound(err) {
156+
te.Logf("waiting for the registry to be removed: the config object does not exist?!")
117157
cr = nil
118158
return true, nil
119159
} else if err != nil {
160+
te.Logf("waiting for the registry to be removed: %s", err)
120161
return false, err
121162
}
122163

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+
123170
conds := GetImageRegistryConditions(cr)
124171
te.Logf("waiting for the registry to be removed: %s", conds)
125172
return conds.Progressing.IsFalse() && conds.Removed.IsTrue(), nil

0 commit comments

Comments
 (0)