Ajay Sainy
Ajay Sainy

Reputation: 379

Watch Kubernetes events using C# library

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

Upvotes: 0

Views: 1286

Answers (1)

Ajay Sainy
Ajay Sainy

Reputation: 379

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

Upvotes: 1

Related Questions