Skip to content

Commit

Permalink
fix(finalizer): patch finalizers instead of apply (#155)
Browse files Browse the repository at this point in the history
When applying the finalizers, the object generation version is bumped. We don't want that so we're patching the finalizers.
  • Loading branch information
itamar-marom authored Apr 17, 2024
1 parent 7f60698 commit b197f03
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions controllers/druid/finalizers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package druid

import (
"context"
"encoding/json"
"fmt"

"github.com/datainfrahq/druid-operator/apis/druid/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand All @@ -34,10 +37,21 @@ func updateFinalizers(ctx context.Context, sdk client.Client, m *v1alpha1.Druid,
}
}

m.SetFinalizers(desiredFinalizers)
_, err := writers.Update(ctx, sdk, m, m, emitEvents)
if err != nil {
return err
if !equality.Semantic.DeepEqual(m.GetFinalizers(), desiredFinalizers) {
m.SetFinalizers(desiredFinalizers)

finalizersBytes, err := json.Marshal(m.GetFinalizers())
if err != nil {
return fmt.Errorf("failed to serialize finalizers patch to bytes: %v", err)
}

patch := []byte(fmt.Sprintf(`[{"op": "replace", "path": "/metadata/finalizers", "value": %s}]`, finalizersBytes))

err = sdk.Patch(ctx, m, client.RawPatch(types.JSONPatchType, patch))
if err != nil {
return err
}

}

return nil
Expand Down

0 comments on commit b197f03

Please sign in to comment.