4.7.2. 限定 Operator 安装范围
要为 Operator Lifecycle Manager(OLM)上的 Operator 安装和升级提供范围规则,请将服务帐户与 Operator 组关联。
集群管理员可借鉴本例,将一组 Operator 限制到指定命名空间中。
流程
新建命名空间:
$ cat <<EOF | oc create -f - apiVersion: v1 kind: Namespace metadata: name: scoped EOF
分配 Operator 的权限范围。这涉及创建新服务帐户、相关角色和角色绑定。
$ cat <<EOF | oc create -f - apiVersion: v1 kind: ServiceAccount metadata: name: scoped namespace: scoped EOF
为简便起见,以下示例授予服务账户在指定命名空间进行任何操作的权限。在生产环境中,应创建更为精细的权限集:
$ cat <<EOF | oc create -f - apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: scoped namespace: scoped rules: - apiGroups: ["*"] resources: ["*"] verbs: ["*"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: scoped-bindings namespace: scoped roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: scoped subjects: - kind: ServiceAccount name: scoped namespace: scoped EOF
在指定的命名空间中创建
OperatorGroup
对象。该 Operator 组以指定的命名空间为目标,以确保其租期仅限于该命名空间。另外,Operator 组允许用户指定服务帐户。指定上一步中创建的服务帐户:
$ cat <<EOF | oc create -f - apiVersion: operators.coreos.com/v1 kind: OperatorGroup metadata: name: scoped namespace: scoped spec: serviceAccountName: scoped targetNamespaces: - scoped EOF
在指定命名空间中安装的任何 Operator 均会关联至此 Operator 组,因此也会关联到指定的服务账户。
在指定命名空间中创建
Subscription
对象以安装 Operator:$ cat <<EOF | oc create -f - apiVersion: operators.coreos.com/v1alpha1 kind: Subscription metadata: name: etcd namespace: scoped spec: channel: singlenamespace-alpha name: etcd source: <catalog_source_name> 1 sourceNamespace: <catalog_source_namespace> 2 EOF
与该 Operator 组相关联的所有 Operator 都仅限于为指定服务账户授予的权限。如果 Operator 请求的权限超出服务账户范围,安装会失败并显示相关错误。
4.7.2.1. 细粒度权限
Operator Lifecycle Manager(OLM)使用 Operator 组中指定的服务账户来创建或更新与正在安装的 Operator 相关的以下资源:
-
ClusterServiceVersion
-
Subscription
-
Secret
-
ServiceAccount
-
Service
-
ClusterRole
和ClusterRoleBinding
-
Role
和RoleBinding
要将 Operator 限制到指定命名空间,集群管理员可以首先向服务账户授予以下权限:
以下角色只是一个通用示例,具体 Operator 可能需要额外规则。
kind: Role rules: - apiGroups: ["operators.coreos.com"] resources: ["subscriptions", "clusterserviceversions"] verbs: ["get", "create", "update", "patch"] - apiGroups: [""] resources: ["services", "serviceaccounts"] verbs: ["get", "create", "update", "patch"] - apiGroups: ["rbac.authorization.k8s.io"] resources: ["roles", "rolebindings"] verbs: ["get", "create", "update", "patch"] - apiGroups: ["apps"] 1 resources: ["deployments"] verbs: ["list", "watch", "get", "create", "update", "patch", "delete"] - apiGroups: [""] 2 resources: ["pods"] verbs: ["list", "watch", "get", "create", "update", "patch", "delete"]
另外,如果任何 Operator 指定了 pull secret,还必须增加以下权限:
kind: ClusterRole 1
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get"]
---
kind: Role
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["create", "update", "patch"]
- 1
- 需要从 OLM 命名空间中获取 secret。