From 60bd9851b111426a01bf8920b5be1ecf8d0dfe44 Mon Sep 17 00:00:00 2001 From: junqian Date: Wed, 3 Feb 2021 16:48:37 +0800 Subject: [PATCH] fix bug for image with more than one tags --- pkg/tapp/controller.go | 5 +++++ pkg/tapp/controller_test.go | 43 ++++++++++++++++++++++++++++++++----- pkg/tapp/instance.go | 13 +++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/pkg/tapp/controller.go b/pkg/tapp/controller.go index 27ae367..8270e77 100644 --- a/pkg/tapp/controller.go +++ b/pkg/tapp/controller.go @@ -832,6 +832,7 @@ func (c *Controller) transformPodActions(tapp *tappv1.TApp, podActions map[strin break } if instance, err := newInstance(tapp, p); err == nil { + setInPlaceUpdateAnnotation(instance) update = append(update, instance) availablePods.Delete(p) } @@ -884,6 +885,7 @@ func (c *Controller) transformPodActions(tapp *tappv1.TApp, podActions map[strin switch action { case updatePod: if instance, err := newInstance(tapp, p); err == nil { + setInPlaceUpdateAnnotation(instance) update = append(update, instance) availablePods.Delete(p) } @@ -1241,6 +1243,9 @@ func setInPlaceUpdateCondition(kubeclient kubernetes.Interface, pod *corev1.Pod, // isUpdating returns true if kubelet is updating image for pod, otherwise returns false func isUpdating(pod *corev1.Pod) bool { + if stateStr, ok := pod.Annotations[InPlaceUpdateStateKey]; !ok || stateStr != InPlaceUpdateStateValue { + return false + } isSameImage := func(expected, real string) bool { return expected == real || "docker.io/"+expected == real || "docker.io/"+expected+":latest" == real || expected+":latest" == real diff --git a/pkg/tapp/controller_test.go b/pkg/tapp/controller_test.go index 775761c..f003c55 100644 --- a/pkg/tapp/controller_test.go +++ b/pkg/tapp/controller_test.go @@ -346,7 +346,7 @@ func TestIsUpdatingPods(t *testing.T) { { Name: "containerB", Image: "2048", - ImageID: "123456", + ImageID: "12345", }, }, }, @@ -407,14 +407,15 @@ func TestIsUpdatingPods(t *testing.T) { { Name: "containerB", Image: "2048", - ImageID: "123456", + ImageID: "12345", }, }, }, } pod3 := &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ - Labels: map[string]string{tappv1.TAppInstanceKey: "3"}, + Labels: map[string]string{tappv1.TAppInstanceKey: "3"}, + Annotations: map[string]string{InPlaceUpdateStateKey: InPlaceUpdateStateValue}, }, Spec: corev1.PodSpec{ Containers: []corev1.Container{ @@ -433,8 +434,40 @@ func TestIsUpdatingPods(t *testing.T) { { Name: "containerA", Image: "docker.io/nginx:1.7.9", + ImageID: "1234567", + }, + { + Name: "containerB", + Image: "2048:latest", ImageID: "123456", }, + }, + }, + } + pod4 := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{tappv1.TAppInstanceKey: "4"}, + Annotations: map[string]string{InPlaceUpdateStateKey: InPlaceUpdateStateValue}, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "containerA", + Image: "nginx:1.7.9", + }, + { + Name: "containerB", + Image: "2048", + }, + }, + }, + Status: corev1.PodStatus{ + ContainerStatuses: []corev1.ContainerStatus{ + { + Name: "containerA", + Image: "nginx", + ImageID: "1234567", + }, { Name: "containerB", Image: "2048:latest", @@ -443,8 +476,8 @@ func TestIsUpdatingPods(t *testing.T) { }, }, } - pods := []*corev1.Pod{pod0, pod1, pod2, pod3} - expectedUpdating := map[string]bool{"1": true, "2": true} + pods := []*corev1.Pod{pod0, pod1, pod2, pod3, pod4} + expectedUpdating := map[string]bool{"4": true} updating := getUpdatingPods(pods) if !reflect.DeepEqual(expectedUpdating, updating) { t.Errorf("Failed to getUpdatingPods, expected: %+v, got: %+v", expectedUpdating, updating) diff --git a/pkg/tapp/instance.go b/pkg/tapp/instance.go index e3273d1..bec2f35 100644 --- a/pkg/tapp/instance.go +++ b/pkg/tapp/instance.go @@ -42,6 +42,12 @@ const ( // updateRetries is the number of Get/Update cycles we perform when an // update fails. updateRetries = 3 + + // InPlaceUpdateStateKey records whether instance is in inPlace-updating. + InPlaceUpdateStateKey string = "tkestack.io/inplace-update-state" + + // InPlaceUpdateStateValue records the value of InPlaceUpdateStateKey in pod annotations . + InPlaceUpdateStateValue string = "true" ) // instance is the control block used to transmit all updates about a single instance. @@ -103,6 +109,13 @@ func newInstance(tapp *tappv1.TApp, id string) (*Instance, error) { return ins, nil } +func setInPlaceUpdateAnnotation(ins *Instance) { + if ins.pod.Annotations == nil { + ins.pod.Annotations = map[string]string{} + } + ins.pod.Annotations[InPlaceUpdateStateKey] = InPlaceUpdateStateValue +} + func getControllerRef(tapp *tappv1.TApp) *metav1.OwnerReference { trueVar := true return &metav1.OwnerReference{