Skip to content

Commit

Permalink
Merge pull request #2881 from jedevc/annotate-distribution-source
Browse files Browse the repository at this point in the history
Support cross-repo mounting for contentutil.CopyChain
  • Loading branch information
tonistiigi authored May 26, 2022
2 parents 354e4f6 + 8df75c0 commit 5d68aa5
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion util/contentutil/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package contentutil
import (
"context"
"io"
"strings"
"sync"

"github.com/containerd/containerd/content"
Expand Down Expand Up @@ -75,7 +76,7 @@ func CopyChain(ctx context.Context, ingester content.Ingester, provider content.
}
})
handlers := []images.Handler{
images.ChildrenHandler(provider),
annotateDistributionSourceHandler(images.ChildrenHandler(provider), desc.Annotations),
filterHandler,
retryhandler.New(limited.FetchHandler(ingester, &localFetcher{provider}, ""), func(_ []byte) {}),
}
Expand All @@ -92,3 +93,45 @@ func CopyChain(ctx context.Context, ingester content.Ingester, provider content.

return nil
}

func annotateDistributionSourceHandler(f images.HandlerFunc, basis map[string]string) images.HandlerFunc {
return func(ctx context.Context, desc ocispecs.Descriptor) ([]ocispecs.Descriptor, error) {
children, err := f(ctx, desc)
if err != nil {
return nil, err
}

// only add distribution source for the config or blob data descriptor
switch desc.MediaType {
case images.MediaTypeDockerSchema2Manifest, ocispecs.MediaTypeImageManifest,
images.MediaTypeDockerSchema2ManifestList, ocispecs.MediaTypeImageIndex:
default:
return children, nil
}

for i := range children {
child := children[i]

for k, v := range basis {
if !strings.HasPrefix(k, "containerd.io/distribution.source.") {
continue
}
if child.Annotations != nil {
if _, ok := child.Annotations[k]; ok {
// don't override if already present
continue
}
}

if child.Annotations == nil {
child.Annotations = map[string]string{}
}
child.Annotations[k] = v
}

children[i] = child
}

return children, nil
}
}

0 comments on commit 5d68aa5

Please sign in to comment.