@@ -19,6 +19,7 @@ import (
1919 "k8s.io/client-go/discovery"
2020 "k8s.io/client-go/rest"
2121 "pkg.package-operator.run/boxcutter/machinery"
22+ machinerytypes "pkg.package-operator.run/boxcutter/machinery/types"
2223 "pkg.package-operator.run/boxcutter/managedcache"
2324 "pkg.package-operator.run/boxcutter/ownerhandling"
2425 "pkg.package-operator.run/boxcutter/validation"
@@ -29,8 +30,19 @@ import (
2930 "github.com/operator-framework/operator-controller/internal/operator-controller/labels"
3031)
3132
32- // DefaultRevisionEngineFactory creates boxcutter RevisionEngines with serviceAccount-scoped clients.
33- type DefaultRevisionEngineFactory struct {
33+ // RevisionEngine defines the interface for reconciling and tearing down revisions.
34+ type RevisionEngine interface {
35+ Teardown (ctx context.Context , rev machinerytypes.Revision , opts ... machinerytypes.RevisionTeardownOption ) (machinery.RevisionTeardownResult , error )
36+ Reconcile (ctx context.Context , rev machinerytypes.Revision , opts ... machinerytypes.RevisionReconcileOption ) (machinery.RevisionResult , error )
37+ }
38+
39+ // RevisionEngineFactory creates a RevisionEngine for a ClusterExtensionRevision.
40+ type RevisionEngineFactory interface {
41+ CreateRevisionEngine (ctx context.Context , rev * ocv1.ClusterExtensionRevision ) (RevisionEngine , error )
42+ }
43+
44+ // defaultRevisionEngineFactory creates boxcutter RevisionEngines with serviceAccount-scoped clients.
45+ type defaultRevisionEngineFactory struct {
3446 Scheme * runtime.Scheme
3547 TrackingCache managedcache.TrackingCache
3648 DiscoveryClient discovery.CachedDiscoveryInterface
@@ -42,8 +54,13 @@ type DefaultRevisionEngineFactory struct {
4254
4355// CreateRevisionEngine constructs a boxcutter RevisionEngine for the given ClusterExtensionRevision.
4456// It reads the ServiceAccount from annotations and creates a scoped client.
45- func (f * DefaultRevisionEngineFactory ) CreateRevisionEngine (_ context.Context , rev * ocv1.ClusterExtensionRevision ) (RevisionEngine , error ) {
46- scopedClient , err := f .getScopedClient (rev )
57+ func (f * defaultRevisionEngineFactory ) CreateRevisionEngine (_ context.Context , rev * ocv1.ClusterExtensionRevision ) (RevisionEngine , error ) {
58+ saNamespace , saName , err := f .getServiceAccount (rev )
59+ if err != nil {
60+ return nil , err
61+ }
62+
63+ scopedClient , err := f .createScopedClient (saNamespace , saName )
4764 if err != nil {
4865 return nil , err
4966 }
@@ -62,33 +79,26 @@ func (f *DefaultRevisionEngineFactory) CreateRevisionEngine(_ context.Context, r
6279 ), nil
6380}
6481
65- func (f * DefaultRevisionEngineFactory ) getScopedClient (rev * ocv1.ClusterExtensionRevision ) (client. Client , error ) {
82+ func (f * defaultRevisionEngineFactory ) getServiceAccount (rev * ocv1.ClusterExtensionRevision ) (string , string , error ) {
6683 annotations := rev .GetAnnotations ()
6784 if annotations == nil {
68- return nil , fmt .Errorf ("revision %q is missing required annotations" , rev .Name )
85+ return "" , "" , fmt .Errorf ("revision %q is missing required annotations" , rev .Name )
6986 }
7087
7188 saName := strings .TrimSpace (annotations [labels .ServiceAccountNameKey ])
7289 saNamespace := strings .TrimSpace (annotations [labels .ServiceAccountNamespaceKey ])
7390
7491 if len (saName ) == 0 {
75- return nil , fmt .Errorf ("revision %q is missing ServiceAccount name annotation" , rev .Name )
92+ return "" , "" , fmt .Errorf ("revision %q is missing ServiceAccount name annotation" , rev .Name )
7693 }
7794 if len (saNamespace ) == 0 {
78- return nil , fmt .Errorf ("revision %q is missing ServiceAccount namespace annotation" , rev .Name )
95+ return "" , "" , fmt .Errorf ("revision %q is missing ServiceAccount namespace annotation" , rev .Name )
7996 }
8097
81- return f . createScopedClient ( saNamespace , saName )
98+ return saNamespace , saName , nil
8299}
83100
84- func (f * DefaultRevisionEngineFactory ) createScopedClient (namespace , serviceAccountName string ) (client.Client , error ) {
85- if f .TokenGetter == nil {
86- return nil , fmt .Errorf ("TokenGetter is required but not configured" )
87- }
88- if f .BaseConfig == nil {
89- return nil , fmt .Errorf ("BaseConfig is required but not configured" )
90- }
91-
101+ func (f * defaultRevisionEngineFactory ) createScopedClient (namespace , serviceAccountName string ) (client.Client , error ) {
92102 saConfig := rest .AnonymousClientConfig (f .BaseConfig )
93103 saConfig .Wrap (func (rt http.RoundTripper ) http.RoundTripper {
94104 return & authentication.TokenInjectingRoundTripper {
@@ -111,7 +121,7 @@ func (f *DefaultRevisionEngineFactory) createScopedClient(namespace, serviceAcco
111121 return scopedClient , nil
112122}
113123
114- // NewDefaultRevisionEngineFactory creates a new DefaultRevisionEngineFactory .
124+ // NewDefaultRevisionEngineFactory creates a new defaultRevisionEngineFactory .
115125func NewDefaultRevisionEngineFactory (
116126 scheme * runtime.Scheme ,
117127 trackingCache managedcache.TrackingCache ,
@@ -120,14 +130,20 @@ func NewDefaultRevisionEngineFactory(
120130 fieldOwnerPrefix string ,
121131 baseConfig * rest.Config ,
122132 tokenGetter * authentication.TokenGetter ,
123- ) * DefaultRevisionEngineFactory {
124- return & DefaultRevisionEngineFactory {
133+ ) (RevisionEngineFactory , error ) {
134+ if baseConfig == nil {
135+ return nil , fmt .Errorf ("baseConfig is required but not provided" )
136+ }
137+ if tokenGetter == nil {
138+ return nil , fmt .Errorf ("tokenGetter is required but not provided" )
139+ }
140+ return & defaultRevisionEngineFactory {
125141 Scheme : scheme ,
126142 TrackingCache : trackingCache ,
127143 DiscoveryClient : discoveryClient ,
128144 RESTMapper : restMapper ,
129145 FieldOwnerPrefix : fieldOwnerPrefix ,
130146 BaseConfig : baseConfig ,
131147 TokenGetter : tokenGetter ,
132- }
148+ }, nil
133149}
0 commit comments