kubernetes - 如何使用 kubectl 补丁将 Serviceaccout 添加到现有的 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 尝试以自动化的方式进行..就像一个 bash 脚本编辑这个 cluserrolebinding,这就是我选择 kubectl 补丁的原因
解决方案
您可以尝试以下命令。有效。参考这里。
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:
- kind: ServiceAccount
name: test-sa
namespace: ns1
- kind: ServiceAccount
name: test-sa
namespace: ns2
推荐阅读
- flutter - 在 Flutter 中自定义应用栏,如 gmail 应用栏设计
- r - 无论绘图尺寸如何,都对 text() 和 legend() 使用恒定的垂直调整
- javascript - 试图让我的单选按钮排成一行
- bash - 致命错误:不是 rasp pi 上的有效对象名称来源
- c# - 中继器无法识别具有运行时错误“未知类型:System.Object 值:SqlDataSource1”的数据源
- elixir - Ecto unique_constraint 未按预期工作
- javascript - 如何将批量参与者传递给 Challonge REST API?
- java - 发送带有任何类型附件的电子邮件
- wxpython - 使用 wxpython 如何让我在一个 Textctrl 面板中键入的单词在另一个面板中显示为加密,反之亦然
- c++ - 在队列中选择备用位置