From 90956e6d3e9df02d932f9954911b89a3fd1619fb Mon Sep 17 00:00:00 2001 From: Lee Verberne Date: Thu, 10 Mar 2022 02:51:35 +0100 Subject: [PATCH] Print kubectl debug messages received when starting a container (#108591) * kubectl debug: print container messages This provides feedback to the user, for example that the server is unable to pull the debug container image. * Label debug container updates as warnings Co-authored-by: Eddie Zaneski Co-authored-by: Eddie Zaneski --- .../src/k8s.io/kubectl/pkg/cmd/debug/debug.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/debug/debug.go b/staging/src/k8s.io/kubectl/pkg/cmd/debug/debug.go index 4a5f33f052e..1eaa4325b6d 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/debug/debug.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/debug/debug.go @@ -357,7 +357,7 @@ func (o *DebugOptions) Run(f cmdutil.Factory, cmd *cobra.Command) error { opts.Config = config opts.AttachFunc = attach.DefaultAttachFunc - if err := handleAttachPod(ctx, f, o.podClient, debugPod.Namespace, debugPod.Name, containerName, opts); err != nil { + if err := o.handleAttachPod(ctx, f, debugPod.Namespace, debugPod.Name, containerName, opts); err != nil { return err } } @@ -701,7 +701,7 @@ func containerNameToRef(pod *corev1.Pod) map[string]*corev1.Container { } // waitForContainer watches the given pod until the container is running -func waitForContainer(ctx context.Context, podClient corev1client.PodsGetter, ns, podName, containerName string) (*corev1.Pod, error) { +func (o *DebugOptions) waitForContainer(ctx context.Context, ns, podName, containerName string) (*corev1.Pod, error) { // TODO: expose the timeout ctx, cancel := watchtools.ContextWithOptionalTimeout(ctx, 0*time.Second) defer cancel() @@ -710,11 +710,11 @@ func waitForContainer(ctx context.Context, podClient corev1client.PodsGetter, ns lw := &cache.ListWatch{ ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { options.FieldSelector = fieldSelector - return podClient.Pods(ns).List(ctx, options) + return o.podClient.Pods(ns).List(ctx, options) }, WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { options.FieldSelector = fieldSelector - return podClient.Pods(ns).Watch(ctx, options) + return o.podClient.Pods(ns).Watch(ctx, options) }, } @@ -722,6 +722,7 @@ func waitForContainer(ctx context.Context, podClient corev1client.PodsGetter, ns var result *corev1.Pod err := intr.Run(func() error { ev, err := watchtools.UntilWithSync(ctx, lw, &corev1.Pod{}, nil, func(ev watch.Event) (bool, error) { + klog.V(2).Infof("watch received event %q with object %T", ev.Type, ev.Object) switch ev.Type { case watch.Deleted: return false, errors.NewNotFound(schema.GroupResource{Resource: "pods"}, "") @@ -740,6 +741,9 @@ func waitForContainer(ctx context.Context, podClient corev1client.PodsGetter, ns if s.State.Running != nil || s.State.Terminated != nil { return true, nil } + if !o.Quiet && s.State.Waiting != nil && s.State.Waiting.Message != "" { + fmt.Fprintf(o.ErrOut, "Warning: container %s: %s\n", containerName, s.State.Waiting.Message) + } return false, nil }) if ev != nil { @@ -751,8 +755,8 @@ func waitForContainer(ctx context.Context, podClient corev1client.PodsGetter, ns return result, err } -func handleAttachPod(ctx context.Context, f cmdutil.Factory, podClient corev1client.PodsGetter, ns, podName, containerName string, opts *attach.AttachOptions) error { - pod, err := waitForContainer(ctx, podClient, ns, podName, containerName) +func (o *DebugOptions) handleAttachPod(ctx context.Context, f cmdutil.Factory, ns, podName, containerName string, opts *attach.AttachOptions) error { + pod, err := o.waitForContainer(ctx, ns, podName, containerName) if err != nil { return err }