|
| 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)) |
0 commit comments