| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
package image |
| 34 |
|
| 35 |
import ( |
| 36 |
"context" |
| 37 |
"fmt" |
| 38 |
|
| 39 |
"github.com/awslabs/soci-snapshotter/cmd/soci/commands/internal" |
| 40 |
"github.com/awslabs/soci-snapshotter/fs/source" |
| 41 |
"github.com/containerd/containerd" |
| 42 |
"github.com/containerd/containerd/cmd/ctr/commands" |
| 43 |
"github.com/containerd/containerd/cmd/ctr/commands/content" |
| 44 |
"github.com/containerd/containerd/images" |
| 45 |
"github.com/containerd/containerd/log" |
| 46 |
ctdsnapshotters "github.com/containerd/containerd/pkg/snapshotters" |
| 47 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 48 |
"github.com/urfave/cli" |
| 49 |
) |
| 50 |
|
| 51 |
const ( |
| 52 |
remoteSnapshotterName = "soci" |
| 53 |
skipContentVerifyOpt = "skip-content-verify" |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
var rpullCommand = cli.Command{ |
| 58 |
Name: "rpull", |
| 59 |
Usage: "pull an image from a registry levaraging soci snapshotter", |
| 60 |
ArgsUsage: "[flags] <ref>", |
| 61 |
Description: `Fetch and prepare an image for use in containerd levaraging soci snapshotter. |
| 62 |
|
| 63 |
After pulling an image, it should be ready to use the same reference in a run |
| 64 |
command. |
| 65 |
`, |
| 66 |
Flags: append(append(append( |
| 67 |
commands.RegistryFlags, |
| 68 |
commands.LabelFlag), |
| 69 |
commands.SnapshotterFlags...), |
| 70 |
cli.BoolFlag{ |
| 71 |
Name: skipContentVerifyOpt, |
| 72 |
Usage: "Skip content verification for layers contained in this image.", |
| 73 |
}, |
| 74 |
|
| 75 |
cli.StringFlag{ |
| 76 |
Name: "soci-index-digest", |
| 77 |
Usage: "The SOCI index digest.", |
| 78 |
}, |
| 79 |
cli.StringFlag{ |
| 80 |
Name: internal.PlatformFlagKey, |
| 81 |
Usage: "The platform to pull.", |
| 82 |
}, |
| 83 |
), |
| 84 |
Action: func(context *cli.Context) error { |
| 85 |
var ( |
| 86 |
ref = context.Args().First() |
| 87 |
config = &rPullConfig{} |
| 88 |
) |
| 89 |
if ref == "" { |
| 90 |
return fmt.Errorf("please provide an image reference") |
| 91 |
} |
| 92 |
|
| 93 |
config.indexDigest = context.String("soci-index-digest") |
| 94 |
|
| 95 |
client, ctx, cancel, err := commands.NewClient(context) |
| 96 |
if err != nil { |
| 97 |
return err |
| 98 |
} |
| 99 |
defer cancel() |
| 100 |
|
| 101 |
ctx, done, err := client.WithLease(ctx) |
| 102 |
if err != nil { |
| 103 |
return err |
| 104 |
} |
| 105 |
defer done(ctx) |
| 106 |
|
| 107 |
fc, err := content.NewFetchConfig(ctx, context) |
| 108 |
if err != nil { |
| 109 |
return err |
| 110 |
} |
| 111 |
config.FetchConfig = fc |
| 112 |
|
| 113 |
if context.Bool(skipContentVerifyOpt) { |
| 114 |
config.skipVerify = true |
| 115 |
} |
| 116 |
|
| 117 |
config.snapshotter = remoteSnapshotterName |
| 118 |
if sn := context.String("snapshotter"); sn != "" { |
| 119 |
config.snapshotter = sn |
| 120 |
} |
| 121 |
|
| 122 |
config.platform = context.String(internal.PlatformFlagKey) |
| 123 |
|
| 124 |
return pull(ctx, client, ref, config) |
| 125 |
}, |
| 126 |
} |
| 127 |
|
| 128 |
type rPullConfig struct { |
| 129 |
*content.FetchConfig |
| 130 |
skipVerify bool |
| 131 |
snapshotter string |
| 132 |
indexDigest string |
| 133 |
platform string |
| 134 |
} |
| 135 |
|
| 136 |
func pull(ctx context.Context, client *containerd.Client, ref string, config *rPullConfig) error { |
| 137 |
pCtx := ctx |
| 138 |
h := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { |
| 139 |
if desc.MediaType != images.MediaTypeDockerSchema1Manifest { |
| 140 |
fmt.Printf("fetching %v... %v\n", desc.Digest.String()[:15], desc.MediaType) |
| 141 |
} |
| 142 |
return nil, nil |
| 143 |
}) |
| 144 |
|
| 145 |
log.G(pCtx).WithField("image", ref).Debug("fetching") |
| 146 |
labels := commands.LabelArgs(config.Labels) |
| 147 |
if _, err := client.Pull(pCtx, ref, []containerd.RemoteOpt{ |
| 148 |
containerd.WithPullLabels(labels), |
| 149 |
containerd.WithResolver(config.Resolver), |
| 150 |
containerd.WithImageHandler(h), |
| 151 |
|
| 152 |
containerd.WithSchema1Conversion, |
| 153 |
containerd.WithPullUnpack, |
| 154 |
containerd.WithPlatform(config.platform), |
| 155 |
containerd.WithPullSnapshotter(config.snapshotter), |
| 156 |
containerd.WithImageHandlerWrapper(source.AppendDefaultLabelsHandlerWrapper( |
| 157 |
config.indexDigest, ctdsnapshotters.AppendInfoHandlerWrapper(ref))), |
| 158 |
}...); err != nil { |
| 159 |
return err |
| 160 |
} |
| 161 |
|
| 162 |
return nil |
| 163 |
} |