From afd42a51049df4b504d0bcdac9eaffea92beeffe Mon Sep 17 00:00:00 2001 From: Ben Wheatley Date: Thu, 14 Sep 2023 18:11:32 +0100 Subject: [PATCH] Fix panic in waitForConsole Resolve this error: ``` panic: interface conversion: runtime.Object is *v1.Status, not *unstructured.Unstructured ``` There was an assumption of the object type that didn't hold true. If a watch `ERROR` event type is received, then it should be cast to a `v1.Status` instead. To avoid this, skip the event if it's of this type. This is likely fine for this case, as per the comment. Also add some extra error handling. https://github.com/kubernetes/apimachinery/blob/fd8daa85285e31da9771dbe372a66dfa20e78489/pkg/watch/watch.go#L43-L70 --- pkg/workloads/console/runner/runner.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/workloads/console/runner/runner.go b/pkg/workloads/console/runner/runner.go index 5c19184a..ff154b48 100644 --- a/pkg/workloads/console/runner/runner.go +++ b/pkg/workloads/console/runner/runner.go @@ -20,6 +20,7 @@ import ( "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/watch" "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" @@ -896,8 +897,24 @@ func (c *Runner) waitForConsole(ctx context.Context, createdCsl workloadsv1alpha return nil, errors.New("watch channel closed") } - obj := event.Object.(*unstructured.Unstructured) - runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), csl) + // We can ignore Bookmark events, because we aren't making requests using bookmarks. + // + // If we encounter an Error event, then igore this too. + // We'll instead wait until the channel is closed, or our context timeout is + // reached. + if event.Type == watch.Bookmark || event.Type == watch.Error { + continue + } + + obj, ok := event.Object.(*unstructured.Unstructured) + if !ok { + return nil, fmt.Errorf("failed to cast watch event") + } + + err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), csl) + if err != nil { + return nil, fmt.Errorf("error converting console object: %w", err) + } if isRunning(csl) { return csl, nil