Skip to content

Commit

Permalink
Support templated replacements in kustomize_build
Browse files Browse the repository at this point in the history
Summary:
This changes kustomize_build's handling of replacement and uses
the string format syntax combined with ctx.vars to enable Make var like
substitution.
This allows us to reduce the dupe in vizier images and just use the var instead.

Test Plan:
Built the vizier images with diffrent values for build_type.
Checked to ensure that the prefix and version were as expected.

Reviewers: jamesbartlett, zasgar, michelle

Reviewed By: jamesbartlett

Signed-off-by: Vihang Mehta <[email protected]>

Differential Revision: https://phab.corp.pixielabs.ai/D12887

GitOrigin-RevId: 261a91f30361c7e8a27dbdf8c8dbf7d76d2236c4
  • Loading branch information
vihangm authored and copybaranaut committed Jan 22, 2023
1 parent 71f10f3 commit a994e7f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 224 deletions.
2 changes: 1 addition & 1 deletion bazel/images.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def image_replacements(image_map, existing_prefix, new_prefix):

for image in image_map.keys():
image = image.removeprefix("$(IMAGE_PREFIX)").removesuffix(":$(BUNDLE_VERSION)")
replacements[existing_prefix + image] = new_prefix + image
replacements[existing_prefix + image] = new_prefix + image + ":{BUNDLE_VERSION}"

return replacements

Expand Down
12 changes: 10 additions & 2 deletions bazel/kustomize.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def _kustomize_build_impl(ctx):
cmds.append('pushd "$TMP/$(dirname "{}")" &> /dev/null'.format(ctx.file.kustomization.path))

for old, new in ctx.attr.replacements.items():
cmds.append('"$KUSTOMIZE_BIN" edit set image {}={}:{}'.format(old, new, ctx.var["BUNDLE_VERSION"]))
old_expanded = old.format(**ctx.var)
new_expanded = new.format(**ctx.var)
cmds.append('"$KUSTOMIZE_BIN" edit set image {}={}'.format(old_expanded, new_expanded))

cmds.append("popd &> /dev/null")

Expand Down Expand Up @@ -60,7 +62,13 @@ kustomize_build = rule(
mandatory = True,
allow_single_file = True,
),
"replacements": attr.string_dict(),
"replacements": attr.string_dict(
doc = """
Will be passed as args to `kustomize edit set image <key>=<value>`
Supports make vars as templates. i.e. {MY_VAR} will be replaced with
values ctx.vars["MY_VAR"] if available.
""",
),
"srcs": attr.label_list(
mandatory = True,
allow_files = True,
Expand Down
3 changes: 0 additions & 3 deletions k8s/operator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ kustomize_build(
exclude = ["crd/base/kustomization.yaml"],
),
kustomization = "crd/base/kustomization.yaml",
toolchains = [
"//k8s:bundle_version",
],
)

pkg_tar(
Expand Down
Loading

0 comments on commit a994e7f

Please sign in to comment.