【问题标题】:How can i add a Serviceaccout using kubectl patch to existing Clusterrolebinding如何使用 kubectl 补丁将 Serviceaccout 添加到现有的 Clusterrolebinding
【发布时间】:2020-05-27 12:17:09
【问题描述】:

这是我现有的集群角色绑定

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 补丁的原因

【问题讨论】:

    标签: kubernetes kustomize


    【解决方案1】:

    你可以试试下面的命令。有效。参考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:
    - kind: ServiceAccount
      name: test-sa
      namespace: ns1
    - kind: ServiceAccount
      name: test-sa
      namespace: ns2
    

    【讨论】:

    • 根据tools.ietf.org/html/rfc6902#section-4.1“如果目标位置指定了一个确实存在的对象成员,则替换该成员的值。”。使用subjects/- 添加到数组的末尾更安全。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 2020-11-14
    • 2019-01-12
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    相关资源
    最近更新 更多