Skip to content

Commit

Permalink
Replace image property with data.read example. (#3)
Browse files Browse the repository at this point in the history
* Delete `image` property from `ytt` rule.
  • Loading branch information
ekhabarov committed Dec 10, 2023
1 parent e46fad3 commit e72ff9a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 46 deletions.
33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <namespace> -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 <namespace> -f -` - applies generated manifests to k8s cluster.

## LICENSE

Expand Down
2 changes: 1 addition & 1 deletion docs/repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Users can avoid this macro and do these steps themselves, if they want more cont
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="rules_ytt_register_toolchains-name"></a>name | base name for all created repos, like "ytt" | <code>"ytt"</code> |
| <a id="rules_ytt_register_toolchains-version"></a>version | Ytt tool version. Supported versions are listed in <code>ytt/private/versions.bzl</code>. | <code>"0.46.2"</code> |
| <a id="rules_ytt_register_toolchains-version"></a>version | Ytt tool version. Supported versions are listed in [versions.bzl](/ytt/private/versions.bzl). | <code>"0.46.2"</code> |
| <a id="rules_ytt_register_toolchains-kwargs"></a>kwargs | passed to each node_repositories call | none |


5 changes: 2 additions & 3 deletions docs/ytt.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
## ytt

<pre>
ytt(<a href="#ytt-name">name</a>, <a href="#ytt-image">image</a>, <a href="#ytt-srcs">srcs</a>)
ytt(<a href="#ytt-name">name</a>, <a href="#ytt-srcs">srcs</a>)
</pre>


Expand All @@ -18,7 +18,6 @@ ytt(<a href="#ytt-name">name</a>, <a href="#ytt-image">image</a>, <a href="#ytt-
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="ytt-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="ytt-image"></a>image | (DEPRECATED) Target that generates a Docker image. Used for extracting image digest. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
| <a id="ytt-srcs"></a>srcs | List of files that will be passed to ytt with -f param. | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
| <a id="ytt-srcs"></a>srcs | List of files that will be passed to <code>ytt</code> with <code>-f</code> parameter.<br><br> It could be <code>*.yaml</code> 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. | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |


16 changes: 15 additions & 1 deletion examples/minimal/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
2 changes: 2 additions & 0 deletions examples/minimal/base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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()
3 changes: 2 additions & 1 deletion examples/minimal/validator.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
41 changes: 13 additions & 28 deletions ytt/private/ytt.bzl
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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,
)]

Expand All @@ -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"],
Expand Down
3 changes: 2 additions & 1 deletion ytt/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit e72ff9a

Please sign in to comment.