Golang/Kubernetes:如何获取/描述节点事件

huangapple go评论83阅读模式
英文:

Golang/Kubernetes: How to get/describe Node events

问题

我正在尝试通过Golang Kubernetes客户端获取特定节点的事件。

类似于当我运行 kubectl describe node <node-name> 时获取的事件的底部部分。

我能够获取Pod的事件,但不确定如何为节点执行相同的操作。

events, _ := coreClient.CoreV1().Events("spark").List(context.TODO(), metav1.ListOptions{FieldSelector: "involvedObject.name=spark-t2f59", TypeMeta: metav1.TypeMeta{Kind: "Pod"}})

更新:

我不确定是否可以使用.Get 来实现这个目标?我不确定下面的语法中期望的字符串是什么?

events, err := coreClient.CoreV1().Events(namespace).Get(context.TODO(), STRING-HERE, metav1.GetOptions{})
英文:

I am trying to get a specific node events through Golang Kubernetes client.

something similar to events we get when I run kubectl describe node &lt;node-name&gt; the lower end part of events.

I am able to get pod events but not sure how to do the same for a node.

events, _ := coreClient.CoreV1().Events(&quot;spark&quot;).List(context.TODO(), metav1.ListOptions{FieldSelector: &quot;involvedObject.name=spark-t2f59&quot;, TypeMeta: metav1.TypeMeta{Kind: &quot;Pod&quot;}})

Update:

I am not sure if using .Get can help with that? I am not sure what string is expected in below syntax ?

events, err := coreClient.CoreV1().Events(namespace).Get(context.TODO(), STRING-HERE, metav1.GetOptions{})

答案1

得分: 2

你应该能够像获取 Pod 的事件一样获取 Node 的事件,只需在查询中替换 "Kind" 选项:

	namespace := "my-namespace"
	nodeName := "my-node-71544c18-8wn3"

	events, err := clientset.CoreV1().Events(namespace).List(context.TODO(), metav1.ListOptions{
		TypeMeta: metav1.TypeMeta{
			Kind: "Node",
		},
		FieldSelector: "involvedObject.name=" + nodeName,
	})
英文:

You should be able to get the Node's events just as you did for a Pod, just by replacing the "Kind" option in your query:

	namespace := &quot;my-namespace&quot;
	nodeName := &quot;my-node-71544c18-8wn3&quot;

	events, err := clientset.CoreV1().Events(namespace).List(context.TODO(), metav1.ListOptions{
		TypeMeta: metav1.TypeMeta{
			Kind: &quot;Node&quot;,
		},
		FieldSelector: &quot;involvedObject.name=&quot; + nodeName,
	})

huangapple
  • 本文由 发表于 2022年5月25日 00:46:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/72366596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定