Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create cluster role in GKE even though I am owner and admin

After creating a new GKE cluster, creating a cluster role failed with the following error:

Error from server (Forbidden): error when creating "./role.yaml":
clusterroles.rbac.authorization.k8s.io "secret-reader" is forbidden: 
attempt to grant extra privileges: [PolicyRule{Resources:["secrets"], 
APIGroups:[""], Verbs:["get"]} PolicyRule{Resources:["secrets"], 
APIGroups:[""], Verbs:["watch"]} PolicyRule{Resources:["secrets"], 
APIGroups:[""], Verbs:["list"]}] user=&{[email protected]  
[system:authenticated] map[authenticator:[GKE]]} ownerrules= . 
[PolicyRule{Resources:["selfsubjectaccessreviews" 
"selfsubjectrulesreviews"], APIGroups:["authorization.k8s.io"], Verbs: 
["create"]} PolicyRule{NonResourceURLs:["/api" "/api/*" "/apis" 
"/apis/*" "/healthz" "/swagger-2.0.0.pb-v1" "/swagger.json" 
"/swaggerapi" "/swaggerapi/*" "/version"], Verbs:["get"]}] 
ruleResolutionErrors=[]

My account has the following permissions in IAM:

Kubernetes Engine Admin

Kubernetes Engine Cluster Admin

Owner

This is my role.yaml (from the Kubernetes docs):

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: secret-reader
rules:
- apiGroups: [""]
  resources: ["secrets"]
  verbs: ["get", "watch", "list"]

According to the RBAC docs of GCloud, I need to

create a RoleBinding that gives your Google identity a cluster-admin role before attempting to create additional Role or ClusterRole permissions.

So I tried this:

export GCP_USER=$(gcloud config get-value account | head -n 1)
kubectl create clusterrolebinding cluster-admin-binding
--clusterrole=cluster-admin --user=$GCP_USER

which succeeded, but I still get the same error when creating the cluster role.

Any ideas what I might be doing wrong?

like image 670
Sebastian Rösch Avatar asked Apr 15 '18 17:04

Sebastian Rösch


People also ask

How do I give access to GKE cluster?

You can use both Identity and Access Management (IAM) and Kubernetes RBAC to control access to your GKE cluster: IAM is not specific to Kubernetes; it provides identity management for multiple Google Cloud products, and operates primarily at the level of the Google Cloud project.

How do you create a cluster in GKE?

To create clusters in GKE, you need to choose a mode of operation: Autopilot or Standard. If you use the Autopilot mode, your cluster is regional by default. If you use the Standard mode, this tutorial creates a zonal cluster. Replace COMPUTE_REGION with the Compute Engine region for the cluster.

What is the difference between role and cluster role in Kubernetes?

A Role always sets permissions within a particular namespace; when you create a Role, you have to specify the namespace it belongs in. ClusterRole, by contrast, is a non-namespaced resource.

What is a GKE cluster?

GKE is a google cloud service which provides managed environment to deploy, manage and scale containerized applications. It becomes easier to create a cluster using GKE with required number of nodes with just few clicks. Nodes in the cluster are VM instances which can be created using another service called Compute Engine.

What is the difference between the storage administrator role and clusterrole?

The storage administrator role can be created to authorize a storage admin to create persistentvolumes A ClusterRole can be used to grant the same permissions as a Role. Because ClusterRoles are cluster-scoped, you can also use them to grant access to: To grant permissions across a whole cluster, you can use a ClusterRoleBinding.

How do I find GKE roles in cloud console?

GKE roles are prefixed with roles/container, such as gcloud iam roles describe roles/container.admin. Go to the Roles section of the IAM & Admin page on Cloud Console. To see the roles for GKE, in the Filter table field, enter Kubernetes Engine.

What is the difference between GKE and roles?

Roles define which GCP resources an account can access and which operations they can perform. In GKE, you can use Cloud IAM to manage which users and service accounts can access, and perform operations in, your clusters.


1 Answers

According to Google Container Engine docs you must first create a RoleBinding that grants you all of the permissions included in the role you want to create.

Get current google identity

$ gcloud info | grep Account

Account: [[email protected]]

Grant cluster-admin to your current identity

$ kubectl create clusterrolebinding myname-cluster-admin-binding --clusterrole=cluster-admin [email protected]

Clusterrolebinding "myname-cluster-admin-binding" created

Now you can create your ClusterRole without any problem.

I found the answer in CoreOS FAQ / Troubleshooting check it out for more information.

like image 124
Manuel Felipe Garcia Rincon Avatar answered Oct 05 '22 20:10

Manuel Felipe Garcia Rincon