diff --git a/staging/src/k8s.io/api/core/v1/annotation_key_constants.go b/staging/src/k8s.io/api/core/v1/annotation_key_constants.go index e59e7bc2649..d3ebf862836 100644 --- a/staging/src/k8s.io/api/core/v1/annotation_key_constants.go +++ b/staging/src/k8s.io/api/core/v1/annotation_key_constants.go @@ -95,10 +95,6 @@ const ( // configuration of a resource for use in a three way diff by UpdateApplyAnnotation. LastAppliedConfigAnnotation = kubectlPrefix + "last-applied-configuration" - // DefaultContainerAnnotationName is an annotation name that can be used to preselect the interesting container - // from a pod when running kubectl. - DefaultContainerAnnotationName = kubectlPrefix + "default-container" - // AnnotationLoadBalancerSourceRangesKey is the key of the annotation on a service to set allowed ingress ranges on their LoadBalancers // // It should be a comma-separated list of CIDRs, e.g. `0.0.0.0/0` to diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/util/helpers.go b/staging/src/k8s.io/kubectl/pkg/cmd/util/helpers.go index 8e9340b4bd0..b7d056a0104 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/util/helpers.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/util/helpers.go @@ -753,8 +753,8 @@ func GetDefaultContainerName(pod *corev1.Pod, enableSuggestedCmdUsage bool, w io if len(pod.Spec.Containers) > 1 { // in case the "kubectl.kubernetes.io/default-container" annotation is present, we preset the opts.Containers to default to selected // container. This gives users ability to preselect the most interesting container in pod. - if annotations := pod.Annotations; annotations != nil && len(annotations[corev1.DefaultContainerAnnotationName]) > 0 { - containerName := annotations[corev1.DefaultContainerAnnotationName] + if annotations := pod.Annotations; annotations != nil && len(annotations[podutils.DefaultContainerAnnotationName]) > 0 { + containerName := annotations[podutils.DefaultContainerAnnotationName] if exists, _ := podutils.FindContainerByName(pod, containerName); exists != nil { return containerName } else { diff --git a/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go b/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go index d9f76af65b7..3fff406909c 100644 --- a/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go +++ b/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go @@ -79,9 +79,9 @@ func logsForObjectWithClient(clientset corev1client.CoreV1Interface, object, opt var containerName string if len(annotations[defaultLogsContainerAnnotationName]) > 0 { containerName = annotations[defaultLogsContainerAnnotationName] - fmt.Fprintf(os.Stderr, "Found deprecated `kubectl.kubernetes.io/default-logs-container` annotation %v in pod/%v\n", containerName, t.Name) - } else if len(annotations[corev1.DefaultContainerAnnotationName]) > 0 { - containerName = annotations[corev1.DefaultContainerAnnotationName] + fmt.Fprintf(os.Stderr, "Using deprecated annotation `kubectl.kubernetes.io/default-logs-container` in pod/%v. Please use `kubectl.kubernetes.io/default-container` instead\n", t.Name) + } else if len(annotations[podutils.DefaultContainerAnnotationName]) > 0 { + containerName = annotations[podutils.DefaultContainerAnnotationName] } if len(containerName) > 0 { if exists, _ := podutils.FindContainerByName(t, containerName); exists != nil { diff --git a/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject_test.go b/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject_test.go index 23a239e3eb0..60c340f73ca 100644 --- a/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject_test.go +++ b/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject_test.go @@ -32,6 +32,7 @@ import ( "k8s.io/apimachinery/pkg/util/diff" fakeexternal "k8s.io/client-go/kubernetes/fake" testclient "k8s.io/client-go/testing" + "k8s.io/kubectl/pkg/util/podutils" ) var ( @@ -429,7 +430,7 @@ func TestLogsForObjectWithClient(t *testing.T) { name: "two container pod with default container selected", podFn: func() *corev1.Pod { pod := testPodWithTwoContainers() - pod.Annotations = map[string]string{corev1.DefaultContainerAnnotationName: "foo-2-c1"} + pod.Annotations = map[string]string{podutils.DefaultContainerAnnotationName: "foo-2-c1"} return pod }, podLogOptions: &corev1.PodLogOptions{}, @@ -439,7 +440,7 @@ func TestLogsForObjectWithClient(t *testing.T) { name: "two container pod with default container selected but also container set explicitly", podFn: func() *corev1.Pod { pod := testPodWithTwoContainers() - pod.Annotations = map[string]string{corev1.DefaultContainerAnnotationName: "foo-2-c1"} + pod.Annotations = map[string]string{podutils.DefaultContainerAnnotationName: "foo-2-c1"} return pod }, podLogOptions: &corev1.PodLogOptions{ @@ -451,7 +452,7 @@ func TestLogsForObjectWithClient(t *testing.T) { name: "two container pod with non-existing default container selected", podFn: func() *corev1.Pod { pod := testPodWithTwoContainers() - pod.Annotations = map[string]string{corev1.DefaultContainerAnnotationName: "non-existing"} + pod.Annotations = map[string]string{podutils.DefaultContainerAnnotationName: "non-existing"} return pod }, podLogOptions: &corev1.PodLogOptions{}, @@ -461,7 +462,7 @@ func TestLogsForObjectWithClient(t *testing.T) { name: "two container pod with default container set, but allContainers also set", podFn: func() *corev1.Pod { pod := testPodWithTwoContainers() - pod.Annotations = map[string]string{corev1.DefaultContainerAnnotationName: "foo-2-c1"} + pod.Annotations = map[string]string{podutils.DefaultContainerAnnotationName: "foo-2-c1"} return pod }, allContainers: true, diff --git a/staging/src/k8s.io/kubectl/pkg/util/podutils/podutils.go b/staging/src/k8s.io/kubectl/pkg/util/podutils/podutils.go index be8c50b05d0..580f3706e49 100644 --- a/staging/src/k8s.io/kubectl/pkg/util/podutils/podutils.go +++ b/staging/src/k8s.io/kubectl/pkg/util/podutils/podutils.go @@ -25,6 +25,10 @@ import ( "k8s.io/utils/integer" ) +// DefaultContainerAnnotationName is an annotation name that can be used to preselect the interesting container +// from a pod when running kubectl. +const DefaultContainerAnnotationName = "kubectl.kubernetes.io/default-container" + // IsPodAvailable returns true if a pod is available; false otherwise. // Precondition for an available pod is that it must be ready. On top // of that, there are two cases when a pod can be considered available: