Skip to content

Commit

Permalink
list,oci_index: automatically add inbuilt annotations on add
Browse files Browse the repository at this point in the history
Adding instance to list should automatically add special annotations if
any.

Signed-off-by: Aditya R <[email protected]>
  • Loading branch information
flouthoc committed Jun 12, 2023
1 parent 805cb83 commit 5e1499b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 7 additions & 5 deletions internal/manifest/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package manifest
import (
"fmt"

compression "github.com/containers/image/v5/pkg/compression/types"
"github.com/containers/image/v5/types"
digest "github.com/opencontainers/go-digest"
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -88,11 +89,12 @@ type ListEdit struct {
UpdateMediaType string

// If Op = ListEditAdd. All fields must be set.
AddDigest digest.Digest
AddSize int64
AddMediaType string
AddPlatform *imgspecv1.Platform
AddAnnotations map[string]string
AddDigest digest.Digest
AddSize int64
AddMediaType string
AddPlatform *imgspecv1.Platform
AddAnnotations map[string]string
AddCompressionAnnotation []compression.Algorithm
}

// ListPublicFromBlob parses a list of manifests.
Expand Down
16 changes: 15 additions & 1 deletion internal/manifest/oci_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime"

platform "github.com/containers/image/v5/internal/pkg/platform"
compression "github.com/containers/image/v5/pkg/compression/types"
"github.com/containers/image/v5/types"
"github.com/opencontainers/go-digest"
imgspec "github.com/opencontainers/image-spec/specs-go"
Expand All @@ -22,7 +23,8 @@ const (
// That also suggests that this instance benefits from
// Zstd compression, so it can be preferred by compatible consumers over instances that
// use gzip, depending on their local policy.
OCI1InstanceAnnotationCompressionZSTD = "io.github.containers.compression.zstd"
OCI1InstanceAnnotationCompressionZSTD = "io.github.containers.compression.zstd"
OCI1InstanceAnnotationCompressionZSTDValue = "true"
)

// OCI1IndexPublic is just an alias for the OCI index type, but one which we can
Expand Down Expand Up @@ -76,6 +78,17 @@ func (index *OCI1IndexPublic) UpdateInstances(updates []ListUpdate) error {
return index.editInstances(editInstances)
}

func addCompressionAnnotations(compressionAlgorithm []compression.Algorithm, annotationsMap map[string]string) {
for _, algo := range compressionAlgorithm {
switch algo.Name() {
case compression.ZstdAlgorithmName:
annotationsMap[OCI1InstanceAnnotationCompressionZSTD] = OCI1InstanceAnnotationCompressionZSTDValue
default:
continue
}
}
}

func (index *OCI1IndexPublic) editInstances(editInstances []ListEdit) error {
addedEntries := []imgspecv1.Descriptor{}
for i, editInstance := range editInstances {
Expand Down Expand Up @@ -103,6 +116,7 @@ func (index *OCI1IndexPublic) editInstances(editInstances []ListEdit) error {
}
index.Manifests[targetIndex].MediaType = editInstance.UpdateMediaType
case ListOpAdd:
addCompressionAnnotations(editInstance.AddCompressionAnnotation, editInstance.AddAnnotations)
addedEntries = append(addedEntries, imgspecv1.Descriptor{
MediaType: editInstance.AddMediaType,
Size: editInstance.AddSize,
Expand Down

0 comments on commit 5e1499b

Please sign in to comment.