Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restart_process: make compatible with k8s_custom_deploy #393

Merged
merged 3 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions restart_process/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ This extension does NOT support process restarts for:
- Images built with `custom_build` using any of the `skips_local_docker`, `disable_push`, or `tag` parameters.
- Images run in Docker Compose resources (use the [`restart_container()`](https://docs.tilt.dev/api.html#api.restart_container) builtin instead)
- Images without a shell (e.g. `scratch`, `distroless`)
- Container commands specified as `command` in Kubernetes YAML will be overridden by this extension.
- However, the `args` field is still available; [reach out](https://tilt.dev/contact) if you need help navigating the interplay between Tilt and these YAML values
- For `ContainerSpec`s that specify a `command` in the Kubernetes YAML:
- If the YAML is known during Tiltfile execution (i.e., isn't using `k8s\_custom\_deploy` or similar), the command will be overridden by this extension.
- However, the `args` field is still available; [reach out](https://tilt.dev/contact) if you need help navigating the interplay between Tilt and these YAML values
- If the YAML comes via `k8s\_custom\_deploy` or similar and specifies a `command` in its `ContainerSpec`, then this extension will not work.
- CRDs

If this extension doesn't work for your use case, [see our docs for alternatives](https://docs.tilt.dev/live_update_reference.html#restarting-your-process).
Expand Down
24 changes: 13 additions & 11 deletions restart_process/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,6 @@ def _helper(base_ref, ref, entrypoint, live_update, restart_file=RESTART_FILE, t
if not trigger:
trigger = []

# declare a new docker build that adds a static binary of tilt-restart-wrapper
# (which makes use of `entr` to watch files and restart processes) to the user's image
df = '''
FROM tiltdev/restart-helper:2021-11-03 as restart-helper

FROM {}
RUN ["touch", "{}"]
COPY --from=restart-helper /tilt-restart-wrapper /
COPY --from=restart-helper /entr /
'''.format(base_ref, restart_file)

# Change the entrypoint to use `tilt-restart-wrapper`.
# `tilt-restart-wrapper` makes use of `entr` (https://github.com/eradman/entr/) to
# re-execute $entrypoint whenever $restart_file changes
Expand All @@ -50,6 +39,19 @@ def _helper(base_ref, ref, entrypoint, live_update, restart_file=RESTART_FILE, t
else:
fail("`entrypoint` must be a string or list of strings: got {}".format(type(entrypoint)))

# declare a new docker build that adds a static binary of tilt-restart-wrapper
# (which makes use of `entr` to watch files and restart processes) to the user's image
# we also set the image's entrypoint to give k8s_custom_deploy a chance of working: https://github.com/tilt-dev/tilt-extensions/issues/391
df = '''
FROM tiltdev/restart-helper:2021-11-03 as restart-helper

FROM {}
RUN ["touch", "{}"]
COPY --from=restart-helper /tilt-restart-wrapper /
COPY --from=restart-helper /entr /
ENTRYPOINT {}
'''.format(base_ref, restart_file, entrypoint_with_entr)

# last live_update step should always be to modify $restart_file, which
# triggers the process wrapper to rerun $entrypoint
# NB: write `date` instead of just `touch`ing because `entr` doesn't respond
Expand Down
7 changes: 7 additions & 0 deletions restart_process/test/Dockerfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM alpine

RUN echo 0 > restart_count.txt

ADD start.sh /

ENTRYPOINT /start.sh
2 changes: 1 addition & 1 deletion restart_process/test/Tiltfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load('../Tiltfile', 'docker_build_with_restart')

k8s_yaml('job.yaml')
docker_build_with_restart('failing_job', '.', '/fail.sh',
docker_build_with_restart('failing_job', '.', dockerfile='Dockerfile.failing', entrypoint='/fail.sh',
live_update=[sync('./fail.sh', '/fail.sh')])
2 changes: 1 addition & 1 deletion restart_process/test/custom.Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load('../Tiltfile', 'custom_build_with_restart')
k8s_yaml('job.yaml')
custom_build_with_restart(
'failing_job',
command='docker build -t $EXPECTED_REF .',
command='docker build -t $EXPECTED_REF -f Dockerfile.failing .',
deps=['fail.sh'],
entrypoint='/fail.sh',
live_update=[sync('./fail.sh', '/fail.sh')])
12 changes: 12 additions & 0 deletions restart_process/test/custom_deploy.Tiltfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load('../Tiltfile', 'docker_build_with_restart')
docker_build_with_restart('testimage', '.', dockerfile='Dockerfile.test', entrypoint='/start.sh', live_update=[sync('start.sh', '/start.sh')])
k8s_custom_deploy(
'custom_deploy',
deps=['deployment.yaml'],
apply_cmd='sed -e"s|image: testimage|image: ${TILT_IMAGE_0}|" deployment.yaml | kubectl apply -f - -oyaml',
delete_cmd='kubectl delete -f deployment.yaml',
image_deps=['testimage'],
)

# trigger a live update and see if the process is restarted
local_resource('test_update', './trigger_custom_deploy_live_update.sh', resource_deps=['custom_deploy'])
16 changes: 16 additions & 0 deletions restart_process/test/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: testdeploy
spec:
selector:
matchLabels:
app: testdeploy
template:
metadata:
labels:
app: testdeploy
spec:
containers:
- name: testcontainer
image: testimage
11 changes: 11 additions & 0 deletions restart_process/test/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
set -euo pipefail
RESTART_COUNT="$(cat restart_count.txt)"
echo "RESTART_COUNT: $RESTART_COUNT"
RESTART_COUNT=$((RESTART_COUNT+1))
echo $RESTART_COUNT > restart_count.txt
while true
do
echo running
sleep 5
done
23 changes: 23 additions & 0 deletions restart_process/test/test-custom-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

# make sure that docker_build_with_restart works with k8s_custom_deploy
# https://github.com/tilt-dev/tilt-extensions/issues/391

cd "$(dirname "$0")" || exit

set -x
tilt down -f custom_deploy.Tiltfile

tilt up -f custom_deploy.Tiltfile --stream > tilt.log &
TILT_PID=$!

sleep 1
timeout 30 tail -f tilt.log | grep -q "test_update │ restart detected"
RESULT=$?
cat tilt.log

kill $TILT_PID
tilt down
rm tilt.log

exit $RESULT
5 changes: 3 additions & 2 deletions restart_process/test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
set -ex

cd "$(dirname "$0")"
./test-docker.sh
./test-custom.sh
./test-docker-fail.sh
./test-custom-fail.sh
./test-custom-deploy.sh
20 changes: 20 additions & 0 deletions restart_process/test/trigger_custom_deploy_live_update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

set -u

# triggers a live update and waits for the tilt logs to reflect the process was restarted
# exits 0 if it's successfully restarted, or 1 if not

touch start.sh
# seconds
TIMEOUT=5
for ((i=0;i<=$TIMEOUT;++i)) do
tilt logs | grep -v test_update | grep -q "RESTART_COUNT: 1"
if [[ $? -eq 0 ]]; then
echo restart detected
exit 0
fi
echo no restart detected
sleep 1
done
exit 1