Skip to content

Commit b5e9abf

Browse files
committed
Add initial Tilt support
Signed-off-by: Andy Goldstein <[email protected]>
1 parent cf47583 commit b5e9abf

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ install.sh
3737

3838
# documentation website asset folder
3939
docs/_site
40+
41+
.tiltbuild/

Tiltfile

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# This loads a helper function that isn't part of core Tilt that simplifies restarting the process in the container
2+
# when files changes.
3+
load('ext://restart_process', 'docker_build_with_restart')
4+
5+
# Treat the main binary as a local resource, so we can automatically rebuild it when any of the deps change. This
6+
# builds it locally, targeting linux, so it can run in a linux container.
7+
local_resource(
8+
'manager_binary',
9+
cmd='''
10+
mkdir -p .tiltbuild/bin
11+
CGO_ENABLED=0 GOOS=linux go build -o .tiltbuild/bin/manager ./cmd/manager
12+
''',
13+
deps=['api', 'cmd/manager', 'internal', 'pkg', 'go.mod', 'go.sum']
14+
)
15+
16+
# Configure our image build. If the file in live_update.sync (.tiltbuild/bin/manager) changes, Tilt
17+
# copies it to the running container and restarts it.
18+
docker_build_with_restart(
19+
# This has to match an image in the k8s_yaml we call below, so Tilt knows to use this image for our Deployment,
20+
# instead of the actual image specified in the yaml.
21+
ref='quay.io/operator-framework/operator-controller:devel',
22+
# This is the `docker build` context, and because we're only copying in the binary we've already had Tilt build
23+
# locally, we set the context to the directory containing the binary.
24+
context='.tiltbuild/bin',
25+
# We use a slimmed-down Dockerfile that only has $binary in it.
26+
dockerfile_contents='''
27+
FROM gcr.io/distroless/static:debug
28+
EXPOSE 8080
29+
WORKDIR /
30+
COPY manager manager
31+
''',
32+
# The set of files Tilt should include in the build. In this case, it's just the binary we built above.
33+
only='manager',
34+
# If .tiltbuild/bin/manager changes, Tilt will copy it into the running container and restart the process.
35+
live_update=[
36+
sync('.tiltbuild/bin/manager', '/manager'),
37+
],
38+
# The command to run in the container.
39+
entrypoint="/manager",
40+
)
41+
42+
# Tell Tilt what to deploy by running kustomize and then doing some manipulation to make things work for Tilt.
43+
objects = decode_yaml_stream(kustomize('config/default'))
44+
for o in objects:
45+
# For Tilt's live_update functionality to work, we have to run the container as root. Remove any PSA labels to allow
46+
# this.
47+
if o['kind'] == 'Namespace' and 'labels' in o['metadata']:
48+
labels_to_delete = [label for label in o['metadata']['labels'] if label.startswith('pod-security.kubernetes.io')]
49+
for label in labels_to_delete:
50+
o['metadata']['labels'].pop(label)
51+
52+
if o['kind'] != 'Deployment':
53+
# We only need to modify Deployments, so we can skip this
54+
continue
55+
56+
# For Tilt's live_update functionality to work, we have to run the container as root. Otherwise, Tilt won't
57+
# be able to untar the updated binary in the container's file system (this is how live update
58+
# works). If there are any securityContexts, remove them.
59+
if "securityContext" in o['spec']['template']['spec']:
60+
o['spec']['template']['spec'].pop('securityContext')
61+
for c in o['spec']['template']['spec']['containers']:
62+
if "securityContext" in c:
63+
c.pop('securityContext')
64+
65+
# Now apply all the yaml
66+
k8s_yaml(encode_yaml_stream(objects))

tilt.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Rapid iterative development with Tilt
2+
3+
[Tilt](https://tilt.dev) is a tool that enables rapid iterative development of containerized workloads.
4+
5+
Here is an example workflow without Tilt for modifying some source code and testing those changes in a cluster:
6+
7+
1. Modify the source code.
8+
2. Build the container image.
9+
3. Either push the image to a registry or load it into your kind cluster.
10+
4. Deploy all the appropriate Kubernetes manifests for your application.
11+
1. Or, if this is an update, you'd instead scale the Deployment to 0 replicas, scale back to 1, and wait for the
12+
new pod to be running.
13+
14+
This process can take minutes, depending on how long each step takes.
15+
16+
Here is the same workflow with Tilt:
17+
18+
1. Run `tilt up`
19+
2. Modify the source code
20+
3. Wait for Tilt to update the container with your changes
21+
22+
This ends up taking a fraction of the time, sometimes on the order of a few seconds!
23+
24+
## Installing Tilt
25+
26+
Follow Tilt's [instructions](https://docs.tilt.dev/install.html) for installation.
27+
28+
## Installing rukpak and catalogd
29+
30+
operator-controller requires [rukpak](https://github.com/operator-framework/rukpak) and
31+
[catalogd](https://github.com/operator-framework/catalogd). Please make sure they're installed, either normally or via
32+
their own Tiltfiles, before proceeding. If you want to use Tilt, make sure you specify a unique `--port` flag to each
33+
`tilt up` invocation.
34+
35+
## Starting Tilt
36+
37+
This is typically as short as:
38+
39+
```shell
40+
tilt up
41+
```
42+
43+
**NOTE:** if you are using Podman, at least as of v4.5.1, you need to do this:
44+
45+
```shell
46+
DOCKER_BUILDKIT=0 tilt up
47+
```
48+
49+
Otherwise, you'll see an error when Tilt tries to build your image that looks similar to:
50+
51+
```text
52+
Build Failed: ImageBuild: stat /var/tmp/libpod_builder2384046170/build/Dockerfile: no such file or directory
53+
```
54+
55+
When Tilt starts, you'll see something like this in your terminal:
56+
57+
```text
58+
Tilt started on http://localhost:10350/
59+
v0.33.1, built 2023-06-28
60+
61+
(space) to open the browser
62+
(s) to stream logs (--stream=true)
63+
(t) to open legacy terminal mode (--legacy=true)
64+
(ctrl-c) to exit
65+
```
66+
67+
Typically, you'll want to press the space bar to have it open the UI in your web browser.
68+
69+
Shortly after starting, Tilt processes the `Tiltfile`, resulting in:
70+
71+
- Building the go binaries
72+
- Building the images
73+
- Loading the images into kind
74+
- Running kustomize and applying everything except the Deployments that reference the images above
75+
- Modifying the Deployments to use the just-built images
76+
- Creating the Deployments
77+
78+
## Making code changes
79+
80+
Any time you change any of the files listed in the `deps` section in the `<binary name>_binary` `local_resource`,
81+
Tilt automatically rebuilds the go binary. As soon as the binary is rebuilt, Tilt pushes it (and only it) into the
82+
appropriate running container, and then restarts the process.

0 commit comments

Comments
 (0)