9.2. 限定 Operator 安装范围
要为在 OLM 上安装和升级 Operator 提供范围规则,请将服务账户与 OperatorGroup 相关联。
集群管理员可借鉴本例,将一组 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。该 OperatorGroup 以指定的命名空间为目标,以确保其租期仅限于相应命名空间。另外,OperatorGroup 还允许用户指定服务账户。指定上一步创建的 ServiceAccount:
$ cat <<EOF | oc create -f - apiVersion: operators.coreos.com/v1 kind: OperatorGroup metadata: name: scoped namespace: scoped spec: serviceAccountName: scoped targetNamespaces: - scoped EOF
在指定命名空间中安装的任何 Operator 均会关联至该 OperatorGroup,因此也会关联至指定的服务账户。
在指定命名空间中创建订阅以安装 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
与该 OperatorGroup 相关联的所有 Operator 已被限制在指定服务账户获得的权限范围内。如果 Operator 请求的权限超出服务账户范围,安装将会失败,并将显示相应错误。
9.2.1. 细粒度权限
OLM 使用 OperatorGroup 中指定的服务账户来创建或更新与正在安装的 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。