gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:04:28 25 4
gpt4 key购买 nike

我如何从 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??

最佳答案

既然您知道命名空间和 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)
}
}

关于go - 如何使用 client-go API 从 Kubernetes 的 pod 获取事件消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69790386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com