| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package index |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"io" |
| 22 |
"os" |
| 23 |
"sort" |
| 24 |
"text/tabwriter" |
| 25 |
"time" |
| 26 |
|
| 27 |
"github.com/awslabs/soci-snapshotter/soci" |
| 28 |
"github.com/containerd/containerd/cmd/ctr/commands" |
| 29 |
"github.com/containerd/containerd/images" |
| 30 |
"github.com/containerd/containerd/platforms" |
| 31 |
specs "github.com/opencontainers/image-spec/specs-go/v1" |
| 32 |
"github.com/urfave/cli" |
| 33 |
) |
| 34 |
|
| 35 |
type filter func(ae *soci.ArtifactEntry) bool |
| 36 |
|
| 37 |
func indexFilter(ae *soci.ArtifactEntry) bool { |
| 38 |
return ae.Type == soci.ArtifactEntryTypeIndex |
| 39 |
} |
| 40 |
|
| 41 |
func platformFilter(platform specs.Platform) filter { |
| 42 |
return func(ae *soci.ArtifactEntry) bool { |
| 43 |
return indexFilter(ae) && ae.Platform == platforms.Format(platform) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func originalDigestFilter(digest string) filter { |
| 48 |
return func(ae *soci.ArtifactEntry) bool { |
| 49 |
return indexFilter(ae) && ae.OriginalDigest == digest |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func anyMatch(fns []filter) filter { |
| 54 |
return func(ae *soci.ArtifactEntry) bool { |
| 55 |
for _, f := range fns { |
| 56 |
if f(ae) { |
| 57 |
return true |
| 58 |
} |
| 59 |
} |
| 60 |
return false |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
var listCommand = cli.Command{ |
| 65 |
Name: "list", |
| 66 |
Usage: "list indices", |
| 67 |
Aliases: []string{"ls"}, |
| 68 |
Flags: []cli.Flag{ |
| 69 |
cli.StringFlag{ |
| 70 |
Name: "ref", |
| 71 |
Usage: "filter indices to those that are associated with a specific image ref", |
| 72 |
}, |
| 73 |
cli.BoolFlag{ |
| 74 |
Name: "quiet, q", |
| 75 |
Usage: "only display the index digests", |
| 76 |
}, |
| 77 |
cli.StringSliceFlag{ |
| 78 |
Name: "platform, p", |
| 79 |
Usage: "filter indices to a specific platform", |
| 80 |
}, |
| 81 |
}, |
| 82 |
Action: func(cliContext *cli.Context) error { |
| 83 |
var artifacts []*soci.ArtifactEntry |
| 84 |
ref := cliContext.String("ref") |
| 85 |
quiet := cliContext.Bool("quiet") |
| 86 |
var plats []specs.Platform |
| 87 |
for _, p := range cliContext.StringSlice("platform") { |
| 88 |
pp, err := platforms.Parse(p) |
| 89 |
if err != nil { |
| 90 |
return err |
| 91 |
} |
| 92 |
plats = append(plats, pp) |
| 93 |
} |
| 94 |
|
| 95 |
client, ctx, cancel, err := commands.NewClient(cliContext) |
| 96 |
if err != nil { |
| 97 |
return err |
| 98 |
} |
| 99 |
defer cancel() |
| 100 |
|
| 101 |
f := indexFilter |
| 102 |
|
| 103 |
is := client.ImageService() |
| 104 |
if ref != "" { |
| 105 |
img, err := is.Get(ctx, ref) |
| 106 |
if err != nil { |
| 107 |
return err |
| 108 |
} |
| 109 |
|
| 110 |
if len(plats) == 0 { |
| 111 |
plats, err = images.Platforms(ctx, client.ContentStore(), img.Target) |
| 112 |
if err != nil { |
| 113 |
return err |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
cs := client.ContentStore() |
| 118 |
var filters []filter |
| 119 |
for _, plat := range plats { |
| 120 |
desc, err := soci.GetImageManifestDescriptor(ctx, cs, img.Target, platforms.OnlyStrict(plat)) |
| 121 |
if err != nil { |
| 122 |
return err |
| 123 |
} |
| 124 |
filters = append(filters, originalDigestFilter(desc.Digest.String())) |
| 125 |
} |
| 126 |
f = anyMatch(filters) |
| 127 |
} else if len(plats) != 0 { |
| 128 |
var filters []filter |
| 129 |
for _, plat := range plats { |
| 130 |
filters = append(filters, platformFilter(plat)) |
| 131 |
} |
| 132 |
f = anyMatch(filters) |
| 133 |
} |
| 134 |
|
| 135 |
db, err := soci.NewDB(soci.ArtifactsDbPath()) |
| 136 |
if err != nil { |
| 137 |
return err |
| 138 |
} |
| 139 |
db.Walk(func(ae *soci.ArtifactEntry) error { |
| 140 |
if f(ae) { |
| 141 |
artifacts = append(artifacts, ae) |
| 142 |
} |
| 143 |
return nil |
| 144 |
}) |
| 145 |
|
| 146 |
sort.Slice(artifacts, func(i, j int) bool { |
| 147 |
return artifacts[i].CreatedAt.After(artifacts[j].CreatedAt) |
| 148 |
}) |
| 149 |
|
| 150 |
if quiet { |
| 151 |
for _, ae := range artifacts { |
| 152 |
os.Stdout.Write([]byte(fmt.Sprintf("%s\n", ae.Digest))) |
| 153 |
} |
| 154 |
return nil |
| 155 |
} |
| 156 |
|
| 157 |
writer := tabwriter.NewWriter(os.Stdout, 8, 8, 4, ' ', 0) |
| 158 |
writer.Write([]byte("DIGEST\tSIZE\tIMAGE REF\tPLATFORM\tMEDIA TYPE\tCREATED\t\n")) |
| 159 |
|
| 160 |
for _, ae := range artifacts { |
| 161 |
imgs, _ := is.List(ctx, fmt.Sprintf("target.digest==%s", ae.ImageDigest)) |
| 162 |
if len(imgs) > 0 { |
| 163 |
for _, img := range imgs { |
| 164 |
writeArtifactEntry(writer, ae, img.Name) |
| 165 |
} |
| 166 |
} else { |
| 167 |
writeArtifactEntry(writer, ae, "") |
| 168 |
} |
| 169 |
} |
| 170 |
writer.Flush() |
| 171 |
return nil |
| 172 |
}, |
| 173 |
} |
| 174 |
|
| 175 |
func writeArtifactEntry(w io.Writer, ae *soci.ArtifactEntry, imageRef string) { |
| 176 |
w.Write([]byte(fmt.Sprintf( |
| 177 |
"%s\t%d\t%s\t%s\t%s\t%s\t\n", |
| 178 |
ae.Digest, |
| 179 |
ae.Size, |
| 180 |
imageRef, |
| 181 |
ae.Platform, |
| 182 |
ae.MediaType, |
| 183 |
getDuration(ae.CreatedAt), |
| 184 |
))) |
| 185 |
} |
| 186 |
|
| 187 |
func getDuration(t time.Time) string { |
| 188 |
if t.IsZero() { |
| 189 |
return "n/a" |
| 190 |
} |
| 191 |
return fmt.Sprintf("%s ago", time.Since(t).Round(time.Second).String()) |
| 192 |
} |