@@ -11,6 +11,7 @@ import (
11
11
rbacv1 "k8s.io/api/rbac/v1"
12
12
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
13
13
apierrors "k8s.io/apimachinery/pkg/api/errors"
14
+ "k8s.io/apimachinery/pkg/api/meta"
14
15
"k8s.io/apimachinery/pkg/labels"
15
16
"k8s.io/apimachinery/pkg/runtime"
16
17
"k8s.io/apimachinery/pkg/types"
@@ -119,6 +120,12 @@ func (r *OperatorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
119
120
in := & operatorsv1.Operator {}
120
121
if err := r .Get (ctx , req .NamespacedName , in ); err != nil {
121
122
if apierrors .IsNotFound (err ) {
123
+ // If the Operator instance is not found, we're likely reconciling because
124
+ // of a DELETE event. Only recreate the Operator if any of its components
125
+ // still exist.
126
+ if exists , err := r .hasExistingComponents (ctx , name ); err != nil || ! exists {
127
+ return reconcile.Result {}, err
128
+ }
122
129
create = true
123
130
in .SetName (name )
124
131
} else {
@@ -219,6 +226,33 @@ func (r *OperatorReconciler) listComponents(ctx context.Context, selector labels
219
226
return componentLists , nil
220
227
}
221
228
229
+ func (r * OperatorReconciler ) hasExistingComponents (ctx context.Context , name string ) (bool , error ) {
230
+ op := & operatorsv1.Operator {}
231
+ op .SetName (name )
232
+ operator := decorators.Operator {Operator : op }
233
+
234
+ selector , err := operator .ComponentSelector ()
235
+ if err != nil {
236
+ return false , err
237
+ }
238
+
239
+ components , err := r .listComponents (ctx , selector )
240
+ if err != nil {
241
+ return false , err
242
+ }
243
+
244
+ for _ , list := range components {
245
+ items , err := meta .ExtractList (list )
246
+ if err != nil {
247
+ return false , fmt .Errorf ("Unable to extract list from runtime.Object" )
248
+ }
249
+ if len (items ) > 0 {
250
+ return true , nil
251
+ }
252
+ }
253
+ return false , nil
254
+ }
255
+
222
256
func (r * OperatorReconciler ) getLastResourceVersion (name types.NamespacedName ) (string , bool ) {
223
257
r .mu .RLock ()
224
258
defer r .mu .RUnlock ()
0 commit comments