如何使用kubectl补丁将Serviceaccout添加到现有Clusterrolebinding

时间:2020-05-27 12:17:09

标签: kubernetes kustomize

这是我现有的clusterrolebinding

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: example-role
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: test-role
subjects:
- kind: ServiceAccount
  name: test-sa
  namespace: ns1

我打算在另一个名称空间(例如:ns2)中添加相同的ServiceAccount(test-sa),并将其与我的ClusterRole“ test-role”绑定。

我尝试过的

subjects:
- kind: ServiceAccount
  name: test-sa
  namespace: ns2

我尝试像

一样应用上面的yaml文件
kubectl patch  clusterrolebinding <clusterrolebinding-name> --type="strategic"  --patch "$(cat role.yaml)"

结果

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: example-role
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: test-role
subjects:
- kind: ServiceAccount
  name: test-sa
  namespace: ns2

它将在新的命名空间中添加带有sa的ClusterRoleBinding,但是删除了我在命名空间ns1中的现有绑定..有什么方法可以合并新的更改,而不是替换..iam尝试以自动方式进行..用于编辑此cluserrolebinding的bash脚本,这就是为什么我选择kubectl补丁

1 个答案:

答案 0 :(得分:3)

您可以尝试以下命令。有效。请参阅here

kubectl patch clusterrolebinding example-role --type='json' -p='[{"op": "add", "path": "/subjects/1", "value": {"kind": "ServiceAccount", "name": "test-sa","namespace": "ns2" } }]'

op-操作add

subjects/1-添加到subjects数组的第一个位置

subjects:
- kind: ServiceAccount
  name: test-sa
  namespace: ns1
- kind: ServiceAccount
  name: test-sa
  namespace: ns2