diff --git a/README.md b/README.md index bcca4ab..66359d3 100644 --- a/README.md +++ b/README.md @@ -35,12 +35,11 @@ load("@rules_ytt//:defs.bzl", "ytt") # Build an image with rules_docker -load("@io_bazel_rules_docker//go:image.bzl", "go_image") +load("@io_bazel_rules_docker//container:container.bzl", "container_image") -go_image( +container_image( name = "image", - srcs = ["main.go"], - importpath = "...", + ... ) # or with rules_oci @@ -60,24 +59,36 @@ ytt( ":base.yaml", ":defaults.yaml", ":values.yaml", + ":image.digest", ], # or - # srcs = glob(["*.yaml"]), - image = ":image.digest", + # srcs = glob(["*.yaml"]) + [":image.digest"], ) ``` Which is equivalent to ```shell -ytt -f base.yaml -f defaults.yaml -f values.yaml --dangerous-allow-all-symlink-destinations +ytt \ + -f base.yaml \ + -f defaults.yaml \ + -f values.yaml \ + -f image.json.sha256, # file created by "image.digest" target + --dangerous-allow-all-symlink-destinations ``` -* `bazel build //:manifests` - generates yaml files and store it in cache. -* `bazel run //:manifests` - prints generated yaml files to stdout. -* `bazel run //:manifests | kubectl -n -f -` - applies generated manifests to k8s cluster. +Inside YAML template digest can be used like this: -Image digest is available inside yaml templates as `data.values.image_digest`. +```yaml +#@ load("@ytt:data", "data") +--- +image: #@ "repo/image@" + data.read("image.json.sha256").strip() +``` +### Commands + +* `bazel build //:manifests` - generates YAML files and retains them in cache. +* `bazel run //:manifests` - prints generated YAML files to stdout. +* `bazel run //:manifests | kubectl -n -f -` - applies generated manifests to k8s cluster. ## LICENSE diff --git a/docs/repositories.md b/docs/repositories.md index efcd7e4..a0d3242 100644 --- a/docs/repositories.md +++ b/docs/repositories.md @@ -93,7 +93,7 @@ Users can avoid this macro and do these steps themselves, if they want more cont | Name | Description | Default Value | | :------------- | :------------- | :------------- | | name | base name for all created repos, like "ytt" | "ytt" | -| version | Ytt tool version. Supported versions are listed in ytt/private/versions.bzl. | "0.46.2" | +| version | Ytt tool version. Supported versions are listed in [versions.bzl](/ytt/private/versions.bzl). | "0.46.2" | | kwargs | passed to each node_repositories call | none | diff --git a/docs/ytt.md b/docs/ytt.md index 26d0137..6b8a6cc 100644 --- a/docs/ytt.md +++ b/docs/ytt.md @@ -7,7 +7,7 @@ ## ytt
-ytt(name, image, srcs)
+ytt(name, srcs)
 
@@ -18,7 +18,6 @@ ytt(name, image, name | A unique name for this target. | Name | required | | -| image | (DEPRECATED) Target that generates a Docker image. Used for extracting image digest. | Label | optional | None | -| srcs | List of files that will be passed to ytt with -f param. | List of labels | required | | +| srcs | List of files that will be passed to ytt with -f parameter.

It could be *.yaml files as well as any other files which can be read later form inside yaml template with [data.read](https://carvel.dev/ytt/docs/v0.46.x/lang-ref-ytt/#data) Starlark function. See [examples](/examples/minimal) for details. | List of labels | required | | diff --git a/examples/minimal/BUILD.bazel b/examples/minimal/BUILD.bazel index a5c64a3..20aafb9 100644 --- a/examples/minimal/BUILD.bazel +++ b/examples/minimal/BUILD.bazel @@ -1,8 +1,22 @@ load("//:defs.bzl", "ytt") +# genrule emulates an output for the following target: +# +# load("@rules_oci//oci:defs.bzl", "oci_image") +# +# oci_image( +# name = "image", +# ... +# ) +genrule( + name = "image.digest", + outs = ["image.json.sha256"], + cmd = "echo sha256:ae59f3b71bffe15917f1b4e94218d1d46f21938ff1d1b2a04dfdf77f3763224e > $@", +) + ytt( name = "manifests", - srcs = glob(["*.yaml"]), + srcs = glob(["*.yaml"]) + [":image.digest"], ) sh_library( diff --git a/examples/minimal/base.yaml b/examples/minimal/base.yaml index a1d9c5f..14c7f0f 100644 --- a/examples/minimal/base.yaml +++ b/examples/minimal/base.yaml @@ -3,3 +3,5 @@ name: #@ data.values.name default: #@ data.values.default static: 1 +#! image.json.sha256 is a file created by image.digest target. +image: #@ "repo/image@" + data.read("image.json.sha256").strip() diff --git a/examples/minimal/validator.sh b/examples/minimal/validator.sh index fd98565..435d048 100755 --- a/examples/minimal/validator.sh +++ b/examples/minimal/validator.sh @@ -1,7 +1,8 @@ got="$(cat $1)" expected="name: YTT default: default value -static: 1" +static: 1 +image: repo/image@sha256:ae59f3b71bffe15917f1b4e94218d1d46f21938ff1d1b2a04dfdf77f3763224e" if [ "$got" = "$expected" ]; then echo "Passed" diff --git a/ytt/private/ytt.bzl b/ytt/private/ytt.bzl index 3e54a30..ded2303 100644 --- a/ytt/private/ytt.bzl +++ b/ytt/private/ytt.bzl @@ -1,40 +1,23 @@ def _impl(ctx): ytt = ctx.toolchains["//ytt:toolchain_type"] - runtools = [] - dv = [] - runfiles = ctx.runfiles() - if ctx.file.image: - image_digest = ctx.actions.declare_file("image_digest.out") - - ctx.actions.run_shell( - inputs = [ctx.file.image], - outputs = [image_digest], - command = "sed -e 's/^/image_digest: /g' {} > {}".format(ctx.file.image.path, image_digest.path), - ) - - runfiles = runfiles.merge(ctx.runfiles(files = [ctx.file.image, image_digest])) - runtools = [image_digest] - dv = ["--data-values-file", image_digest.path] - - yttargs = ctx.actions.args() - yttargs.add("--dangerous-allow-all-symlink-destinations") - yttargs.add_all("", dv) + args = ctx.actions.args() + args.add("--dangerous-allow-all-symlink-destinations") for f in ctx.files.srcs: - yttargs.add("-f", f.path) + args.add("-f", f.path) manifest = ctx.actions.declare_file(ctx.label.name + ".yaml") ctx.actions.run_shell( inputs = ctx.files.srcs, outputs = [manifest], - arguments = [yttargs], + arguments = [args], command = "{ytt} $@ > {manifest}".format( ytt = ytt.ytt_info.binary.path, manifest = manifest.path, ), - tools = [ytt.ytt_info.binary] + runtools, + tools = [ytt.ytt_info.binary], ) sh = ctx.actions.declare_file("{}_render.sh".format(ctx.label.name)) @@ -47,7 +30,7 @@ def _impl(ctx): return [DefaultInfo( files = depset([manifest]), - runfiles = runfiles.merge(ctx.runfiles(files = [manifest])), + runfiles = ctx.runfiles(files = [manifest]), executable = sh, )] @@ -57,11 +40,13 @@ ytt = rule( "srcs": attr.label_list( allow_files = True, mandatory = True, - doc = "List of files that will be passed to ytt with -f param.", - ), - "image": attr.label( - allow_single_file = True, - doc = "(DEPRECATED) Target that generates a Docker image. Used for extracting image digest.", + doc = """List of files that will be passed to `ytt` with `-f` parameter. + + It could be `*.yaml` files as well as any other files which can be read + later form inside yaml template with + [data.read](https://carvel.dev/ytt/docs/v0.46.x/lang-ref-ytt/#data) + Starlark function. See [examples](/examples/minimal) for details. + """ ), }, toolchains = ["//ytt:toolchain_type"], diff --git a/ytt/repositories.bzl b/ytt/repositories.bzl index 4fde954..a5b5089 100644 --- a/ytt/repositories.bzl +++ b/ytt/repositories.bzl @@ -97,7 +97,8 @@ def rules_ytt_register_toolchains(name = "ytt", version = "0.46.2", **kwargs): Args: name: base name for all created repos, like "ytt" - version: Ytt tool version. Supported versions are listed in `ytt/private/versions.bzl`. + version: Ytt tool version. Supported versions are listed in + [versions.bzl](/ytt/private/versions.bzl). **kwargs: passed to each node_repositories call """ for platform in PLATFORMS.keys():