Skip to content

Commit 603e4c9

Browse files
committed
operator: avoid unnecessary reconcile
Signed-off-by: zhzhuang-zju <[email protected]>
1 parent f26d76a commit 603e4c9

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

operator/pkg/controller/karmada/controller.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,16 @@ func (ctrl *Controller) Reconcile(ctx context.Context, req controllerruntime.Req
9292
return ctrl.removeFinalizer(ctx, karmada)
9393
}
9494

95-
if err := ctrl.updateKarmada(ctx, karmada); err != nil {
95+
// Simulate a mutation webhook by setting defaults. Return and wait for the next reconciliation if spec is updated.
96+
specUpdated, err := ctrl.updateKarmada(ctx, karmada)
97+
if err != nil {
9698
return controllerruntime.Result{}, err
9799
}
100+
if specUpdated {
101+
return controllerruntime.Result{}, nil
102+
}
98103

104+
// Simulate a validation webhook by validating the resource.
99105
if err := ctrl.validateKarmada(ctx, karmada); err != nil {
100106
klog.ErrorS(err, "Validation failed for karmada", "name", karmada.Name)
101107
return controllerruntime.Result{}, nil
@@ -129,7 +135,7 @@ func (ctrl *Controller) removeFinalizer(ctx context.Context, karmada *operatorv1
129135
})
130136
}
131137

132-
func (ctrl *Controller) updateKarmada(ctx context.Context, karmada *operatorv1alpha1.Karmada) error {
138+
func (ctrl *Controller) updateKarmada(ctx context.Context, karmada *operatorv1alpha1.Karmada) (bool, error) {
133139
// The object is not being deleted, so if it does not have our finalizer,
134140
// then lets add the finalizer and update the object. This is equivalent
135141
// registering our finalizer.
@@ -144,12 +150,13 @@ func (ctrl *Controller) updateKarmada(ctx context.Context, karmada *operatorv1al
144150

145151
// Set the defaults for karmada
146152
operatorscheme.Scheme.Default(karmada)
153+
specUpdated := !reflect.DeepEqual(karmada.Spec, older.Spec)
147154

148-
if updated || !reflect.DeepEqual(karmada.Spec, older.Spec) {
149-
return ctrl.Update(ctx, karmada)
155+
if updated || specUpdated {
156+
return specUpdated, ctrl.Update(ctx, karmada)
150157
}
151158

152-
return nil
159+
return specUpdated, nil
153160
}
154161

155162
// SetupWithManager creates a controller and register to controller manager.

0 commit comments

Comments
 (0)