title: Kustomize: How to cause a generator to actually merge an object instead of duplicating
body: <p>I'm using <code>kustomize</code> to manage my <code>argocd</code> manifests.</p>
  <p>My structure looks like this:</p>
  <pre><code>argocd
  ├── base
  │   ├── argocd-ui-ingress.yaml
  │   └── kustomization.yaml
  └── overlays
      └── dev
          ├── argocd-cm-patch.yaml
          ├── argocd-repo-server-ksops-patch.yaml
          ├── ksops-secret-generator.yaml
          ├── kustomization.yaml
          └── secret-argocd-notifications.yaml
      └── prod
          ├── argocd-cm-patch.yaml
          ├── argocd-repo-server-ksops-patch.yaml
          ├── ksops-secret-generator.yaml
          ├── kustomization.yaml
          └── secret-argocd-notifications.yaml
  </code></pre>
  <p>This is my <code>overlays/dev/kustomization.yaml</code>  file:</p>
  <pre class="lang-yaml prettyprint-override"><code>apiVersion: kustomize.config.k8s.io/v1beta1
  kind: Kustomization
  
  resources:
  - https://raw.githubusercontent.com/argoproj/argo-cd/v2.3.2/manifests/install.yaml
  - ../../base
  
  patchesStrategicMerge:
    - argocd-cm-patch.yaml
    - argocd-repo-server-ksops-patch.yaml
  
  patches:
  - patch: |-
      - op: add
        path: /spec/rules/0/host
        value: argocd.foo.com
      - op: add
        path: /spec/tls/0/hosts/0
        value: argocd.foo.com
    target:
      kind: Ingress
      name: argocd-ui
  
  generators:
    - ksops-secret-generator.yaml
  </code></pre>
  <p>This is my <code>ksops-secret-generator.yaml</code> file:</p>
  <pre class="lang-yaml prettyprint-override"><code># Create a local Kubernetes Secret
  apiVersion: viaduct.ai/v1
  kind: ksops
  metadata:
    name: ksops-secret-generator
  files:
    - secret-argocd-notifications.yaml
  
  </code></pre>
  <p>And lastly, my <code>secret-argocd-notifications.yaml</code> looks like this:</p>
  <pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
  kind: Secret
  metadata:
      name: argocd-notifications-secret
      namespace: argocd
  data:
      foo: [ENC]...
  </code></pre>
  <p>Since it's a <code>sops</code>-encrypted file, I'm using <a href="https://github.com/viaduct-ai/kustomize-sops" rel="nofollow noreferrer">ksops</a> plugin to generate the usable Secret resource.</p>
  <p>The problem that I'm having currently, is that the remote resource I'm using (<a href="https://raw.githubusercontent.com/argoproj/argo-cd/v2.3.2/manifests/install.yaml" rel="nofollow noreferrer">https://raw.githubusercontent.com/argoproj/argo-cd/v2.3.2/manifests/install.yaml</a>), already includes a Secret named <code>argocd-notifications-secret</code>. So when I run <code>kustomize build overlays/dev --enable-alpha-plugins</code>, I get two Secret resources named <code>argocd-notifications-secret</code>. One from my local file that gets generated through the <code>ksops-secret-generator</code>, <em>and</em> the one that is included in the remote manifest.</p>
  <p>I tried to solve this by adding the <code>replace</code> (and also <code>merge</code>, got the same result) behavior annotation to my local file:</p>
  <pre class="lang-yaml prettyprint-override"><code>    annotations:
          kustomize.config.k8s.io/behavior: replace
  </code></pre>
  <p>But ended with this error:</p>
  <pre><code>Error: merging from generator &amp;{0xc000d4c0a0 &lt;nil&gt;}: id resid.ResId{Gvk:resid.Gvk{Group:&quot;&quot;, Version:&quot;v1&quot;, Kind:&quot;Secret&quot;, isClusterScoped:false}, Name:&quot;argocd-notifications-secret&quot;, Namespace:&quot;argocd&quot;} does not exist; cannot merge or replace
  
  </code></pre>
  <p>I also can't add my local file under <code>patchesStrategicMerge</code> section, because then it just adds the raw <code>sops</code> encrypted file (without using the <code>generator</code> at all).</p>
  <p>Is there a way to handle that scenario?</p>