diff --git a/.gitignore b/.gitignore index c0a7a54..911b954 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +tilt-settings.yaml # Binaries for programs and plugins *.exe diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..24ff8cf --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,63 @@ +# Contributing + +## Building + +Pre-requisites: + +- make +- Go compiler + +To build the controller use the following command: + +```console +make all +``` + +To run the unit tests: + +```console +make test +``` + +## Development + +To run the controller for development purposes, you can use [Tilt](https://tilt.dev/). + +### Pre-requisites + +Please follow the [Tilt installation documentation](https://docs.tilt.dev/install.html) to install the command line tool. + +A development Kubernetes cluster is needed to run the controller. +You can use [k3d](https://k3d.io/) to create a local cluster for development purposes. + +### Settings + +The `tilt-settings.yaml.example` acts as a template for the `tilt-settings.yaml` file that you need to create in the root of this repository. +Copy the example file and edit it to match your environment. +The `tilt-settings.yaml` file is ignored by git, so you can safely edit it without worrying about committing it by mistake. + +The following settings can be configured: + +- `registry`: the container registry where the controller image will be pushed. + If you don't have a private registry, you can use `ghcr.io` as long as your + cluster has access to it. + +Example: + +```yaml +registry: ghcr.io/your-gh-username/kwasm-operator +``` + +### Running the controller + +The `Tiltfile` included in this repository will take care of the following: + +- Create the `kwasm` namespace and install the controller helm-chart in it. +- Inject the development image in the deployment. +- Automatically reload the controller when you make changes to the code. + +To run the controller, you just need to run the following command against an empty cluster: + +```console +$ tilt up --stream +``` diff --git a/.dockerignore b/Dockerfile.dockerignore similarity index 100% rename from .dockerignore rename to Dockerfile.dockerignore diff --git a/Tiltfile b/Tiltfile new file mode 100644 index 0000000..1017b76 --- /dev/null +++ b/Tiltfile @@ -0,0 +1,74 @@ +# -*- mode: Python -*- + +tilt_settings_file = "./tilt-settings.yaml" +settings = read_yaml(tilt_settings_file) + +kubectl_cmd = "kubectl" + +# verify kubectl command exists +if str(local("command -v " + kubectl_cmd + " || true", quiet = True)) == "": + fail("Required command '" + kubectl_cmd + "' not found in PATH") + +# Create the kwasm namespace +# This is required since the helm() function doesn't support the create_namespace flag +load('ext://namespace', 'namespace_create') +namespace_create('kwasm') + +# Install kwasm-operator helm chart +install = helm( + './charts/kwasm-operator/', + name='kwasm-operator', + namespace='kwasm', + set=['image.repository=' + settings.get('registry')] +) + +objects = decode_yaml_stream(install) +for o in objects: + # Update the root security group. Tilt requires root access to update the + # running process. + if o.get('kind') == 'Deployment' and o.get('metadata').get('name') == 'kwasm-operator': + o['spec']['template']['spec']['securityContext']['runAsNonRoot'] = False + # Disable the leader election to speed up the startup time. + o['spec']['template']['spec']['containers'][0]['args'].remove('--leader-elect') + break +updated_install = encode_yaml_stream(objects) +k8s_yaml(updated_install) + +# enable hot reloading by doing the following: +# - locally build the whole project +# - create a docker imagine using tilt's hot-swap wrapper +# - push that container to the local tilt registry +# Once done, rebuilding now should be a lot faster since only the relevant +# binary is rebuilt and the hot swat wrapper takes care of the rest. +local_resource( + 'manager', + "CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/manager ./", + deps = [ + "main.go", + "go.mod", + "go.sum", + "controllers", + ], +) + +# Build the docker image for our controller. We use a specific Dockerfile +# since tilt can't run on a scratch container. +entrypoint = ['/manager', '-zap-devel'] +dockerfile = 'tilt.dockerfile' + +load('ext://restart_process', 'docker_build_with_restart') +docker_build_with_restart( + settings.get('registry'), + '.', + dockerfile = dockerfile, + entrypoint = entrypoint, + # `only` here is important, otherwise, the container will get updated + # on _any_ file change. + only=[ + './bin', + ], + live_update = [ + sync('./bin/manager', '/manager'), + ], +) + diff --git a/charts/kwasm-operator/templates/deployment.yaml b/charts/kwasm-operator/templates/deployment.yaml index dddf1b4..9827b61 100644 --- a/charts/kwasm-operator/templates/deployment.yaml +++ b/charts/kwasm-operator/templates/deployment.yaml @@ -27,6 +27,8 @@ spec: {{- toYaml .Values.podSecurityContext | nindent 8 }} containers: - name: {{ .Chart.Name }} + args: + - --leader-elect env: - name: CONTROLLER_NAMESPACE value: {{ .Release.Namespace }} diff --git a/tilt-settings.yaml.example b/tilt-settings.yaml.example new file mode 100644 index 0000000..c346c3a --- /dev/null +++ b/tilt-settings.yaml.example @@ -0,0 +1,2 @@ +# Replace with a development registry that offers pull & push access +registry: ghcr.io//kwasm-operator diff --git a/tilt.dockerfile b/tilt.dockerfile new file mode 100644 index 0000000..2fbfdce --- /dev/null +++ b/tilt.dockerfile @@ -0,0 +1,5 @@ +FROM alpine +WORKDIR / +COPY ./bin/manager /manager + +ENTRYPOINT ["/manager"]