From 57f3191f623cb9534d2f90aa7c8c79e1d1db8c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadeu=C5=A1=20Varnas?= Date: Fri, 13 Sep 2024 15:13:53 +0300 Subject: [PATCH] fix: don't patch finalizers while resource already exists (#137) --- actions/create_handler.go | 1 + actions/create_handler_test.go | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/actions/create_handler.go b/actions/create_handler.go index 4f75791..4c5bc8d 100644 --- a/actions/create_handler.go +++ b/actions/create_handler.go @@ -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") diff --git a/actions/create_handler_test.go b/actions/create_handler_test.go index d930bb4..b853b21 100644 --- a/actions/create_handler_test.go +++ b/actions/create_handler_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "testing" + "time" "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" @@ -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 @@ -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 { @@ -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 {