Is it possible to always mount a config map to all pods in Kubernetes

3/11/2019

I am trying to follow something similar to the Istio injection model where Istio is able to run a mutating admission webhook in order to auto inject their sidecar.

We would like to do something similar, but with some config maps. We have a need to mount config maps to all new pods in a given namespace, always mounted at the same path. Is it possible to create a mutating admission webhook that will allow me to mount this config map at the known path while admitting new pods?

docs to mutating webhooks: https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/

-- Bwvolleyball
kubernetes

1 Answer

3/11/2019

This should be entirely possible and is, in fact, an aligned use-case for a custom mutating admission webhook. Unfortunately, the official documentation on actually implementing them is somewhat sparse.

This is the most useful reference material I found when I was working on this mutating admission webhook.

The general process is as follows:

  1. Generate and deploy certificates as demonstrated here
  2. Deploy MutatingWebhookConfiguration to your cluster. This configuration allows us to tell the API server where to send the AdmissionReview objects. This is also where you should specify which operations on which API resources in which namespaces you want to target.
  3. Write the actual webhook server, which should accept AdmissionReview objects at the specified endpoint (/mutate is the convention) and return AdmissionResponse objects with the mutated object, as is shown here (note: in the linked example, I added an annotation to incoming pods that fit a certain criteria, while your application would add a field for the ConfigMap)
  4. Deploy the webhook server and expose it using normal methods (Deployment and Service, or whatever fits your use case). Make sure it's accessible at the location you specified in the configuration for the MutatingWebhookConfiguration

Hope this was enough information! Let me know if I left anything too vague / was unclear.

-- Monkeyanator
Source: StackOverflow