Skip to content

Commit 74dac59

Browse files
committed
🌱 Simplify Boxcutter applier by removing status interpretation logic
The Apply method now returns only an error instead of (bool, string, error). The responsibility for interpreting ClusterExtensionRevision status conditions is removed from the applier, simplifying the interface and making the applier focus solely on creating/updating revisions.
1 parent 049f813 commit 74dac59

3 files changed

Lines changed: 15 additions & 38 deletions

File tree

internal/operator-controller/applier/boxcutter.go

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"helm.sh/helm/v3/pkg/release"
1414
"helm.sh/helm/v3/pkg/storage/driver"
1515
apierrors "k8s.io/apimachinery/pkg/api/errors"
16-
"k8s.io/apimachinery/pkg/api/meta"
1716
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1817
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1918
"k8s.io/apimachinery/pkg/runtime"
@@ -283,7 +282,7 @@ type Boxcutter struct {
283282
FieldOwner string
284283
}
285284

286-
func (bc *Boxcutter) Apply(ctx context.Context, contentFS fs.FS, ext *ocv1.ClusterExtension, objectLabels, revisionAnnotations map[string]string) (bool, string, error) {
285+
func (bc *Boxcutter) Apply(ctx context.Context, contentFS fs.FS, ext *ocv1.ClusterExtension, objectLabels, revisionAnnotations map[string]string) error {
287286
return bc.apply(ctx, contentFS, ext, objectLabels, revisionAnnotations)
288287
}
289288

@@ -308,21 +307,21 @@ func (bc *Boxcutter) createOrUpdate(ctx context.Context, obj client.Object) erro
308307
return bc.Client.Patch(ctx, obj, client.Apply, client.FieldOwner(bc.FieldOwner), client.ForceOwnership)
309308
}
310309

311-
func (bc *Boxcutter) apply(ctx context.Context, contentFS fs.FS, ext *ocv1.ClusterExtension, objectLabels, revisionAnnotations map[string]string) (bool, string, error) {
310+
func (bc *Boxcutter) apply(ctx context.Context, contentFS fs.FS, ext *ocv1.ClusterExtension, objectLabels, revisionAnnotations map[string]string) error {
312311
// Generate desired revision
313312
desiredRevision, err := bc.RevisionGenerator.GenerateRevision(ctx, contentFS, ext, objectLabels, revisionAnnotations)
314313
if err != nil {
315-
return false, "", err
314+
return err
316315
}
317316

318317
if err := controllerutil.SetControllerReference(ext, desiredRevision, bc.Scheme); err != nil {
319-
return false, "", fmt.Errorf("set ownerref: %w", err)
318+
return fmt.Errorf("set ownerref: %w", err)
320319
}
321320

322321
// List all existing revisions
323322
existingRevisions, err := bc.getExistingRevisions(ctx, ext.GetName())
324323
if err != nil {
325-
return false, "", err
324+
return err
326325
}
327326

328327
currentRevision := &ocv1.ClusterExtensionRevision{}
@@ -344,7 +343,7 @@ func (bc *Boxcutter) apply(ctx context.Context, contentFS fs.FS, ext *ocv1.Clust
344343
// inplace patch was successful, no changes in phases
345344
state = StateUnchanged
346345
default:
347-
return false, "", fmt.Errorf("patching %s Revision: %w", desiredRevision.Name, err)
346+
return fmt.Errorf("patching %s Revision: %w", desiredRevision.Name, err)
348347
}
349348
}
350349

@@ -358,7 +357,7 @@ func (bc *Boxcutter) apply(ctx context.Context, contentFS fs.FS, ext *ocv1.Clust
358357
case StateNeedsInstall:
359358
err := preflight.Install(ctx, plainObjs)
360359
if err != nil {
361-
return false, "", err
360+
return err
362361
}
363362
// TODO: jlanford's IDE says that "StateNeedsUpgrade" condition is always true, but
364363
// it isn't immediately obvious why that is. Perhaps len(existingRevisions) is
@@ -367,7 +366,7 @@ func (bc *Boxcutter) apply(ctx context.Context, contentFS fs.FS, ext *ocv1.Clust
367366
case StateNeedsUpgrade:
368367
err := preflight.Upgrade(ctx, plainObjs)
369368
if err != nil {
370-
return false, "", err
369+
return err
371370
}
372371
}
373372
}
@@ -381,34 +380,15 @@ func (bc *Boxcutter) apply(ctx context.Context, contentFS fs.FS, ext *ocv1.Clust
381380
desiredRevision.Spec.Revision = revisionNumber
382381

383382
if err = bc.garbageCollectOldRevisions(ctx, prevRevisions); err != nil {
384-
return false, "", fmt.Errorf("garbage collecting old revisions: %w", err)
383+
return fmt.Errorf("garbage collecting old revisions: %w", err)
385384
}
386385

387386
if err := bc.createOrUpdate(ctx, desiredRevision); err != nil {
388-
return false, "", fmt.Errorf("creating new Revision: %w", err)
387+
return fmt.Errorf("creating new Revision: %w", err)
389388
}
390-
currentRevision = desiredRevision
391389
}
392390

393-
progressingCondition := meta.FindStatusCondition(currentRevision.Status.Conditions, ocv1.TypeProgressing)
394-
availableCondition := meta.FindStatusCondition(currentRevision.Status.Conditions, ocv1.ClusterExtensionRevisionTypeAvailable)
395-
succeededCondition := meta.FindStatusCondition(currentRevision.Status.Conditions, ocv1.ClusterExtensionRevisionTypeSucceeded)
396-
397-
if progressingCondition == nil && availableCondition == nil && succeededCondition == nil {
398-
return false, "New revision created", nil
399-
} else if progressingCondition != nil && progressingCondition.Status == metav1.ConditionTrue {
400-
switch progressingCondition.Reason {
401-
case ocv1.ReasonSucceeded:
402-
return true, "", nil
403-
case ocv1.ClusterExtensionRevisionReasonRetrying:
404-
return false, "", errors.New(progressingCondition.Message)
405-
default:
406-
return false, progressingCondition.Message, nil
407-
}
408-
} else if succeededCondition != nil && succeededCondition.Status != metav1.ConditionTrue {
409-
return false, succeededCondition.Message, nil
410-
}
411-
return true, "", nil
391+
return nil
412392
}
413393

414394
// garbageCollectOldRevisions deletes archived revisions beyond ClusterExtensionRevisionRetentionLimit.

internal/operator-controller/applier/boxcutter_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -926,18 +926,14 @@ func TestBoxcutter_Apply(t *testing.T) {
926926
labels.PackageNameKey: "test-package",
927927
}
928928
}
929-
installSucceeded, installStatus, err := boxcutter.Apply(t.Context(), testFS, ext, nil, revisionAnnotations)
929+
err := boxcutter.Apply(t.Context(), testFS, ext, nil, revisionAnnotations)
930930

931931
// Assert
932932
if tc.expectedErr != "" {
933933
require.Error(t, err)
934934
assert.Contains(t, err.Error(), tc.expectedErr)
935-
assert.False(t, installSucceeded)
936-
assert.Empty(t, installStatus)
937935
} else {
938936
require.NoError(t, err)
939-
assert.False(t, installSucceeded)
940-
assert.Equal(t, "New revision created", installStatus)
941937
}
942938

943939
if tc.validate != nil {

internal/operator-controller/controllers/boxcutter_reconcile_steps.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"sigs.k8s.io/controller-runtime/pkg/log"
2929

3030
ocv1 "github.com/operator-framework/operator-controller/api/v1"
31+
"github.com/operator-framework/operator-controller/internal/operator-controller/applier"
3132
"github.com/operator-framework/operator-controller/internal/operator-controller/labels"
3233
)
3334

@@ -93,7 +94,7 @@ func MigrateStorage(m StorageMigrator) ReconcileStepFunc {
9394
}
9495
}
9596

96-
func ApplyBundleWithBoxcutter(a Applier) ReconcileStepFunc {
97+
func ApplyBundleWithBoxcutter(a *applier.Boxcutter) ReconcileStepFunc {
9798
return func(ctx context.Context, state *reconcileState, ext *ocv1.ClusterExtension) (*ctrl.Result, error) {
9899
l := log.FromContext(ctx)
99100
revisionAnnotations := map[string]string{
@@ -108,7 +109,7 @@ func ApplyBundleWithBoxcutter(a Applier) ReconcileStepFunc {
108109
}
109110

110111
l.Info("applying bundle contents")
111-
if _, _, err := a.Apply(ctx, state.imageFS, ext, objLbls, revisionAnnotations); err != nil {
112+
if err := a.Apply(ctx, state.imageFS, ext, objLbls, revisionAnnotations); err != nil {
112113
// If there was an error applying the resolved bundle,
113114
// report the error via the Progressing condition.
114115
setStatusProgressing(ext, wrapErrorWithResolutionInfo(state.resolvedRevisionMetadata.BundleMetadata, err))

0 commit comments

Comments
 (0)