Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alan.donham/hackathon compression #67

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

alandonham
Copy link

@alandonham alandonham commented Dec 3, 2024

This changes adds an option to compress using the zstd library. The library used is in pure Go, which allows cross compilation to work as expected.

Going forward, we likely will want to look at tuning the compression level, though for now it remains at the default value.

Adding hooks for zstd compression in _oci_image_layer_impl.
…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.
Comment on lines 39 to 53
var tw *tar.Writer
var zstdWriter *zstd.Writer
var gzipWriter *gzip.Writer
var mediaType string

if config.UseZstd {
zstdWriter = zstd.NewWriter(wc)
mediaType = ocispec.MediaTypeImageLayerZstd
tw = tar.NewWriter(zstdWriter)
} else {
gzipWriter = gzip.NewWriter(wc)
gzipWriter.Name = path.Base(out.Name())
mediaType = ocispec.MediaTypeImageLayerGzip
tw = tar.NewWriter(gzipWriter)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: other than tar.Writer being its own thing, you shouldn't need to declare compression-specific Writers. Consider instead having the compression writer being a generic interface. io.WriteCloser might be appropriate.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look into this. I attempted this approach initially, which resulted in some compilation errors due to the interface not being fully implemented, so I put this together to keep making progress.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gzip and zstd Writer interfaces are not 1 to 1. They both implement a similar interface, but diverge enough that we cannot use a common variable to store the Writer.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but they don't need to be.

If all you need is Write and Close, then you can just have the type be an io.WriteCloser, and pass that around everywhere. That way, your function implementations can be generic with respect to what the types can do, not what they are, if that makes sense.

oci/image.bzl Outdated Show resolved Hide resolved
@DataDog DataDog deleted a comment from nacl Dec 3, 2024
@alandonham alandonham marked this pull request as ready for review December 4, 2024 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants