move default container annotation to kubectl pkg

This commit is contained in:
pacoxu 2021-02-26 10:11:12 +08:00
parent b54e823dbc
commit 27bd94e54d
5 changed files with 14 additions and 13 deletions

View File

@ -95,10 +95,6 @@ const (
// configuration of a resource for use in a three way diff by UpdateApplyAnnotation. // configuration of a resource for use in a three way diff by UpdateApplyAnnotation.
LastAppliedConfigAnnotation = kubectlPrefix + "last-applied-configuration" 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 // 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 // It should be a comma-separated list of CIDRs, e.g. `0.0.0.0/0` to

View File

@ -753,8 +753,8 @@ func GetDefaultContainerName(pod *corev1.Pod, enableSuggestedCmdUsage bool, w io
if len(pod.Spec.Containers) > 1 { 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 // 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. // container. This gives users ability to preselect the most interesting container in pod.
if annotations := pod.Annotations; annotations != nil && len(annotations[corev1.DefaultContainerAnnotationName]) > 0 { if annotations := pod.Annotations; annotations != nil && len(annotations[podutils.DefaultContainerAnnotationName]) > 0 {
containerName := annotations[corev1.DefaultContainerAnnotationName] containerName := annotations[podutils.DefaultContainerAnnotationName]
if exists, _ := podutils.FindContainerByName(pod, containerName); exists != nil { if exists, _ := podutils.FindContainerByName(pod, containerName); exists != nil {
return containerName return containerName
} else { } else {

View File

@ -79,9 +79,9 @@ func logsForObjectWithClient(clientset corev1client.CoreV1Interface, object, opt
var containerName string var containerName string
if len(annotations[defaultLogsContainerAnnotationName]) > 0 { if len(annotations[defaultLogsContainerAnnotationName]) > 0 {
containerName = annotations[defaultLogsContainerAnnotationName] containerName = annotations[defaultLogsContainerAnnotationName]
fmt.Fprintf(os.Stderr, "Found deprecated `kubectl.kubernetes.io/default-logs-container` annotation %v in pod/%v\n", containerName, t.Name) 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[corev1.DefaultContainerAnnotationName]) > 0 { } else if len(annotations[podutils.DefaultContainerAnnotationName]) > 0 {
containerName = annotations[corev1.DefaultContainerAnnotationName] containerName = annotations[podutils.DefaultContainerAnnotationName]
} }
if len(containerName) > 0 { if len(containerName) > 0 {
if exists, _ := podutils.FindContainerByName(t, containerName); exists != nil { if exists, _ := podutils.FindContainerByName(t, containerName); exists != nil {

View File

@ -32,6 +32,7 @@ import (
"k8s.io/apimachinery/pkg/util/diff" "k8s.io/apimachinery/pkg/util/diff"
fakeexternal "k8s.io/client-go/kubernetes/fake" fakeexternal "k8s.io/client-go/kubernetes/fake"
testclient "k8s.io/client-go/testing" testclient "k8s.io/client-go/testing"
"k8s.io/kubectl/pkg/util/podutils"
) )
var ( var (
@ -429,7 +430,7 @@ func TestLogsForObjectWithClient(t *testing.T) {
name: "two container pod with default container selected", name: "two container pod with default container selected",
podFn: func() *corev1.Pod { podFn: func() *corev1.Pod {
pod := testPodWithTwoContainers() pod := testPodWithTwoContainers()
pod.Annotations = map[string]string{corev1.DefaultContainerAnnotationName: "foo-2-c1"} pod.Annotations = map[string]string{podutils.DefaultContainerAnnotationName: "foo-2-c1"}
return pod return pod
}, },
podLogOptions: &corev1.PodLogOptions{}, 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", name: "two container pod with default container selected but also container set explicitly",
podFn: func() *corev1.Pod { podFn: func() *corev1.Pod {
pod := testPodWithTwoContainers() pod := testPodWithTwoContainers()
pod.Annotations = map[string]string{corev1.DefaultContainerAnnotationName: "foo-2-c1"} pod.Annotations = map[string]string{podutils.DefaultContainerAnnotationName: "foo-2-c1"}
return pod return pod
}, },
podLogOptions: &corev1.PodLogOptions{ podLogOptions: &corev1.PodLogOptions{
@ -451,7 +452,7 @@ func TestLogsForObjectWithClient(t *testing.T) {
name: "two container pod with non-existing default container selected", name: "two container pod with non-existing default container selected",
podFn: func() *corev1.Pod { podFn: func() *corev1.Pod {
pod := testPodWithTwoContainers() pod := testPodWithTwoContainers()
pod.Annotations = map[string]string{corev1.DefaultContainerAnnotationName: "non-existing"} pod.Annotations = map[string]string{podutils.DefaultContainerAnnotationName: "non-existing"}
return pod return pod
}, },
podLogOptions: &corev1.PodLogOptions{}, podLogOptions: &corev1.PodLogOptions{},
@ -461,7 +462,7 @@ func TestLogsForObjectWithClient(t *testing.T) {
name: "two container pod with default container set, but allContainers also set", name: "two container pod with default container set, but allContainers also set",
podFn: func() *corev1.Pod { podFn: func() *corev1.Pod {
pod := testPodWithTwoContainers() pod := testPodWithTwoContainers()
pod.Annotations = map[string]string{corev1.DefaultContainerAnnotationName: "foo-2-c1"} pod.Annotations = map[string]string{podutils.DefaultContainerAnnotationName: "foo-2-c1"}
return pod return pod
}, },
allContainers: true, allContainers: true,

View File

@ -25,6 +25,10 @@ import (
"k8s.io/utils/integer" "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. // IsPodAvailable returns true if a pod is available; false otherwise.
// Precondition for an available pod is that it must be ready. On top // 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: // of that, there are two cases when a pod can be considered available: