【问题标题】:Watch Kubernetes events using C# library使用 C# 库观看 Kubernetes 事件
【发布时间】:2020-07-08 07:10:54
【问题描述】:

这是我第一次玩 K8s api/client。我正在尝试监视所有命名空间中的事件。 下面是方法,运行时打印:

异常:对象引用未设置为对象的实例。
关闭

关于如何使用C# client library 的文档不足。有人可以帮助我理解我做错了什么吗?

这是方法:

    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);
        }
    }

附带说明一下,我可以在 kubectl 代理之后使用 http://localhost:8080/api/v1/events?watch=1&resourceVersion=27958468 在本地观看

【问题讨论】:

  • @PavelAnikhouski 感谢您的链接,但它对我没有帮助。我了解 NullReferenceException,只是不确定代码的哪一部分导致它。我尝试调试但找不到任何空值。如果您希望我提供更多信息,请告诉我

标签: c# kubernetes


【解决方案1】:

发现问题。我使用了错误的“路径”。它应该是 api/v1/watch/events,而不是 api/v1/events。 NullReferenceException 发生在库的 watcher.cs line 149

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-19
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多