@@ -19,6 +19,7 @@ package controllers
1919import (
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{}
3940type resolvedRevisionMetadataKey struct {}
4041type 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+
4259func 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 {
90107func 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 {
156179func 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