My NodeJS microservice is deployed to k8s cluster.
I would like this microservice to access the k8s API server. For that, I guess I need to create a ServiceAccount
for it. So I did this:
apiVersion: v1
kind: ServiceAccount
metadata:
name: my-app-service-account
namespace: myapp-ns
Then, I also created a ClusterRole
to define the permissions:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: myapp-cluster-role
namespace: myapp-ns
rules:
- apiGroups: ["*"]
resources: ["*"]
verbs: ["*"]
Finally, I created a ClusterRoleBinding
:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: my-app-role-binding
namespace: myapp-ns
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: myapp-cluster-role
subjects:
kind: ServiceAccount
name: my-app-service-account
When I deploy them (I use Ansible to do the deployment), I get the following error:
"error": 400, "msg": "Failed to create object: b'{\"kind\":\"Status\",\"apiVersion\":\"v1\",\"metadata\":{},\"status\":\"Failure\",\"message\":\"ClusterRoleBinding in version \\\\\"v1\\\\\" cannot be handled as a ClusterRoleBinding: json: cannot unmarshal object into Go struct field ClusterRoleBinding.subjects of type []v1.Subject\",\"reason\":\"BadRequest\",\"code\":400}\\n'",
Why this error? Where am I wrong?
I'd reckon the issue is with the resources, not with Ansible.
Take a look:
To summarize:
Clusterrole
is a not a namespaced resource, hence you should not specify it-
in the.subjects
.namespace
from.metadata
to.suspects...
More explanation on namespaced/non namespaced resources:
kubectl api-resources
I encourage you to check on the following docs: