2

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??

javier_orta
  • 457
  • 4
  • 15
  • In every case I've seen thus far, running `kubectl --v=100` (or whatever the magic cut-over `-v` number is) will show _everything_ it calls on the server to assemble the output, and `kubectl api-resources | grep Event` shows that `events` are first-class resources in k8s. What have you already tried and what outcome is it producing for you? Please [edit your question](https://stackoverflow.com/posts/69790386/edit) and include your attempt along with the error you're getting. – mdaniel Oct 31 '21 at 21:15

1 Answers1

8

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)
    }
}
Theofilos Papapanagiotou
  • 5,133
  • 1
  • 18
  • 24