Can't assign a deployment creation role to serviceAccount in Kubernetes

12/12/2019

I want to have a service account that can create a deployment. So I am creating a service account, then a role and then a rolebinding. The yaml files are below:

ServiceAccount:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: testsa
  namespace: default

Role:

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: testrole
  namespace: default
rules:
  - apiGroups:
      - ""
      - batch
      - apps
    resources:
      - jobs
      - pods
      - deployments
      - deployments/scale   
      - replicasets
    verbs:
      - create
      - delete
      - get
      - list
      - patch
      - update
      - watch
      - scale 

RoleBinding:

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: testrolebinding
  namespace: default
subjects:
  - kind: ServiceAccount
    name: testsa
    namespace: default
roleRef:
  kind: Role
  name: testrole
  apiGroup: rbac.authorization.k8s.io

But after applying these files, when I do the following command to check if the service account can create a deployment, it answers no.

kubectl auth can-i --as=system:serviceaccount:default:testsa create deployment

The exact answer is: no - no RBAC policy matched

It works fine when I do checks for Pods.

What am I doing wrong?

My kubernetes versions are as follows:

kubectl version --short

Client Version: v1.16.1
Server Version: v1.12.10-gke.17
-- Rafa
google-kubernetes-engine
kubernetes
kubernetes-deployment
rbac

1 Answer

12/12/2019

Since you're using a 1.12 cluster, you should include the extensions API group in the Role for the deployments resource.

This was deprecated in Kubernetes 1.16 in favor of the apps group: https://kubernetes.io/blog/2019/07/18/api-deprecations-in-1-16/

-- snormore
Source: StackOverflow