Skip to content

Commit

Permalink
converter: fix platfrom matcher
Browse files Browse the repository at this point in the history
We should only handle specific platform for pulling and
pushing by `platforms.MatchComparer`, otherwise acceld
will pull the layer data of all platforms for an image.

Signed-off-by: Yan Song <imeoer@linux.alibaba.com>
  • Loading branch information
imeoer committed Mar 14, 2023
1 parent dbd7827 commit ae6a28f
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 42 deletions.
2 changes: 1 addition & 1 deletion pkg/content/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (pvd *LocalProvider) Image(ctx context.Context, ref string) (*ocispec.Descr
if err != nil {
return nil, err
}
target := image.Target()
target := containerd.NewImageWithPlatform(pvd.client, image.Metadata(), pvd.platformMC).Target()
return &target, nil
}

Expand Down
4 changes: 1 addition & 3 deletions pkg/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/containerd/containerd/reference/docker"
"github.com/goharbor/acceleration-service/pkg/content"
"github.com/goharbor/acceleration-service/pkg/driver"
nydusUtils "github.com/goharbor/acceleration-service/pkg/driver/nydus/utils"
"github.com/goharbor/acceleration-service/pkg/errdefs"
"github.com/goharbor/acceleration-service/pkg/utils"
)
Expand All @@ -51,7 +50,6 @@ func New(opts ...ConvertOpt) (*Converter, error) {
if options.platformMC != nil {
platformMC = options.platformMC
}
platformMC = nydusUtils.ExcludeNydusPlatformComparer{MatchComparer: platformMC}

driver, err := driver.NewLocalDriver(options.driverType, options.driverConfig, platformMC)
if err != nil {
Expand Down Expand Up @@ -80,7 +78,7 @@ func (cvt *Converter) pull(ctx context.Context, source string) error {
// Write a diff id label of layer in content store for simplifying
// diff id calculation to speed up the conversion.
// See: /~https://github.com/containerd/containerd/blob/e4fefea5544d259177abb85b64e428702ac49c97/images/diffid.go#L49
if err := utils.UpdateLayerDiffID(ctx, cvt.provider.ContentStore(), *image); err != nil {
if err := utils.UpdateLayerDiffID(ctx, cvt.provider.ContentStore(), *image, cvt.platformMC); err != nil {
return errors.Wrap(err, "update layer diff id")
}

Expand Down
7 changes: 3 additions & 4 deletions pkg/driver/nydus/nydus.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
nydusify "github.com/containerd/nydus-snapshotter/pkg/converter"
"github.com/goharbor/acceleration-service/pkg/driver/nydus/parser"
"github.com/goharbor/acceleration-service/pkg/errdefs"
"github.com/goharbor/harbor/src/jobservice/logger"
"github.com/opencontainers/image-spec/specs-go"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
Expand Down Expand Up @@ -227,12 +226,12 @@ func (d *Driver) convert(ctx context.Context, provider accelcontent.Provider, so
}

func (d *Driver) makeManifestIndex(ctx context.Context, cs content.Store, oci, nydus ocispec.Descriptor) (*ocispec.Descriptor, error) {
ociDescs, err := utils.GetManifests(ctx, cs, oci)
ociDescs, err := utils.GetManifests(ctx, cs, oci, d.platformMC)
if err != nil {
return nil, errors.Wrap(err, "get oci image manifest list")
}

nydusDescs, err := utils.GetManifests(ctx, cs, nydus)
nydusDescs, err := utils.GetManifests(ctx, cs, nydus, d.platformMC)
if err != nil {
return nil, errors.Wrap(err, "get nydus image manifest list")
}
Expand Down Expand Up @@ -284,7 +283,7 @@ func (d *Driver) getChunkDict(ctx context.Context, provider accelcontent.Provide
bootstrapReader, _, err := parser.PullAsChunkDict(ctx, d.chunkDictRef, false)
if err != nil {
if errdefs.NeedsRetryWithHTTP(err) {
logger.Infof("try to pull chunk dict image with plain HTTP for %s", d.chunkDictRef)
logrus.Infof("try to pull chunk dict image with plain HTTP for %s", d.chunkDictRef)
bootstrapReader, _, err = parser.PullAsChunkDict(ctx, d.chunkDictRef, true)
if err != nil {
return nil, errors.Wrapf(err, "try to pull chunk dict image %s", d.chunkDictRef)
Expand Down
7 changes: 1 addition & 6 deletions pkg/platformutil/platformutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

"github.com/containerd/containerd/platforms"
nydusUtils "github.com/goharbor/acceleration-service/pkg/driver/nydus/utils"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

Expand Down Expand Up @@ -33,11 +32,7 @@ func ParsePlatforms(all bool, ss string) (platforms.MatchComparer, error) {
platforms = append(platforms, plat)
}
}
platformMC, err := NewMatchComparer(all, platforms)
if err != nil {
return nil, err
}
return nydusUtils.ExcludeNydusPlatformComparer{MatchComparer: platformMC}, nil
return NewMatchComparer(all, platforms)
}

// Ported from nerdctl project, copyright The containerd Authors.
Expand Down
10 changes: 6 additions & 4 deletions pkg/remote/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ func NewDockerConfigCredFunc() CredentialFunc {

func NewResolver(insecure, plainHTTP bool, credFunc CredentialFunc) remotes.Resolver {
registryHosts := docker.ConfigureDefaultRegistries(
docker.WithAuthorizer(docker.NewAuthorizer(
newDefaultClient(insecure),
credFunc,
)),
docker.WithAuthorizer(
docker.NewDockerAuthorizer(
docker.WithAuthClient(newDefaultClient(insecure)),
docker.WithAuthCreds(credFunc),
),
),
docker.WithClient(newDefaultClient(insecure)),
docker.WithPlainHTTP(func(host string) (bool, error) {
return plainHTTP, nil
Expand Down
29 changes: 5 additions & 24 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/labels"
"github.com/containerd/containerd/platforms"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
Expand Down Expand Up @@ -59,32 +60,12 @@ func WriteJSON(ctx context.Context, cs content.Store, x interface{}, oldDesc oci
return &newDesc, nil
}

func GetManifests(ctx context.Context, provider content.Provider, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
var descs []ocispec.Descriptor
switch desc.MediaType {
case images.MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest:
descs = append(descs, desc)
case images.MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex:
p, err := content.ReadBlob(ctx, provider, desc)
if err != nil {
return nil, err
}

var index ocispec.Index
if err := json.Unmarshal(p, &index); err != nil {
return nil, err
}

descs = append(descs, index.Manifests...)
default:
return nil, nil
}

return descs, nil
func GetManifests(ctx context.Context, provider content.Provider, desc ocispec.Descriptor, platform platforms.MatchComparer) ([]ocispec.Descriptor, error) {
return images.FilterPlatforms(images.ChildrenHandler(provider), platform)(ctx, desc)
}

func UpdateLayerDiffID(ctx context.Context, cs content.Store, image ocispec.Descriptor) error {
maniDescs, err := GetManifests(ctx, cs, image)
func UpdateLayerDiffID(ctx context.Context, cs content.Store, image ocispec.Descriptor, platform platforms.MatchComparer) error {
maniDescs, err := GetManifests(ctx, cs, image, platform)
if err != nil {
return errors.Wrap(err, "get manifests")
}
Expand Down

0 comments on commit ae6a28f

Please sign in to comment.