Skip to content

Commit 3436287

Browse files
authored
Merge pull request #71 from qianjun1993/inplaceUpdate-2
fix bug for image with more than one tags when create Tapp
2 parents 6527f71 + 60bd985 commit 3436287

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

pkg/tapp/controller.go

+5
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ func (c *Controller) transformPodActions(tapp *tappv1.TApp, podActions map[strin
832832
break
833833
}
834834
if instance, err := newInstance(tapp, p); err == nil {
835+
setInPlaceUpdateAnnotation(instance)
835836
update = append(update, instance)
836837
availablePods.Delete(p)
837838
}
@@ -884,6 +885,7 @@ func (c *Controller) transformPodActions(tapp *tappv1.TApp, podActions map[strin
884885
switch action {
885886
case updatePod:
886887
if instance, err := newInstance(tapp, p); err == nil {
888+
setInPlaceUpdateAnnotation(instance)
887889
update = append(update, instance)
888890
availablePods.Delete(p)
889891
}
@@ -1241,6 +1243,9 @@ func setInPlaceUpdateCondition(kubeclient kubernetes.Interface, pod *corev1.Pod,
12411243

12421244
// isUpdating returns true if kubelet is updating image for pod, otherwise returns false
12431245
func isUpdating(pod *corev1.Pod) bool {
1246+
if stateStr, ok := pod.Annotations[InPlaceUpdateStateKey]; !ok || stateStr != InPlaceUpdateStateValue {
1247+
return false
1248+
}
12441249
isSameImage := func(expected, real string) bool {
12451250
return expected == real || "docker.io/"+expected == real ||
12461251
"docker.io/"+expected+":latest" == real || expected+":latest" == real

pkg/tapp/controller_test.go

+38-5
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func TestIsUpdatingPods(t *testing.T) {
346346
{
347347
Name: "containerB",
348348
Image: "2048",
349-
ImageID: "123456",
349+
ImageID: "12345",
350350
},
351351
},
352352
},
@@ -407,14 +407,15 @@ func TestIsUpdatingPods(t *testing.T) {
407407
{
408408
Name: "containerB",
409409
Image: "2048",
410-
ImageID: "123456",
410+
ImageID: "12345",
411411
},
412412
},
413413
},
414414
}
415415
pod3 := &corev1.Pod{
416416
ObjectMeta: metav1.ObjectMeta{
417-
Labels: map[string]string{tappv1.TAppInstanceKey: "3"},
417+
Labels: map[string]string{tappv1.TAppInstanceKey: "3"},
418+
Annotations: map[string]string{InPlaceUpdateStateKey: InPlaceUpdateStateValue},
418419
},
419420
Spec: corev1.PodSpec{
420421
Containers: []corev1.Container{
@@ -433,8 +434,40 @@ func TestIsUpdatingPods(t *testing.T) {
433434
{
434435
Name: "containerA",
435436
Image: "docker.io/nginx:1.7.9",
437+
ImageID: "1234567",
438+
},
439+
{
440+
Name: "containerB",
441+
Image: "2048:latest",
436442
ImageID: "123456",
437443
},
444+
},
445+
},
446+
}
447+
pod4 := &corev1.Pod{
448+
ObjectMeta: metav1.ObjectMeta{
449+
Labels: map[string]string{tappv1.TAppInstanceKey: "4"},
450+
Annotations: map[string]string{InPlaceUpdateStateKey: InPlaceUpdateStateValue},
451+
},
452+
Spec: corev1.PodSpec{
453+
Containers: []corev1.Container{
454+
{
455+
Name: "containerA",
456+
Image: "nginx:1.7.9",
457+
},
458+
{
459+
Name: "containerB",
460+
Image: "2048",
461+
},
462+
},
463+
},
464+
Status: corev1.PodStatus{
465+
ContainerStatuses: []corev1.ContainerStatus{
466+
{
467+
Name: "containerA",
468+
Image: "nginx",
469+
ImageID: "1234567",
470+
},
438471
{
439472
Name: "containerB",
440473
Image: "2048:latest",
@@ -443,8 +476,8 @@ func TestIsUpdatingPods(t *testing.T) {
443476
},
444477
},
445478
}
446-
pods := []*corev1.Pod{pod0, pod1, pod2, pod3}
447-
expectedUpdating := map[string]bool{"1": true, "2": true}
479+
pods := []*corev1.Pod{pod0, pod1, pod2, pod3, pod4}
480+
expectedUpdating := map[string]bool{"4": true}
448481
updating := getUpdatingPods(pods)
449482
if !reflect.DeepEqual(expectedUpdating, updating) {
450483
t.Errorf("Failed to getUpdatingPods, expected: %+v, got: %+v", expectedUpdating, updating)

pkg/tapp/instance.go

+13
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ const (
4242
// updateRetries is the number of Get/Update cycles we perform when an
4343
// update fails.
4444
updateRetries = 3
45+
46+
// InPlaceUpdateStateKey records whether instance is in inPlace-updating.
47+
InPlaceUpdateStateKey string = "tkestack.io/inplace-update-state"
48+
49+
// InPlaceUpdateStateValue records the value of InPlaceUpdateStateKey in pod annotations .
50+
InPlaceUpdateStateValue string = "true"
4551
)
4652

4753
// 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) {
103109
return ins, nil
104110
}
105111

112+
func setInPlaceUpdateAnnotation(ins *Instance) {
113+
if ins.pod.Annotations == nil {
114+
ins.pod.Annotations = map[string]string{}
115+
}
116+
ins.pod.Annotations[InPlaceUpdateStateKey] = InPlaceUpdateStateValue
117+
}
118+
106119
func getControllerRef(tapp *tappv1.TApp) *metav1.OwnerReference {
107120
trueVar := true
108121
return &metav1.OwnerReference{

0 commit comments

Comments
 (0)