move default container annotation to kubectl pkg

Kubernetes-commit: 27bd94e54d2f8a7676411b280ab9d0ac7f9e921b
This commit is contained in:
pacoxu 2021-02-26 10:11:12 +08:00 committed by Kubernetes Publisher
parent 360e8d2b15
commit 8d367eb060
4 changed files with 14 additions and 9 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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,

View File

@ -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: