Skip to content

Commit 864c12f

Browse files
committed
Address copilot comments
1 parent e9ab9dc commit 864c12f

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

internal/operator-controller/controllers/clusterextension_controller.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ const (
6161
// ReconcileStepFunc represents a single step in the ClusterExtension reconciliation process.
6262
// It takes a context and ClusterExtension object as input and returns:
6363
// - A potentially modified context for the next step
64-
// - An optional reconciliation result that if non-nil will stop reconciliation
6564
// - Any error that occurred during reconciliation, which will be returned to the caller
6665
type ReconcileStepFunc func(context.Context, *ocv1.ClusterExtension) (context.Context, *ctrl.Result, error)
66+
67+
// ReconcileSteps is an ordered collection of reconciliation steps that are executed sequentially.
68+
// Each step receives the output context from the previous step, allowing data to flow through the pipeline.
6769
type ReconcileSteps []ReconcileStepFunc
6870

6971
// Reconcile executes a series of reconciliation steps in sequence for a ClusterExtension.
@@ -75,16 +77,16 @@ type ReconcileSteps []ReconcileStepFunc
7577
func (steps *ReconcileSteps) Reconcile(ctx context.Context, ext *ocv1.ClusterExtension) (ctrl.Result, error) {
7678
var res *ctrl.Result
7779
var err error
78-
stepCtx := ctx
80+
nextStepCtx := ctx
7981
for _, step := range *steps {
80-
stepCtx, res, err = step(stepCtx, ext)
82+
nextStepCtx, res, err = step(nextStepCtx, ext)
8183
if err != nil {
8284
return ctrl.Result{}, err
8385
}
8486
if res != nil {
8587
return *res, nil
8688
}
87-
if stepCtx == nil {
89+
if nextStepCtx == nil {
8890
return ctrl.Result{}, nil
8991
}
9092
}
@@ -309,6 +311,7 @@ func (r *ClusterExtensionReconciler) SetupWithManager(mgr ctrl.Manager, opts ...
309311
for _, applyOpt := range opts {
310312
applyOpt(ctrlBuilder)
311313
}
314+
312315
return ctrlBuilder.Build(r)
313316
}
314317

internal/operator-controller/controllers/clusterextension_reconcile_steps.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controllers
1919
import (
2020
"context"
2121
"errors"
22+
"fmt"
2223
"io/fs"
2324

2425
apimeta "k8s.io/apimachinery/pkg/api/meta"
@@ -39,6 +40,22 @@ type revisionStatesKey struct{}
3940
type resolvedRevisionMetadataKey struct{}
4041
type imageFSKey struct{}
4142

43+
func valueFromContext[T any](ctx context.Context, key any) (*T, error) {
44+
v := ctx.Value(key)
45+
if v == nil {
46+
return nil, fmt.Errorf("value not found in context under key %v", key)
47+
}
48+
ptv, ok := v.(*T)
49+
if !ok {
50+
tv, ok := v.(T)
51+
if !ok {
52+
return nil, fmt.Errorf("value found in context under key %v is not of type %T or pointer to it", key, v)
53+
}
54+
return &tv, nil
55+
}
56+
return ptv, nil
57+
}
58+
4259
func HandleFinalizers(f finalizer.Finalizer) ReconcileStepFunc {
4360
return func(ctx context.Context, ext *ocv1.ClusterExtension) (context.Context, *ctrl.Result, error) {
4461
l := log.FromContext(ctx)
@@ -90,7 +107,10 @@ func RetrieveRevisionStates(r RevisionStatesGetter) ReconcileStepFunc {
90107
func RetrieveRevisionMetadata(r resolve.Resolver) ReconcileStepFunc {
91108
return func(ctx context.Context, ext *ocv1.ClusterExtension) (context.Context, *ctrl.Result, error) {
92109
l := log.FromContext(ctx)
93-
revisionStates := ctx.Value(revisionStatesKey{}).(*RevisionStates)
110+
revisionStates, err := valueFromContext[RevisionStates](ctx, revisionStatesKey{})
111+
if err != nil {
112+
return ctx, nil, err
113+
}
94114
var resolvedRevisionMetadata *RevisionMetadata
95115
if len(revisionStates.RollingOut) == 0 {
96116
l.Info("resolving bundle")
@@ -138,7 +158,10 @@ func UnpackBundle(i imageutil.Puller, cache imageutil.Cache) ReconcileStepFunc {
138158
return func(ctx context.Context, ext *ocv1.ClusterExtension) (context.Context, *ctrl.Result, error) {
139159
l := log.FromContext(ctx)
140160
l.Info("unpacking resolved bundle")
141-
resolvedRevisionMetadata := ctx.Value(resolvedRevisionMetadataKey{}).(*RevisionMetadata)
161+
resolvedRevisionMetadata, err := valueFromContext[RevisionMetadata](ctx, resolvedRevisionMetadataKey{})
162+
if err != nil {
163+
return ctx, nil, err
164+
}
142165
imageFS, _, _, err := i.Pull(ctx, ext.GetName(), resolvedRevisionMetadata.Image, cache)
143166
if err != nil {
144167
revisionStates := ctx.Value(revisionStatesKey{}).(*RevisionStates)
@@ -156,9 +179,18 @@ func UnpackBundle(i imageutil.Puller, cache imageutil.Cache) ReconcileStepFunc {
156179
func ApplyBundle(a Applier) ReconcileStepFunc {
157180
return func(ctx context.Context, ext *ocv1.ClusterExtension) (context.Context, *ctrl.Result, error) {
158181
l := log.FromContext(ctx)
159-
resolvedRevisionMetadata := ctx.Value(resolvedRevisionMetadataKey{}).(*RevisionMetadata)
160-
imageFS := ctx.Value(imageFSKey{}).(fs.FS)
161-
revisionStates := ctx.Value(revisionStatesKey{}).(*RevisionStates)
182+
resolvedRevisionMetadata, err := valueFromContext[RevisionMetadata](ctx, resolvedRevisionMetadataKey{})
183+
if err != nil {
184+
return ctx, nil, err
185+
}
186+
imageFS, err := valueFromContext[fs.FS](ctx, imageFSKey{})
187+
if err != nil {
188+
return ctx, nil, err
189+
}
190+
revisionStates, err := valueFromContext[RevisionStates](ctx, revisionStatesKey{})
191+
if err != nil {
192+
return ctx, nil, err
193+
}
162194
storeLbls := map[string]string{
163195
labels.BundleNameKey: resolvedRevisionMetadata.Name,
164196
labels.PackageNameKey: resolvedRevisionMetadata.Package,
@@ -180,7 +212,7 @@ func ApplyBundle(a Applier) ReconcileStepFunc {
180212
// to ensure exponential backoff can occur:
181213
// - Permission errors (it is not possible to watch changes to permissions.
182214
// The only way to eventually recover from permission errors is to keep retrying).
183-
rolloutSucceeded, rolloutStatus, err := a.Apply(ctx, imageFS, ext, objLbls, storeLbls)
215+
rolloutSucceeded, rolloutStatus, err := a.Apply(ctx, *imageFS, ext, objLbls, storeLbls)
184216

185217
// Set installed status
186218
if rolloutSucceeded {

0 commit comments

Comments
 (0)