<p>I made a demo with kubernetes/go-client where i tried to list pods from my cluster.</p>
<pre><code>    config, err := rest.InClusterConfig()

    if err != nil {
        panic(err.Error())
    }

    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err.Error())
    }

    pods, err := clientset.CoreV1().Pods(&quot;&quot;).List(context.TODO(), metav1.ListOptions{})

    fmt.Fprint(w, &quot;There are d pods in the cluster\n&quot;, len(pods.Items))
</code></pre>
<p>I created serviceaccount token to assign to the pod where this code is running in.</p>
<p>But when code is executed pods.Items has no pods.</p>
<p>I deployed this pod inside minikube. When I launch some kubectl command for listing pods, this way I can get resources so it is no t permissions problems.</p>
<p>I wonder what is happening and how i can fix it.</p>
<hr />
<p>Repository <a href="https://github.com/srpepperoni/inframanager.git" rel="nofollow noreferrer">https://github.com/srpepperoni/inframanager.git</a></p>
<p>Image is pushed into: <a href="https://hub.docker.com/r/jaimeyh/inframanager" rel="nofollow noreferrer">https://hub.docker.com/r/jaimeyh/inframanager</a></p>
<p>The endpoint I have problems with is this one :</p>
<pre><code>mux.HandleFunc(&quot;/getPods&quot;, GetPodsFromNamespace)
</code></pre>

Jaime Yera
<p>You need to check if the <code>err</code> on the last line is non-nil.</p>
<pre><code>pods, err := clientset.CoreV1().Pods(&quot;&quot;).List(context.TODO(), metav1.ListOptions{})
</code></pre>
<blockquote>
<p>OK, there is the problem. pods is forbidden: User &quot;system:serviceaccount:mis-pruebas:sa-prueba-go&quot; cannot list resource &quot;pods&quot; in API group &quot;&quot; at the cluster scope</p>
</blockquote>
<p>As the error message indicates, the ServiceAccount does not have permission to list pods at cluster scope. You need to create Role and bind it to the ServiceAccount.</p>
<p>The article <a href="https://kubernetes.io/docs/reference/access-authn-authz/rbac/" rel="nofollow noreferrer">Using RBAC Authorization</a> even has a <a href="https://kubernetes.io/docs/reference/access-authn-authz/rbac/#role-example" rel="nofollow noreferrer">role example</a> for how to create such a role.</p>
