-
Notifications
You must be signed in to change notification settings - Fork 386
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
Refactor blob copying and compression #1579
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The over-300-line-long function would benefit from splitting up, and copy/copy.go has grown entirely too long. As a first step, move it to a new copy/blob.go. Only moves unmodified code, should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
... so that the file reads from top-level callers down to implementation details. Only moves unchanged code, should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
We will use srcStream for another purpose. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
The purpose of sourceStream is to make it a bit easier to split the blob copy pipeline stages into separate functions; they will update a *sourceStream, which we can pass a single parameter. For now, build it and use it to initialize a digestingReader; then update other code that should consume the current pipeline data. NOTE: That is a judgement call, it is not always obvious what data was intended to be used and what is a bug. As a geneeral principle, use srcInfo (the original unmodified data) in user-visible error reports, notably using the original digest. Otherwise, lean towards using srcStream.info. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
One part of moving to use stream throughout the pipeline. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Use stream.info throughout. Should not change behavior. (In some cases that's dubious, we are currently losing data like annotations compression/decompression. This commit doesn't change that, only adds a FIXME? to eventually review.) Signed-off-by: Miloslav Trmač <mitr@redhat.com>
…cryptionStep Only moves the code with minimal necessary adjustments, should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
243bfff
to
a2ce743
Compare
Should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This smaller function can use simpler identifiers. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
…cryptionStep Only moves the code with minimal necessary adjustments, should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Make the "not encrypting" case truly special, and then we don't need extra variables across the function. Should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
It contains decompression code as well. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
…nStep Only moves the code with minimal necessary adjustments, should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
We can use shorter variable names in this short function now. Should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
…n edits Only moves code, should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Only moves unchanged code, should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Only moves the code with minimal necessary adjustments, should not change behavior. Those adjustments are not completely trivial in this case: we could just do a "defer stream.Close()" in copyBlobFromStream, now we need a separate closers field in blobPipelineCompressionStepData. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
The compression meaning is clear in context here. Should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Except for the encrypted content case, we already have the data available, and this will allow us to just pass around a *blobPipelineDetectCompressionStepData without an extra parameter for srcCompressorName. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
That's actually shorter because in most cases we just use a field initializer instead of setting a variable; and we don't need that variable in the first place. For now, do just the minimal edit, we will clean this up a bit later. Should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Don't use else after each step that returns early. The function nows looks like a set of ~special cases, with the general fallback of doing nothing. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
to avoid ambiguity. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
…nStep ... so that we only implement the pipe handling once. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
The logic is a simple sequence of condition + decision, which is fairly regullar, and doesn't have to be in a single function. So, split it up. Eliminate the recently-introduced closers array again, in most cases. Should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
... now that it only could effectively apply to a single element. Shoud not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
The alternatives are, mostly, things to _try_; we can typically fall back to just using the original. So, just use a loop to take advantage of the regular structure. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
a2ce743
to
2756d70
Compare
Wooooow, I need to grab a coffee before reviewing 😄 |
vrothberg
reviewed
Jun 16, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merged
mtrmac
added a commit
to mtrmac/image
that referenced
this pull request
Jul 21, 2022
copy.Image can now copy non-image OCI artifacts. Added support for sigstore signatures: they (and related cosign attachments) can be copied along with images after opt-in in registries.d. Signatures can be created by copy.Image and enforced via policy.json (currently with public/private key pairs only). Now requires Go 1.17. GPGMe now must be new enough to be visible via pkg-config. github.com/pkg/errors is no longer used; that might affect caller-observable error types (in particular, errors.{As,Is} might need to be used instead of pkg/errors.Cause). Changes default paths on FreeBSD. - Remove unused Makefile variables - Config files should live in /usr/local on FreeBSD - docker: validate received parts - Use go env to fetch the go path - docker: add workaround for CloudFront - Improve errors messages when image missing from list - Stop calling gpgme-config - Fix codespell errors - Make sure github.com/opencontainers/runc >= 1.1.2 is used - Cirrus: use Ubuntu 22.04 LTS - Merge pull request containers#1576 from mtrmac/private-image - Merge pull request containers#1577 from mtrmac/mocks - Merge pull request containers#1571 from mtrmac/go1.17 - Merge pull request containers#1578 from mtrmac/sourced-image-struct - Fix error on parallel multiple image pullings with additionallayerstore - Merge pull request containers#1579 from mtrmac/copy-layers-refactor - Reject OCI artifacts in manifest.OCI1.ImageID - Reject OCI artifacts in manifest.OCI1.Inspect - Refuse to convert non-image OCI artifacts to Docker formats - Reject OCI artifacts in image.manifestOCI1.OCIConfig - Introduce SourcedImage.CanChangeLayerCompression, use it in copy.Image - Use an updated CI image - Use strings.ReplaceAll instead of strings.Replace(..., -1) - Move the main helper removal case to the main path on RemoveAllAuthentication - Merge pull request containers#1588 from mtrmac/pkg_errors - Merge pull request containers#1589 from mtrmac/private-dest-impls - Merge pull request containers#1590 from mtrmac/private-src-impls - Merge pull request containers#1592 from mtrmac/blobcache-wrap-private - Use "io.ReadAll" instead of "os.ReadAll" - Merge pull request containers#1596 from mtrmac/cosign-payload - Generalize copy.Image to be able to copy signatures with any format - Merge pull request containers#1593 from mtrmac/cosign-sigs - Introduce signature.Cosign as a format - Add use-cosign-attachments to registries.d/*.yaml - Add support for reading and writing Cosign attachments, incl. signatures - Merge pull request containers#1595 from mtrmac/cosign-docker - Add support for creating Cosign signatures - Fix a long-standing incorrect comment - Fix JSON syntax in the policy.json(5) man page - Correctly decode Cosign-generated payloads - Add Cosign verification support - s/sigstore/lookaside/g in comments and documentation - Refer to lookasideStorage instead of signatureStorage in code - Add lookaside and lookaside-staging, hide sigstore and sigstore-staging - Merge pull request containers#1605 from mtrmac/sigstore - Fix a typo in error messages - Remove a copy&pasted test entry - Add context to some test failures - Use more valid data in TestPRSignedByIsSignatureAuthorAccepted - Generalize keyPath/keyData exclusivity checks - Remove repetition in tests - Accept multiple keyrings in newEphemeralGPGSigningMechanism - Allow accepting multiple GPG keyrings via signedBy.keyPaths - Switch to golang native error wrapping - Point out use-sigstore-registries in sigstoreSigned documentation - Use .pub extension for public keys in sigstoreSigned examples - copy: print copy info once when writer==io.Discard - Silence a "potentially unused parameter" warning - Read signatures from UnparsedImage instead of ImageSource directly - Consolidate reading messages, and checking for support, into a helper - build(deps): bump github.com/containers/storage from 1.40.0 to 1.40.2 - build(deps): bump github.com/docker/docker - build(deps): bump github.com/klauspost/compress from 1.15.2 to 1.15.3 - build(deps): bump github.com/klauspost/compress from 1.15.3 to 1.15.4 - build(deps): bump github.com/docker/docker - build(deps): bump github.com/proglottis/gpgme from 0.1.1 to 0.1.2 - build(deps): bump github.com/vbauerster/mpb/v7 from 7.4.1 to 7.4.2 - build(deps): bump github.com/imdario/mergo from 0.3.12 to 0.3.13 - build(deps): bump github.com/klauspost/compress from 1.15.4 to 1.15.5 - build(deps): bump github.com/sylabs/sif/v2 from 2.7.0 to 2.7.1 - build(deps): bump github.com/klauspost/compress from 1.15.5 to 1.15.6 - build(deps): bump github.com/stretchr/testify from 1.7.1 to 1.7.2 - build(deps): bump github.com/docker/docker - build(deps): bump github.com/stretchr/testify from 1.7.2 to 1.7.4 - build(deps): bump github.com/stretchr/testify from 1.7.4 to 1.7.5 - build(deps): bump github.com/stretchr/testify from 1.7.5 to 1.8.0 - build(deps): bump github.com/klauspost/compress from 1.15.6 to 1.15.7 - build(deps): bump github.com/proglottis/gpgme from 0.1.2 to 0.1.3 - build(deps): bump github.com/klauspost/compress from 1.15.7 to 1.15.8 - build(deps): bump github.com/sirupsen/logrus from 1.8.1 to 1.9.0 - build(deps): bump github.com/theupdateframework/go-tuf - build(deps): bump github.com/BurntSushi/toml from 1.1.0 to 1.2.0 Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Merged
mtrmac
added a commit
to mtrmac/image
that referenced
this pull request
Jul 21, 2022
copy.Image can now copy non-image OCI artifacts. Added support for sigstore signatures: they (and related cosign attachments) can be copied along with images after opt-in in registries.d. Signatures can be created by copy.Image and enforced via policy.json (currently with public/private key pairs only). Now requires Go 1.17. GPGMe now must be new enough to be visible via pkg-config. github.com/pkg/errors is no longer used; that might affect caller-observable error types (in particular, errors.{As,Is} might need to be used instead of pkg/errors.Cause). Changes default paths on FreeBSD. - Remove unused Makefile variables - Config files should live in /usr/local on FreeBSD - docker: validate received parts - Use go env to fetch the go path - docker: add workaround for CloudFront - Improve errors messages when image missing from list - Stop calling gpgme-config - Fix codespell errors - Make sure github.com/opencontainers/runc >= 1.1.2 is used - Cirrus: use Ubuntu 22.04 LTS - Merge pull request containers#1576 from mtrmac/private-image - Merge pull request containers#1577 from mtrmac/mocks - Merge pull request containers#1571 from mtrmac/go1.17 - Merge pull request containers#1578 from mtrmac/sourced-image-struct - Fix error on parallel multiple image pullings with additionallayerstore - Merge pull request containers#1579 from mtrmac/copy-layers-refactor - Reject OCI artifacts in manifest.OCI1.ImageID - Reject OCI artifacts in manifest.OCI1.Inspect - Refuse to convert non-image OCI artifacts to Docker formats - Reject OCI artifacts in image.manifestOCI1.OCIConfig - Introduce SourcedImage.CanChangeLayerCompression, use it in copy.Image - Use an updated CI image - Use strings.ReplaceAll instead of strings.Replace(..., -1) - Move the main helper removal case to the main path on RemoveAllAuthentication - Merge pull request containers#1588 from mtrmac/pkg_errors - Merge pull request containers#1589 from mtrmac/private-dest-impls - Merge pull request containers#1590 from mtrmac/private-src-impls - Merge pull request containers#1592 from mtrmac/blobcache-wrap-private - Use "io.ReadAll" instead of "os.ReadAll" - Merge pull request containers#1596 from mtrmac/cosign-payload - Generalize copy.Image to be able to copy signatures with any format - Merge pull request containers#1593 from mtrmac/cosign-sigs - Introduce signature.Cosign as a format - Add use-cosign-attachments to registries.d/*.yaml - Add support for reading and writing Cosign attachments, incl. signatures - Merge pull request containers#1595 from mtrmac/cosign-docker - Add support for creating Cosign signatures - Fix a long-standing incorrect comment - Fix JSON syntax in the policy.json(5) man page - Correctly decode Cosign-generated payloads - Add Cosign verification support - s/sigstore/lookaside/g in comments and documentation - Refer to lookasideStorage instead of signatureStorage in code - Add lookaside and lookaside-staging, hide sigstore and sigstore-staging - Merge pull request containers#1605 from mtrmac/sigstore - Fix a typo in error messages - Remove a copy&pasted test entry - Add context to some test failures - Use more valid data in TestPRSignedByIsSignatureAuthorAccepted - Generalize keyPath/keyData exclusivity checks - Remove repetition in tests - Accept multiple keyrings in newEphemeralGPGSigningMechanism - Allow accepting multiple GPG keyrings via signedBy.keyPaths - Switch to golang native error wrapping - Point out use-sigstore-registries in sigstoreSigned documentation - Use .pub extension for public keys in sigstoreSigned examples - copy: print copy info once when writer==io.Discard - Silence a "potentially unused parameter" warning - Read signatures from UnparsedImage instead of ImageSource directly - Consolidate reading messages, and checking for support, into a helper - build(deps): bump github.com/containers/storage from 1.40.0 to 1.40.2 - build(deps): bump github.com/docker/docker - build(deps): bump github.com/klauspost/compress from 1.15.2 to 1.15.3 - build(deps): bump github.com/klauspost/compress from 1.15.3 to 1.15.4 - build(deps): bump github.com/docker/docker - build(deps): bump github.com/proglottis/gpgme from 0.1.1 to 0.1.2 - build(deps): bump github.com/vbauerster/mpb/v7 from 7.4.1 to 7.4.2 - build(deps): bump github.com/imdario/mergo from 0.3.12 to 0.3.13 - build(deps): bump github.com/klauspost/compress from 1.15.4 to 1.15.5 - build(deps): bump github.com/sylabs/sif/v2 from 2.7.0 to 2.7.1 - build(deps): bump github.com/klauspost/compress from 1.15.5 to 1.15.6 - build(deps): bump github.com/stretchr/testify from 1.7.1 to 1.7.2 - build(deps): bump github.com/docker/docker - build(deps): bump github.com/stretchr/testify from 1.7.2 to 1.7.4 - build(deps): bump github.com/stretchr/testify from 1.7.4 to 1.7.5 - build(deps): bump github.com/stretchr/testify from 1.7.5 to 1.8.0 - build(deps): bump github.com/klauspost/compress from 1.15.6 to 1.15.7 - build(deps): bump github.com/proglottis/gpgme from 0.1.2 to 0.1.3 - build(deps): bump github.com/klauspost/compress from 1.15.7 to 1.15.8 - build(deps): bump github.com/sirupsen/logrus from 1.8.1 to 1.9.0 - build(deps): bump github.com/theupdateframework/go-tuf - build(deps): bump github.com/BurntSushi/toml from 1.1.0 to 1.2.0 Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is the last refactor PR for #1574 , I promise.
copyBlobFromStream
has grown into a 300-line function carrying a lot of variables, and it’s become increasingly untenable to add more logic to it, even if that logic is conceptually quite local. It’s well past the time it should have been split up. I have hoped to do that after unit tests exist, sadly that’s not panned out.Major themes:
copyBlobFromStream
and closest helpers tocopy/blob.go
.sourceStream
. The copy pipeline is still not just ignorant plumbing, it carries around all the state (e.g. how encryption and decryption interact, and how the outcome of modifying the stream needs to be reflected in the updated blob descriptor); but it’s much more encapsulated now:blobPipeline…Step
functions; those functions returnblobPipeline…StepData
structures. The top-level pipeline, and the following pipeline stops, thus still have access to the same data as before, but with the…StepData
wrappers, it’s much fewer variables to track, and the step interdependencies are clearly visible.…StepData
objects, allowing more encapsulation. The top-level code is not completely ignorant, it e.g. it needs to be aware of how the various edits interact (like the inability to compress and decompress at the same time).copy/encryption.go
, mostly unchanged.copy/compression.go
. The broad outline is the same, but it’s split even further:copyBlobFromStream
with extra variables.Various corner cases of the copy pipeline are somewhat dubious, and that’s been made maybe more visible by these changes (e.g. how we don’t immediately update
MediaType
on encryption/decryption, but make checks based on its value anyway; how some decisions are done on the original data even if we have decrypted it in the meantime; and more). For the most part, this PR leans towards not changing those things, to decrease risk of destabilizing the codebase.See the individual commits for details. I’m afraid this is large, and with minimal test coverage. Ultimately, I think this is a good longer-term direction, but I could accept an opinion that this refactoring is currently not worth it and we should just live with making
copyBlobFromStream
uglier for some(?) more time.