20.3. 为自定义资源定义创建集群角色
创建集群范围的自定义资源定义(CRD)后,您可以为其授予权限。如果使用 admin、edit 和 view 默认集群角色,请利用集群角色聚合来制定规则。
重要
您必须为每个角色明确分配权限。权限更多的角色不会继承权限较少角色的规则。如果要为某个角色分配规则,还必须将该操作动词分配给具有更多权限的角色。例如,如果您将"get crontabs"权限授予 view 角色,则还必须将其授予 edit 和 admin 角色。admin 或 edit 角色通常分配给通过项目 模板创建项目 的用户。
先决条件
- 创建 CRD。
流程
为 CRD 创建集群角色定义文件。集群角色定义是一个 YAML 文件,其中包含适用于各个集群角色的规则。OpenShift Container Platform 控制器会将您指定的规则添加至默认集群角色中。
集群角色定义的 YAML 文件示例
apiVersion: rbac.authorization.k8s.io/v1 1 kind: ClusterRole items: - metadata: name: aggregate-cron-tabs-admin-edit 2 labels: rbac.authorization.k8s.io/aggregate-to-admin: "true" 3 rbac.authorization.k8s.io/aggregate-to-edit: "true" 4 rules: - apiGroups: ["stable.example.com"] 5 resources: ["crontabs"] 6 verbs: ["get", "list", "watch", "create", "update", "patch", "delete", "deletecollection"] 7 - metadata: name: aggregate-cron-tabs-view 8 labels: # Add these permissions to the "view" default role. rbac.authorization.k8s.io/aggregate-to-view: "true" 9 rbac.authorization.k8s.io/aggregate-to-cluster-reader: "true" 10 rules: - apiGroups: ["stable.example.com"] 11 resources: ["crontabs"] 12 verbs: ["get", "list", "watch"] 13
创建集群角色:
oc create -f <file-name>.yaml