Skip to content

Build a simple yet powerful Kubernetes MutatingAdmissionWebhook that seamlessly intercepts and mutates Pod/Deployment creation requests before they are persisted to etcd.

License

Notifications You must be signed in to change notification settings

andreistefanciprian/pod-labeler-k8s-webhook

Repository files navigation

Pod Labeler Mutating Webhook

Overview

This project implements a Kubernetes MutatingAdmissionWebhook, serving as an admission controller in the Kubernetes cluster. The webhook intercepts Pod creation requests and automatically adds an extra label to Pods (eg: webhook=auto-labeled) if their target namespace has the label pod-labeler=enabled.

Additionally, the webhook code can be easily modified to perform various other changes to Pod objects, such as altering their names, adding security parameters or injecting a sidecar.

Admission Controllers and webhooks in the K8s Architecture

Admission Controllers and webhooks in K8s Architecture

Prerequisites

Before getting started with the webhook, ensure that the following tools and resources are available:

  • Docker: The webhook runs as a container, so Docker is necessary.
  • Kubernetes Cluster: You'll need a running Kubernetes cluster where the webhook will be deployed.
    • Use my terraform code to build a Private GKE Cluster for this purpose. Or use Kind or Docker-Desktop to build a local cluster
  • cert-manager: Required for generating TLS certificates for the webhook and injecting caBundle in webhook configuration.
  • Go: The webhook is written in Go.
  • jq: Used for parsing and manipulating JSON data in the Makefile.
  • Makefile: The project uses a Makefile for automation and building. Understanding Makefile syntax will help you work with the provided build and deployment scripts.

Build and Run the Webhook

Build, Register, Deploy and Test the webhook using the provided tasks:

  1. Build and push the Docker image to the container registry:

    make build
    
  2. Deploy and Register webhook:

    make install
    
  3. Test webhook:

    # check logs while creating test Pods and Deployments
    kubectl logs -l app.kubernetes.io/name=pod-labeler -f
    
    # create Pods and Deployments
    make test
    
    # cleanup test pods
    make test-clean
    
  4. Unregister and Remove the webhook:

    make uninstall
    

Feel free to adjust the tasks and configurations as needed to fit your specific environment.

License

This project is licensed under the MIT License. Feel free to use and modify it according to your requirements.

About

Build a simple yet powerful Kubernetes MutatingAdmissionWebhook that seamlessly intercepts and mutates Pod/Deployment creation requests before they are persisted to etcd.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published