kubernetes-client / csharp

Officially supported dotnet Kubernetes Client library
Apache License 2.0
1.1k stars 295 forks source link

[documentation request] add example: watch customresource (and handle timeouts) #1298

Closed lknite closed 1 year ago

lknite commented 1 year ago

Describe the bug There seems to be some inconsistency around watching a CRD, including an example would help to resolve confusion.

Inconsistency

Example

            var generic = new GenericClient(kubeclient, group, version, plural);

            while (true)
            {
                try
                {
                    await foreach (var (type, item) in generic.WatchNamespacedAsync<CrdInvite>("gge"))
                    {
                        Console.WriteLine("(event) [" + type + "] " + plural + "." + group + "/" + version + ": " + item.Metadata.Name);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception occured while performing 'watch': " + ex);
                }
            }

In the kubernetes documentation it suggests handling the watch timeout like so, how does the kubernetes client library do it?

image

IvanJosipovic commented 1 year ago

Check out this repo, it's a informer implementation. If you put a loop around the Start code it should do what you want.

https://github.com/microsoft/reverse-proxy/tree/main/src/Kubernetes.Controller/Client

lknite commented 1 year ago

I think there may a "most effective" way to write this code, but not sure what that'd be. In anycase, everything's working good enough for now to close out this ticket.