Skip to content

Commit

Permalink
Updating go implementation for zstd, removing the defer call, which c…
Browse files Browse the repository at this point in the history
…auses invalid memory access when the writer closes. We already have calls to Close(), so we really don't need to defer this.

Updating oci_image_layer rule to define outputs inside the impl, so that it can choose the correct extension for the archive.
  • Loading branch information
alandonham committed Dec 3, 2024
1 parent 8759548 commit 72246d8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
2 changes: 0 additions & 2 deletions go/cmd/ocitool/createlayer_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ func CreateLayerCmd(c *cli.Context) error {
if(config.UseZstd){
zstdWriter = zstd.NewWriter(wc)
mediaType = ocispec.MediaTypeImageLayerZstd
defer zstdWriter.Close()
tw = tar.NewWriter(zstdWriter)
} else {
gzipWriter = gzip.NewWriter(wc)
gzipWriter.Name = path.Base(out.Name())
mediaType = ocispec.MediaTypeImageLayerGzip
defer gzipWriter.Close()
tw = tar.NewWriter(gzipWriter)
}

Expand Down
16 changes: 10 additions & 6 deletions oci/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ def _oci_image_layer_impl(ctx):

descriptor_file = ctx.actions.declare_file("{}.descriptor.json".format(ctx.label.name))

archive = None
if ctx.attr.zstd_compression:
archive = ctx.actions.declare_file(ctx.label.name + ".tar.zst")
else:
archive = ctx.actions.declare_file(ctx.label.name + ".tar.gz")

ctx.actions.run(
executable = toolchain.sdk.ocitool,
arguments = [
"create-layer",
"--out={}".format(ctx.outputs.layer.path),
"--out={}".format(archive.path),
"--outd={}".format(descriptor_file.path),
"--dir={}".format(ctx.attr.directory),
"--bazel-label={}".format(ctx.label),
Expand All @@ -42,15 +48,16 @@ def _oci_image_layer_impl(ctx):
inputs = ctx.files.files + ctx.files.file_map,
outputs = [
descriptor_file,
ctx.outputs.layer,
archive,
],
)

return [
OCIDescriptor(
descriptor_file = descriptor_file,
file = ctx.outputs.layer,
file = archive,
),
DefaultInfo(files = depset([archive])),
]

oci_image_layer = rule(
Expand All @@ -77,9 +84,6 @@ oci_image_layer = rule(
),
},
toolchains = ["@com_github_datadog_rules_oci//oci:toolchain"],
outputs = {
"layer": "%{name}-layer.tar.gz",
},
)

def _oci_image_index_impl(ctx):
Expand Down

0 comments on commit 72246d8

Please sign in to comment.