bazelbuild / rules_k8s

This repository contains rules for interacting with Kubernetes configurations / clusters.
Apache License 2.0
290 stars 137 forks source link

Stamp substitution in image names does not work as expected #727

Open chrismgrayftsinc opened 1 year ago

chrismgrayftsinc commented 1 year ago

I have a macro that I am converting from using --define variable substitution to bazel stamps. Here is a snippet:

    docker_push(
        name = "push-" + project_name,
        image = ":" + project_name + "-image",
        registry = "index.docker.io",
        repository = repository,
        tag = "{STABLE_VERSION}",
        stamp = "@io_bazel_rules_docker//stamp:always",
    )

    k8s_object(
        name = "test-k8s",
        cluster = "staging.k8s.local",
        images = {
            repository + ":{STABLE_VERSION}": "//:" + project_name + "-image",
        },
        template = k8s_template,
    )

We also use jsonnet and bazel stamps to generate the k8s_template:

jsonnet_to_json(
    name = "deployment-staging-json",
    src = "deployment-staging.jsonnet",
    outs = ["deployment-staging.json"],
    ext_strs = {"VERSION": "{STABLE_VERSION}"},
    stamp_keys = ["VERSION"],
    visibility = ["//:__subpackages__"],
    yaml_stream = True,
    deps = [
        ":deployment",
    ],
)

so the template is generated with the stamp key substituted in. My intuition was that the {STABLE_VERSION} tag would be stamp-substituted but it is not. When I run bazel run --workspace_status_command="echo STABLE_VERSION test" :test-k8s.replace, I get the error

2023/05/02 16:18:21 The following images given as --image_spec were not found in the template:
2023/05/02 16:18:21 <repo>/<service>:{STABLE_VERSION}
2023/05/02 16:18:21 --allow_unused_images can be specified to ignore this error.

It is interesting to note that the image is uploaded to Docker Hub with the expected test tag, so there is some substitution done on the image tag, just not when looking in the k8s template I guess.

chrismgrayftsinc commented 1 year ago

I do see the example custom resolver in the examples directory. Perhaps that would be the way to go.