Skip to content

Commit

Permalink
fix: don't patch finalizers while resource already exists (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
varnastadeus authored Sep 13, 2024
1 parent 1afe013 commit 57f3191
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions actions/create_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (h *createHandler) Handle(ctx context.Context, action *castai.ClusterAction
newObj.SetUID(obj.GetUID())
newObj.SetGeneration(obj.GetGeneration())
newObj.SetManagedFields(obj.GetManagedFields())
newObj.SetFinalizers(obj.GetFinalizers())

// Status fields should be omitted.
delete(obj.Object, "status")
Expand Down
33 changes: 32 additions & 1 deletion actions/create_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"testing"
"time"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
Expand All @@ -23,6 +24,7 @@ func Test_newCreateHandler(t *testing.T) {
_ = appsv1.AddToScheme(scheme)
ctx := context.Background()

now := metav1.Time{Time: time.Date(2024, time.September, 1, 0, 0, 0, 0, time.Local)}
tests := map[string]struct {
objs []runtime.Object
action *castai.ClusterAction
Expand Down Expand Up @@ -75,8 +77,11 @@ func Test_newCreateHandler(t *testing.T) {
})),
},
},
objs: []runtime.Object{newDeployment(func(d *appsv1.Deployment) {})},
objs: []runtime.Object{newDeployment(func(d *appsv1.Deployment) {
d.CreationTimestamp = now
})},
want: newDeployment(func(d *appsv1.Deployment) {
d.CreationTimestamp = now
d.Labels = map[string]string{"changed": "true"}
}),
convertFn: func(i map[string]interface{}) client.Object {
Expand All @@ -85,6 +90,32 @@ func Test_newCreateHandler(t *testing.T) {
return out
},
},
"should not patch already existing resource finalizers": {
action: &castai.ClusterAction{
ActionCreate: &castai.ActionCreate{
GroupVersionResource: castai.GroupVersionResource{
Group: appsv1.SchemeGroupVersion.Group,
Version: appsv1.SchemeGroupVersion.Version,
Resource: "deployments",
},
Object: getObj(t, newDeployment(func(d *appsv1.Deployment) {
})),
},
},
objs: []runtime.Object{newDeployment(func(d *appsv1.Deployment) {
d.CreationTimestamp = now
d.Finalizers = []string{"autoscaling.cast.ai/recommendation"}
})},
want: newDeployment(func(d *appsv1.Deployment) {
d.CreationTimestamp = now
d.Finalizers = []string{"autoscaling.cast.ai/recommendation"}
}),
convertFn: func(i map[string]interface{}) client.Object {
out := &appsv1.Deployment{}
_ = runtime.DefaultUnstructuredConverter.FromUnstructured(i, out)
return out
},
},
}

for name, test := range tests {
Expand Down

0 comments on commit 57f3191

Please sign in to comment.