Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monokle policies as CRDs #1

Merged
merged 26 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e86aede
feat: add basic policy CRDs
f1ames Sep 1, 2023
474756f
chore: add test script for CRDs informer
f1ames Sep 4, 2023
ecbce85
feat: integrate policy CRDs into webhook [WIP]
f1ames Sep 6, 2023
b1b7b3d
feat: integrate policy CRDs into webhook
f1ames Sep 25, 2023
404ab26
fix: fix deployment script for local testing
f1ames Sep 25, 2023
28a1b30
fix: read namespace name from within webhook pod
f1ames Sep 26, 2023
c7b92b0
refactor: improve logging and cleanup code
f1ames Sep 26, 2023
a723b2e
chore: update README file
f1ames Sep 27, 2023
7cc0f97
refactor: rework project structure
f1ames Sep 27, 2023
f4cbd76
fix: add minor code adjustements
f1ames Sep 27, 2023
80249e1
fix: rework admission controller webhook to not be namespace bound
f1ames Sep 27, 2023
6f908b3
feat: introduce MonoklePolicyBinding [WIP]
f1ames Sep 28, 2023
e65bf62
refactor: draft ValidatorManager class
f1ames Sep 28, 2023
e6ce712
refactor: add support for policy binding [WIP]
f1ames Sep 29, 2023
13a9515
fix: fix policy binding CRD schema
f1ames Sep 29, 2023
15b48d3
feat: add namespace filtering
f1ames Oct 2, 2023
bf7b56f
refactor: deploy admission controller to dedicated namespace
f1ames Oct 2, 2023
7da3da5
fix: move ignored namespace check to very beginning of webhook handler
f1ames Oct 2, 2023
9d64438
feat: Send k8s warnings on 'Warn' action
f1ames Oct 2, 2023
579a0b3
fix: make admission controller match all resource kinds
f1ames Oct 3, 2023
db7a2c8
fix: mark specific fields as required in Monokle CRDs
f1ames Oct 3, 2023
4bd6a00
chore: cleanup outdated comments
f1ames Oct 3, 2023
d959809
fix: use wildcard for webhook scope by default
f1ames Oct 3, 2023
586aeed
feat: block resource-link plugin
f1ames Oct 3, 2023
32378af
chore: fix imports
f1ames Oct 3, 2023
7b86f83
fix: fix routing initialization
f1ames Oct 3, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/.idea/
webhook.yaml
deployment.yaml
k8s/manifests/webhook.yaml
k8s/manifests/deployment.yaml
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
FROM node:18.16.0-bullseye-slim

WORKDIR /workdir
COPY kac-api/package*.json ./
COPY admission-webhook/package*.json ./
RUN npm ci --ignore-scripts

COPY ./kac-api/src ./src
COPY ./kac-api/tsconfig.json ./
COPY ./admission-webhook/src ./src
COPY ./admission-webhook/tsconfig.json ./

CMD ["npm", "run", "prod"]
EXPOSE 8443
99 changes: 76 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,78 +1,131 @@
# K8s Admission Controller Demo
# Monokle Admission Controller

This was heavily inspired by https://github.com/stackrox/admission-controller-webhook-demo.
Monokle Admission Controller is an admission controller for validating resources in the cluster.

And since it is hackaton PoC there was massive amount of duct tape applied in some places ;)
## Development

## Prerequisites
### Prerequisites

* Minikube (or any other K8s cluster running)
* kubectl
* Skaffold
* nodejs

## Running
### Running

### Minikube
#### Minikube

Start Minikube:

```bash
minikube start --uuid 00000000-0000-0000-0000-000000000001 --extra-config=apiserver.enable-admission-plugins=ValidatingAdmissionWebhook
```

Every resource will be deployed to `webhook-demo` namespace, to watch it you can run:
#### Deploying

```bash
watch kubectl -n webhook-demo get all,ValidatingWebhookConfiguration,MutatingWebhookConfiguration
./scripts/deploy.sh
```

### Deploying
Namespaced resources (webhook server) will be deployed to dedicated `monokle-admission-controller` namespace, to watch it you can run:

```bash
./deploy.sh
watch kubectl -n monokle-admission-controller get all,CustomResourceDefinition,ValidatingWebhookConfiguration,MutatingWebhookConfiguration
```

After it runs, the result should be something like:

```bash
NAME READY STATUS RESTARTS AGE
pod/webhook-server-55dd5d6f44-lwwnw 1/1 Running 0 11s
NAME READY STATUS RESTARTS AGE
pod/webhook-server-7cd54c9fcf-wdkdn 0/1 Error 1 (13s ago) 25s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/webhook-server ClusterIP 10.96.18.123 <none> 443/TCP 11s
service/webhook-server ClusterIP 10.97.66.194 <none> 443/TCP 25s

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/webhook-server 1/1 1 1 11s
deployment.apps/webhook-server 0/1 1 0 25s

NAME DESIRED CURRENT READY AGE
replicaset.apps/webhook-server-55dd5d6f44 1 1 1 11s
replicaset.apps/webhook-server-7cd54c9fcf 1 1 0 25s

NAME CREATED AT
customresourcedefinition.apiextensions.k8s.io/policies.monokle.com 2023-10-02T12:17:02Z
customresourcedefinition.apiextensions.k8s.io/policybindings.monokle.com 2023-10-02T12:17:02Z

NAME WEBHOOKS AGE
validatingwebhookconfiguration.admissionregistration.k8s.io/demo-webhook 1 6s
validatingwebhookconfiguration.admissionregistration.k8s.io/demo-webhook 1 17s
```

For getting info about CRDs:

```bash
kubectl get crd
kubectl describe crd policies.monokle.com
kubectl describe crd policybindings.monokle.com
```

#### Testing

First you need to create policy resource, for example:

```bash
kubectl apply -f examples/policy-sample-1.yaml
kubectl apply -f examples/policy-sample-2.yaml
```

Then it needs to be bind to be used for validation. Either without scope (globally to all, but ignored namespaces) or with `matchResource` field:

```bash
kubectl apply -f examples/policy-binding-sample-1.yaml
kubectl apply -f examples/policy-binding-sample-2.yaml
kubectl apply -f examples/policy-binding-sample-3.yaml
```

### Testing
You can inspect deployed policies with:

You can try to create sample resource and see webhook response:
```bash
kubectl get policy
kubectl describe policy

kubectl get policybinding
kubectl describe policybinding
```

Then you can try to create sample resource and see webhook response:

```bash
kubectl -n webhook-demo create -f examples/pod-with-conflict.yaml
kubectl apply -f examples/pod-valid.yaml
kubectl apply -f examples/pod-warning.yaml
kubectl apply -f examples/pod-errors.yaml
```

### Iterating
#### Iterating

After everything is running you can allow hot-reload by running:

```bash
skaffold dev
skaffold dev -f k8s/skaffold.yaml
```

**Important**: Skaffold will recreate deployment on every change so make sure that webhook pod doesn't get rejected by it's previous version (via Validation Admission Controller).

## Refs
You can also do manual clean-up and re-run `./deploy.sh` script again:

```bash
kubectl delete all -n monokle-admission-controller --all && \
kubectl delete validatingwebhookconfiguration.admissionregistration.k8s.io/demo-webhook -n monokle-admission-controller && \
kubectl delete namespace monokle-admission-controller && \
kubectl delete namespace nstest1 && \
kubectl delete namespace nstest2 && \
kubectl delete crd policies.monokle.com && \
kubectl delete crd policybindings.monokle.com
```

### Refs

* https://kubernetes.io/blog/2019/03/21/a-guide-to-kubernetes-admission-controllers/
* https://github.com/stackrox/admission-controller-webhook-demo/tree/master
* https://www.witodelnat.eu/blog/2021/local-kubernetes-development
* https://minikube.sigs.k8s.io/docs/tutorials/using_psp/
* https://minikube.sigs.k8s.io/docs/tutorials/using_psp/
* https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/
* https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/
98 changes: 0 additions & 98 deletions README.old.md

This file was deleted.

File renamed without changes.
Loading