<p>how can I add object to array via Kustomize? As a result I would like to have two <code>ServiceAccount</code>s added to <code>subjects</code>, like so:</p>
<pre><code>apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: binding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:auth-delegator
subjects:
  - kind: ServiceAccount
    name: name
    namespace: test1
  - kind: ServiceAccount
    name: name
    namespace: test2
</code></pre>
<p>I'm trying with that patch:</p>
<pre><code>- op: add
  path: &quot;/subjects/0&quot;
  value:
    kind: ServiceAccount
    name: name
    namespace: test1
</code></pre>
<p>And another patch for second environment:</p>
<pre><code>- op: add
  path: &quot;/subjects/1&quot;
  value:
    kind: ServiceAccount
    name: name
    namespace: test2
</code></pre>
<p>But in result I'm getting duplicated <code>subjects</code>, so of course it is wrong:</p>
<pre><code>apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: binding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:auth-delegator
subjects:
  - kind: ServiceAccount
    name: name
    namespace: test1 // the same...
  - kind: ServiceAccount
    name: name
    namespace: test1 // ...as here
</code></pre>
<p>What would be a proper way to add it?</p>

Murakami
<p>If I start with a ClusterRoleBinding that looks like this in <code>crb.yaml</code>:</p>
<pre><code>apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: binding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:auth-delegator
subjects: []
</code></pre>
<p>And I create a <code>kustomization.yaml</code> file like this:</p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - crb.yaml

patches:
  - target:
      kind: ClusterRoleBinding
      name: binding
    patch: |
      - op: add
        path: /subjects/0
        value:
          kind: ServiceAccount
          name: name
          namespace: test1

  - target:
      kind: ClusterRoleBinding
      name: binding
    patch: |
      - op: add
        path: /subjects/1
        value:
          kind: ServiceAccount
          name: name
          namespace: test2
</code></pre>
<p>Then I get as output:</p>
<pre><code>apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: binding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:auth-delegator
subjects:
- kind: ServiceAccount
  name: name
  namespace: test1
- kind: ServiceAccount
  name: name
  namespace: test2
</code></pre>
<p>Which is I think what you're looking for. Does this help? Note that instead of explicitly setting an index in the <code>path</code>, like:</p>
<pre><code>path: /subjects/0
</code></pre>
<p>We can instead specify:</p>
<pre><code>path: /subjects/-
</code></pre>
<p>Which means &quot;append to the list&quot;, and in this case will generate the same output.</p>
