From 325d0e4d7f6d503120310ce02c60e19a47a82ce4 Mon Sep 17 00:00:00 2001 From: free6om Date: Mon, 15 Apr 2024 13:02:32 +0800 Subject: [PATCH] fix: restart not working (#7049) --- pkg/controller/rsm2/in_place_update_util.go | 11 ++++- .../rsm2/in_place_update_util_test.go | 43 ------------------ pkg/controller/rsm2/instance_util_test.go | 27 ++++++++++++ pkg/controller/rsm2/suite_test.go | 44 +++++++++++++++++++ 4 files changed, 81 insertions(+), 44 deletions(-) diff --git a/pkg/controller/rsm2/in_place_update_util.go b/pkg/controller/rsm2/in_place_update_util.go index 3de831b42c4..6eaf867eb64 100644 --- a/pkg/controller/rsm2/in_place_update_util.go +++ b/pkg/controller/rsm2/in_place_update_util.go @@ -32,6 +32,7 @@ import ( "k8s.io/kubernetes/pkg/features" workloads "github.com/apecloud/kubeblocks/apis/workloads/v1alpha1" + "github.com/apecloud/kubeblocks/pkg/constant" "github.com/apecloud/kubeblocks/pkg/dataprotection/utils" viper "github.com/apecloud/kubeblocks/pkg/viperx" ) @@ -60,7 +61,15 @@ func supportPodVerticalScaling() bool { func filterInPlaceFields(src *corev1.PodTemplateSpec) *corev1.PodTemplateSpec { template := src.DeepCopy() // filter annotations - template.Annotations = nil + // keep Restart annotation + var annotations map[string]string + if len(template.Annotations) > 0 { + if restart, ok := template.Annotations[constant.RestartAnnotationKey]; ok { + annotations = make(map[string]string, 1) + annotations[constant.RestartAnnotationKey] = restart + } + } + template.Annotations = annotations // filter labels template.Labels = nil // filter spec.containers[*].images & spec.initContainers[*].images diff --git a/pkg/controller/rsm2/in_place_update_util_test.go b/pkg/controller/rsm2/in_place_update_util_test.go index fff9bc99827..7e43053b4cc 100644 --- a/pkg/controller/rsm2/in_place_update_util_test.go +++ b/pkg/controller/rsm2/in_place_update_util_test.go @@ -192,46 +192,3 @@ var _ = Describe("instance util test", func() { }) }) }) - -func buildRandomPod() *corev1.Pod { - randStr := rand.String(8) - deadline := rand.Int63nRange(0, 1024*1024) - randInt1 := rand.Int() - randInt2 := rand.Int() - return builder.NewPodBuilder(namespace, name). - AddLabels(randStr, randStr). - AddAnnotations(randStr, randStr). - SetActiveDeadlineSeconds(&deadline). - AddTolerations(corev1.Toleration{ - Key: randStr, - Operator: corev1.TolerationOpEqual, - Value: randStr, - }). - AddInitContainer(corev1.Container{ - Name: "init-container", - Image: randStr, - Resources: corev1.ResourceRequirements{ - Requests: map[corev1.ResourceName]resource.Quantity{ - corev1.ResourceCPU: resource.MustParse(fmt.Sprintf("%dm", randInt1)), - corev1.ResourceMemory: resource.MustParse(fmt.Sprintf("%dm", randInt2)), - }, - Limits: map[corev1.ResourceName]resource.Quantity{ - corev1.ResourceCPU: resource.MustParse(fmt.Sprintf("%dm", randInt1)), - corev1.ResourceMemory: resource.MustParse(fmt.Sprintf("%dm", randInt2)), - }, - }}). - AddContainer(corev1.Container{ - Name: "container", - Image: randStr, - Resources: corev1.ResourceRequirements{ - Requests: map[corev1.ResourceName]resource.Quantity{ - corev1.ResourceCPU: resource.MustParse(fmt.Sprintf("%dm", randInt1)), - corev1.ResourceMemory: resource.MustParse(fmt.Sprintf("%dm", randInt2)), - }, - Limits: map[corev1.ResourceName]resource.Quantity{ - corev1.ResourceCPU: resource.MustParse(fmt.Sprintf("%dm", randInt1)), - corev1.ResourceMemory: resource.MustParse(fmt.Sprintf("%dm", randInt2)), - }, - }}). - GetObject() -} diff --git a/pkg/controller/rsm2/instance_util_test.go b/pkg/controller/rsm2/instance_util_test.go index 4e82c9e23a2..7f76672ab17 100644 --- a/pkg/controller/rsm2/instance_util_test.go +++ b/pkg/controller/rsm2/instance_util_test.go @@ -21,6 +21,7 @@ package rsm2 import ( "fmt" + "time" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -28,6 +29,7 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" workloads "github.com/apecloud/kubeblocks/apis/workloads/v1alpha1" @@ -410,4 +412,29 @@ var _ = Describe("instance util test", func() { Expect(instanceNameList).Should(Equal(podNamesExpected)) }) }) + + Context("filterInPlaceFields", func() { + It("should work well", func() { + pod := buildRandomPod() + restartTime := (metav1.Time{Time: time.Now()}).Format(time.RFC3339) + pod.Annotations[constant.RestartAnnotationKey] = restartTime + podTemplateSpec := &corev1.PodTemplateSpec{ + ObjectMeta: pod.ObjectMeta, + Spec: pod.Spec, + } + result := filterInPlaceFields(podTemplateSpec) + Expect(result).ShouldNot(BeNil()) + Expect(result.Annotations).Should(HaveKey(constant.RestartAnnotationKey)) + Expect(result.Annotations[constant.RestartAnnotationKey]).Should(Equal(restartTime)) + Expect(result.Labels).Should(BeNil()) + Expect(result.Spec.ActiveDeadlineSeconds).Should(BeNil()) + Expect(result.Spec.Tolerations).Should(BeNil()) + for _, container := range result.Spec.InitContainers { + Expect(container.Image).Should(BeEmpty()) + } + for _, container := range result.Spec.Containers { + Expect(container.Image).Should(BeEmpty()) + } + }) + }) }) diff --git a/pkg/controller/rsm2/suite_test.go b/pkg/controller/rsm2/suite_test.go index 830179674bf..7267c8fda36 100644 --- a/pkg/controller/rsm2/suite_test.go +++ b/pkg/controller/rsm2/suite_test.go @@ -31,6 +31,7 @@ import ( "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/rand" workloads "github.com/apecloud/kubeblocks/apis/workloads/v1alpha1" "github.com/apecloud/kubeblocks/pkg/constant" @@ -151,6 +152,49 @@ func mockCompressedInstanceTemplates(ns, name string) (*corev1.ConfigMap, string return templateObj, string(templateRefByte), nil } +func buildRandomPod() *corev1.Pod { + randStr := rand.String(8) + deadline := rand.Int63nRange(0, 1024*1024) + randInt1 := rand.Int() + randInt2 := rand.Int() + return builder.NewPodBuilder(namespace, name). + AddLabels(randStr, randStr). + AddAnnotations(randStr, randStr). + SetActiveDeadlineSeconds(&deadline). + AddTolerations(corev1.Toleration{ + Key: randStr, + Operator: corev1.TolerationOpEqual, + Value: randStr, + }). + AddInitContainer(corev1.Container{ + Name: "init-container", + Image: randStr, + Resources: corev1.ResourceRequirements{ + Requests: map[corev1.ResourceName]resource.Quantity{ + corev1.ResourceCPU: resource.MustParse(fmt.Sprintf("%dm", randInt1)), + corev1.ResourceMemory: resource.MustParse(fmt.Sprintf("%dm", randInt2)), + }, + Limits: map[corev1.ResourceName]resource.Quantity{ + corev1.ResourceCPU: resource.MustParse(fmt.Sprintf("%dm", randInt1)), + corev1.ResourceMemory: resource.MustParse(fmt.Sprintf("%dm", randInt2)), + }, + }}). + AddContainer(corev1.Container{ + Name: "container", + Image: randStr, + Resources: corev1.ResourceRequirements{ + Requests: map[corev1.ResourceName]resource.Quantity{ + corev1.ResourceCPU: resource.MustParse(fmt.Sprintf("%dm", randInt1)), + corev1.ResourceMemory: resource.MustParse(fmt.Sprintf("%dm", randInt2)), + }, + Limits: map[corev1.ResourceName]resource.Quantity{ + corev1.ResourceCPU: resource.MustParse(fmt.Sprintf("%dm", randInt1)), + corev1.ResourceMemory: resource.MustParse(fmt.Sprintf("%dm", randInt2)), + }, + }}). + GetObject() +} + func TestAPIs(t *testing.T) { RegisterFailHandler(Fail)