Kubernetes Webhook - get the spec of the object to be created

11/1/2021

I have a Kubernetes (Mutating) Webhook that is called upon Namespace creation. I would like to add an annotation to the namespace, and so the jsonpatch object that I return is [{"op": "add", "path": "/metadata/annotations/my_annotation", "value": "enabled"}].

However, sometimes the Namespace object to be created doesn't have any annotations, and such objects would fail to be created with the following error message: Internal error occurred: add operation does not apply: doc is missing path: "/metadata/annotations/my_annotation": missing value.

Of course, in such cases I could create the entire annotations field, but I need to know whether the Namespace to be created has any annotations or not. The fields I get in the Admission Review Request are:

{
    UID,
    Kind,
    Resource,
    SubResource,
    RequestKind,
    RequestResource,
    RequestSubResource,
    Name,
    Namespace,
    Operation,
    UserInfo: {Username, UID, Groups, Extra}, 
    Object,
    OldObject,
    Options,
    DryRun
}

None of them is the Namespace spec (not even "Object").

Is there a way to get the spec within the webhook?

-- Bolchojeet
kubernetes
webhooks

0 Answers