From a7882d8a4264cdb9eaab9dc3d67cf0cc5afd5886 Mon Sep 17 00:00:00 2001 From: John Howard Date: Tue, 2 Mar 2021 09:07:50 -0800 Subject: [PATCH] Improve warnings for default-container Right now, there is no way to use these annotations, support multiple `kubectl` versions, and not give users annoying warnings every time they run `kubectl log`. If a user is setting *both* annotations, they clearly know that the old on is deprecated. Therefor, we should not warn them. --- .../kubectl/pkg/polymorphichelpers/logsforobject.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go b/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go index 3c05c2e8ba4..f6fb2400923 100644 --- a/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go +++ b/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go @@ -79,11 +79,15 @@ func logsForObjectWithClient(clientset corev1client.CoreV1Interface, object, opt // 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 { + if len(annotations[podutils.DefaultContainerAnnotationName]) > 0 { + containerName = annotations[podutils.DefaultContainerAnnotationName] + } else if len(annotations[defaultLogsContainerAnnotationName]) > 0 { + // Only log deprecation if we have only the old annotation. This allows users to + // set both to support multiple versions of kubectl; if they are setting both + // they must already know it is deprecated, so we don't need to add noisy + // warnings. 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 {