Merge pull request #8224 from vvoland/label-dist-source

labels: Add LabelDistributionSource
This commit is contained in:
Akihiro Suda 2023-03-08 21:37:00 +09:00 committed by GitHub
commit 71a7234f67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 11 deletions

View File

@ -27,6 +27,7 @@ import (
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images" "github.com/containerd/containerd/images"
imagelist "github.com/containerd/containerd/integration/images" imagelist "github.com/containerd/containerd/integration/images"
"github.com/containerd/containerd/labels"
"github.com/containerd/containerd/platforms" "github.com/containerd/containerd/platforms"
ocispec "github.com/opencontainers/image-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1"
) )
@ -105,7 +106,7 @@ func TestImagePullWithDistSourceLabel(t *testing.T) {
defer client.ImageService().Delete(ctx, imageName) defer client.ImageService().Delete(ctx, imageName)
cs := client.ContentStore() cs := client.ContentStore()
key := fmt.Sprintf("containerd.io/distribution.source.%s", source) key := labels.LabelDistributionSource + "." + source
// only check the target platform // only check the target platform
childrenHandler := images.LimitManifests(images.ChildrenHandler(cs), pMatcher, 1) childrenHandler := images.LimitManifests(images.ChildrenHandler(cs), pMatcher, 1)

View File

@ -23,3 +23,7 @@ const LabelUncompressed = "containerd.io/uncompressed"
// LabelSharedNamespace is added to a namespace to allow that namespaces // LabelSharedNamespace is added to a namespace to allow that namespaces
// contents to be shared. // contents to be shared.
const LabelSharedNamespace = "containerd.io/namespace.shareable" const LabelSharedNamespace = "containerd.io/namespace.shareable"
// LabelDistributionSource is added to content to indicate its origin.
// e.g., "containerd.io/distribution.source.docker.io=library/redis"
const LabelDistributionSource = "containerd.io/distribution.source"

View File

@ -30,11 +30,6 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1"
) )
var (
// labelDistributionSource describes the source blob comes from.
labelDistributionSource = "containerd.io/distribution.source"
)
// AppendDistributionSourceLabel updates the label of blob with distribution source. // AppendDistributionSourceLabel updates the label of blob with distribution source.
func AppendDistributionSourceLabel(manager content.Manager, ref string) (images.HandlerFunc, error) { func AppendDistributionSourceLabel(manager content.Manager, ref string) (images.HandlerFunc, error) {
refspec, err := reference.Parse(ref) refspec, err := reference.Parse(ref)
@ -108,7 +103,7 @@ func appendDistributionSourceLabel(originLabel, repo string) string {
} }
func distributionSourceLabelKey(source string) string { func distributionSourceLabelKey(source string) string {
return fmt.Sprintf("%s.%s", labelDistributionSource, source) return fmt.Sprintf("%s.%s", labels.LabelDistributionSource, source)
} }
// selectRepositoryMountCandidate will select the repo which has longest // selectRepositoryMountCandidate will select the repo which has longest

View File

@ -20,6 +20,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/containerd/containerd/labels"
"github.com/containerd/containerd/reference" "github.com/containerd/containerd/reference"
) )
@ -69,7 +70,7 @@ func TestAppendDistributionLabel(t *testing.T) {
} }
func TestDistributionSourceLabelKey(t *testing.T) { func TestDistributionSourceLabelKey(t *testing.T) {
expected := "containerd.io/distribution.source.testsource" expected := labels.LabelDistributionSource + ".testsource"
if got := distributionSourceLabelKey("testsource"); !reflect.DeepEqual(got, expected) { if got := distributionSourceLabelKey("testsource"); !reflect.DeepEqual(got, expected) {
t.Fatalf("expected %v, but got %v", expected, got) t.Fatalf("expected %v, but got %v", expected, got)
} }
@ -116,12 +117,12 @@ func TestSelectRepositoryMountCandidate(t *testing.T) {
}, },
{ {
refspec: reference.Spec{Locator: "user@host/path"}, refspec: reference.Spec{Locator: "user@host/path"},
source: map[string]string{"containerd.io/distribution.source.host": "foo,path,bar"}, source: map[string]string{labels.LabelDistributionSource + ".host": "foo,path,bar"},
expected: "bar", expected: "bar",
}, },
{ {
refspec: reference.Spec{Locator: "user@host/path"}, refspec: reference.Spec{Locator: "user@host/path"},
source: map[string]string{"containerd.io/distribution.source.host": "foo,bar,path"}, source: map[string]string{labels.LabelDistributionSource + ".host": "foo,bar,path"},
expected: "bar", expected: "bar",
}, },
} { } {

View File

@ -28,6 +28,7 @@ import (
"github.com/containerd/containerd/content" "github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images" "github.com/containerd/containerd/images"
"github.com/containerd/containerd/labels"
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
"github.com/containerd/containerd/platforms" "github.com/containerd/containerd/platforms"
ocispec "github.com/opencontainers/image-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@ -367,7 +368,7 @@ func annotateDistributionSourceHandler(f images.HandlerFunc, manager content.Man
} }
for k, v := range info.Labels { for k, v := range info.Labels {
if !strings.HasPrefix(k, "containerd.io/distribution.source.") { if !strings.HasPrefix(k, labels.LabelDistributionSource+".") {
continue continue
} }