1- //go:build experimental
1+ //go:build !standard
22
33package controllers
44
99 "strings"
1010 "time"
1111
12- ocv1 "github.com/operator-framework/operator-controller/api/v1"
1312 appsv1 "k8s.io/api/apps/v1"
1413 corev1 "k8s.io/api/core/v1"
1514 "k8s.io/apimachinery/pkg/api/meta"
@@ -33,6 +32,8 @@ import (
3332 "sigs.k8s.io/controller-runtime/pkg/log"
3433 "sigs.k8s.io/controller-runtime/pkg/predicate"
3534 "sigs.k8s.io/controller-runtime/pkg/source"
35+
36+ ocv1 "github.com/operator-framework/operator-controller/api/v1"
3637)
3738
3839const (
@@ -65,17 +66,17 @@ type accessManager interface {
6566//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=clusterextensionrevisions/status,verbs=update;patch
6667//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=clusterextensionrevisions/finalizers,verbs=update
6768
68- func (c * ClusterExtensionRevisionReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (res ctrl.Result , err error ) {
69+ func (c * ClusterExtensionRevisionReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
6970 l := log .FromContext (ctx ).WithName ("cluster-extension-revision" )
7071 ctx = log .IntoContext (ctx , l )
7172
7273 rev := & ocv1.ClusterExtensionRevision {}
7374 if err := c .Client .Get (
7475 ctx , req .NamespacedName , rev ); err != nil {
75- return res , client .IgnoreNotFound (err )
76+ return ctrl. Result {} , client .IgnoreNotFound (err )
7677 }
7778
78- l = l .WithValues ("key" , req .NamespacedName . String ())
79+ l = l .WithValues ("key" , req .String ())
7980 l .Info ("reconcile starting" )
8081 defer l .Info ("reconcile ending" )
8182
@@ -86,36 +87,25 @@ func (c *ClusterExtensionRevisionReconciler) Reconcile(ctx context.Context, req
8687 // To not leave unactionable resources on the cluster, we are going to just
8788 // reap the revision reverences and propagate the Orphan deletion.
8889 if rev .DeletionTimestamp .IsZero () {
89- err := client .IgnoreNotFound (
90+ return ctrl. Result {}, client .IgnoreNotFound (
9091 c .Client .Delete (ctx , rev , client .PropagationPolicy (metav1 .DeletePropagationOrphan ), client.Preconditions {
9192 UID : ptr .To (rev .GetUID ()),
9293 ResourceVersion : ptr .To (rev .GetResourceVersion ()),
9394 }),
9495 )
95- if err != nil {
96- return res , err
97- }
98- // we get requeued to remove the finalizer.
99- return res , nil
100- }
101- if err := c .removeFinalizer (ctx , rev , clusterExtensionRevisionTeardownFinalizer ); err != nil {
102- return res , err
10396 }
104- return res , nil
97+ return ctrl. Result {}, c . removeFinalizer ( ctx , rev , clusterExtensionRevisionTeardownFinalizer )
10598 }
10699
107100 return c .reconcile (ctx , controller , rev )
108101}
109102
110103func (c * ClusterExtensionRevisionReconciler ) reconcile (
111104 ctx context.Context , ce * ocv1.ClusterExtension , rev * ocv1.ClusterExtensionRevision ,
112- ) (res ctrl.Result , err error ) {
105+ ) (ctrl.Result , error ) {
113106 l := log .FromContext (ctx )
114107
115- revision , opts , previous , err := toBoxcutterRevision (ce .Name , rev )
116- if err != nil {
117- return res , fmt .Errorf ("converting CM to revision: %w" , err )
118- }
108+ revision , opts , previous := toBoxcutterRevision (ce .Name , rev )
119109
120110 var objects []client.Object
121111 for _ , phase := range revision .GetPhases () {
@@ -131,13 +121,13 @@ func (c *ClusterExtensionRevisionReconciler) reconcile(
131121 // We can't lookup the complete ClusterExtension when it's already deleted.
132122 // This only works when the controller-manager is not restarted during teardown.
133123 if err := c .Client .Get (ctx , client .ObjectKeyFromObject (ce ), ce ); err != nil {
134- return res , err
124+ return ctrl. Result {} , err
135125 }
136126 }
137127
138128 accessor , err := c .AccessManager .GetWithUser (ctx , ce , rev , objects )
139129 if err != nil {
140- return res , fmt .Errorf ("get cache: %w" , err )
130+ return ctrl. Result {} , fmt .Errorf ("get cache: %w" , err )
141131 }
142132
143133 // Boxcutter machinery setup.
@@ -160,60 +150,52 @@ func (c *ClusterExtensionRevisionReconciler) reconcile(
160150 //
161151 tres , err := re .Teardown (ctx , * revision )
162152 if err != nil {
163- return res , fmt .Errorf ("revision teardown: %w" , err )
153+ return ctrl. Result {} , fmt .Errorf ("revision teardown: %w" , err )
164154 }
165155
166156 l .Info ("teardown report" , "report" , tres .String ())
167157
168158 if ! tres .IsComplete () {
169- return res , nil
159+ return ctrl. Result {} , nil
170160 }
171161 if err := c .AccessManager .FreeWithUser (ctx , ce , rev ); err != nil {
172- return res , fmt .Errorf ("get cache: %w" , err )
173- }
174- if err := c .removeFinalizer (ctx , rev , clusterExtensionRevisionTeardownFinalizer ); err != nil {
175- return res , err
162+ return ctrl.Result {}, fmt .Errorf ("get cache: %w" , err )
176163 }
177- return res , nil
164+ return ctrl. Result {}, c . removeFinalizer ( ctx , rev , clusterExtensionRevisionTeardownFinalizer )
178165 }
179166
180167 //
181168 // Reconcile
182169 //
183170 if err := c .ensureFinalizer (ctx , rev , clusterExtensionRevisionTeardownFinalizer ); err != nil {
184- return res , err
171+ return ctrl. Result {} , err
185172 }
186173 rres , err := re .Reconcile (ctx , * revision , opts ... )
187174 if err != nil {
188- return res , fmt .Errorf ("revision reconcile: %w" , err )
175+ return ctrl. Result {} , fmt .Errorf ("revision reconcile: %w" , err )
189176 }
190177 l .Info ("reconcile report" , "report" , rres .String ())
191178
192179 // Retry failing preflight checks with a flat 10s retry.
193180 // TODO: report status, backoff?
194181 if verr := rres .GetValidationError (); verr != nil {
195182 l .Info ("preflight error, retrying after 10s" , "err" , verr .String ())
196-
197- res .RequeueAfter = 10 * time .Second
198- //nolint:nilerr
199- return res , nil
183+ return ctrl.Result {RequeueAfter : 10 * time .Second }, nil
200184 }
201185 for _ , pres := range rres .GetPhases () {
202186 if verr := pres .GetValidationError (); verr != nil {
203187 l .Info ("preflight error, retrying after 10s" , "err" , verr .String ())
204-
205- res .RequeueAfter = 10 * time .Second
206- //nolint:nilerr
207- return res , nil
188+ return ctrl.Result {RequeueAfter : 10 * time .Second }, nil
208189 }
209190 }
210191
192+ //nolint:nestif
211193 if rres .IsComplete () {
212194 // Archive other revisions.
213195 for _ , a := range previous {
214196 if err := c .Client .Patch (ctx , a , client .RawPatch (
215197 types .MergePatchType , []byte (`{"data":{"state":"Archived"}}` ))); err != nil {
216- return res , fmt .Errorf ("archive previous Revision: %w" , err )
198+ return ctrl. Result {} , fmt .Errorf ("archive previous Revision: %w" , err )
217199 }
218200 }
219201
@@ -286,7 +268,7 @@ func (c *ClusterExtensionRevisionReconciler) reconcile(
286268 meta .RemoveStatusCondition (& rev .Status .Conditions , "InTransition" )
287269 }
288270
289- return res , c .Client .Status ().Update (ctx , rev )
271+ return ctrl. Result {} , c .Client .Status ().Update (ctx , rev )
290272}
291273
292274func (c * ClusterExtensionRevisionReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
@@ -373,9 +355,9 @@ func getControllingClusterExtension(obj client.Object) (*ocv1.ClusterExtension,
373355}
374356
375357func toBoxcutterRevision (clusterExtensionName string , rev * ocv1.ClusterExtensionRevision ) (
376- r * boxcutter.Revision , opts []boxcutter.RevisionReconcileOption , previous []client.Object , err error ,
358+ * boxcutter.Revision , []boxcutter.RevisionReconcileOption , []client.Object ,
377359) {
378- r = & boxcutter.Revision {
360+ r : = & boxcutter.Revision {
379361 Name : rev .Name ,
380362 Owner : rev ,
381363 Revision : rev .Spec .Revision ,
@@ -397,6 +379,7 @@ func toBoxcutterRevision(clusterExtensionName string, rev *ocv1.ClusterExtension
397379 r .Phases = append (r .Phases , phase )
398380 }
399381
382+ previous := make ([]client.Object , 0 , len (rev .Spec .Previous ))
400383 for _ , specPrevious := range rev .Spec .Previous {
401384 prev := & unstructured.Unstructured {}
402385 prev .SetName (specPrevious .Name )
@@ -405,9 +388,9 @@ func toBoxcutterRevision(clusterExtensionName string, rev *ocv1.ClusterExtension
405388 previous = append (previous , prev )
406389 }
407390
408- opts = []boxcutter.RevisionReconcileOption {
391+ opts : = []boxcutter.RevisionReconcileOption {
409392 boxcutter .WithPreviousOwners (previous ),
410- boxcutter .WithProbe (boxcutter .ProgressProbeType , boxcutter .ProbeFunc (func (obj client.Object ) (success bool , messages []string ) {
393+ boxcutter .WithProbe (boxcutter .ProgressProbeType , boxcutter .ProbeFunc (func (obj client.Object ) (bool , []string ) {
411394 deployGK := schema.GroupKind {
412395 Group : "apps" , Kind : "Deployment" ,
413396 }
@@ -430,12 +413,11 @@ func toBoxcutterRevision(clusterExtensionName string, rev *ocv1.ClusterExtension
430413 return true , nil
431414 }
432415 }
433-
434416 return false , []string {"not available or not fully updated" }
435417 })),
436418 }
437419 if rev .Spec .LifecycleState == ocv1 .ClusterExtensionRevisionLifecycleStatePaused {
438420 opts = append (opts , boxcutter.WithPaused {})
439421 }
440- return
422+ return r , opts , previous
441423}
0 commit comments