Proposal: exit_settings builtin and --on-exit CLI option for customizing exit behavior
Describe the Feature You Want
We have a number of requests (#2427, #3059, #4294, #5210, #3831) related to being able to control what happens when tilt up or tilt ci exits. A brief summary:
- Enable tearing down of resources on exit (#2427, #5210). Either:
- the equivalent of
tilt down, where theTiltfileis re-executed (so thatif config.tilt_subcommand == 'down':statements could run), or - clean up the resources without re-executing the
Tiltfile-- use the manifests computed from the lastTiltfileresult to clean up or disable all resources
- Provide multiple ways of specifying the behavior: CLI option(s), Tiltfile builtins, user config file
- Provide custom behavior on exit rather than using
tilt downandif config.tilt_subcommand == 'down':statements (#3831, #3059) - There are a couple other CLI flags and features related to behavior on exit that have no
Tiltfileequivalent that could be exposed:tilt down --delete-namespaces,tilt up/ci --output-snapshot-on-exit
Proposal
This issue proposes to address the above issues and requests with a new Tiltfile builtin exit_settings() and CLI option tilt up/ci --on-exit.
def exit_settings(
down: bool = False,
disable: bool = False,
delete_namespaces: bool = False,
callback: Function = None,
snapshot: Union[bool,str] = False,
)
"""Customize behavior of `tilt up` or `tilt ci` when it exits (e.g., due to interrupt or term signal).
Default behavior is to do nothing, leaving any existing k8s or docker-compose resources running.
Args:
down: If true, execute the equivalent of `tilt down` at exit. The `Tiltfile` will be re-executed so that any `if config.tilt_subcommand == 'down':` conditional statements can be run. Mutually exclusive with `disable`: If both `down` and `disable` are true, `down` wins.
disable: If true, disable all resources at exit. The effect is similar to `down` except that the `Tiltfile` is not re-executed.
delete_namespaces: If true and one of `down` or `disable` is specified, delete any k8s namespaces that were created.
callback: a Starlark function to be executed at exit. Can be used to run custom logic and `local` commands. If `down` or `disable` are also specified, the callback function will be executed before resources are torn down.
snapshot: If true, Tilt will output a snapshot file named `tilt-snapshot-XXXXXX.json` at exit. If a string, represents a snapshot filename to save. A literal `%s` can be used in the pathname and will be replaced with a value that ensures the filename will be unique.
"""
CLI option --on-exit
A common flag --on-exit will be added to tilt up and tilt ci that allows specifying down, disable, delete_namespaces, or snapshot options.
--on-exit strings Values: down, disable, delete_namespace, snapshot. Customize Tilt behavior on exit. See `exit_settings` Tiltfile builtin.
In addition to the values stated, --on-exit will allow a string of the form snapshot=snapshot-%s.json to customize the snapshot filename. Equivalent to --output-snapshot-on-exit string option.
Current Behavior
Tilt does not allow much in the way of customizing what happens when tilt up or tilt ci exits.
Why Do You Want This?
Address multiple issues and user requests related to on-exit behavior.
For what it's worth, I think there are 4 general product use-cases:
- When I start and stop my tilt session, I want it to be completely isolated (no shared build state, no shared runtime state. Very common among CI/testing workflows.)
- When I stop and start my test session, I want it to reconnect an existing workspace (shared build state, shared runtime state. Very common among data science / ML workloads on a shared dev cluster.)
- When I stop tilt, I want it to stop consuming all runtime resources (shared build state, no shared runtime state. Very common among people who are running everything locally).
- My build system is caching dependencies incorrectly, so want to try again without a build cache. (no shared build state, preserve runtime state, very commonly needed when debugging, think
docker build --pullorbazel clean)
There are also confounding factors.
- You often want to preserve dev databases across runs unless they're explicitly cleared, so that you have a persistent set of data to work with (i.e., this is why people often tell tilt to preserve volumes with
‘tilt.dev/down-policy: keep). - You often want to preserve namespaces, because they don't contain a lot of state anyway, and Kubernetes has lots of bugs where it can deadlock on namespace deletion (ask me how I know this).
I'm not necessarily saying that exit_settings is the wrong solution for this. But I do think this is a hard product problem. And that encoding the decision in the Tiltfile, or when you run 'tilt up', is not necessarily the right place for that decision.
Another use case for me would be to resume FluxCD-managed resources after shutting down tilt.
Chiming in in support for this proposal. While @nicks presents good points about this not being the best overarching solution, Tilt already requires a lot of intervention to get to run properly. Adding exit_settings would only provide flexibility for cases where a correct/clean method is not feasible or practical.