Skip to content

Commit fd009e1

Browse files
authored
Merge pull request #54 from flavio/tilt
Introduce Tilt to simplify the development process
2 parents a94c44e + 12d1c49 commit fd009e1

File tree

7 files changed

+147
-0
lines changed

7 files changed

+147
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
tilt-settings.yaml
12

23
# Binaries for programs and plugins
34
*.exe

CONTRIBUTING.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Contributing
2+
3+
## Building
4+
5+
Pre-requisites:
6+
7+
- make
8+
- Go compiler
9+
10+
To build the controller use the following command:
11+
12+
```console
13+
make all
14+
```
15+
16+
To run the unit tests:
17+
18+
```console
19+
make test
20+
```
21+
22+
## Development
23+
24+
To run the controller for development purposes, you can use [Tilt](https://tilt.dev/).
25+
26+
### Pre-requisites
27+
28+
Please follow the [Tilt installation documentation](https://docs.tilt.dev/install.html) to install the command line tool.
29+
30+
A development Kubernetes cluster is needed to run the controller.
31+
You can use [k3d](https://k3d.io/) to create a local cluster for development purposes.
32+
33+
### Settings
34+
35+
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.
36+
Copy the example file and edit it to match your environment.
37+
The `tilt-settings.yaml` file is ignored by git, so you can safely edit it without worrying about committing it by mistake.
38+
39+
The following settings can be configured:
40+
41+
- `registry`: the container registry where the controller image will be pushed.
42+
If you don't have a private registry, you can use `ghcr.io` as long as your
43+
cluster has access to it.
44+
45+
Example:
46+
47+
```yaml
48+
registry: ghcr.io/your-gh-username/kwasm-operator
49+
```
50+
51+
### Running the controller
52+
53+
The `Tiltfile` included in this repository will take care of the following:
54+
55+
- Create the `kwasm` namespace and install the controller helm-chart in it.
56+
- Inject the development image in the deployment.
57+
- Automatically reload the controller when you make changes to the code.
58+
59+
To run the controller, you just need to run the following command against an empty cluster:
60+
61+
```console
62+
$ tilt up --stream
63+
```
File renamed without changes.

Tiltfile

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# -*- mode: Python -*-
2+
3+
tilt_settings_file = "./tilt-settings.yaml"
4+
settings = read_yaml(tilt_settings_file)
5+
6+
kubectl_cmd = "kubectl"
7+
8+
# verify kubectl command exists
9+
if str(local("command -v " + kubectl_cmd + " || true", quiet = True)) == "":
10+
fail("Required command '" + kubectl_cmd + "' not found in PATH")
11+
12+
# Create the kwasm namespace
13+
# This is required since the helm() function doesn't support the create_namespace flag
14+
load('ext://namespace', 'namespace_create')
15+
namespace_create('kwasm')
16+
17+
# Install kwasm-operator helm chart
18+
install = helm(
19+
'./charts/kwasm-operator/',
20+
name='kwasm-operator',
21+
namespace='kwasm',
22+
set=['image.repository=' + settings.get('registry')]
23+
)
24+
25+
objects = decode_yaml_stream(install)
26+
for o in objects:
27+
# Update the root security group. Tilt requires root access to update the
28+
# running process.
29+
if o.get('kind') == 'Deployment' and o.get('metadata').get('name') == 'kwasm-operator':
30+
o['spec']['template']['spec']['securityContext']['runAsNonRoot'] = False
31+
# Disable the leader election to speed up the startup time.
32+
o['spec']['template']['spec']['containers'][0]['args'].remove('--leader-elect')
33+
break
34+
updated_install = encode_yaml_stream(objects)
35+
k8s_yaml(updated_install)
36+
37+
# enable hot reloading by doing the following:
38+
# - locally build the whole project
39+
# - create a docker imagine using tilt's hot-swap wrapper
40+
# - push that container to the local tilt registry
41+
# Once done, rebuilding now should be a lot faster since only the relevant
42+
# binary is rebuilt and the hot swat wrapper takes care of the rest.
43+
local_resource(
44+
'manager',
45+
"CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/manager ./",
46+
deps = [
47+
"main.go",
48+
"go.mod",
49+
"go.sum",
50+
"controllers",
51+
],
52+
)
53+
54+
# Build the docker image for our controller. We use a specific Dockerfile
55+
# since tilt can't run on a scratch container.
56+
entrypoint = ['/manager', '-zap-devel']
57+
dockerfile = 'tilt.dockerfile'
58+
59+
load('ext://restart_process', 'docker_build_with_restart')
60+
docker_build_with_restart(
61+
settings.get('registry'),
62+
'.',
63+
dockerfile = dockerfile,
64+
entrypoint = entrypoint,
65+
# `only` here is important, otherwise, the container will get updated
66+
# on _any_ file change.
67+
only=[
68+
'./bin',
69+
],
70+
live_update = [
71+
sync('./bin/manager', '/manager'),
72+
],
73+
)
74+

charts/kwasm-operator/templates/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ spec:
2727
{{- toYaml .Values.podSecurityContext | nindent 8 }}
2828
containers:
2929
- name: {{ .Chart.Name }}
30+
args:
31+
- --leader-elect
3032
env:
3133
- name: CONTROLLER_NAMESPACE
3234
value: {{ .Release.Namespace }}

tilt-settings.yaml.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Replace with a development registry that offers pull & push access
2+
registry: ghcr.io/<your github handle>/kwasm-operator

tilt.dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM alpine
2+
WORKDIR /
3+
COPY ./bin/manager /manager
4+
5+
ENTRYPOINT ["/manager"]

0 commit comments

Comments
 (0)