Skip to content

Commit

Permalink
Merge pull request #1983 from djdongjin/improve-pull
Browse files Browse the repository at this point in the history
Minor clean up for imgutil
  • Loading branch information
AkihiroSuda authored Feb 9, 2023
2 parents adee602 + b584d31 commit d41705c
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 233 deletions.
21 changes: 21 additions & 0 deletions pkg/api/types/image_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,27 @@ type ImagePushOptions struct {
AllowNondistributableArtifacts bool
}

// ImagePullOptions specifies options for `nerdctl (image) pull`.
type ImagePullOptions struct {
Stdout io.Writer
Stderr io.Writer
GOptions GlobalCommandOptions
// Unpack the image for the current single platform (auto/true/false)
Unpack string
// Pull content for a specific platform
Platform []string
// Pull content for all platforms
AllPlatforms bool
// Verify the image (none|cosign)
Verify string
// Path to the public key file, KMS, URI or Kubernetes Secret for --verify=cosign
CosignKey string
// Suppress verbose output
Quiet bool
// multiaddr of IPFS API (default uses $IPFS_PATH env variable if defined or local directory ~/.ipfs)
IPFSAddress string
}

// ImageTagOptions specifies options for `nerdctl (image) tag`.
type ImageTagOptions struct {
// GOptions is the global options
Expand Down
40 changes: 0 additions & 40 deletions pkg/api/types/pull_types.go

This file was deleted.

76 changes: 3 additions & 73 deletions pkg/cmd/image/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"errors"
"fmt"
"io"
"regexp"
"strings"
"text/tabwriter"
"text/template"
Expand All @@ -33,7 +32,6 @@ import (
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/pkg/progress"
"github.com/containerd/containerd/platforms"
dockerreference "github.com/containerd/containerd/reference/docker"
"github.com/containerd/containerd/snapshots"
"github.com/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/formatter"
Expand All @@ -55,15 +53,15 @@ func List(ctx context.Context, client *containerd.Client, options types.ImageLis
}

if f.Dangling != nil {
imageList = filterDangling(imageList, *f.Dangling)
imageList = imgutil.FilterDangling(imageList, *f.Dangling)
}

imageList, err = filterByLabel(ctx, client, imageList, f.Labels)
imageList, err = imgutil.FilterByLabel(ctx, client, imageList, f.Labels)
if err != nil {
return err
}

imageList, err = filterByReference(imageList, f.Reference)
imageList, err = imgutil.FilterByReference(imageList, f.Reference)
if err != nil {
return err
}
Expand All @@ -88,74 +86,6 @@ func List(ctx context.Context, client *containerd.Client, options types.ImageLis
return printImages(ctx, client, imageList, options)
}

func filterByReference(imageList []images.Image, filters []string) ([]images.Image, error) {
var filteredImageList []images.Image
logrus.Debug(filters)
for _, image := range imageList {
logrus.Debug(image.Name)
var matches int
for _, f := range filters {
var ref dockerreference.Reference
var err error
ref, err = dockerreference.ParseAnyReference(image.Name)
if err != nil {
return nil, fmt.Errorf("unable to parse image name: %s while filtering by reference because of %s", image.Name, err.Error())
}

familiarMatch, err := dockerreference.FamiliarMatch(f, ref)
if err != nil {
return nil, err
}
regexpMatch, err := regexp.MatchString(f, image.Name)
if err != nil {
return nil, err
}
if familiarMatch || regexpMatch {
matches++
}
}
if matches == len(filters) {
filteredImageList = append(filteredImageList, image)
}
}
return filteredImageList, nil
}

func filterDangling(imageList []images.Image, dangling bool) []images.Image {
var filtered []images.Image
for _, image := range imageList {
_, tag := imgutil.ParseRepoTag(image.Name)

if dangling && tag == "" {
filtered = append(filtered, image)
}
if !dangling && tag != "" {
filtered = append(filtered, image)
}
}
return filtered
}

func filterByLabel(ctx context.Context, client *containerd.Client, imageList []images.Image, filters map[string]string) ([]images.Image, error) {
for lk, lv := range filters {
var imageLabels []images.Image
for _, img := range imageList {
ci := containerd.NewImage(client, img)
cfg, _, err := imgutil.ReadImageConfig(ctx, ci)
if err != nil {
return nil, err
}
if val, ok := cfg.Config.Labels[lk]; ok {
if val == lv || lv == "" {
imageLabels = append(imageLabels, img)
}
}
}
imageList = imageLabels
}
return imageList, nil
}

type imagePrintable struct {
// TODO: "Containers"
CreatedAt string
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/image/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/sirupsen/logrus"
)

// Pull pulls an image specified by `rawRef`.
func Pull(ctx context.Context, client *containerd.Client, rawRef string, options types.ImagePullOptions) error {
ocispecPlatforms, err := platformutil.NewOCISpecPlatformSlice(options.AllPlatforms, options.Platform)
if err != nil {
Expand All @@ -54,8 +55,8 @@ func Pull(ctx context.Context, client *containerd.Client, rawRef string, options
return nil
}

// EnsureImage pulls an image either from ipfs or from registry.
func EnsureImage(ctx context.Context, client *containerd.Client, rawRef string, ocispecPlatforms []v1.Platform, pull string, unpack *bool, quiet bool, options types.ImagePullOptions) (*imgutil.EnsuredImage, error) {

var ensured *imgutil.EnsuredImage

if scheme, ref, err := referenceutil.ParseIPFSRefWithScheme(rawRef); err == nil {
Expand Down
Loading

0 comments on commit d41705c

Please sign in to comment.