Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(finalizer): patch finalizers instead of apply #155

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading