<p>I would like to write an <a href="https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#mutatingadmissionwebhook" rel="noreferrer">mutating webhook</a> to add a default ingress class to all ingress object, that do not explicitly provide one.</p>

<p>According to the <a href="https://github.com/kubernetes/kubernetes/blob/master/test/images/webhook/addlabel.go#L29" rel="noreferrer">examples</a> I found I need to provide a proper json patch for the webhook to return.</p>

<p>I first tried my patches using kubectl:</p>

<pre><code>$ kubectl patch ingress mying --type='json' -p='[{"op": "add", "path": "/metadata/annotations/key", "value":"value"}]'
The  "" is invalid
</code></pre>

<p>Looks like this is not working when there is not already an annotations element present.</p>

<pre><code>$ kubectl patch ingress mying --type='json' -p='[{"op": "add", "path": "/metadata/annotations", "value":{"key":"value"}}]'
ingress.extensions/kafka-monitoring-topics-ui patched
</code></pre>

<p>Creating the complete annotations element works fine, however in my case I need a key of <code>kubernetes.io/ingress.class</code> which contains a slash.</p>

<pre><code>kubectl patch ingress mying --type='json' -p='[{"op": "add", "path": "/metadata/annotations", "value":{"kubernetes.io/ingress.class":"value"}}]'
ingress.extensions/kafka-monitoring-topics-ui patched
</code></pre>

<p>This works fine when creating the annotation object. However, if there is already a some annotation present and I simply want to <em>add</em> one, it seems to be impossible to add one.</p>

<p>Simply using <code>[{"op": "add", "path": "/metadata/annotations", "value":{"kubernetes.io/ingress.class":"value"}}]</code> removes all existing annotation, while something like <code>'[{"op": "add", "path": "/metadata/annotations/kubernetes.io/ingress.class", "value": "value"}]</code> does not work because of the contained slash.</p>

<p><strong>Long story short: What is the correct way to simply add a ingress class using a proper patch?</strong></p>

<p>PS: Yes, I am aware of <code>kubectl annotate</code>, but unfortunately that does not help with my webhook.</p>

<p>much easier for me was to <code>annotate</code> rather than <code>patch</code>:</p>

<pre><code>kubectl annotate ingress mying kubernetes.io/ingress.class=value
</code></pre>

<p>add <code>--dry-run -o yaml</code> flags if you want to test it before applying the change.</p>
