1
- //go:build experimental
1
+ //go:build !standard
2
2
3
3
package controllers
4
4
9
9
"strings"
10
10
"time"
11
11
12
- ocv1 "github.com/operator-framework/operator-controller/api/v1"
13
12
appsv1 "k8s.io/api/apps/v1"
14
13
corev1 "k8s.io/api/core/v1"
15
14
"k8s.io/apimachinery/pkg/api/meta"
@@ -33,6 +32,8 @@ import (
33
32
"sigs.k8s.io/controller-runtime/pkg/log"
34
33
"sigs.k8s.io/controller-runtime/pkg/predicate"
35
34
"sigs.k8s.io/controller-runtime/pkg/source"
35
+
36
+ ocv1 "github.com/operator-framework/operator-controller/api/v1"
36
37
)
37
38
38
39
const (
@@ -65,17 +66,17 @@ type accessManager interface {
65
66
//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=clusterextensionrevisions/status,verbs=update;patch
66
67
//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=clusterextensionrevisions/finalizers,verbs=update
67
68
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 ) {
69
70
l := log .FromContext (ctx ).WithName ("cluster-extension-revision" )
70
71
ctx = log .IntoContext (ctx , l )
71
72
72
73
rev := & ocv1.ClusterExtensionRevision {}
73
74
if err := c .Client .Get (
74
75
ctx , req .NamespacedName , rev ); err != nil {
75
- return res , client .IgnoreNotFound (err )
76
+ return ctrl. Result {} , client .IgnoreNotFound (err )
76
77
}
77
78
78
- l = l .WithValues ("key" , req .NamespacedName . String ())
79
+ l = l .WithValues ("key" , req .String ())
79
80
l .Info ("reconcile starting" )
80
81
defer l .Info ("reconcile ending" )
81
82
@@ -86,36 +87,25 @@ func (c *ClusterExtensionRevisionReconciler) Reconcile(ctx context.Context, req
86
87
// To not leave unactionable resources on the cluster, we are going to just
87
88
// reap the revision reverences and propagate the Orphan deletion.
88
89
if rev .DeletionTimestamp .IsZero () {
89
- err := client .IgnoreNotFound (
90
+ return ctrl. Result {}, client .IgnoreNotFound (
90
91
c .Client .Delete (ctx , rev , client .PropagationPolicy (metav1 .DeletePropagationOrphan ), client.Preconditions {
91
92
UID : ptr .To (rev .GetUID ()),
92
93
ResourceVersion : ptr .To (rev .GetResourceVersion ()),
93
94
}),
94
95
)
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
103
96
}
104
- return res , nil
97
+ return ctrl. Result {}, c . removeFinalizer ( ctx , rev , clusterExtensionRevisionTeardownFinalizer )
105
98
}
106
99
107
100
return c .reconcile (ctx , controller , rev )
108
101
}
109
102
110
103
func (c * ClusterExtensionRevisionReconciler ) reconcile (
111
104
ctx context.Context , ce * ocv1.ClusterExtension , rev * ocv1.ClusterExtensionRevision ,
112
- ) (res ctrl.Result , err error ) {
105
+ ) (ctrl.Result , error ) {
113
106
l := log .FromContext (ctx )
114
107
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 )
119
109
120
110
var objects []client.Object
121
111
for _ , phase := range revision .GetPhases () {
@@ -131,13 +121,13 @@ func (c *ClusterExtensionRevisionReconciler) reconcile(
131
121
// We can't lookup the complete ClusterExtension when it's already deleted.
132
122
// This only works when the controller-manager is not restarted during teardown.
133
123
if err := c .Client .Get (ctx , client .ObjectKeyFromObject (ce ), ce ); err != nil {
134
- return res , err
124
+ return ctrl. Result {} , err
135
125
}
136
126
}
137
127
138
128
accessor , err := c .AccessManager .GetWithUser (ctx , ce , rev , objects )
139
129
if err != nil {
140
- return res , fmt .Errorf ("get cache: %w" , err )
130
+ return ctrl. Result {} , fmt .Errorf ("get cache: %w" , err )
141
131
}
142
132
143
133
// Boxcutter machinery setup.
@@ -160,60 +150,52 @@ func (c *ClusterExtensionRevisionReconciler) reconcile(
160
150
//
161
151
tres , err := re .Teardown (ctx , * revision )
162
152
if err != nil {
163
- return res , fmt .Errorf ("revision teardown: %w" , err )
153
+ return ctrl. Result {} , fmt .Errorf ("revision teardown: %w" , err )
164
154
}
165
155
166
156
l .Info ("teardown report" , "report" , tres .String ())
167
157
168
158
if ! tres .IsComplete () {
169
- return res , nil
159
+ return ctrl. Result {} , nil
170
160
}
171
161
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 )
176
163
}
177
- return res , nil
164
+ return ctrl. Result {}, c . removeFinalizer ( ctx , rev , clusterExtensionRevisionTeardownFinalizer )
178
165
}
179
166
180
167
//
181
168
// Reconcile
182
169
//
183
170
if err := c .ensureFinalizer (ctx , rev , clusterExtensionRevisionTeardownFinalizer ); err != nil {
184
- return res , err
171
+ return ctrl. Result {} , err
185
172
}
186
173
rres , err := re .Reconcile (ctx , * revision , opts ... )
187
174
if err != nil {
188
- return res , fmt .Errorf ("revision reconcile: %w" , err )
175
+ return ctrl. Result {} , fmt .Errorf ("revision reconcile: %w" , err )
189
176
}
190
177
l .Info ("reconcile report" , "report" , rres .String ())
191
178
192
179
// Retry failing preflight checks with a flat 10s retry.
193
180
// TODO: report status, backoff?
194
181
if verr := rres .GetValidationError (); verr != nil {
195
182
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
200
184
}
201
185
for _ , pres := range rres .GetPhases () {
202
186
if verr := pres .GetValidationError (); verr != nil {
203
187
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
208
189
}
209
190
}
210
191
192
+ //nolint:nestif
211
193
if rres .IsComplete () {
212
194
// Archive other revisions.
213
195
for _ , a := range previous {
214
196
if err := c .Client .Patch (ctx , a , client .RawPatch (
215
197
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 )
217
199
}
218
200
}
219
201
@@ -286,7 +268,7 @@ func (c *ClusterExtensionRevisionReconciler) reconcile(
286
268
meta .RemoveStatusCondition (& rev .Status .Conditions , "InTransition" )
287
269
}
288
270
289
- return res , c .Client .Status ().Update (ctx , rev )
271
+ return ctrl. Result {} , c .Client .Status ().Update (ctx , rev )
290
272
}
291
273
292
274
func (c * ClusterExtensionRevisionReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
@@ -373,9 +355,9 @@ func getControllingClusterExtension(obj client.Object) (*ocv1.ClusterExtension,
373
355
}
374
356
375
357
func 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 ,
377
359
) {
378
- r = & boxcutter.Revision {
360
+ r : = & boxcutter.Revision {
379
361
Name : rev .Name ,
380
362
Owner : rev ,
381
363
Revision : rev .Spec .Revision ,
@@ -397,6 +379,7 @@ func toBoxcutterRevision(clusterExtensionName string, rev *ocv1.ClusterExtension
397
379
r .Phases = append (r .Phases , phase )
398
380
}
399
381
382
+ previous := make ([]client.Object , 0 , len (rev .Spec .Previous ))
400
383
for _ , specPrevious := range rev .Spec .Previous {
401
384
prev := & unstructured.Unstructured {}
402
385
prev .SetName (specPrevious .Name )
@@ -405,9 +388,9 @@ func toBoxcutterRevision(clusterExtensionName string, rev *ocv1.ClusterExtension
405
388
previous = append (previous , prev )
406
389
}
407
390
408
- opts = []boxcutter.RevisionReconcileOption {
391
+ opts : = []boxcutter.RevisionReconcileOption {
409
392
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 ) {
411
394
deployGK := schema.GroupKind {
412
395
Group : "apps" , Kind : "Deployment" ,
413
396
}
@@ -430,12 +413,11 @@ func toBoxcutterRevision(clusterExtensionName string, rev *ocv1.ClusterExtension
430
413
return true , nil
431
414
}
432
415
}
433
-
434
416
return false , []string {"not available or not fully updated" }
435
417
})),
436
418
}
437
419
if rev .Spec .LifecycleState == ocv1 .ClusterExtensionRevisionLifecycleStatePaused {
438
420
opts = append (opts , boxcutter.WithPaused {})
439
421
}
440
- return
422
+ return r , opts , previous
441
423
}
0 commit comments