I was getting a similar error but from a pod using golang client in default namespace:
pods is forbidden: User "system:serviceaccount:default:default" cannot list resource "pods" in API group "" at the cluster scope
Golang code snippet:
if configMode == "IN_CLUSTER" {
// creates the in-cluster config
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
return config, err
}
I used the solution provided by @Gazi but with ClusterRole, that will let you access resources cluster-wide. It was modified for get and list only:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: query-role
namespace: default
rules:
- apiGroups: ["", "apps", "batch"]
resources: [ "deployments", "jobs", "pods", "replicasets", "services" ]
verbs: [ "get", "list" ]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: query-role-binding
namespace: default
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: query-role
subjects:
- kind: ServiceAccount
name: default
namespace: default
Following links are useful:
https://kubernetes.io/docs/reference/access-authn-authz/rbac/
https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/