<p>Anyone who can tell me how to get pods under the service with client-go the client library of kubernetes? </p>

<p>thanks</p>

<p>I found this accepted answer a little lacking, as far as clarity. This code works under 1.10. In this example, my svc deployments all have a controlled name based on the artifact that it is fronting, and the pods leverage that as well. Please note I am a Java programmer learning go, so there may be a little too much OO for some go enthusiasts</p>
<pre class="lang-golang prettyprint-override"><code>package main

import (
    &quot;os&quot;
    &quot;log&quot;
    &quot;path/filepath&quot;
    &quot;k8s.io/client-go/tools/clientcmd&quot;
    &quot;k8s.io/client-go/kubernetes&quot;
    typev1 &quot;k8s.io/client-go/kubernetes/typed/core/v1&quot;
    metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;
    corev1 &quot;k8s.io/api/core/v1&quot;
    &quot;fmt&quot;
    &quot;strings&quot;
    &quot;errors&quot;
    &quot;k8s.io/apimachinery/pkg/labels&quot;
)

func main(){
    kubeconfig := filepath.Join(
        os.Getenv(&quot;HOME&quot;), &quot;.kube&quot;, &quot;config&quot;,
    )
    namespace:=&quot;FOO&quot;
    k8sClient, err:= getClient(kubeconfig)
    if err!=nil{
        fmt.Fprintf(os.Stderr, &quot;error: %v\n&quot;, err)
        os.Exit(1)
    }

    svc, err:=getServiceForDeployment(&quot;APP_NAME&quot;, namespace, k8sClient)
    if err!=nil{
        fmt.Fprintf(os.Stderr, &quot;error: %v\n&quot;, err)
        os.Exit(2)
    }

    pods, err:=getPodsForSvc(svc, namespace, k8sClient)
    if err!=nil{
        fmt.Fprintf(os.Stderr, &quot;error: %v\n&quot;, err)
        os.Exit(2)
    }
    
}

func getClient(configLocation string) (typev1.CoreV1Interface, error){
    kubeconfig := filepath.Clean(configLocation)
    config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, kubeconfig)
    if err != nil {
        log.Fatal(err)
    }
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        return nil, err
    }
    return clientset.CoreV1(), nil
}

func getServiceForDeployment(deployment string, namespace string, k8sClient typev1.CoreV1Interface) (*corev1.Service, error){
    listOptions := metav1.ListOptions{}
    svcs, err := k8sClient.Services(namespace).List(listOptions)
    if err != nil{
        log.Fatal(err)
    }
    for _, svc:=range svcs.Items{
        if strings.Contains(svc.Name, deployment){
            fmt.Fprintf(os.Stdout, &quot;service name: %v\n&quot;, svc.Name)
            return &amp;svc, nil
        }
    }
    return nil, errors.New(&quot;cannot find service for deployment&quot;)
}

func getPodsForSvc(svc *corev1.Service, namespace string, k8sClient typev1.CoreV1Interface) (*corev1.PodList, error){
    set := labels.Set(svc.Spec.Selector)
    listOptions:= metav1.ListOptions{LabelSelector: set.AsSelector().String()}
    pods, err:=  k8sClient.Pods(namespace).List(listOptions)
    for _,pod:= range pods.Items{
        fmt.Fprintf(os.Stdout, &quot;pod name: %v\n&quot;, pod.Name)
    }
    return pods, err
}
</code></pre>
