<p>I have a GKE/GCE ingress file which has 2 hosts.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
  - host: myfirstdomain # &lt;-------- override this via kustomize
    http:
      paths:
      - path: /abc
        backend:
          serviceName: abc
          servicePort: abc
      - path: /def
        backend:
          serviceName: def
          servicePort: def
      - path: /ghi
        backend:
          serviceName: ghi
          servicePort: ghi
  - host: myseconddomain # &lt;-------- override this via kustomize
    http:
      paths:
      - backend:
          serviceName: xyz
          servicePort: xyz
</code></pre>
<p>I want to declare the <code>host</code> values via kustomize so that i can specify different <code>host</code> values for different environments.</p>
<p>My overlay patch file for dev environment looks like this:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
  - host: dev1.example.com
  - host: dev2.example.com
</code></pre>
<p>However, the output of running <code>kustomize build overlays/dev</code> looks like this:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
  - host: dev1.example.com
  - host: dev2.example.com
</code></pre>
<hr />
<h1>All other specs are lost.</h1>
<p>It ends up replacing the <strong>entire</strong> <code>host</code> blocks instead of replacing just the <code>host</code> lines.</p>
<p>How can i solve this? I am using:</p>
<ul>
<li><strong>kustomize</strong> version <code>v4.1.2</code></li>
<li><strong>kubectl</strong> version <code>v1.21.0</code></li>
</ul>

<p>You can not do that using <strong><a href="https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/patchesstrategicmerge/" rel="noreferrer">strategic merge</a></strong> since the <code>spec.rules</code> is an array of items. You have to use <strong><a href="https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/patchesjson6902/" rel="noreferrer">json merge</a></strong> by providing the exact paths to be impacted.</p>
<p>You can create a separate patch file for json merge like below:</p>
<pre class="lang-yaml prettyprint-override"><code># overlays/dev/ingress-json-patch.yaml

- op: replace
  path: /spec/rules/0/host
  value: dev1.example.com
- op: replace
  path: /spec/rules/1/host
  value: dev2.example.com
</code></pre>
<p>Then refer to that patch file by adding the following into your <code>kustomization.yaml</code>:</p>
<pre class="lang-yaml prettyprint-override"><code># overlays/dev/kustomization.yaml

patchesJson6902:
- target:
    group: extensions
    version: v1beta1
    kind: Ingress
    name: my-ingress
  path: overlays/dev/ingress-json-patch.yaml
</code></pre>
