Skip to content

Commit 9e94fd2

Browse files
committed
Address reviewer comments
1 parent a57f1b8 commit 9e94fd2

File tree

4 files changed

+43
-51
lines changed

4 files changed

+43
-51
lines changed

internal/operator-controller/applier/boxcutter.go

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
)
3131

3232
const (
33-
RevisionHashAnnotation = "olm.operatorframework.io/hash"
3433
ClusterExtensionRevisionPreviousLimit = 5
3534
)
3635

@@ -216,19 +215,15 @@ func (bc *Boxcutter) getObjects(rev *ocv1.ClusterExtensionRevision) []client.Obj
216215
return objs
217216
}
218217

219-
func (bc *Boxcutter) ensureGVKIsSet(obj client.Object) error {
220-
if !obj.GetObjectKind().GroupVersionKind().Empty() {
221-
return nil
222-
}
223-
224-
gvk, err := apiutil.GVKForObject(obj, bc.Scheme)
225-
if err != nil {
226-
return err
218+
func (bc *Boxcutter) createOrUpdate(ctx context.Context, obj client.Object) error {
219+
if obj.GetObjectKind().GroupVersionKind().Empty() {
220+
gvk, err := apiutil.GVKForObject(obj, bc.Scheme)
221+
if err != nil {
222+
return err
223+
}
224+
obj.GetObjectKind().SetGroupVersionKind(gvk)
227225
}
228-
229-
obj.GetObjectKind().SetGroupVersionKind(gvk)
230-
231-
return nil
226+
return bc.Client.Patch(ctx, obj, client.Apply, client.FieldOwner(bc.FieldOwner), client.ForceOwnership)
232227
}
233228

234229
func (bc *Boxcutter) apply(ctx context.Context, contentFS fs.FS, ext *ocv1.ClusterExtension, objectLabels, revisionAnnotations map[string]string) (bool, string, error) {
@@ -251,54 +246,52 @@ func (bc *Boxcutter) apply(ctx context.Context, contentFS fs.FS, ext *ocv1.Clust
251246
currentRevision := &ocv1.ClusterExtensionRevision{}
252247
state := StateNeedsInstall
253248
// check if we can update the current revision.
254-
if len(existingRevisions) > 0 { // nolint:nestif
249+
if len(existingRevisions) > 0 {
255250
// try first to update the current revision.
256251
currentRevision = &existingRevisions[len(existingRevisions)-1]
257252
desiredRevision.Spec.Previous = currentRevision.Spec.Previous
258253
desiredRevision.Spec.Revision = currentRevision.Spec.Revision
259254
desiredRevision.Name = currentRevision.Name
260255

261-
if err := bc.ensureGVKIsSet(desiredRevision); err != nil {
262-
return false, "", fmt.Errorf("setting gvk failed: %w", err)
263-
}
264-
if err = bc.Client.Patch(ctx, desiredRevision, client.Apply, client.FieldOwner(bc.FieldOwner), client.ForceOwnership); err != nil {
265-
if !apierrors.IsInvalid(err) {
266-
return false, "", fmt.Errorf("patching %s Revision: %w", desiredRevision.Name, err)
267-
}
256+
err := bc.createOrUpdate(ctx, desiredRevision)
257+
switch {
258+
case apierrors.IsInvalid(err):
268259
// We could not update the current revision due to trying to update an immutable field.
269260
// Therefore, we need to create a new revision.
270261
state = StateNeedsUpgrade
271-
} else {
262+
case err == nil:
272263
// inplace patch was successful, no changes in phases
273264
state = StateUnchanged
265+
default:
266+
return false, "", fmt.Errorf("patching %s Revision: %w", desiredRevision.Name, err)
274267
}
275268
}
276269

277-
if state != StateUnchanged { // nolint:nestif
278-
// Preflights
279-
plainObjs := bc.getObjects(desiredRevision)
280-
for _, preflight := range bc.Preflights {
281-
if shouldSkipPreflight(ctx, preflight, ext, state) {
282-
continue
270+
// Preflights
271+
plainObjs := bc.getObjects(desiredRevision)
272+
for _, preflight := range bc.Preflights {
273+
if shouldSkipPreflight(ctx, preflight, ext, state) {
274+
continue
275+
}
276+
switch state {
277+
case StateNeedsInstall:
278+
err := preflight.Install(ctx, plainObjs)
279+
if err != nil {
280+
return false, "", err
283281
}
284-
switch state {
285-
case StateNeedsInstall:
286-
err := preflight.Install(ctx, plainObjs)
287-
if err != nil {
288-
return false, "", err
289-
}
290-
// TODO: jlanford's IDE says that "StateNeedsUpgrade" condition is always true, but
291-
// it isn't immediately obvious why that is. Perhaps len(existingRevisions) is
292-
// always greater than 0 (seems unlikely), or shouldSkipPreflight always returns
293-
// true (and we continue) when state == StateNeedsInstall?
294-
case StateNeedsUpgrade:
295-
err := preflight.Upgrade(ctx, plainObjs)
296-
if err != nil {
297-
return false, "", err
298-
}
282+
// TODO: jlanford's IDE says that "StateNeedsUpgrade" condition is always true, but
283+
// it isn't immediately obvious why that is. Perhaps len(existingRevisions) is
284+
// always greater than 0 (seems unlikely), or shouldSkipPreflight always returns
285+
// true (and we continue) when state == StateNeedsInstall?
286+
case StateNeedsUpgrade:
287+
err := preflight.Upgrade(ctx, plainObjs)
288+
if err != nil {
289+
return false, "", err
299290
}
300291
}
292+
}
301293

294+
if state != StateUnchanged {
302295
// need to create new revision
303296
prevRevisions := existingRevisions
304297
revisionNumber := latestRevisionNumber(prevRevisions) + 1
@@ -310,10 +303,7 @@ func (bc *Boxcutter) apply(ctx context.Context, contentFS fs.FS, ext *ocv1.Clust
310303
return false, "", fmt.Errorf("garbage collecting old Revisions: %w", err)
311304
}
312305

313-
if err := bc.ensureGVKIsSet(desiredRevision); err != nil {
314-
return false, "", fmt.Errorf("setting gvk failed: %w", err)
315-
}
316-
if err := bc.Client.Patch(ctx, desiredRevision, client.Apply, client.FieldOwner(bc.FieldOwner), client.ForceOwnership); err != nil {
306+
if err := bc.createOrUpdate(ctx, desiredRevision); err != nil {
317307
return false, "", fmt.Errorf("creating new Revision: %w", err)
318308
}
319309
currentRevision = desiredRevision

internal/operator-controller/applier/boxcutter_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,9 @@ func TestBoxcutter_Apply(t *testing.T) {
343343
if !ok {
344344
return fmt.Errorf("expected ClusterExtensionRevision, got %T", obj)
345345
}
346+
fmt.Println(cer.Spec.Revision)
346347
if cer.Spec.Revision != revNum {
348+
fmt.Println("AAA")
347349
return apierrors.NewInvalid(cer.GroupVersionKind().GroupKind(), cer.GetName(), field.ErrorList{field.Invalid(field.NewPath("spec.phases"), "immutable", "spec.phases is immutable")})
348350
}
349351
return client.Patch(ctx, obj, patch, opts...)
@@ -453,7 +455,7 @@ func TestBoxcutter_Apply(t *testing.T) {
453455
},
454456
},
455457
{
456-
name: "new revision created when hash differs",
458+
name: "new revision created when objects in new revision are different",
457459
mockBuilder: &mockBundleRevisionBuilder{
458460
makeRevisionFunc: func(bundleFS fs.FS, ext *ocv1.ClusterExtension, objectLabels, revisionAnnotations map[string]string) (*ocv1.ClusterExtensionRevision, error) {
459461
return &ocv1.ClusterExtensionRevision{

test/experimental-e2e/experimental_e2e_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ func TestClusterExtensionConfigSupport(t *testing.T) {
390390
}, pollDuration, pollInterval)
391391
}
392392

393-
func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
393+
func TestClusterExtensionVersionUpdate(t *testing.T) {
394394
t.Log("When a cluster extension is installed from a catalog")
395395
t.Log("When resolving upgrade edges")
396396

@@ -423,7 +423,7 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
423423
}, pollDuration, pollInterval)
424424

425425
t.Log("It allows to upgrade the ClusterExtension to a non-successor version")
426-
t.Log("By updating the ClusterExtension resource to a non-successor version")
426+
t.Log("By forcing update of ClusterExtension resource to a non-successor version")
427427
// 1.2.0 does not replace/skip/skipRange 1.0.0.
428428
clusterExtension.Spec.Source.Catalog.Version = "1.2.0"
429429
clusterExtension.Spec.Source.Catalog.UpgradeConstraintPolicy = ocv1.UpgradeConstraintPolicySelfCertified

testdata/images/bundles/test-operator/v1.2.0/manifests/testoperator.clusterserviceversion.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,4 @@ spec:
148148
provider:
149149
name: Simple Bundle
150150
url: https://simple-bundle.domain
151-
version: 1.1.0
151+
version: 1.2.0

0 commit comments

Comments
 (0)