From e33a217bdb434a5fb5c6c21d234082b04d348412 Mon Sep 17 00:00:00 2001 From: vie-serendipity <60083692+vie-serendipity@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:49:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20deployment=20render=20should=20only=20mu?= =?UTF-8?q?tate=20workloads=20created=20by=20yurtap=E2=80=A6=20(#1974)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: deployment render should only mutate workloads created by yurtappset v1alpha1 --- .../v1alpha1/deploymentrender_default.go | 3 ++ .../v1alpha1/deploymentrender_webhook_test.go | 42 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/pkg/yurtmanager/webhook/deploymentrender/v1alpha1/deploymentrender_default.go b/pkg/yurtmanager/webhook/deploymentrender/v1alpha1/deploymentrender_default.go index b5fa043b3b0..7e13760ce2f 100644 --- a/pkg/yurtmanager/webhook/deploymentrender/v1alpha1/deploymentrender_default.go +++ b/pkg/yurtmanager/webhook/deploymentrender/v1alpha1/deploymentrender_default.go @@ -73,6 +73,9 @@ func (webhook *DeploymentRenderHandler) Default(ctx context.Context, obj runtime var instance client.Object switch app.Kind { case "YurtAppSet": + if app.APIVersion != v1alpha1.SchemeGroupVersion.String() { + return nil + } instance = &v1alpha1.YurtAppSet{} case "YurtAppDaemon": instance = &v1alpha1.YurtAppDaemon{} diff --git a/pkg/yurtmanager/webhook/deploymentrender/v1alpha1/deploymentrender_webhook_test.go b/pkg/yurtmanager/webhook/deploymentrender/v1alpha1/deploymentrender_webhook_test.go index 003e1794a0e..3bc3d7fffe7 100644 --- a/pkg/yurtmanager/webhook/deploymentrender/v1alpha1/deploymentrender_webhook_test.go +++ b/pkg/yurtmanager/webhook/deploymentrender/v1alpha1/deploymentrender_webhook_test.go @@ -118,6 +118,45 @@ var defaultAppDaemon = &v1alpha1.YurtAppDaemon{ }, } +var deploymentByYasv1beta1 = &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test1", + Namespace: "default", + OwnerReferences: []metav1.OwnerReference{{ + APIVersion: "apps.openyurt.io/v1beta1", + Kind: "YurtAppSet", + Name: "yurtappset-patch", + }}, + Labels: map[string]string{ + "apps.openyurt.io/pool-name": "nodepool-test", + }, + }, + Status: appsv1.DeploymentStatus{}, + Spec: appsv1.DeploymentSpec{ + Replicas: &replica, + Selector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "app": "test", + }, + }, + Template: corev1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{ + "app": "test", + }, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "nginx", + Image: "nginx", + }, + }, + }, + }, + }, +} + var defaultDeployment = &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: "test1", @@ -328,6 +367,9 @@ func TestDeploymentRenderHandler_Default(t *testing.T) { if err := webhook.Default(context.TODO(), daemonDeployment); err != nil { t.Fatal(err) } + if err := webhook.Default(context.TODO(), deploymentByYasv1beta1); err != nil { + t.Fatal(err) + } }) } }