Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a patch to add a kubernetes annotation

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.

like image 203
michas Avatar asked Apr 08 '19 12:04

michas


People also ask

How do I update my pod annotations?

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.

What is patch in Kubernetes?

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.


2 Answers

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.

like image 159
Efrat Levitan Avatar answered Oct 19 '22 01:10

Efrat Levitan


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

like image 43
abvarun226 Avatar answered Oct 19 '22 00:10

abvarun226