diff --git a/Tiltfile b/Tiltfile index 21341a6bb59c..e6b8409e2133 100644 --- a/Tiltfile +++ b/Tiltfile @@ -4,12 +4,17 @@ envsubst_cmd = "./hack/tools/bin/envsubst" clusterctl_cmd = "./bin/clusterctl" kubectl_cmd = "kubectl" kubernetes_version = "v1.25.3" +default_build_engine = "docker" if str(local("command -v " + kubectl_cmd + " || true", quiet = True)) == "": fail("Required command '" + kubectl_cmd + "' not found in PATH") load("ext://uibutton", "cmd_button", "location", "text_input") +# detect if docker images should be built using podman +if "Podman Engine" in str(local("docker version || podman version", quiet = True)): + default_build_engine = "podman" + # set defaults version_settings(True, ">=0.30.8") @@ -17,6 +22,7 @@ settings = { "enable_providers": ["docker"], "kind_cluster_name": os.getenv("CAPI_KIND_CLUSTER_NAME", "capi-test"), "debug": {}, + "build_engine": default_build_engine, } # global settings @@ -252,18 +258,40 @@ def build_docker_image(image, context, binary_name, additional_docker_build_comm # Set up an image build for the provider. The live update configuration syncs the output from the local_resource # build into the container. - docker_build( - ref = image, - context = context + "/.tiltbuild/bin/", - dockerfile_contents = dockerfile_contents, - build_args = {"binary_name": binary_name}, - target = "tilt", - only = binary_name, - live_update = [ - sync(context + "/.tiltbuild/bin/" + binary_name, "/" + binary_name), - run("sh /restart.sh"), - ], - ) + if settings.get("build_engine") == "podman": + bin_context = context + "/.tiltbuild/bin/" + + # Write dockerfile_contents to a Dockerfile as custom_build doesn't support dockerfile_contents nor stdin. + # The Dockerfile is in the context path to simplify the below podman command. + local("tee %s/Dockerfile" % (shlex.quote(bin_context)), quiet = True, stdin = dockerfile_contents) + + custom_build( + ref = image, + command = ( + "set -ex\n" + + "podman build -t $EXPECTED_REF --build-arg binary_name=%s --target tilt %s\n" + + "podman push --format=docker $EXPECTED_REF\n" + ) % (binary_name, shlex.quote(bin_context)), + deps = [bin_context], + skips_local_docker = True, + live_update = [ + sync(bin_context + binary_name, "/" + binary_name), + run("sh /restart.sh"), + ], + ) + else: + docker_build( + ref = image, + context = context + "/.tiltbuild/bin/", + dockerfile_contents = dockerfile_contents, + build_args = {"binary_name": binary_name}, + target = "tilt", + only = binary_name, + live_update = [ + sync(context + "/.tiltbuild/bin/" + binary_name, "/" + binary_name), + run("sh /restart.sh"), + ], + ) def get_port_forwards(debug): port_forwards = [] diff --git a/docs/book/src/developer/tilt.md b/docs/book/src/developer/tilt.md index 1e2f255731aa..00fe6b089247 100644 --- a/docs/book/src/developer/tilt.md +++ b/docs/book/src/developer/tilt.md @@ -63,6 +63,9 @@ If you prefer JSON, you can create a `tilt-settings.json` file instead. YAML wil **default_registry** (String, default=""): The image registry to use if you need to push images. See the [Tilt documentation](https://docs.tilt.dev/api.html#api.default_registry) for more details. +**build_engine** (String, default="docker"): The engine used to build images. Can either be `docker` or `podman`. +NB: the default is dynamic and will be "podman" if the string "Podman Engine" is found in `docker version` (or in `podman version` if the command fails). + **kind_cluster_name** (String, default="capi-test"): The name of the kind cluster to use when preloading images. **provider_repos** (Array[]String, default=[]): A list of paths to all the providers you want to use. Each provider must have a @@ -458,3 +461,13 @@ syntax highlighting and auto-formatting. To enable it for Tiltfile a file associ "Tiltfile": "starlark", }, ``` + +## Using Podman + +[Podman](https://podman.io) can be used instead of docker by following these actions: + +1. Enable the podman unix socket (eg. `systemctl --user enable --now podman.socket` on Fedora) +1. Set `build_engine` to `podman` in `tilt-settings.yaml` (optional, only if both docker & podman are installed) +1. Define the env variable `DOCKER_HOST` to the right socket while running tilt (eg. `DOCKER_HOST=unix:///run/user/$(id -u)/podman/podman.sock tilt up`) + +NB: The socket defined by `DOCKER_HOST` is used only for the `hack/tools/tilt-prepare` command, the image build is running the `podman build`/`podman push` commands. \ No newline at end of file