A Kubernetes Mutating Admission Controller Webhook that will copy labels from a namespace into pods automatically
A MutatingAdmissionWebhook will inject itself in the admission process to intercept changes in objects. In this case this will look at changes to Pod objects specifically. The webhook will take the pod info in, take the pod's namespace, and call the kubernetes api with a service account to gather the labels defined inside that namespace, and then re-apply (based on the LABELS variable) those labels to the pods.
Apply customizations to the yaml files inside the deploy folder as needed
- Find and replace all instances of
kube-systemwith something else if you want to install in a different namespace - Modify the
certificate.yamlfile as needed- Install certmanager and apply the
selfsigned.yaml(ifselfsigned-issuerdoesn't exist) - Alternatively setup a ClusterIssuer/Issuer using cert-manager and edit the
certificate.yamlwith customizations - Replace
kube-systemwith alternative namespace if installing elsewhere
- Install certmanager and apply the
- Modify the
deployment.yamlfile as needed with the following options- Change environment variables as needed (see Available Variables below)
- Change the replica and resources values as needed depending on how many targeted pods your evironment might have (the defaults are probably fine for anything under 10k)
- Modify the
mutatingwebhook.yamlas needed- Remove the
namespaceSelectorlines if you want all pods targeted (careful as this will include itself/kube-system) - Change the
namespaceSelectorto select the namespaces you want to target (see namespaceSelector docs)
- Remove the
Apply in the following order after making any needed modifications
kubectl apply -f certificate.yaml
kubectl apply -f service-account.yaml
kubectl apply -f deployment.yaml
kubectl apply -f mutatingwebhook.yamlThese variables can be set when running the docker image to customize the functionality
| Variable | Default | Description |
|---|---|---|
| DEBUG | False | If set to True will enable additional output including a dump of input and output objects for debugging purposes |
| LABELS | * | The labels to be copied into pods from their namespaces, if set to '*' will copy all, else can put in multiple values separated by commas |
- Check that the pod is up
- Verify the namespace targeting has been setup correctly
- Verify the namespace has the labels setup from the
namespaceSelector(if applicable) - Enable
DEBUGand check the logs
kubectl get po -l app=pod-labeler-webhookEnable the DEBUG variable if needed for additional output
kubectl logs -l app=pod-labeler-webhook