你可以使用client-go API从Kubernetes中获取Pod的事件消息。

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

How can I get events messages from a pod from Kubernetes using client-go API?

问题

你可以使用client-go Kubernetes API来获取Pod的事件消息,类似于使用kubectl describe pod命令的效果。你可以通过以下步骤来实现:

  1. 导入必要的包:
import (
    "fmt"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/tools/clientcmd"
)
  1. 创建一个Kubernetes客户端:
config, err := clientcmd.BuildConfigFromFlags("", "/path/to/kubeconfig")
if err != nil {
    panic(err.Error())
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
    panic(err.Error())
}
  1. 使用客户端获取Pod的事件:
podName := "spark-t2f59"
namespace := "spark"

events, err := clientset.CoreV1().Events(namespace).Search(clientset.CoreV1().Events(namespace).List(context.TODO(), metav1.ListOptions{}), metav1.ObjectReference{
    Kind:      "Pod",
    Name:      podName,
    Namespace: namespace,
})
if err != nil {
    panic(err.Error())
}

for _, event := range events.Items {
    fmt.Printf("Type: %s, Reason: %s, Age: %s, From: %s, Message: %s\n", event.Type, event.Reason, event.Age, event.Source.Component, event.Message)
}

请注意,你需要将/path/to/kubeconfig替换为你的kubeconfig文件的路径。此外,你还需要根据你的需求修改podName和namespace。

这样,你就可以使用client-go获取与kubectl describe pod相同的事件输出了。

英文:

How can I get events messages from a pod, like this command using 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

Is there a way to get the same output of events but using client-go instead of kubectl??

答案1

得分: 8

由于您知道命名空间和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)
	}
}
英文:

Since you know the namespace and the pod name, you can do:

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

huangapple
  • 本文由 发表于 2021年11月1日 04:34:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/69790386.html
匿名

发表评论

匿名网友

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

确定