Skip to content

Commit

Permalink
Merge pull request #71 from qianjun1993/inplaceUpdate-2
Browse files Browse the repository at this point in the history
fix bug for image with more than one tags when create Tapp
  • Loading branch information
hex108 authored Feb 23, 2021
2 parents 6527f71 + 60bd985 commit 3436287
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
5 changes: 5 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,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
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: InPlaceUpdateStateValue},
},
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: 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",
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{"4": true}
updating := getUpdatingPods(pods)
if !reflect.DeepEqual(expectedUpdating, updating) {
t.Errorf("Failed to getUpdatingPods, expected: %+v, got: %+v", expectedUpdating, updating)
Expand Down
13 changes: 13 additions & 0 deletions pkg/tapp/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 3436287

Please sign in to comment.