I would like to grant a Kubernetes service account privileges for executing kubectl --token $token get pod --all-namespaces
. I'm familiar with doing this for a single namespace but don't know how to do it for all (including new ones that may be created in the future and without granting the service account full admin privileges).
Currently I receive this error message:
Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:kube-system:test" cannot list resource "pods" in API group "" at the cluster scope
What (cluster) roles and role bindings are required?
UPDATE Assigning role view
to the service with the following ClusterRoleBinding
works and is a step forward. However, I'd like to confine the service account's privileges further to the minimum required.
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: test
subjects:
- kind: ServiceAccount
name: test
namespace: kube-system
roleRef:
kind: ClusterRole
name: view
apiGroup: rbac.authorization.k8s.io
The service account's token can be extracted as follows:
secret=$(kubectl get serviceaccount test -n kube-system -o=jsonpath='{.secrets[0].name}')
token=$(kubectl get secret $secret -n kube-system -o=jsonpath='{.data.token}' | base64 --decode -)