简体   繁体   English

使用 cluster-admin 角色创建新的 CRD 资源

[英]Creating new CRD resource with cluster-admin role

I created a new service account and a rolebining giving him the role of cluster-admin as follows.我创建了一个新的服务帐户和一个角色绑定,为他提供了集群管理员的角色,如下所示。 I applied a new CRD resource with it and I expected it to fail as the default cluster-admin role can not manage CRD unless a new ClusterRole is created with aggregate-to-admin label, but the CRD was created and I do not understand why.我用它应用了一个新的 CRD 资源,我预计它会失败,因为默认集群管理员角色无法管理 CRD,除非使用聚合到管理员 label 创建新的 ClusterRole,但是创建了 CRD,我不明白为什么.

https://kubernetes.io/docs/reference/access-authn-authz/rbac/#aggregated-clusterroles https://kubernetes.io/docs/reference/access-authn-authz/rbac/#aggregated-clusterroles

kubectl create -f new_crd.yaml --as=system:serviceaccount:test-ns:test kubectl create -f new_crd.yaml --as=system:serviceaccount:test-ns:test

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: test-rolebinding
subjects:
- kind: ServiceAccount
  name: test
  namespace: test-ns
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io

Addressing the part of the last comment:解决最后评论的部分:

I do not understand the purpose of using aggregate-to-admin label -- I thought its purpose is to add rules to cluster-admin but if cluster-admin can do anything in the first place then why it is used?我不明白使用聚合到管理员 label 的目的——我认为它的目的是向集群管理员添加规则,但是如果集群管理员首先可以做任何事情,那么为什么要使用它?

aggregate-to-admin is a label used to aggregate ClusterRoles . aggregate-to-admin是一个label用于聚合ClusterRoles This exact is used to aggregate ClusterRoles to an admin ClusterRole .这确切用于将ClusterRoles聚合到admin ClusterRole

A side note!旁注!

cluster-admin and admin are two separate ClusterRoles . cluster-adminadmin是两个独立的ClusterRoles

I will include the example of aggregating ClusterRoles with an explanation below.我将包含聚合ClusterRoles的示例,并在下面进行解释。

You can read in the official Kubernetes documentation:您可以阅读官方 Kubernetes 文档:

Default ClusterRole默认集群角色 Default ClusterRoleBinding默认 ClusterRoleBinding Description描述
cluster-admin集群管理员 system:masters group系统:大师组 Allows super-user access to perform any action on any resource.允许超级用户访问对任何资源执行任何操作。 When used in a ClusterRoleBinding, it gives full control over every resource in the cluster and in all namespaces.在 ClusterRoleBinding 中使用时,它可以完全控制集群和所有命名空间中的每个资源。 When used in a RoleBinding, it gives full control over every resource in the role binding's namespace, including the namespace itself.在 RoleBinding 中使用时,它可以完全控制角色绑定命名空间中的每个资源,包括命名空间本身。
admin行政 None没有任何 Allows admin access, intended to be granted within a namespace using a RoleBinding.允许管理员访问,旨在使用 RoleBinding 在命名空间内授予。 If used in a RoleBinding, allows read/write access to most resources in a namespace, including the ability to create roles and role bindings within the namespace.如果在 RoleBinding 中使用,则允许对命名空间中的大多数资源进行读/写访问,包括在命名空间中创建角色和角色绑定的能力。 This role does not allow write access to resource quota or to the namespace itself.此角色不允许对资源配额或命名空间本身进行写访问。

Using aggregated ClusterRoles使用聚合ClusterRoles

The principle behind aggregated Clusterroles is to have one ClusterRole that have multiple other ClusterRoles aggregated to it.聚合Clusterroles背后的原理是让一个ClusterRole聚合多个其他ClusterRoles

Let's assume that:让我们假设:

  • A ClusterRole : aggregated-clusterrole will be aggregating two other ClusterRoles that will have needed permissions on some actions. ClusterRole : aggregated-clusterrole ClusterRoles聚合另外两个对某些操作具有所需权限的 ClusterRole。
  • A ClusterRole : clusterrole-one will be used to add some permissions to aggregated-clusterrole A ClusterRole : clusterrole-one将用于向aggregated-clusterrole添加一些权限
  • A ClusterRole : clusterrole-two will be used to add some permissions to aggregated-clusterrole一个ClusterRole : clusterrole-two将用于向aggregated-clusterrole添加一些权限

An example of such setup could be implemented by YAML definitions like below:此类设置的示例可以通过YAML定义实现,如下所示:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: aggregated-clusterrole
aggregationRule:
  clusterRoleSelectors:
  - matchLabels:
      rbac.example.com/put-here-any-label-name: "true" # <-- IMPORTANT
rules: [] 

Above definition will be aggregating ClusterRoles created with a label :上面的定义将聚合ClusterRoles创建的label

  • rbac.example.com/put-here-any-label-name: "true"

Describing this ClusterRole without aggregating any ClusterRoles with previously mentioned label :描述此ClusterRole而不将任何ClusterRoles与前面提到label

  • $ kubectl describe clusterrole aggregated-clusterrole
Name:         aggregated-clusterrole
Labels:       <none>
Annotations:  <none>
PolicyRule:
  Resources  Non-Resource URLs  Resource Names  Verbs
  ---------  -----------------  --------------  -----

Two ClusterRoles that will be used are the following:将使用的两个ClusterRoles如下:

clusterrole-one.yaml : clusterrole-one.yaml

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: clusterrole-one
  labels:
    rbac.example.com/put-here-any-label-name: "true" # <-- IMPORTANT
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch"]

clusterrole-two.yaml : clusterrole-two.yaml

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: clusterrole-two
  labels:
    rbac.example.com/put-here-any-label-name: "true" # <-- IMPORTANT
rules:
- apiGroups: [""]
  resources: ["services"]
  verbs: ["create", "delete"]

After applying above definitions, you can check if aggregated-clusterrole have permissions used in clusterrole-one and clusterrole-two :应用上述定义后,您可以检查aggregated-clusterrole是否具有在clusterrole-oneclusterrole-two中使用的权限:

  • $ kubectl describe clusterrole aggregated-clusterrole
Name:         aggregated-clusterrole
Labels:       <none>
Annotations:  <none>
PolicyRule:
  Resources  Non-Resource URLs  Resource Names  Verbs
  ---------  -----------------  --------------  -----
  services   []                 []              [create delete]
  pods       []                 []              [get list watch]

Additional resources:其他资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM