Skip to content

Commit d246832

Browse files
committed
Only re-create operator resource if it has existing components
This commit fixes a bug in the operator controller that causes it to ALWAYS re-create deleted operator objects, even when no other resources associated with that operator exist.
1 parent 472dd03 commit d246832

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

pkg/controller/operators/operator_controller.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
rbacv1 "k8s.io/api/rbac/v1"
1212
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1313
apierrors "k8s.io/apimachinery/pkg/api/errors"
14+
"k8s.io/apimachinery/pkg/api/meta"
1415
"k8s.io/apimachinery/pkg/labels"
1516
"k8s.io/apimachinery/pkg/runtime"
1617
"k8s.io/apimachinery/pkg/types"
@@ -119,6 +120,12 @@ func (r *OperatorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
119120
in := &operatorsv1.Operator{}
120121
if err := r.Get(ctx, req.NamespacedName, in); err != nil {
121122
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+
}
122129
create = true
123130
in.SetName(name)
124131
} else {
@@ -219,6 +226,33 @@ func (r *OperatorReconciler) listComponents(ctx context.Context, selector labels
219226
return componentLists, nil
220227
}
221228

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+
222256
func (r *OperatorReconciler) getLastResourceVersion(name types.NamespacedName) (string, bool) {
223257
r.mu.RLock()
224258
defer r.mu.RUnlock()

0 commit comments

Comments
 (0)