Skip to content

Commit

Permalink
Ensure all layers are here when tagging
Browse files Browse the repository at this point in the history
Signed-off-by: apostasie <[email protected]>
  • Loading branch information
apostasie committed Sep 18, 2024
1 parent 0ba3d8a commit 009096d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/cmd/image/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/containerd/nerdctl/v2/pkg/api/types"
"github.com/containerd/nerdctl/v2/pkg/idutil/imagewalker"
"github.com/containerd/nerdctl/v2/pkg/imgutil"
"github.com/containerd/nerdctl/v2/pkg/referenceutil"
)

Expand Down Expand Up @@ -64,6 +65,12 @@ func Tag(ctx context.Context, client *containerd.Client, options types.ImageTagO
return err
}
image.Name = target.String()

err = imgutil.EnsureAllContent(ctx, client, srcName, "", options.GOptions)
if err != nil {
return err
}

if _, err = imageService.Create(ctx, image); err != nil {
if errdefs.IsAlreadyExists(err) {
if err = imageService.Delete(ctx, image.Name); err != nil {
Expand Down
39 changes: 39 additions & 0 deletions pkg/imgutil/imgutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/containerd/nerdctl/v2/pkg/idutil/imagewalker"
"github.com/containerd/nerdctl/v2/pkg/imgutil/dockerconfigresolver"
"github.com/containerd/nerdctl/v2/pkg/imgutil/pull"
"github.com/containerd/nerdctl/v2/pkg/platformutil"
)

// EnsuredImage contains the image existed in containerd and its metadata.
Expand Down Expand Up @@ -102,6 +103,44 @@ func GetExistingImage(ctx context.Context, client *containerd.Client, snapshotte
return res, nil
}

func EnsureAllContent(ctx context.Context, client *containerd.Client, rawRef string, platform string, options types.GlobalCommandOptions) error {
named, err := distributionref.ParseDockerRef(rawRef)
if err != nil {
return err
}
refDomain := distributionref.Domain(named)

// Get a resolver
var dOpts []dockerconfigresolver.Opt
if options.InsecureRegistry {
log.G(ctx).Warnf("skipping verifying HTTPS certs for %q", refDomain)
dOpts = append(dOpts, dockerconfigresolver.WithSkipVerifyCerts(true))
}
dOpts = append(dOpts, dockerconfigresolver.WithHostsDirs(options.HostsDir))
resolver, err := dockerconfigresolver.New(ctx, refDomain, dOpts...)
if err != nil {
return err
}

var platformComparer platforms.MatchComparer
if platform != "" {
parsed, err := platforms.Parse(platform)
if err != nil {
return err
}
platformComparer = platformutil.NewMatchComparerFromOCISpecPlatformSlice([]ocispec.Platform{parsed})
} else {
platformComparer = platforms.Default()
}

opts := []containerd.RemoteOpt{
containerd.WithResolver(resolver),
containerd.WithPlatformMatcher(platformComparer),
}
_, err = client.Fetch(ctx, rawRef, opts...)
return err
}

// EnsureImage ensures the image.
//
// # When insecure is set, skips verifying certs, and also falls back to HTTP when the registry does not speak HTTPS
Expand Down

0 comments on commit 009096d

Please sign in to comment.