Skip to content

Commit

Permalink
fix: add history entries for tars
Browse files Browse the repository at this point in the history
Signed-off-by: thesayyn <[email protected]>
  • Loading branch information
thesayyn committed Jul 18, 2024
1 parent e822897 commit e422780
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 6 deletions.
6 changes: 5 additions & 1 deletion examples/assert.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def assert_oci_config(
architecture_eq = None,
os_eq = None,
variant_eq = None,
labels_eq = None):
labels_eq = None,
history_eq = None):
"assert that an oci_image has specified config metadata according to https://github.com/opencontainers/image-spec/blob/main/config.md"
pick = []

Expand Down Expand Up @@ -66,6 +67,9 @@ def assert_oci_config(
if variant_eq:
config_json["variant"] = variant_eq

if history_eq:
config_json["history"] = history_eq

pick += ["." + k for k in config_json.keys()]

if len(config.keys()):
Expand Down
36 changes: 36 additions & 0 deletions examples/assertion/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,42 @@ sh_test(
data = [":case12"],
)

# Case 13: image should have history entries
tar(
name = "case13_empty_tar",
srcs = [],
)

tar(
name = "case13_empty_tar2",
srcs = [],
)

oci_image(
name = "case13",
architecture = "arm64",
os = "linux",
tars = [
":case13_empty_tar",
":case13_empty_tar2",
],
)

assert_oci_config(
name = "test_case13",
history_eq = [
{
"created": "1970-01-01T00:00:00Z",
"created_by": "bazel build //examples/assertion:case13_empty_tar",
},
{
"created": "1970-01-01T00:00:00Z",
"created_by": "bazel build //examples/assertion:case13_empty_tar2",
},
],
image = ":case13",
)

# build them as test.
build_test(
name = "test",
Expand Down
2 changes: 1 addition & 1 deletion examples/multi_architecture_image/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ genrule(
assert_contains(
name = "check_digest",
actual = ":hash",
expected = "sha256:9cc41e3583d5385aa6adc686c15f4e4625ec46fceabde54146add048aa95e716",
expected = "sha256:a2b8ae94672721788b67874f27cf3574fada3ccccc69f483bcb43de653573fe0",
)
19 changes: 17 additions & 2 deletions oci/private/descriptor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set -o pipefail -o errexit -o nounset
PATH="$HPATH:$PATH"
archive="$1"
output="$2"
label="$3"

digest="$(regctl digest <"$archive")"
diffid="$digest"
Expand All @@ -28,5 +29,19 @@ elif zstd -t <"$archive" 2>/dev/null; then
diffid=$(zstd --decompress --format=zstd <"$archive" | regctl digest)
fi

jq -n --arg compression "$compression" --arg diffid "$diffid" --arg digest "$digest" --argjson size "$size" \
'{digest: $digest, diffid: $diffid, compression: $compression, size: $size }' >"$output"
jq -n \
--arg compression "$compression" \
--arg diffid "$diffid" \
--arg digest "$digest" \
--argjson size "$size" \
--arg label "$label" \
'{
digest: $digest,
diffid: $diffid,
compression: $compression,
size: $size,
history: {
created: "1970-01-01T00:00:00Z",
created_by: "bazel build \($label)"
}
}' >"$output"
1 change: 1 addition & 0 deletions oci/private/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def _calculate_descriptor(ctx, idx, layer, zstd, jq, coreutils, regctl):
args = ctx.actions.args()
args.add(layer)
args.add(descriptor)
args.add(layer.owner)
ctx.actions.run(
executable = util.maybe_wrap_launcher_for_windows(ctx, ctx.executable._descriptor_sh),
inputs = [layer],
Expand Down
4 changes: 2 additions & 2 deletions oci/private/image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function base_from_scratch() {
layers: []
}' | update_manifest
# Create the image config when there is annotations
jq -n --argjson platform "$platform" '{config:{}, rootfs:{type: "layers", diff_ids:[]}} + $platform' | update_config >/dev/null
jq -n --argjson platform "$platform" '{config:{}, history:[], rootfs:{type: "layers", diff_ids:[]}} + $platform' | update_config >/dev/null
}

function base_from() {
Expand Down Expand Up @@ -91,7 +91,7 @@ function add_layer() {
desc="$(jq --arg comp_ext "${comp_ext}" '.compression |= (if . != "" then "\($comp_ext)\(.)" end)' <<< "$desc")"

new_config_digest=$(
get_config | jq --argjson desc "$desc" '.rootfs.diff_ids += [$desc.diffid]' | update_config
get_config | jq --argjson desc "$desc" '.rootfs.diff_ids += [$desc.diffid] | .history += [$desc.history]' | update_config
)

get_manifest |
Expand Down

0 comments on commit e422780

Please sign in to comment.