<p>I have the following Ingress resource:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: main-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "86400"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "86400"

spec:
  tls:
  - secretName: the-secret
    hosts:
      - sample.domain.com
      - sample2.domain.com
      - rabbit.domain.com
      - hub.domain.com
      - grafana.domain.com

  rules:

  - host: sample.domain.com
    http:
      paths:
      - path: /
        backend:
          serviceName: fe-srvc
          servicePort: 80
      - path: /api
        backend:
          serviceName: be-srvc
          servicePort: 80

  - host: sample2.domain.com
    http:
      paths:
      - path: /
        backend:
          serviceName: fe2-srvc
          servicePort: 80
      - path: /api
        backend:
          serviceName: be-srvc
          servicePort: 80

## The Extra Services ###
  - host: rabbit.domain.com
    http:
      paths:
      - path: /
        backend:
          serviceName: rabbitmq-srvc
          servicePort: 80
</code></pre>

<p>and I want to patch it after it is deployed.</p>

<p>So I use this, to try and replace the <code>be-srvc</code> value with <code>some-srvc</code> :</p>

<pre><code>kubectl patch ing/main-ingress --patch '{ "spec" : { "rules": [{"http":{"paths":[ {"- path":"/"},{"backend":{"serviceName":"other-srvc"}},{"servicePort":"80"} ] }}]}}'
</code></pre>

<p>and I get this error:</p>

<pre><code>The Ingress "main-ingress" is invalid:
* spec.rules[0].http.backend.serviceName: Required value
* spec.rules[0].http.backend.servicePort: Invalid value: 0: must be between 1 and 65535, inclusive
</code></pre>

<p>Any insight would be appreciated!</p>

<p>Your patch has a number of problems; for example <code>"- path"</code> instead of <code>"path"</code> but also incorrect referencing of object levels. However, even if you fixed the mistakes this would not work as intended. Let's see why.</p>

<p><code>kubectl patch</code> is a request for a <strong><em>strategic merge patch</em></strong>. When patching arrays, like the <code>.spec.rules</code> and <code>.spec.rules.http.paths</code> in this case, a <em>strategic merge patch</em> can use the defined <em>patch type</em> and <em>merge patch merge key</em> for the object to do The Right Thing. However, in case of the Ingress object no one bothered to define these. This means that any patch will overwrite the entire object; it will not be a nice merge that one is hoping for.</p>

<p>To accomplish the particular change referred to in the question you can do:</p>

<pre><code>kubectl get ing/main-ingress -o json \ 
  | jq '(.spec.rules[].http.paths[].backend.serviceName | select(. == "be-srvc")) |= "some-srvc"' \
  | kubectl apply -f -
</code></pre>

<p>The above will change all occurrences of the <code>be-srvc</code> Service to <code>some-srvc</code>. Keep in mind that there is a short race condition here: if the Ingress is modified after <code>kubectl get</code> ran the change will fail with the error <code>Operation cannot be fulfilled on ingresses.extensions "xx": the object has been modified</code>; to handle that case you need implement a retry logic.</p>

<p>If the indexes are known in the arrays mentioned above you can accomplish the patch directly:</p>

<pre><code>kubectl patch ing/main-ingress --type=json \
  -p='[{"op": "replace", "path": "/spec/rules/0/http/paths/1/backend/serviceName", "value":"some-srvc"}]'
kubectl patch ing/main-ingress --type=json \
  -p='[{"op": "replace", "path": "/spec/rules/1/http/paths/1/backend/serviceName", "value":"some-srvc"}]'
</code></pre>

<p>The two commands above will change the backends for <code>sample.domain.com/api</code> and <code>sample2.domain.com/api</code> to <code>some-srvc</code>.</p>

<p>The two commands can also be combined like this:</p>

<pre><code>kubectl patch ing/main-ingress --type=json \
  -p='[{"op": "replace", "path": "/spec/rules/0/http/paths/1/backend/serviceName", "value":"some-srvc"}, {"op": "replace", "path": "/spec/rules/1/http/paths/1/backend/serviceName", "value":"some-srvc"}]'
</code></pre>

<p>This has the same effect and as an added bonus there is no race condition here; the patch guaranteed to be atomic.</p>
