From 71db08d15a580cee1592eb699a3ff448f5fe4fe1 Mon Sep 17 00:00:00 2001 From: pacoxu Date: Mon, 1 Mar 2021 11:00:05 +0800 Subject: [PATCH] fix: will logs the default container only even --all-containers is specified Signed-off-by: pacoxu --- .../pkg/polymorphichelpers/logsforobject.go | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go b/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go index 3fff406909c..3c05c2e8ba4 100644 --- a/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go +++ b/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go @@ -73,30 +73,31 @@ func logsForObjectWithClient(clientset corev1client.CoreV1Interface, object, opt return ret, nil case *corev1.Pod: - // 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 := t.GetAnnotations(); annotations != nil && len(opts.Container) == 0 { - var containerName string - if len(annotations[defaultLogsContainerAnnotationName]) > 0 { - containerName = annotations[defaultLogsContainerAnnotationName] - 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 { - opts.Container = containerName - } else { - fmt.Fprintf(os.Stderr, "Default container name %q not found in a pod\n", containerName) - } - } - } // if allContainers is true, then we're going to locate all containers and then iterate through them. At that point, "allContainers" is false if !allContainers { + // 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 := t.GetAnnotations(); annotations != nil && len(opts.Container) == 0 { + var containerName string + if len(annotations[defaultLogsContainerAnnotationName]) > 0 { + containerName = annotations[defaultLogsContainerAnnotationName] + 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 { + opts.Container = containerName + } else { + fmt.Fprintf(os.Stderr, "Default container name %q not found in a pod\n", containerName) + } + } + } + var containerName string if opts == nil || len(opts.Container) == 0 { // We don't know container name. In this case we expect only one container to be present in the pod (ignoring InitContainers). - // If there is more than one container we should return an error showing all container names. + // If there is more than one container, we should return an error showing all container names. if len(t.Spec.Containers) != 1 { containerNames := getContainerNames(t.Spec.Containers) initContainerNames := getContainerNames(t.Spec.InitContainers)