如何使用client-go API从Kubernetes的pod获取事件消息?

brvekthn  于 2023-10-17  发布在  Kubernetes
关注(0)|答案(1)|浏览(114)

如何从pod获取事件消息,例如使用client-go Kubernetes API的命令:

kubectl describe pod spark-t2f59 -n spark

Events:
  Type     Reason             Age   From                Message
  ----     ------             ----  ----                -------
  Warning  FailedScheduling   104s  default-scheduler   0/19 nodes are available: 15 Insufficient cpu, 19 Insufficient memory.
  Warning  FailedScheduling   104s  default-scheduler   0/19 nodes are available: 15 Insufficient cpu, 19 Insufficient memory.
  Warning  FailedScheduling   45s   default-scheduler   0/20 nodes are available: 16 Insufficient cpu, 20 Insufficient memory.
  Warning  FailedScheduling   34s   default-scheduler   0/20 nodes are available: 16 Insufficient cpu, 20 Insufficient memory.
  Normal   NotTriggerScaleUp  97s   cluster-autoscaler  pod didn't trigger scale-up (it wouldn't fit if a new node is added): 1 Insufficient memory, 1 max node group size reached

有没有一种方法可以获得相同的事件输出,但使用client-go而不是kubectl??

rn0zuynd

rn0zuynd1#

由于您知道命名空间和pod名称,因此可以执行以下操作:

package main

import (
    "context"
    "fmt"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/rest"
)

func main() {
    config, _ := rest.InClusterConfig()
    clientset, _ := kubernetes.NewForConfig(config)
    events, _ := clientset.CoreV1().Events("spark").List(context.TODO(),metav1.ListOptions{FieldSelector: "involvedObject.name=spark-t2f59", TypeMeta: metav1.TypeMeta{Kind: "Pod"}})
    for _, item := range events.Items {
        fmt.Println(item)
    }
}

相关问题