0

This is my first time playing with K8s api/client. I am trying to watch for events in all namespaces. Below is the method, on running it prints:

Exception : Object reference not set to an instance of an object.
Closed

There aren't enough docs on how to use the C# client library. Can someone help me in understanding what I am doing wrong?

This is the method:

    public async Task Temp() {
        var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
        IKubernetes client = new Kubernetes(config);
        var resourceVersion = client.ListEventForAllNamespaces().ResourceVersion();
        var path = $"api/v1/events";
        // wait for events
        while (true)
        {
            var eventsWatcher = await client.WatchObjectAsync<V1EventList>(
            timeoutSeconds: int.MaxValue,
            path: path,
            resourceVersion: resourceVersion,
            onEvent: new Action<WatchEventType, V1EventList>((x, y) =>
            {
                Console.WriteLine(x.ToString());
            }),
            onClosed: new Action(() =>
            {
                Console.WriteLine("Closed");
            }),
            onError: new Action<Exception>((e) =>
            {
                Console.WriteLine($"Exception : {e.Message}");
            }));

        
            Thread.Sleep(1000);
        }
    }

On a side note, I am able to watch locally using http://localhost:8080/api/v1/events?watch=1&resourceVersion=27958468 after kubectl proxy

Ajay Sainy
  • 249
  • 1
  • 9
  • 20
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Pavel Anikhouski Jul 08 '20 at 07:50
  • @PavelAnikhouski Thanks for the link but it does not help me. I understand NullReferenceException, just not sure which part of the code is causing it. I tried debugging but could not find any null value. Let me know if you would like me to provide more info – Ajay Sainy Jul 08 '20 at 21:22

1 Answers1

0

Found the issue. I was using the wrong "path". Instead of api/v1/events it should be api/v1/watch/events. The NullReferenceException was happening within the library's watcher.cs line 149

enter image description here

Ajay Sainy
  • 249
  • 1
  • 9
  • 20