From f55079b0c1197c85ab82822ee0a3e911f1c370fd Mon Sep 17 00:00:00 2001 From: Seth Jennings Date: Wed, 24 May 2017 15:18:44 -0500 Subject: [PATCH] UPSTREAM: 46305: clear init container status annotations when cleared in status --- .../k8s.io/kubernetes/pkg/api/v1/pod/util.go | 3 ++ .../kubernetes/pkg/api/v1/pod/util_test.go | 50 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/vendor/k8s.io/kubernetes/pkg/api/v1/pod/util.go b/vendor/k8s.io/kubernetes/pkg/api/v1/pod/util.go index 040071370a44..7b25b623baf8 100644 --- a/vendor/k8s.io/kubernetes/pkg/api/v1/pod/util.go +++ b/vendor/k8s.io/kubernetes/pkg/api/v1/pod/util.go @@ -115,6 +115,9 @@ func SetInitContainersStatusesAnnotations(pod *v1.Pod) error { } pod.Annotations[v1.PodInitContainerStatusesAnnotationKey] = string(value) pod.Annotations[v1.PodInitContainerStatusesBetaAnnotationKey] = string(value) + } else { + delete(pod.Annotations, v1.PodInitContainerStatusesAnnotationKey) + delete(pod.Annotations, v1.PodInitContainerStatusesBetaAnnotationKey) } return nil } diff --git a/vendor/k8s.io/kubernetes/pkg/api/v1/pod/util_test.go b/vendor/k8s.io/kubernetes/pkg/api/v1/pod/util_test.go index 4e04f834510e..a5b80f5c6f08 100644 --- a/vendor/k8s.io/kubernetes/pkg/api/v1/pod/util_test.go +++ b/vendor/k8s.io/kubernetes/pkg/api/v1/pod/util_test.go @@ -17,6 +17,7 @@ limitations under the License. package pod import ( + "encoding/json" "reflect" "strings" "testing" @@ -342,3 +343,52 @@ func collectSecretPaths(t *testing.T, path *field.Path, name string, tp reflect. return secretPaths } + +func TestSetInitContainersStatusesAnnotations(t *testing.T) { + testStatuses := []v1.ContainerStatus{ + { + Name: "test", + }, + } + value, _ := json.Marshal(testStatuses) + testAnnotation := string(value) + tests := []struct { + name string + pod *v1.Pod + annotations map[string]string + }{ + { + name: "Populate annotations from status", + pod: &v1.Pod{ + Status: v1.PodStatus{ + InitContainerStatuses: testStatuses, + }, + }, + annotations: map[string]string{ + v1.PodInitContainerStatusesAnnotationKey: testAnnotation, + v1.PodInitContainerStatusesBetaAnnotationKey: testAnnotation, + }, + }, + { + name: "Clear annotations if no status", + pod: &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + v1.PodInitContainerStatusesAnnotationKey: testAnnotation, + v1.PodInitContainerStatusesBetaAnnotationKey: testAnnotation, + }, + }, + Status: v1.PodStatus{ + InitContainerStatuses: []v1.ContainerStatus{}, + }, + }, + annotations: map[string]string{}, + }, + } + for _, test := range tests { + SetInitContainersStatusesAnnotations(test.pod) + if !reflect.DeepEqual(test.pod.Annotations, test.annotations) { + t.Errorf("%v, actual = %v, expected = %v", test.name, test.pod.Annotations, test.annotations) + } + } +}