<p>I have an ongoing requirement to patch my nginx-ingress daemonset each time I wish to expose new TCP ports.  I have reviewed the documentation and I cannot understand the correct kubectl patch syntax to perform the patch.  An excerpt from the yaml follows:</p>
<pre><code>spec:
    revisionHistoryLimit: 10
    selector:
      matchLabels:
        name: nginx-ingress-microk8s
    template:
      metadata:
        creationTimestamp: null
        labels:
          name: nginx-ingress-microk8s
      spec:
        containers:
        - args:
          - /nginx-ingress-controller
          - --configmap=$(POD_NAMESPACE)/nginx-load-balancer-microk8s-conf
          - --default-backend-service=ingress/custom-default-backend
          - --tcp-services-configmap=$(POD_NAMESPACE)/nginx-ingress-tcp-microk8s-conf
          - --udp-services-configmap=$(POD_NAMESPACE)/nginx-ingress-udp-microk8s-conf
          - --ingress-class=public
          - ' '
          - --publish-status-address=127.0.0.1
          env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: metadata.namespace
          image: k8s.gcr.io/ingress-nginx/controller:v0.44.0
          imagePullPolicy: IfNotPresent
          lifecycle:
            preStop:
              exec:
                command:
                - /wait-shutdown
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5
          name: nginx-ingress-microk8s
          ports:
          - containerPort: 80
            hostPort: 80
            name: http
            protocol: TCP
          - containerPort: 443
            hostPort: 443
            name: https
            protocol: TCP
          - containerPort: 10254
            hostPort: 10254
            name: health
            protocol: TCP
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5
          resources: {}
</code></pre>
<p>I want to use kubectl patch to append another port definition under ports i.e.</p>
<pre><code>          - containerPort: 1234
            hostPort: 1234
            name: my-port-1234
            protocol: TCP
</code></pre>
<p>Patching a config map was simple using:</p>
<pre><code>kubectl patch configmap nginx-ingress-tcp-microk8s-conf -n ingress --type merge -p '{&quot;data&quot;:{&quot;1234&quot;:&quot;namespace1/api-connect:1234&quot;}}'
</code></pre>
<p>but I cannot understand how to amend the command to cope with the more complex update required for the Daemonset.</p>
<p>Any assistance gratefully received. Thanks</p>

<p>As already mentioned by David in the comment it is better to keep every change under version control.</p>
<p>But if you really need to do this, here is the command:</p>
<pre><code>kubectl patch ds -n ingress nginx-ingress-microk8s-controller --type='json' -p='[{&quot;op&quot;: &quot;add&quot;, &quot;path&quot;: &quot;/spec/template/spec/containers/0/ports/-&quot;, &quot;value&quot;:{&quot;containerPort&quot;:1234,&quot;name&quot;:&quot;my-port-1234&quot;,&quot;hostPort&quot;:1234,&quot;protocol&quot;:&quot;TCP&quot;}}]'
</code></pre>
<p>patch command is explained in k8s docs: <a href="https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/" rel="nofollow noreferrer">update-api-object-kubectl-patch</a>,
and the json type patch details are explained in <a href="https://www.rfc-editor.org/rfc/rfc6902" rel="nofollow noreferrer">rfc6902</a>.</p>
