Skip to content

Commit

Permalink
Merge pull request #54 from flavio/tilt
Browse files Browse the repository at this point in the history
Introduce Tilt to simplify the development process
  • Loading branch information
flavio authored Dec 21, 2023
2 parents a94c44e + 12d1c49 commit fd009e1
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tilt-settings.yaml

# Binaries for programs and plugins
*.exe
Expand Down
63 changes: 63 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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
```
File renamed without changes.
74 changes: 74 additions & 0 deletions Tiltfile
Original file line number Diff line number Diff line change
@@ -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'),
],
)

2 changes: 2 additions & 0 deletions charts/kwasm-operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ spec:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: {{ .Chart.Name }}
args:
- --leader-elect
env:
- name: CONTROLLER_NAMESPACE
value: {{ .Release.Namespace }}
Expand Down
2 changes: 2 additions & 0 deletions tilt-settings.yaml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Replace with a development registry that offers pull & push access
registry: ghcr.io/<your github handle>/kwasm-operator
5 changes: 5 additions & 0 deletions tilt.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM alpine
WORKDIR /
COPY ./bin/manager /manager

ENTRYPOINT ["/manager"]

0 comments on commit fd009e1

Please sign in to comment.