2022-08-25 18:32:40 +00:00
# Developer Instructions
2022-08-29 16:32:56 +00:00
This document covers basic needs to work with Kyverno codebase.
It contains instructions to build, run, and test Kyverno.
2023-03-24 15:24:51 +00:00
- [Open project in devcontainer ](#open-project-in-devcontainer-recommended )
2022-08-29 16:32:56 +00:00
- [Tools ](#tools )
2022-08-29 20:36:04 +00:00
- [Building local binaries ](#building-local-binaries )
2022-09-04 13:31:24 +00:00
- [Building kyvernopre locally ](#building-kyvernopre-locally )
- [Building kyverno locally ](#building-kyverno-locally )
- [Building cli locally ](#building-cli-locally )
2022-08-30 13:15:13 +00:00
- [Building local images ](#building-local-images )
2022-09-04 13:31:24 +00:00
- [Building local images with ko ](#building-local-images-with-ko )
2022-08-30 16:10:38 +00:00
- [Pushing images ](#pushing-images )
2023-01-19 10:31:43 +00:00
- [Images tagging strategy ](#images-tagging-strategy )
2022-09-04 13:31:24 +00:00
- [Pushing images with ko ](#pushing-images-with-ko )
2022-08-31 08:06:12 +00:00
- [Deploying a local build ](#deploying-a-local-build )
2022-09-04 13:31:24 +00:00
- [Create a local cluster ](#create-a-local-cluster )
- [Build and load local images ](#build-and-load-local-images )
- [Deploy with helm ](#deploy-with-helm )
2022-08-31 14:19:37 +00:00
- [Code generation ](#code-generation )
2022-09-04 13:31:24 +00:00
- [Generating kubernetes API client ](#generating-kubernetes-api-client )
- [Generating API deep copy functions ](#generating-api-deep-copy-functions )
- [Generating CRD definitions ](#generating-crd-definitions )
- [Generating API docs ](#generating-api-docs )
2022-09-05 15:08:07 +00:00
- [Generating helm charts CRDs ](#generating-helm-charts-crds )
- [Generating helm charts docs ](#generating-helm-charts-docs )
2022-10-10 09:14:17 +00:00
- [Debugging local code ](#debugging-local-code )
2024-04-23 05:47:16 +00:00
- [Profiling ](#profiling )
- [Other Topics ](#other-topics )
- [Selecting Issues ](#selecting-issues )
2022-08-29 16:32:56 +00:00
2023-03-24 15:24:51 +00:00
## Open project in devcontainer (recommended)
- Clone the project to your local machine.
- Make sure that you have the Visual Studio Code editor installed on your system.
2023-06-06 16:17:23 +00:00
- Make sure that you have wsl(Ubuntu preferred) and Docker installed on your system and on wsl too (docker.sock (UNIX socket) file is necessary to enable devcontainer to communicate with docker running in host machine).
2023-03-24 15:24:51 +00:00
2023-06-06 16:17:23 +00:00
- Open the project in Visual Studio Code, once the project is opened hit F1 and type wsl, now click on "Reopen in WSL".
2023-03-24 15:24:51 +00:00
- If you haven't already done so, install the **Dev Containers** extension in Visual Studio Code.
- Once the extension is installed, you should see a green icon in the bottom left corner of the window.
2023-06-06 16:17:23 +00:00
- After you have installed Dev Containers extension, it should automatically detect the .devcontainer folder inside the project opened in wsl, and should suggest you to open the project in container.
2023-03-24 15:24:51 +00:00
- If it doesn't suggest you, then press < kbd > Ctrl</ kbd > + < kbd > Shift</ kbd > + < kbd > p</ kbd > and search "reopen in container" and click on it.
- If everything goes well, the project should be opened in your devcontainer.
- Then follow the steps as mentioned below to configure the project.
2022-08-29 16:32:56 +00:00
## Tools
Building and/or testing Kyverno requires additional tooling.
We use `make` to simplify installing the tools we use.
Tools will be installed in the `.tools` folder when possible, this allows keeping installed tools local to the Kyverno repository.
The `.tools` folder is ignored by `git` and binaries should not be committed.
2022-08-29 20:36:04 +00:00
> **Note**: If you don't install tools, they will be downloaded/installed as necessary when running `make` targets.
You can manually install tools by running:
```console
2022-08-29 16:32:56 +00:00
make install-tools
```
To remove installed tools, run:
2022-08-29 20:36:04 +00:00
```console
2022-08-29 16:32:56 +00:00
make clean-tools
```
2022-08-29 20:36:04 +00:00
## Building local binaries
The Kyverno repository contains code for three different binaries:
- [`kyvernopre` ](#building-kyvernopre-locally ):
Binary to update/cleanup existing resources in clusters. This is typically run as an init container before Kyverno controller starts.
- [`kyverno` ](#building-kyverno-locally ):
The Kyverno controller binary.
- [`cli` ](#building-cli-locally ):
The Kyverno command line interface.
> **Note**: You can build all binaries at once by running `make build-all`.
### Building kyvernopre locally
To build `kyvernopre` binary on your local system, run:
```console
2023-01-29 20:49:45 +00:00
make build-kyverno-init
2022-08-29 20:36:04 +00:00
```
2023-01-29 20:49:45 +00:00
The binary should be created at `./cmd/kyverno-init/kyvernopre` .
2022-08-29 20:36:04 +00:00
### Building kyverno locally
To build `kyverno` binary on your local system, run:
```console
make build-kyverno
```
The binary should be created at `./cmd/kyverno/kyverno` .
### Building cli locally
To build `cli` binary on your local system, run:
```console
make build-cli
```
The binary should be created at `./cmd/cli/kubectl-kyverno/kubectl-kyverno` .
2022-08-29 16:32:56 +00:00
2022-08-30 13:15:13 +00:00
## Building local images
In the same spirit as [building local binaries ](#building-local-binaries ), you can build local docker images instead of local binaries.
2022-11-14 15:34:46 +00:00
`ko` is used to build images, please refer to [Building local images with ko ](#building-local-images-with-ko ).
2022-09-04 13:31:24 +00:00
2022-09-09 08:30:29 +00:00
### Image tags
Building images uses repository tags. To fetch repository tags into your fork run the following commands:
```sh
git remote add upstream https://github.com/kyverno/kyverno
git fetch upstream --tags
```
2022-08-30 13:15:13 +00:00
### Building local images with ko
When building local images with ko you can't specify the registry used to create the image names. It will always be `ko.local` .
> **Note**: You can build all local images at once by running `make ko-build-all`.
#### Building kyvernopre image locally
To build `kyvernopre` image on your local system, run:
```console
2023-01-29 20:49:45 +00:00
make ko-build-kyverno-init
2022-08-30 13:15:13 +00:00
```
The resulting image should be available locally, named `ko.local/github.com/kyverno/kyverno/cmd/initcontainer` .
#### Building kyverno image locally
To build `kyverno` image on your local system, run:
```console
make ko-build-kyverno
```
The resulting image should be available locally, named `ko.local/github.com/kyverno/kyverno/cmd/kyverno` .
#### Building cli image locally
To build `cli` image on your local system, run:
```console
make ko-build-cli
```
The resulting image should be available locally, named `ko.local/github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno` .
2022-08-30 16:10:38 +00:00
## Pushing images
Pushing images is very similar to [building local images ](#building-local-images ), except that built images will be published on a remote image registry.
2022-11-14 15:34:46 +00:00
`ko` is used to build and publish images, please refer to [Pushing images with ko ](#pushing-images-with-ko ).
2022-08-30 16:10:38 +00:00
When pushing images you can specify the registry you want to publish images to by setting the `REGISTRY` environment variable (default value is `ghcr.io` ).
2023-01-19 10:31:43 +00:00
### Images tagging strategy
When publishing images, we are using the following strategy:
- All published images are tagged with `latest` . Images tagged with `latest` should not be considered stable and can come from multiple release branches or main.
- In addition to `latest` , dev images are tagged with the following pattern `<major>.<minor>-dev-N-<git hash>` where `N` is a two-digit number beginning at one for the major-minor combination and incremented by one on each subsequent tagged image.
- In addition to `latest` , release images are tagged with the following pattern `<major>.<minor>.<patch>-<pre release>` . The pre release part is optional and only applies to pre releases (`-beta.1`, `-rc.2` , ...).
2022-08-30 16:10:38 +00:00
### Pushing images with ko
Authenticating to the remote registry is done automatically in the `Makefile` with `ko login` .
To allow authentication you will need to set `REGISTRY_USERNAME` and `REGISTRY_PASSWORD` environment variables before invoking targets responsible for pushing images.
> **Note**: You can push all images at once by running `make ko-publish-all` or `make ko-publish-all-dev`.
#### Pushing kyvernopre image
To push `kyvernopre` image on a remote registry, run:
```console
# push stable image
2023-01-29 20:49:45 +00:00
make ko-publish-kyverno-init
2022-08-30 16:10:38 +00:00
```
or
```console
# push dev image
2023-01-29 20:49:45 +00:00
make ko-publish-kyverno-init-dev
2022-08-30 16:10:38 +00:00
```
The resulting image should be available remotely, named `ghcr.io/kyverno/kyvernopre` (by default, if `REGISTRY` environment variable was not set).
#### Pushing kyverno image
To push `kyverno` image on a remote registry, run:
```console
# push stable image
make ko-publish-kyverno
```
or
```console
# push dev image
make ko-publish-kyverno-dev
```
The resulting image should be available remotely, named `ghcr.io/kyverno/kyverno` (by default, if `REGISTRY` environment variable was not set).
#### Pushing cli image
To push `cli` image on a remote registry, run:
```console
# push stable image
make ko-publish-cli
```
or
```console
# push dev image
make ko-publish-cli-dev
```
The resulting image should be available remotely, named `ghcr.io/kyverno/kyverno-cli` (by default, if `REGISTRY` environment variable was not set).
2022-08-31 08:06:12 +00:00
## Deploying a local build
After [building local images ](#building-local-images ), it is often useful to deploy those images in a local cluster.
2022-09-04 13:31:24 +00:00
We use [KinD ](https://kind.sigs.k8s.io/ ) to create local clusters easily, and have targets to:
- [Create a local cluster ](#create-a-local-cluster )
- [Build and load local images ](#build-and-load-local-images )
- [Deploy with helm ](#deploy-with-helm )
2022-08-31 08:06:12 +00:00
### Create a local cluster
If you already have a local KinD cluster running, you can skip this step.
To create a local KinD cluster, run:
```console
make kind-create-cluster
```
2024-02-07 11:02:31 +00:00
You can override the k8s version by setting the `KIND_IMAGE` environment variable (default value is `kindest/node:v1.29.1` ).
2022-08-31 08:06:12 +00:00
You can also override the KinD cluster name by setting the `KIND_NAME` environment variable (default value is `kind` ).
### Build and load local images
To build local images and load them on a local KinD cluster, run:
```console
# build kyvernopre image and load it in KinD cluster
2023-01-29 20:49:45 +00:00
make kind-load-kyverno-init
2022-08-31 08:06:12 +00:00
```
or
```console
# build kyverno image and load it in KinD cluster
make kind-load-kyverno
```
or
```console
# build kyvernopre and kyverno images and load them in KinD cluster
make kind-load-all
```
You can override the KinD cluster name by setting the `KIND_NAME` environment variable (default value is `kind` ).
### Deploy with helm
To build local images, load them on a local KinD cluster, and deploy helm charts, run:
```console
# build images, load them in KinD cluster and deploy kyverno helm chart
make kind-deploy-kyverno
```
or
```console
# deploy kyverno-policies helm chart
make kind-deploy-kyverno-policies
```
or
```console
# build images, load them in KinD cluster and deploy helm charts
make kind-deploy-all
```
This will build local images, load built images in every node of the KinD cluster, and deploy `kyverno` and/or `kyverno-policies` helm charts in the cluster (overriding image repositories and tags).
You can override the KinD cluster name by setting the `KIND_NAME` environment variable (default value is `kind` ).
2022-08-31 14:19:37 +00:00
## Code generation
We are using code generation tools to create the following portions of code:
- [Generating kubernetes API client ](#generating-kubernetes-api-client )
- [Generating API deep copy functions ](#generating-api-deep-copy-functions )
- [Generating CRD definitions ](#generating-crd-definitions )
2022-09-01 11:23:31 +00:00
- [Generating API docs ](#generating-api-docs )
2022-08-31 14:19:37 +00:00
> **Note**: You can run `make codegen-all` to build all generated code at once.
### Generating kubernetes API client
2022-09-01 06:14:56 +00:00
Based on the [APIs golang code definitions ](./api ), you can generate the corresponding Kubernetes client by running:
```console
# generate clientset, listers and informers
make codegen-client-all
```
or
```console
# generate clientset
make codegen-client-clientset
```
or
```console
# generate listers
make codegen-client-listers
```
or
```console
# generate informers
make codegen-client-informers
```
This will output generated files in the [/pkg/client ](./pkg/client ) package.
2022-08-31 14:19:37 +00:00
### Generating API deep copy functions
Based on the [APIs golang code definitions ](./api ), you can generate the corresponding deep copy functions by running:
```console
# generate all deep copy functions
make codegen-deepcopy-all
```
or
```console
# generate kyverno deep copy functions
make codegen-deepcopy-kyverno
```
or
```console
# generate policy reports deep copy functions
make codegen-deepcopy-report
```
This will output files named `zz_generated.deepcopy.go` in every API package.
### Generating CRD definitions
Based on the [APIs golang code definitions ](./api ), you can generate the corresponding CRDs manifests by running:
```console
# generate all CRDs
make codegen-crds-all
```
or
```console
# generate Kyverno CRDs
make codegen-crds-kyverno
```
or
```console
# generate policy reports CRDs
make codegen-crds-report
```
This will output CRDs manifests [/config/crds ](./config/crds ).
2022-09-01 11:23:31 +00:00
### Generating API docs
Based on the [APIs golang code definitions ](./api ), you can generate the corresponding API reference docs by running:
```console
# generate API docs
make codegen-api-docs
```
This will output API docs in [/docs/crd ](./docs/crd ).
2022-09-05 15:08:07 +00:00
### Generating helm charts CRDs
Based on the [APIs golang code definitions ](./api ), you can generate the corresponding CRD definitions for helm charts by running:
```console
# generate helm CRDs
make codegen-helm-crds
```
This will output CRDs templates in [/charts/kyverno/templates/crds.yaml ](./charts/kyverno/templates/crds.yaml ).
> **Note**: You can run `make codegen-helm-all` to generate CRDs and docs at once.
### Generating helm charts docs
Based on the helm charts default values:
- [kyverno ](./charts/kyverno/values.yaml )
- [kyverno-policies ](./charts/kyverno-policies/values.yaml )
You can generate the corresponding helm chart docs by running:
```console
# generate helm docs
make codegen-helm-docs
```
This will output docs in helm charts respective `README.md` :
- [kyverno ](./charts/kyverno/README.md )
- [kyverno-policies ](./charts/kyverno-policies/README.md )
> **Note**: You can run `make codegen-helm-all` to generate CRDs and docs at once.
2022-10-10 09:14:17 +00:00
## Debugging local code
2022-08-25 18:32:40 +00:00
2022-10-10 09:14:17 +00:00
Running Kyverno on a local machine without deploying it in a remote cluster can be useful, especially for debugging purpose.
You can run Kyverno locally or in your IDE of choice with a few steps:
2022-08-25 18:32:40 +00:00
2022-10-10 09:14:17 +00:00
1. Create a local cluster
- You can create a simple cluster with [KinD ](https://kind.sigs.k8s.io/ ) with `make kind-create-cluster`
1. Deploy Kyverno manifests except the Kyverno `Deployment`
2023-01-18 06:34:14 +00:00
- Kyverno is going to run on your local machine, so it should not run in cluster at the same time
2022-10-10 09:14:17 +00:00
- You can deploy the manifests by running `make debug-deploy`
2023-07-21 06:46:14 +00:00
1. There are multiple environment variables that need to be configured. The variables can be found in [here ](./.vscode/launch.json ). Their values can be set using the command `export $NAME=value`
2022-10-10 09:14:17 +00:00
1. To run Kyverno locally against the remote cluster you will need to provide `--kubeconfig` and `--serverIP` arguments:
- `--kubeconfig` must point to your kubeconfig file (usually `~/.kube/config` )
- `--serverIP` must be set to `<local ip>:9443` (`< local ip > ` is the private ip adress of your local machine)
2023-04-28 13:28:07 +00:00
- `--backgroundServiceAccountName` must be set to `system:serviceaccount:kyverno:kyverno-background-controller`
2024-01-21 09:57:20 +00:00
- `--caSecretName` must be set to `kyverno-svc.kyverno.svc.kyverno-tls-ca`
- `--tlsSecretName` must be set to `kyverno-svc.kyverno.svc.kyverno-tls-pair`
2022-08-25 18:32:40 +00:00
2022-10-10 09:14:17 +00:00
Once you are ready with the steps above, Kyverno can be started locally with:
```console
2024-01-21 09:57:20 +00:00
go run ./cmd/kyverno/ --kubeconfig ~/.kube/config --serverIP=< local-ip > :9443 --backgroundServiceAccountName=system:serviceaccount:kyverno:kyverno-background-controller --caSecretName=kyverno-svc.kyverno.svc.kyverno-tls-ca --tlsSecretName=kyverno-svc.kyverno.svc.kyverno-tls-pair
2022-08-25 18:32:40 +00:00
```
2022-10-10 09:14:17 +00:00
You will need to adapt those steps to run debug sessions in your IDE of choice, but the general idea remains the same.
2024-04-23 05:47:16 +00:00
## Profiling
### Enable profiling
To profile Kyverno application running inside a Kubernetes pod, set `--profile` flag to `true` in [install.yaml ](https://github.com/kyverno/kyverno/blob/main/definitions/install.yaml ). The default profiling port is 6060, and it can be configured via `profile-port` .
```
--profile
Set this flag to 'true', to enable profiling.
--profile-port string
Enable profiling at given port, defaults to 6060. (default "6060")
```
### Expose the endpoint on a local port
You can get at the application in the pod by port forwarding with kubectl, for example:
````shell
$ kubectl -n kyverno get pod
NAME READY STATUS RESTARTS AGE
kyverno-7d67c967c6-slbpr 1/1 Running 0 19s
````
````bash
$ kubectl -n kyverno port-forward kyverno-7d67c967c6-slbpr 6060
Forwarding from 127.0.0.1:6060 -> 6060
Forwarding from [::1]:6060 -> 6060
````
The HTTP endpoint will now be available as a local port.
Alternatively, use a Service of the type `LoadBalancer` to expose Kyverno. An example Service manifest is given below:
```yaml
apiVersion: v1
kind: Service
metadata:
name: pproc-service
namespace: kyverno
spec:
selector:
app: kyverno
ports:
- protocol: TCP
port: 6060
targetPort: 6060
type: LoadBalancer
```
### Generate the data
You can then generate the file for the **memory** profile with curl and pipe the data to a file:
````shell
$ curl http://localhost:6060/debug/pprof/heap > heap.pprof
````
Generate the file for the **CPU** profile with curl and pipe the data to a file:
```shell
curl "http://localhost:6060/debug/pprof/profile?seconds=60" > cpu.pprof
```
### Analyze the data
To analyze the data:
````shell
go tool pprof heap.pprof
````
### Read more about profiling
- [Profiling Golang Programs on Kubernetes ](https://danlimerick.wordpress.com/2017/01/24/profiling-golang-programs-on-kubernetes/ )
- [Official GO blog ](https://blog.golang.org/pprof )
## Other Topics
Additional advanced developer docs are avaialable in the [developer docs folder ](./docs/dev/ ).
## Selecting Issues
When you are ready to contribute, you can select issue at [Good First Issues ](https://github.com/orgs/kyverno/projects/10 ).