Skip to content

Commit

Permalink
fix bug for image with more than one tags
Browse files Browse the repository at this point in the history
  • Loading branch information
qianjun1993 committed Feb 3, 2021
1 parent 9ccd7a7 commit 0101ff7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
7 changes: 7 additions & 0 deletions pkg/tapp/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -1241,6 +1243,11 @@ 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 {
if stateStr != "true" {
return false
}
}
isSameImage := func(expected, real string) bool {
return expected == real || "docker.io/"+expected == real ||
"docker.io/"+expected+":latest" == real || expected+":latest" == real
Expand Down
43 changes: 38 additions & 5 deletions pkg/tapp/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func TestIsUpdatingPods(t *testing.T) {
{
Name: "containerB",
Image: "2048",
ImageID: "123456",
ImageID: "12345",
},
},
},
Expand Down Expand Up @@ -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: "true"},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
Expand All @@ -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: "true"},
},
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",
Expand All @@ -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{"1": true, "4": true}
updating := getUpdatingPods(pods)
if !reflect.DeepEqual(expectedUpdating, updating) {
t.Errorf("Failed to getUpdatingPods, expected: %+v, got: %+v", expectedUpdating, updating)
Expand Down
10 changes: 10 additions & 0 deletions pkg/tapp/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ 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"
)

// instance is the control block used to transmit all updates about a single instance.
Expand Down Expand Up @@ -103,6 +106,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] = "true"
}

func getControllerRef(tapp *tappv1.TApp) *metav1.OwnerReference {
trueVar := true
return &metav1.OwnerReference{
Expand Down

0 comments on commit 0101ff7

Please # to comment.