K8S Go Client: How do you list all the pods associated to a Service?

7,553 views
Skip to first unread message

buck.r...@gmail.com

unread,
Dec 10, 2017, 2:42:02 PM12/10/17
to Kubernetes user discussion and Q&A
Given a namespace name, and a service name, I'd like to use the In-Cluster Golang API to list the pod addresses for those pods associated to a service.

Which APIs would I call to get this information? I'm not quite seeing how to do this.

Thanks in advance.

-Bob

buck.r...@gmail.com

unread,
Dec 10, 2017, 3:29:58 PM12/10/17
to Kubernetes user discussion and Q&A

John Belamaric

unread,
Dec 11, 2017, 11:25:48 AM12/11/17
to kubernet...@googlegroups.com
Are you sure you want pods and not endpoints?
> --
> You received this message because you are subscribed to the Google Groups "Kubernetes user discussion and Q&A" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kubernetes-use...@googlegroups.com.
> To post to this group, send email to kubernet...@googlegroups.com.
> Visit this group at https://groups.google.com/group/kubernetes-users.
> For more options, visit https://groups.google.com/d/optout.

Robert Buck

unread,
Dec 11, 2017, 3:20:25 PM12/11/17
to kubernet...@googlegroups.com
Endpoints would work.

You received this message because you are subscribed to a topic in the Google Groups "Kubernetes user discussion and Q&A" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kubernetes-users/Fuufm7l-di8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kubernetes-use...@googlegroups.com.

John Belamaric

unread,
Dec 11, 2017, 3:48:58 PM12/11/17
to kubernet...@googlegroups.com
Ok, so you just need to query for the endpoints object with the same name as your service (in the same namespace). This also allows you to differentiate between ready and not ready pods.

Robert Buck

unread,
Dec 11, 2017, 5:08:52 PM12/11/17
to kubernet...@googlegroups.com
Such a query would look like?

John Belamaric

unread,
Dec 11, 2017, 9:41:40 PM12/11/17
to kubernet...@googlegroups.com
Based on https://github.com/kubernetes/client-go/tree/master/examples/out-of-cluster-client-configuration the code below works.

eps, err := clientset.CoreV1().Endpoints("default").List(metav1.ListOptions{FieldSelector:"metadata.name=kubernetes"})

Here is a simple program. For in-cluster you just change the way the config is created (uncomment the in-cluster line and get rid of the other stuff).

package main

import (
"flag"
"fmt"
"os"
"path/filepath"

)

func oocConfig() (*rest.Config, error) {
var kubeconfig *string
if home := homeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()

// use the current context in kubeconfig
return clientcmd.BuildConfigFromFlags("", *kubeconfig)
}

func main() {
// creates the in-cluster config
//config, err := rest.InClusterConfig()

config, err := oocConfig()

if err != nil {
panic(err.Error())
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
eps, err := clientset.CoreV1().Endpoints("default").List(metav1.ListOptions{FieldSelector:"metadata.name=kubernetes"})
if err != nil {
panic(err.Error())
}
fmt.Printf("%v\n", eps)
}

func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}

Robert Buck

unread,
Dec 14, 2017, 6:35:06 PM12/14/17
to kubernet...@googlegroups.com
Sweet. Thank you!!! Jumping right on this.
Reply all
Reply to author
Forward
0 new messages