Skip to content

Commit

Permalink
fix: restart not working (#7049)
Browse files Browse the repository at this point in the history
  • Loading branch information
free6om authored Apr 15, 2024
1 parent 6d47d89 commit 325d0e4
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 44 deletions.
11 changes: 10 additions & 1 deletion pkg/controller/rsm2/in_place_update_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
43 changes: 0 additions & 43 deletions pkg/controller/rsm2/in_place_update_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
27 changes: 27 additions & 0 deletions pkg/controller/rsm2/instance_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ package rsm2

import (
"fmt"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

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"
Expand Down Expand Up @@ -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())
}
})
})
})
44 changes: 44 additions & 0 deletions pkg/controller/rsm2/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 325d0e4

Please sign in to comment.