Kubernetes k8s dosen和x27中rolebinding的补丁服务帐户;我不能正常工作

Kubernetes k8s dosen和x27中rolebinding的补丁服务帐户;我不能正常工作,kubernetes,patch,service-accounts,Kubernetes,Patch,Service Accounts,我试图将服务帐户修补到rolebinding,但当我运行修补命令时,它替换了rolebinding yml中的整个主题字段。这里我展示了我为预期输出执行的现有配置和命令 要修补的命令: kubectl patch rolebinding test-team-binding --patch "$(cat patch-file.yml)" patch-file.yml: subjects: - kind: ServiceAccount name: user3 nam

我试图将服务帐户修补到rolebinding,但当我运行修补命令时,它替换了rolebinding yml中的整个主题字段。这里我展示了我为预期输出执行的现有配置和命令

要修补的命令:

kubectl patch rolebinding test-team-binding  --patch "$(cat patch-file.yml)" 
patch-file.yml:

subjects:
- kind: ServiceAccount
  name: user3
  namespace: test-namespace
rolebinding.yml:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  creationTimestamp: "2020-09-08T11:24:54Z"
  managedFields:
  - apiVersion: rbac.authorization.k8s.io/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .: {}
          f:kubectl.kubernetes.io/last-applied-configuration: {}
      f:roleRef:
        f:apiGroup: {}
        f:kind: {}
        f:name: {}
      f:subjects: {}
    manager: kubectl
    operation: Update
    time: "2020-10-06T07:37:58Z"
  name: test-team-binding
  namespace: test-namespace
  resourceVersion: "45697451"
  selfLink: /apis/rbac.authorization.k8s.io/v1/namespaces/test-namespace/rolebindings/test-team-binding
  uid: b602b333-4ee8-4601-8c75-f3707bb19d68
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: test-team
subjects:
- kind: ServiceAccount
  name: user1
  namespace: test-namespace
- kind: ServiceAccount
  name: user2
  namespace: test-namespace
预期产出:

subjects:
- kind: ServiceAccount
  name: user1
  namespace: test-namespace
- kind: ServiceAccount
  name: user2
  namespace: test-namespace
- kind: ServiceAccount
  name: user3
  namespace: test-namespace
结果输出:

subjects:
- kind: ServiceAccount
  name: user3
  namespace: test-namespace

您可以通过在json类型的patch命令中指定操作来添加/替换/删除,默认情况下,patch命令将替换该值。下面的命令应适用于您的要求

kubectl patch rolebinding test-team-binding --type=json -p='[{"op": "add", "path": "/subjects/3", "value": {"kind": "ServiceAccount","name":"user3","namespace":"test-namespace" } }]'
谢谢,
Kiruba

您可以通过在json类型的patch命令中指定操作来添加/替换/删除,默认情况下,patch命令将替换该值。下面的命令应适用于您的要求

kubectl patch rolebinding test-team-binding --type=json -p='[{"op": "add", "path": "/subjects/3", "value": {"kind": "ServiceAccount","name":"user3","namespace":"test-namespace" } }]'
谢谢,
Kiruba

我想将其追加到列表中,但此解决方案是将其追加到列表的第三部分。这不符合我的要求。您正在尝试追加到数组中,因此我们需要指定要追加此内容的位置。我刚刚选择了3以匹配您的预期输出,如果您还没有2个值,则可能会导致错误。在正常情况下,您可以使用/subjects/1插入到第一个位置,在修补过程中故障不会发生变化。我想将其附加到列表中,但此解决方案是将其附加到列表的第三部分。这不符合我的要求。您正在尝试附加到数组中,因此我们需要指定要附加此内容的位置。我刚刚选择了3以匹配您的预期输出,如果您还没有2个值,则可能会导致错误。在正常情况下,您可以使用/subjects/1插入到第一个位置,在补丁期间故障不会发生变化。