如何使用 kubectl 优雅地更改集群角色

0 kubernetes

默认情况下,clusterrole system:node 中的数据如下:

$ kubectl get clusterrole system:node -o yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
name: system:node
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - create
  - delete
- apiGroups:
  - ""
  resources:
  - pods/status
  verbs:
  - patch
  - update
Run Code Online (Sandbox Code Playgroud)

现在,我想更改 clusterrole system:node,并- patch在 pods resources 下添加一个,它应该是这样的:

- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
  - list
  - watch
  - patch
Run Code Online (Sandbox Code Playgroud)

我可以使用 来更新它kubectl edit,但我想在 bash 脚本中更新它,所以kubectl edit不合适,是否有其他使用 kubectl 的解决方案?

Blo*_*je5 8

您可以使用包含 yaml 定义的kubectl apply -f node-role.yamlwhere以及您的更改。如果角色已经存在,则将更新该角色(否则创建它)。node-role.yamlClusterRolekubectl apply