| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package commands |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"errors" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"net/http" |
| 25 |
"sort" |
| 26 |
"strings" |
| 27 |
|
| 28 |
"github.com/awslabs/soci-snapshotter/cmd/soci/commands/internal" |
| 29 |
"github.com/awslabs/soci-snapshotter/fs" |
| 30 |
"github.com/awslabs/soci-snapshotter/soci" |
| 31 |
"github.com/awslabs/soci-snapshotter/soci/store" |
| 32 |
"github.com/containerd/containerd/cmd/ctr/commands" |
| 33 |
"github.com/containerd/containerd/reference" |
| 34 |
dockercliconfig "github.com/docker/cli/cli/config" |
| 35 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 36 |
"github.com/urfave/cli" |
| 37 |
oraslib "oras.land/oras-go/v2" |
| 38 |
"oras.land/oras-go/v2/registry/remote" |
| 39 |
"oras.land/oras-go/v2/registry/remote/auth" |
| 40 |
) |
| 41 |
|
| 42 |
|
| 43 |
var PushCommand = cli.Command{ |
| 44 |
Name: "push", |
| 45 |
Usage: "push SOCI artifacts to a registry", |
| 46 |
ArgsUsage: "[flags] <ref>", |
| 47 |
Description: `Push SOCI artifacts to a registry by image reference. |
| 48 |
If multiple soci indices exist for the given image, the most recent one will be pushed. |
| 49 |
|
| 50 |
After pushing the soci artifacts, they should be available in the registry. Soci artifacts will be pushed only |
| 51 |
if they are available in the snapshotter's local content store. |
| 52 |
`, |
| 53 |
Flags: append(append(append(append( |
| 54 |
commands.RegistryFlags, |
| 55 |
commands.LabelFlag), |
| 56 |
commands.SnapshotterFlags...), |
| 57 |
internal.PlatformFlags...), |
| 58 |
internal.ExistingIndexFlag, |
| 59 |
cli.Uint64Flag{ |
| 60 |
Name: "max-concurrent-uploads", |
| 61 |
Usage: "Max concurrent uploads. Default is 10", |
| 62 |
Value: 10, |
| 63 |
}, |
| 64 |
cli.BoolFlag{ |
| 65 |
Name: "quiet, q", |
| 66 |
Usage: "quiet mode", |
| 67 |
}, |
| 68 |
), |
| 69 |
Action: func(cliContext *cli.Context) error { |
| 70 |
ref := cliContext.Args().First() |
| 71 |
quiet := cliContext.Bool("quiet") |
| 72 |
if ref == "" { |
| 73 |
return fmt.Errorf("please provide an image reference to push") |
| 74 |
} |
| 75 |
|
| 76 |
client, ctx, cancel, err := commands.NewClient(cliContext) |
| 77 |
if err != nil { |
| 78 |
return err |
| 79 |
} |
| 80 |
defer cancel() |
| 81 |
|
| 82 |
cs := client.ContentStore() |
| 83 |
is := client.ImageService() |
| 84 |
img, err := is.Get(ctx, ref) |
| 85 |
if err != nil { |
| 86 |
return err |
| 87 |
} |
| 88 |
|
| 89 |
ps, err := internal.GetPlatforms(ctx, cliContext, img, cs) |
| 90 |
if err != nil { |
| 91 |
return err |
| 92 |
} |
| 93 |
|
| 94 |
artifactsDb, err := soci.NewDB(soci.ArtifactsDbPath()) |
| 95 |
if err != nil { |
| 96 |
return err |
| 97 |
} |
| 98 |
|
| 99 |
refspec, err := reference.Parse(ref) |
| 100 |
if err != nil { |
| 101 |
return err |
| 102 |
} |
| 103 |
|
| 104 |
dst, err := remote.NewRepository(refspec.Locator) |
| 105 |
if err != nil { |
| 106 |
return err |
| 107 |
} |
| 108 |
authClient := auth.DefaultClient |
| 109 |
|
| 110 |
var username string |
| 111 |
var secret string |
| 112 |
if cliContext.IsSet("user") { |
| 113 |
username = cliContext.String("user") |
| 114 |
if i := strings.IndexByte(username, ':'); i > 0 { |
| 115 |
secret = username[i+1:] |
| 116 |
username = username[0:i] |
| 117 |
} |
| 118 |
} else { |
| 119 |
cf := dockercliconfig.LoadDefaultConfigFile(io.Discard) |
| 120 |
if cf.ContainsAuth() { |
| 121 |
if ac, err := cf.GetAuthConfig(refspec.Hostname()); err == nil { |
| 122 |
username = ac.Username |
| 123 |
secret = ac.Password |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
authClient.Credential = func(_ context.Context, host string) (auth.Credential, error) { |
| 129 |
return auth.Credential{ |
| 130 |
Username: username, |
| 131 |
Password: secret, |
| 132 |
}, nil |
| 133 |
} |
| 134 |
|
| 135 |
ctx, src, err := store.NewContentStore(ctx, store.WithType(store.ContentStoreType(cliContext.GlobalString("content-store"))), store.WithNamespace(cliContext.GlobalString("namespace"))) |
| 136 |
if err != nil { |
| 137 |
return fmt.Errorf("cannot create local content store: %w", err) |
| 138 |
} |
| 139 |
|
| 140 |
dst.Client = authClient |
| 141 |
dst.PlainHTTP = cliContext.Bool("plain-http") |
| 142 |
|
| 143 |
debug := cliContext.GlobalBool("debug") |
| 144 |
if debug { |
| 145 |
dst.Client = &debugClient{client: authClient} |
| 146 |
} else { |
| 147 |
dst.Client = authClient |
| 148 |
} |
| 149 |
existingIndexOption := cliContext.String(internal.ExistingIndexFlagName) |
| 150 |
if !internal.SupportedArg(existingIndexOption, internal.SupportedExistingIndexOptions) { |
| 151 |
return fmt.Errorf("unexpected value for flag %s: %s, expected types %v", |
| 152 |
internal.ExistingIndexFlagName, existingIndexOption, internal.SupportedExistingIndexOptions) |
| 153 |
} |
| 154 |
|
| 155 |
options := oraslib.DefaultCopyGraphOptions |
| 156 |
options.PreCopy = func(_ context.Context, desc ocispec.Descriptor) error { |
| 157 |
if !quiet { |
| 158 |
fmt.Printf("pushing artifact with digest: %v\n", desc.Digest) |
| 159 |
} |
| 160 |
return nil |
| 161 |
} |
| 162 |
options.PostCopy = func(_ context.Context, desc ocispec.Descriptor) error { |
| 163 |
if !quiet { |
| 164 |
fmt.Printf("successfully pushed artifact with digest: %v\n", desc.Digest) |
| 165 |
} |
| 166 |
return nil |
| 167 |
} |
| 168 |
options.OnCopySkipped = func(ctx context.Context, desc ocispec.Descriptor) error { |
| 169 |
if !quiet { |
| 170 |
fmt.Printf("skipped artifact with digest: %v\n", desc.Digest) |
| 171 |
} |
| 172 |
return nil |
| 173 |
} |
| 174 |
|
| 175 |
for _, platform := range ps { |
| 176 |
indexDescriptors, imgManifestDesc, err := soci.GetIndexDescriptorCollection(ctx, cs, artifactsDb, img, []ocispec.Platform{platform}) |
| 177 |
if err != nil { |
| 178 |
return err |
| 179 |
} |
| 180 |
|
| 181 |
if len(indexDescriptors) == 0 { |
| 182 |
return fmt.Errorf("could not find any soci indices to push") |
| 183 |
} |
| 184 |
|
| 185 |
sort.Slice(indexDescriptors, func(i, j int) bool { |
| 186 |
return indexDescriptors[i].CreatedAt.Before(indexDescriptors[j].CreatedAt) |
| 187 |
}) |
| 188 |
indexDesc := indexDescriptors[len(indexDescriptors)-1] |
| 189 |
|
| 190 |
if existingIndexOption != internal.Allow { |
| 191 |
if !quiet { |
| 192 |
fmt.Println("checking if a soci index already exists in remote repository...") |
| 193 |
} |
| 194 |
client := fs.NewOCIArtifactClient(dst) |
| 195 |
referrers, err := client.AllReferrers(ctx, ocispec.Descriptor{Digest: imgManifestDesc.Digest}) |
| 196 |
if err != nil && !errors.Is(err, fs.ErrNoReferrers) { |
| 197 |
return fmt.Errorf("failed to fetch list of referrers: %w", err) |
| 198 |
} |
| 199 |
if len(referrers) > 0 { |
| 200 |
var foundMessage string |
| 201 |
if len(referrers) > 1 { |
| 202 |
foundMessage = "multiple soci indices found in remote repository" |
| 203 |
} else { |
| 204 |
foundMessage = fmt.Sprintf("soci index found in remote repository with digest: %s", referrers[0].Digest.String()) |
| 205 |
} |
| 206 |
switch existingIndexOption { |
| 207 |
case internal.Skip: |
| 208 |
if !quiet { |
| 209 |
fmt.Printf("%s: skipping pushing artifacts for image manifest: %s\n", foundMessage, imgManifestDesc.Digest.String()) |
| 210 |
} |
| 211 |
continue |
| 212 |
case internal.Warn: |
| 213 |
fmt.Printf("[WARN] %s: pushing index anyway\n", foundMessage) |
| 214 |
|
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
if quiet { |
| 221 |
fmt.Println(indexDesc.Digest.String()) |
| 222 |
} else { |
| 223 |
fmt.Printf("pushing soci index with digest: %v\n", indexDesc.Digest) |
| 224 |
} |
| 225 |
|
| 226 |
err = oraslib.CopyGraph(context.Background(), src, dst, indexDesc.Descriptor, options) |
| 227 |
if err != nil { |
| 228 |
return fmt.Errorf("error pushing graph to remote: %w", err) |
| 229 |
} |
| 230 |
|
| 231 |
} |
| 232 |
return nil |
| 233 |
}, |
| 234 |
} |
| 235 |
|
| 236 |
type debugClient struct { |
| 237 |
client remote.Client |
| 238 |
} |
| 239 |
|
| 240 |
func (c *debugClient) Do(req *http.Request) (*http.Response, error) { |
| 241 |
fmt.Printf("http req %s %s\n", req.Method, req.URL) |
| 242 |
res, err := c.client.Do(req) |
| 243 |
if err != nil { |
| 244 |
fmt.Printf("http err %v\n", err) |
| 245 |
} else { |
| 246 |
fmt.Printf("http res %s\n", res.Status) |
| 247 |
} |
| 248 |
return res, err |
| 249 |
} |