Debian Codegraph @

cmd/soci/commands/ztoc/list.go

raw symbols SCIP
1 /*
2 Copyright The Soci Snapshotter Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package ztoc
18
19 import (
20 "fmt"
21 "os"
22 "text/tabwriter"
23
24 "github.com/awslabs/soci-snapshotter/soci"
25 "github.com/containerd/containerd/cmd/ctr/commands"
26 "github.com/containerd/containerd/images"
27 "github.com/containerd/containerd/platforms"
28 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
29 "github.com/urfave/cli"
30 )
31
32 var listCommand = cli.Command{
33 Name: "list",
34 Description: "list ztocs",
35 Aliases: []string{"ls"},
36 Flags: []cli.Flag{
37 cli.StringFlag{
38 Name: "ztoc-digest",
39 Usage: "filter ztocs by digest",
40 },
41 cli.StringFlag{
42 Name: "image-ref",
43 Usage: "filter ztocs to those that are associated with a specific image",
44 },
45 cli.BoolFlag{
46 Name: "verbose, v",
47 Usage: "display extra debugging messages",
48 },
49 cli.BoolFlag{
50 Name: "quiet, q",
51 Usage: "only display the index digests",
52 },
53 },
54 Action: func(cliContext *cli.Context) error {
55 db, err := soci.NewDB(soci.ArtifactsDbPath())
56 if err != nil {
57 return err
58 }
59 ztocDgst := cliContext.String("ztoc-digest")
60 imgRef := cliContext.String("image-ref")
61 verbose := cliContext.Bool("verbose")
62 quiet := cliContext.Bool("quiet")
63
64 var artifacts []*soci.ArtifactEntry
65 if imgRef == "" {
66 db.Walk(func(ae *soci.ArtifactEntry) error {
67 if ae.Type == soci.ArtifactEntryTypeLayer && (ztocDgst == "" || ae.Digest == ztocDgst) {
68 artifacts = append(artifacts, ae)
69 }
70 return nil
71 })
72 } else {
73 client, ctx, cancel, err := commands.NewClient(cliContext)
74 if err != nil {
75 return err
76 }
77 defer cancel()
78
79 is := client.ImageService()
80 img, err := is.Get(ctx, imgRef)
81 if err != nil {
82 return err
83 }
84 platform, err := images.Platforms(ctx, client.ContentStore(), img.Target)
85 if err != nil {
86 return err
87 }
88 var layers []ocispec.Descriptor
89 for _, p := range platform {
90 manifest, err := images.Manifest(ctx, client.ContentStore(), img.Target, platforms.OnlyStrict(p))
91 if err != nil && verbose {
92 // print a warning message if a manifest can't be resolved
93 // continue looking for manifests of other platforms
94 fmt.Printf("no image manifest for platform %s/%s. err: %v\n", p.Architecture, p.OS, err)
95 } else {
96 layers = append(layers, manifest.Layers...)
97 }
98 }
99 if len(layers) == 0 {
100 return fmt.Errorf("no image layers. could not filter ztoc")
101 }
102
103 db.Walk(func(ae *soci.ArtifactEntry) error {
104 if ae.Type == soci.ArtifactEntryTypeLayer {
105 if ztocDgst == "" {
106 // add all ztocs associated with the image
107 for _, l := range layers {
108 if ae.OriginalDigest == l.Digest.String() {
109 artifacts = append(artifacts, ae)
110 }
111 }
112 } else {
113 // only add the specific ztoc if the ztoc is with an image layer
114 for _, l := range layers {
115 if ae.Digest == ztocDgst && ae.OriginalDigest == l.Digest.String() {
116 artifacts = append(artifacts, ae)
117 }
118 }
119 }
120 }
121 return nil
122 })
123 if ztocDgst != "" && len(artifacts) == 0 {
124 return fmt.Errorf("the specified ztoc doesn't exist or it's not with the specified image")
125 }
126 }
127
128 if quiet {
129 for _, ae := range artifacts {
130 os.Stdout.Write([]byte(fmt.Sprintf("%s\n", ae.Digest)))
131 }
132 return nil
133 }
134
135 writer := tabwriter.NewWriter(os.Stdout, 8, 8, 4, ' ', 0)
136 writer.Write([]byte("DIGEST\tSIZE\tLAYER DIGEST\t\n"))
137 for _, artifact := range artifacts {
138 writer.Write([]byte(fmt.Sprintf("%s\t%d\t%s\t\n", artifact.Digest, artifact.Size, artifact.OriginalDigest)))
139 }
140 writer.Flush()
141 return nil
142 },
143 }