我如何使用 kubectl 补丁将 Serviceaccout 添加到现有的 Clusterrolebinding
How can i add a Serviceaccout using kubectl patch to existing Clusterrolebinding
这是我现有的 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
我计划在另一个命名空间(对于 eg: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 中的现有绑定被删除了..有什么方法可以合并新的更改而不是替换..我正在尝试以自动化的方式进行..就像用于编辑此 cluserrolebinding 的 bash 脚本,这就是我选择 kubectl patch
的原因
您可以尝试以下命令。有效。参考 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
这是我现有的 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
我计划在另一个命名空间(对于 eg: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 中的现有绑定被删除了..有什么方法可以合并新的更改而不是替换..我正在尝试以自动化的方式进行..就像用于编辑此 cluserrolebinding 的 bash 脚本,这就是我选择 kubectl patch
的原因您可以尝试以下命令。有效。参考 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