I would like to write an mutating webhook to add a default ingress class to all ingress object, that do not explicitly provide one.
According to the examples I found I need to provide a proper json patch for the webhook to return.
I first tried my patches using kubectl:
$ kubectl patch ingress mying --type='json' -p='[{"op": "add", "path": "/metadata/annotations/key", "value":"value"}]'
The "" is invalid
Looks like this is not working when there is not already an annotations element present.
$ kubectl patch ingress mying --type='json' -p='[{"op": "add", "path": "/metadata/annotations", "value":{"key":"value"}}]'
ingress.extensions/kafka-monitoring-topics-ui patched
Creating the complete annotations element works fine, however in my case I need a key of kubernetes.io/ingress.class
which contains a slash.
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
This works fine when creating the annotation object. However, if there is already a some annotation present and I simply want to add one, it seems to be impossible to add one.
Simply using [{"op": "add", "path": "/metadata/annotations", "value":{"kubernetes.io/ingress.class":"value"}}]
removes all existing annotation, while something like '[{"op": "add", "path": "/metadata/annotations/kubernetes.io/ingress.class", "value": "value"}]
does not work because of the contained slash.
Long story short: What is the correct way to simply add a ingress class using a proper patch?
PS: Yes, I am aware of kubectl annotate
, but unfortunately that does not help with my webhook.
show that, using kubectl, the operator can change the annotations on a single pod. In other words, API server UPDATE requests for pods can contain changes to the annotations field.
If you communicate with Kubernetes clusters via kubectl CLI, then you might be familiar with the edit or apply subcommands. Just like these two commands, we are less familiar with the patch command of kubectl. The patch command enables you to change part of a resource specification, specifying the changed part on CLI.
much easier for me was to annotate
rather than patch
:
kubectl annotate ingress mying kubernetes.io/ingress.class=value
add --dry-run -o yaml
flags if you want to test it before applying the change.
Replace the forward slash (/
) in kubernetes.io/ingress.class
with ~1
.
Your command should look like this,
$ kubectl patch ingress mying --type='json' -p='[{"op": "add", "path": "/metadata/annotations/kubernetes.io~1ingress.class", "value":"nginx"}]'
Reference: RFC 6901 https://www.rfc-editor.org/rfc/rfc6901#section-3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With