@@ -2,20 +2,15 @@ package operators
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
"reflect"
7
6
8
7
"github.com/go-logr/logr"
9
- admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
10
- rbacv1 "k8s.io/api/rbac/v1"
11
8
apierrors "k8s.io/apimachinery/pkg/api/errors"
12
9
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
10
"k8s.io/apimachinery/pkg/runtime"
14
- utilerrors "k8s.io/apimachinery/pkg/util/errors"
15
11
ctrl "sigs.k8s.io/controller-runtime"
16
12
"sigs.k8s.io/controller-runtime/pkg/builder"
17
13
"sigs.k8s.io/controller-runtime/pkg/client"
18
- "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
19
14
"sigs.k8s.io/controller-runtime/pkg/event"
20
15
"sigs.k8s.io/controller-runtime/pkg/handler"
21
16
"sigs.k8s.io/controller-runtime/pkg/predicate"
@@ -100,10 +95,6 @@ func (r *OperatorConditionGeneratorReconciler) Reconcile(ctx context.Context, re
100
95
return ctrl.Result {}, client .IgnoreNotFound (err )
101
96
}
102
97
103
- if err , ok := r .processFinalizer (ctx , in ); ! ok {
104
- return ctrl.Result {}, err
105
- }
106
-
107
98
operatorCondition := & operatorsv2.OperatorCondition {
108
99
ObjectMeta : metav1.ObjectMeta {
109
100
// For now, only generate an OperatorCondition with the same name as the csv.
@@ -179,112 +170,3 @@ func (r *OperatorConditionGeneratorReconciler) ensureOperatorCondition(operatorC
179
170
existingOperatorCondition .Spec .ServiceAccounts = operatorCondition .Spec .ServiceAccounts
180
171
return r .Client .Update (context .TODO (), existingOperatorCondition )
181
172
}
182
-
183
- // Return values, err, ok; ok == true: continue Reconcile, ok == false: exit Reconcile
184
- func (r * OperatorConditionGeneratorReconciler ) processFinalizer (ctx context.Context , csv * operatorsv1alpha1.ClusterServiceVersion ) (error , bool ) {
185
- myFinalizerName := "operators.coreos.com/csv-cleanup"
186
- log := r .log .WithValues ("name" , csv .GetName ()).WithValues ("namespace" , csv .GetNamespace ())
187
-
188
- if csv .ObjectMeta .DeletionTimestamp .IsZero () {
189
- // CSV is not being deleted, add finalizer if not present
190
- if ! controllerutil .ContainsFinalizer (csv , myFinalizerName ) {
191
- patch := csv .DeepCopy ()
192
- controllerutil .AddFinalizer (patch , myFinalizerName )
193
- if err := r .Client .Patch (ctx , patch , client .MergeFrom (csv )); err != nil {
194
- log .Error (err , "Adding finalizer" )
195
- return err , false
196
- }
197
- }
198
- return nil , true
199
- }
200
-
201
- if ! controllerutil .ContainsFinalizer (csv , myFinalizerName ) {
202
- // Finalizer has been removed; stop reconciliation as the CSV is being deleted
203
- return nil , false
204
- }
205
-
206
- // CSV is being deleted and the finalizer still present; do any clean up
207
- ownerSelector := ownerutil .CSVOwnerSelector (csv )
208
- listOptions := client.ListOptions {
209
- LabelSelector : ownerSelector ,
210
- }
211
- deleteOptions := client.DeleteAllOfOptions {
212
- ListOptions : listOptions ,
213
- }
214
- // Look for resources owned by this CSV, and delete them.
215
- log .WithValues ("selector" , ownerSelector ).Info ("Cleaning up resources after CSV deletion" )
216
- var errs []error
217
-
218
- err := r .Client .DeleteAllOf (ctx , & rbacv1.ClusterRoleBinding {}, & deleteOptions )
219
- if client .IgnoreNotFound (err ) != nil {
220
- log .Error (err , "Deleting ClusterRoleBindings on CSV delete" )
221
- errs = append (errs , err )
222
- }
223
-
224
- err = r .Client .DeleteAllOf (ctx , & rbacv1.ClusterRole {}, & deleteOptions )
225
- if client .IgnoreNotFound (err ) != nil {
226
- log .Error (err , "Deleting ClusterRoles on CSV delete" )
227
- errs = append (errs , err )
228
- }
229
-
230
- err = r .Client .DeleteAllOf (ctx , & admissionregistrationv1.MutatingWebhookConfiguration {}, & deleteOptions )
231
- if client .IgnoreNotFound (err ) != nil {
232
- log .Error (err , "Deleting MutatingWebhookConfigurations on CSV delete" )
233
- errs = append (errs , err )
234
- }
235
-
236
- err = r .Client .DeleteAllOf (ctx , & admissionregistrationv1.ValidatingWebhookConfiguration {}, & deleteOptions )
237
- if client .IgnoreNotFound (err ) != nil {
238
- log .Error (err , "Deleting ValidatingWebhookConfigurations on CSV delete" )
239
- errs = append (errs , err )
240
- }
241
-
242
- // Make sure things are deleted
243
- crbList := & rbacv1.ClusterRoleBindingList {}
244
- err = r .Client .List (ctx , crbList , & listOptions )
245
- if err != nil {
246
- errs = append (errs , err )
247
- } else if len (crbList .Items ) != 0 {
248
- errs = append (errs , fmt .Errorf ("waiting for ClusterRoleBindings to delete" ))
249
- }
250
-
251
- crList := & rbacv1.ClusterRoleList {}
252
- err = r .Client .List (ctx , crList , & listOptions )
253
- if err != nil {
254
- errs = append (errs , err )
255
- } else if len (crList .Items ) != 0 {
256
- errs = append (errs , fmt .Errorf ("waiting for ClusterRoles to delete" ))
257
- }
258
-
259
- mwcList := & admissionregistrationv1.MutatingWebhookConfigurationList {}
260
- err = r .Client .List (ctx , mwcList , & listOptions )
261
- if err != nil {
262
- errs = append (errs , err )
263
- } else if len (mwcList .Items ) != 0 {
264
- errs = append (errs , fmt .Errorf ("waiting for MutatingWebhookConfigurations to delete" ))
265
- }
266
-
267
- vwcList := & admissionregistrationv1.ValidatingWebhookConfigurationList {}
268
- err = r .Client .List (ctx , vwcList , & listOptions )
269
- if err != nil {
270
- errs = append (errs , err )
271
- } else if len (vwcList .Items ) != 0 {
272
- errs = append (errs , fmt .Errorf ("waiting for ValidatingWebhookConfigurations to delete" ))
273
- }
274
-
275
- // Return any errors
276
- if err := utilerrors .NewAggregate (errs ); err != nil {
277
- return err , false
278
- }
279
-
280
- // If no errors, remove our finalizer from the CSV and update
281
- patch := csv .DeepCopy ()
282
- controllerutil .RemoveFinalizer (patch , myFinalizerName )
283
- if err := r .Client .Patch (ctx , patch , client .MergeFrom (csv )); err != nil {
284
- log .Error (err , "Removing finalizer" )
285
- return err , false
286
- }
287
-
288
- // Stop reconciliation as the csv is being deleted
289
- return nil , false
290
- }
0 commit comments