|
1 |
| -## The Architecture of Controllers |
2 |
| -Since controllers are in charge of meeting the desired state of the resources in Kubernetes, they somehow need to be informed about the changes on the resources and perform certain operations if needed. For this, controllers follow a special architecture to |
| 1 | +<div align="center"> |
| 2 | +<p align="center"> |
| 3 | +<img width="960" height="309" alt="final-k8s" src="https://github.com/user-attachments/assets/e5ef535e-a07a-4cd5-9fbd-926a0c62cf39" /> |
| 4 | +</p> |
3 | 5 |
|
4 |
| -1) observe the resources, |
5 |
| -2) inform any events (updating, deleting, adding) done on the resources, |
6 |
| -3) keep a local cache to decrease the load on API Server, |
7 |
| -4) keep a work queue to pick up events, |
8 |
| -5) run workers to perform reconciliation on resources picked up from work queue. |
| 6 | +[](https://www.youtube.com/watch?v=mAr62XBVbmg) |
| 7 | +</div> |
9 | 8 |
|
10 |
| - <a href="https://www.nakamasato.com/kubernetes-training/kubernetes-operator/client-go/informer/" > |
11 |
| - <h4 class="text-yellow-300 text-lg">Ref: Official Docs</h4> |
12 |
| - </a> |
13 |
| - <a href="https://github.com/kubernetes/community/blob/8cafef897a22026d42f5e5bb3f104febe7e29830/contributors/devel/controllers.md"> |
14 |
| - <h4 class="text-yellow-300 text-lg">Writing Controllers (Imp)</h4> |
15 |
| - </a> |
| 9 | +> 📽️ Click the image above to watch the full 25-minute walkthrough on YouTube. |
| 10 | +> It includes setup, explanation, CVE scan demo, and auto resource creation. |
16 | 11 |
|
17 |
| -# Factory & Informers |
18 | 12 |
|
19 |
| - |
| 13 | +# 🛡️ Kubernetes CVE Scanner with Custom Controller + Admission Webhook |
20 | 14 |
|
21 |
| -# Single Informer |
| 15 | +This project includes a **Kubernetes custom controller** that: |
| 16 | +- Automatically creates **Services** and **Ingresses** for every `Deployment`. |
| 17 | +- Integrates with a **Validating Admission Webhook** to scan container images using **Trivy**. |
| 18 | +- Optionally allows skipping CVE checks with an environment variable. |
22 | 19 |
|
23 |
| - |
| 20 | +--- |
24 | 21 |
|
25 |
| - |
| 22 | +## 🚀 Installation Guide |
26 | 23 |
|
| 24 | +### 1️⃣ Create a Kubernetes Cluster |
27 | 25 |
|
28 |
| -[//]: # (https://github.com/user-attachments/assets/851d2b2b-d268-4894-a15a-dbe8b501b3cc) |
| 26 | +Make sure you have a running Kubernetes cluster (like KinD, Minikube, or EKS). |
29 | 27 |
|
30 |
| -## Definition : Informer |
31 | 28 |
|
32 |
| -Informer monitors the changes of target resource. An informer is created for each of the target resources if you need to handle multiple resources (e.g. podInformer, deploymentInformer). |
| 29 | +### 2️⃣ Install `cert-manager` |
33 | 30 |
|
| 31 | +```bash |
| 32 | +kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.18.2/cert-manager.yaml |
| 33 | +``` |
| 34 | +This will install the necessary CRDs and controllers for certificate management. |
34 | 35 |
|
| 36 | +### 3️⃣ Deploy Trivy as a Service |
| 37 | +```bash |
| 38 | +kubectl apply -f docs/trivy-manifest/deployment.yml |
| 39 | +kubectl apply -f docs/trivy-manifest/service.yml |
| 40 | +``` |
| 41 | +Trivy will act as the backend scanner for your webhook. |
| 42 | +> Note: We are running using trivy client you can see the command [here](https://github.com/aquasecurity/trivy/discussions/2119) |
35 | 43 |
|
36 |
| -```md |
37 |
| -1) Initialize the Controller |
38 |
| - * The NewController function sets up the Kubernetes controller with a work queue, informer, and WebSocket connection. |
39 |
| - * It listens for Deployment events (Add, Update, Delete) and enqueues them. |
| 44 | +### 4️⃣ Create Cluster Role & Bindings |
| 45 | +* Grant required permissions for: |
| 46 | + - Deployments |
| 47 | + - Services |
| 48 | + - Secrets |
| 49 | + - Ingresses |
| 50 | + - ValidatingWebhookConfigurations |
| 51 | +```bash |
| 52 | +kubectl apply -f manifest/cluster-permission.yaml |
40 | 53 |
|
41 |
| -2) Run the Controller |
42 |
| - * The Run method waits for cache synchronization and starts the worker loop. |
43 |
| - * It continuously processes events from the work queue. |
| 54 | +``` |
44 | 55 |
|
45 |
| -3) Process Deployment Events |
46 |
| - * The processItem method retrieves Deployment events from the queue and determines the necessary action. |
47 |
| - * It fetches the Deployment details and handles errors, deletions, and updates. |
| 56 | +### 5️⃣ Deploy Controller + Webhook |
| 57 | +* This manifest includes: |
| 58 | + - Namespace |
| 59 | + - Deployment |
| 60 | + - Service |
| 61 | + - TLS Issuers + Certs |
| 62 | + - ValidatingWebhookConfiguration |
48 | 63 |
|
49 |
| -4) Handle Deployment Changes |
50 |
| - * `handleAdd`, `handleUpdate`, and `handleDel` respond to Deployment changes. |
51 |
| - * Updates track Replica count and Image changes and send logs via WebSocket. |
| 64 | +```ts |
| 65 | +kubectl apply -f manifest/k8s-controller-webhook.yaml |
| 66 | +``` |
| 67 | +### 6️⃣ Test Webhook |
| 68 | +```ts |
| 69 | +# contain cve |
| 70 | +$ kubectl apply -f manifest/webhook-example/initContainerDeployment.yml |
| 71 | +# look for first time it might fail (look at the logs of the application (k8s-custom-controller) and |
| 72 | +# see if they return a long list of CVE -> then start creating again (Working on to optimize) |
52 | 73 |
|
53 |
| -5) Send Updates via WebSocket |
54 |
| - * The updateLogs function logs and sends JSON messages about Deployment changes. |
55 |
| - * The WebSocket connection ensures real-time updates for external systems. |
| 74 | +# pure zero cve (does not contain cve) |
| 75 | +$ kubectl apply -f manifest/webhook-example/pureZeroCVE.yml |
| 76 | + |
| 77 | +# contain cve but bypass (i mean create the deployment even after having CVE) |
| 78 | +# due to this parameter `name: BYPASS_CVE_DENIED` set as yes or true |
| 79 | +$ kubectl apply -f manifest/webhook-example/ZeroInitCVE.yml |
| 80 | +``` |
| 81 | +> Todo: |
| 82 | +> Better docs and guide |
| 83 | +
|
| 84 | +<p align="center"> |
| 85 | +<img width="450" height="450" alt="image" src="https://github.com/user-attachments/assets/92fe17a5-bffe-469d-beb3-0769bb85d4a5" /> |
| 86 | +</p> |
| 87 | +
|
| 88 | +## Author |
| 89 | +
|
| 90 | +Built with 💙 by **Rahul Vishwakarma** |
| 91 | +
|
| 92 | +> Happy Scan-ing! |
0 commit comments