Kubernetes Watch returns empty result (empty Type, empty Object)

101 views Asked by At

I am doing a watch on some resources with Kubernetes client-go.

func watchGVR(ctx context.Context, args *Arguments, dynClient *dynamic.DynamicClient, gvr schema.GroupVersionResource) error {
    //if gvr.Group==" events.k8s.io" && gvr.Resource==
    fmt.Printf("Watching %q %q\n", gvr.Group, gvr.Resource)
    watch, err := dynClient.Resource(gvr).Watch(context.TODO(), metav1.ListOptions{})
    if err != nil {
        fmt.Printf("..Error watching %v. group %q version %q resource %q\n", err,
            gvr.Group, gvr.Version, gvr.Resource)
        return err
    }
    defer watch.Stop()
    for {
        select {
        case event := <-watch.ResultChan():
            handleEvent(gvr, event)
        case <-ctx.Done():
            return nil
        }
    }
}

func handleEvent(gvr schema.GroupVersionResource, event watch.Event) {
    if event.Object == nil {
        fmt.Printf("event.Object is nil? Skipping this event. Type=%s %+v gvr: (group=%s version=%s resource=%s)\n", event.Type, event,
            gvr.Group, gvr.Version, gvr.Resource)
        return
    }
    gvk := event.Object.GetObjectKind().GroupVersionKind()
    obj, ok := event.Object.(*unstructured.Unstructured)
    if !ok {
        fmt.Printf("Internal Error, could not cast to Unstructered %T %+v\n", event.Object, event.Object)
        return
    }
....

This works fine, except that for some resources I get continuously empty results: event.Object is nil and event.Type is the empty string.

This happens for resources like this:

event.Object is nil? Skipping this event. Type= {Type: Object:} gvr: (group=operator.cluster.x-k8s.io version=v1alpha2 resource=addonproviders)

event.Object is nil? Skipping this event. Type= {Type: Object:} gvr: (group=operator.cluster.x-k8s.io version=v1alpha2 resource=coreproviders)

What could be the reasons for that?

1

There are 1 answers

0
guettli On BEST ANSWER

I found the issue.

I need to check for the result when reading from the channel:

        case event := <-watch.ResultChan():
            handleEvent(gvr, event)

to

        case event, ok := <-watch.ResultChan():
            if !ok {
                fmt.Printf("ResultChan is closed %+v\n", gvr)
                return nil
            }
            handleEvent(gvr, event)

The channel gets closed if there are no objects for this resource.