-
Notifications
You must be signed in to change notification settings - Fork 1.3k
util/estargz: simplify implementation and define consts for labels #5933
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,84 @@ | ||
| package estargz | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| ctdlabels "github.com/containerd/containerd/v2/pkg/labels" | ||
| "github.com/containerd/stargz-snapshotter/estargz" | ||
| "github.com/containerd/containerd/v2/pkg/labels" | ||
| ocispecs "github.com/opencontainers/image-spec/specs-go/v1" | ||
| ) | ||
|
|
||
| const ( | ||
| // TargetRefLabel is a label which contains image reference. | ||
| // | ||
| // It is a copy of [stargz-snapshotter/fs/source.targetRefLabel]. | ||
| // | ||
| // [stargz-snapshotter/fs/source.targetRefLabel]: https://github.com/containerd/stargz-snapshotter/blob/v0.16.3/fs/source/source.go#L64-L65 | ||
| TargetRefLabel = "containerd.io/snapshot/remote/stargz.reference" | ||
|
|
||
| // TargetDigestLabel is a label which contains layer digest. | ||
| // | ||
| // It is a copy of [stargz-snapshotter/fs/source.targetDigestLabel]. | ||
| // | ||
| // [stargz-snapshotter/fs/source.targetDigestLabel]: https://github.com/containerd/stargz-snapshotter/blob/v0.16.3/fs/source/source.go#L67-L68 | ||
| TargetDigestLabel = "containerd.io/snapshot/remote/stargz.digest" | ||
|
|
||
| // TargetImageLayersLabel is a label which contains layer digests contained in | ||
| // the target image. | ||
| // | ||
| // It is a copy of [stargz-snapshotter/fs/source.targetImageLayersLabel]. | ||
| // | ||
| // [stargz-snapshotter/fs/source.targetImageLayersLabel]: https://github.com/containerd/stargz-snapshotter/blob/v0.16.3/fs/source/source.go#L70-L72 | ||
| TargetImageLayersLabel = "containerd.io/snapshot/remote/stargz.layers" | ||
|
|
||
| // TargetSessionLabel is a label which contains session IDs usable for | ||
| // authenticating the target snapshot. | ||
| // | ||
| // It has no equivalent in github.com/containerd/stargz-snapshotter. | ||
| TargetSessionLabel = "containerd.io/snapshot/remote/stargz.session" | ||
| ) | ||
|
|
||
| const ( | ||
| // TOCJSONDigestAnnotation is an annotation for an image layer. This stores the | ||
| // digest of the TOC JSON. | ||
| // This annotation is valid only when it is specified in `.[]layers.annotations` | ||
| // of an image manifest. | ||
| // | ||
| // This is a copy of [estargz.TOCJSONDigestAnnotation] | ||
| // | ||
| // [estargz.TOCJSONDigestAnnotation]: https://pkg.go.dev/github.com/containerd/stargz-snapshotter/estargz@v0.16.3#TOCJSONDigestAnnotation | ||
| TOCJSONDigestAnnotation = "containerd.io/snapshot/stargz/toc.digest" | ||
|
|
||
| // StoreUncompressedSizeAnnotation is an additional annotation key for eStargz to enable lazy | ||
| // pulling on containers/storage. Stargz Store is required to expose the layer's uncompressed size | ||
| // to the runtime but current OCI image doesn't ship this information by default. So we store this | ||
| // to the special annotation. | ||
| // | ||
| // This is a copy of [estargz.StoreUncompressedSizeAnnotation] | ||
| // | ||
| // [estargz.StoreUncompressedSizeAnnotation]: https://pkg.go.dev/github.com/containerd/stargz-snapshotter/estargz@v0.16.3#StoreUncompressedSizeAnnotation | ||
| StoreUncompressedSizeAnnotation = "io.containers.estargz.uncompressed-size" | ||
|
Comment on lines
+58
to
+59
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious why this uses a different prefix (
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is because this annotation is necessary to make the filesystem work with |
||
| ) | ||
|
|
||
| func SnapshotLabels(ref string, descs []ocispecs.Descriptor, targetIndex int) map[string]string { | ||
| if len(descs) < targetIndex { | ||
| return nil | ||
| } | ||
| desc := descs[targetIndex] | ||
| labels := make(map[string]string) | ||
| for _, k := range []string{estargz.TOCJSONDigestAnnotation, estargz.StoreUncompressedSizeAnnotation} { | ||
| labels[k] = desc.Annotations[k] | ||
| } | ||
| labels["containerd.io/snapshot/remote/stargz.reference"] = ref | ||
| labels["containerd.io/snapshot/remote/stargz.digest"] = desc.Digest.String() | ||
| var ( | ||
| layersKey = "containerd.io/snapshot/remote/stargz.layers" | ||
| layers string | ||
| ) | ||
|
|
||
| var layers string | ||
| for _, l := range descs[targetIndex:] { | ||
| ls := fmt.Sprintf("%s,", l.Digest.String()) | ||
| // This avoids the label hits the size limitation. | ||
| // Skipping layers is allowed here and only affects performance. | ||
| if err := ctdlabels.Validate(layersKey, layers+ls); err != nil { | ||
| if err := labels.Validate(TargetImageLayersLabel, layers+l.Digest.String()); err != nil { | ||
| break | ||
| } | ||
| layers += ls | ||
| layers += l.Digest.String() + "," | ||
|
Comment on lines
+72
to
+75
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slight change here; previously this would check the length including the trailing |
||
| } | ||
| return map[string]string{ | ||
| TOCJSONDigestAnnotation: desc.Annotations[TOCJSONDigestAnnotation], | ||
| StoreUncompressedSizeAnnotation: desc.Annotations[StoreUncompressedSizeAnnotation], | ||
| TargetRefLabel: ref, | ||
| TargetDigestLabel: desc.Digest.String(), | ||
| TargetImageLayersLabel: strings.TrimSuffix(layers, ","), | ||
| } | ||
| labels[layersKey] = strings.TrimSuffix(layers, ",") | ||
| return labels | ||
| } | ||
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.
Why isn't this imported (or upstream constant just used instead). Same for next.
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.
I went back-and-forth a bit; initially I had these as alias (
TOCJSONDigestAnnotation = estargz.TOCJSONDigestAnnotation), but as we already would have to define the non-exported ones locally (and theestargzpackage containing much more than just the consts), and these consts to not change, I thought it would be OK to just have our own copy, as long as we refer to where they originate from.