1

I have created a job in kubernetes through client-go api. Now I want to get the log of the job, but I can't find the log api of job in client-go. Therefore, I want to obtain the name of all the pods in a job to obtain the POD logs by name, and then obtain the logs of the job.

So,how to get the name of pod in a job in kubernetes through client-go?

Thanks so much.

Worlder_Mo
  • 83
  • 1
  • 9
  • i think `client-go` is just a client, not an api. k8s api is language agnostic. so you can try examine the http response of apis first. – Lei Yang Feb 11 '22 at 03:30
  • [Here](https://stackoverflow.com/a/67543730/17126151) is an answer for similar problem how to do it. Let me know if this is helpful for you – RadekW Feb 11 '22 at 09:57
  • I got it with labelselector. Thanks. – Worlder_Mo Feb 12 '22 at 02:00
  • Could you post an answer what you exactly did? As you can read [here](https://stackoverflow.com/help/self-answer) it is very good practice and it will be helpful in future for other people – RadekW Feb 14 '22 at 09:36
  • OK,I will do it later. – Worlder_Mo Feb 15 '22 at 09:15

1 Answers1

5

I create a pod with label, and then I get it through LabelSelector. Like it :

    config, err := clientcmd.BuildConfigFromFlags("", "~/.kube/config")
    if err != nil {
        println("config build error")
    }
    
    client, err := kubernetes.NewForConfig(config)
    
    pods, err := client.CoreV1().Pods("test").List(context.TODO(),
        v1.ListOptions{LabelSelector: "name=label_name"})
    
    for _, v := range pods.Items {
        log := client.CoreV1().Pods("test").GetLogs(v.Name, &v12.PodLogOptions{})
    }
Worlder_Mo
  • 83
  • 1
  • 9