/
compose_images.go
208 lines (179 loc) · 5.22 KB
/
compose_images.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"strings"
"text/tabwriter"
"github.com/containerd/containerd"
"github.com/containerd/containerd/pkg/progress"
"github.com/containerd/containerd/snapshots"
"github.com/containerd/nerdctl/pkg/clientutil"
"github.com/containerd/nerdctl/pkg/cmd/compose"
"github.com/containerd/nerdctl/pkg/formatter"
"github.com/containerd/nerdctl/pkg/imgutil"
"github.com/containerd/nerdctl/pkg/labels"
"github.com/containerd/nerdctl/pkg/strutil"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)
func newComposeImagesCommand() *cobra.Command {
var composeImagesCommand = &cobra.Command{
Use: "images [flags] [SERVICE...]",
Short: "List images used by created containers in services",
RunE: composeImagesAction,
SilenceUsage: true,
SilenceErrors: true,
}
composeImagesCommand.Flags().String("format", "", "Format the output. Supported values: [json]")
composeImagesCommand.Flags().BoolP("quiet", "q", false, "Only show numeric image IDs")
return composeImagesCommand
}
func composeImagesAction(cmd *cobra.Command, args []string) error {
globalOptions, err := processRootCmdFlags(cmd)
if err != nil {
return err
}
quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
return err
}
format, err := cmd.Flags().GetString("format")
if err != nil {
return err
}
if format != "json" && format != "" {
return fmt.Errorf("unsupported format %s, supported formats are: [json]", format)
}
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
if err != nil {
return err
}
defer cancel()
options, err := getComposeOptions(cmd, globalOptions.DebugFull, globalOptions.Experimental)
if err != nil {
return err
}
c, err := compose.New(client, globalOptions, options, cmd.OutOrStdout(), cmd.ErrOrStderr())
if err != nil {
return err
}
serviceNames, err := c.ServiceNames(args...)
if err != nil {
return err
}
containers, err := c.Containers(ctx, serviceNames...)
if err != nil {
return err
}
if quiet {
return printComposeImageIDs(ctx, containers)
}
sn := client.SnapshotService(globalOptions.Snapshotter)
return printComposeImages(ctx, cmd, containers, sn, format)
}
func printComposeImageIDs(ctx context.Context, containers []containerd.Container) error {
ids := []string{}
for _, c := range containers {
image, err := c.Image(ctx)
if err != nil {
return err
}
metaImage := image.Metadata()
id := metaImage.Target.Digest.String()
if !strutil.InStringSlice(ids, id) {
ids = append(ids, id)
}
}
for _, id := range ids {
// always truncate image ids.
fmt.Println(strings.Split(id, ":")[1][:12])
}
return nil
}
func printComposeImages(ctx context.Context, cmd *cobra.Command, containers []containerd.Container, sn snapshots.Snapshotter, format string) error {
type composeImagePrintable struct {
ContainerName string
Repository string
Tag string
ImageID string
Size string
}
imagePrintables := make([]composeImagePrintable, len(containers))
eg, ctx := errgroup.WithContext(ctx)
for i, c := range containers {
i, c := i, c
eg.Go(func() error {
info, err := c.Info(ctx, containerd.WithoutRefreshedMetadata)
if err != nil {
return err
}
containerName := info.Labels[labels.Name]
image, err := c.Image(ctx)
if err != nil {
return err
}
size, err := imgutil.UnpackedImageSize(ctx, sn, image)
if err != nil {
return err
}
metaImage := image.Metadata()
repository, tag := imgutil.ParseRepoTag(metaImage.Name)
imageID := metaImage.Target.Digest.String()
if repository == "" {
repository = "<none>"
}
if tag == "" {
tag = "<none>"
}
if format != "json" {
imageID = strings.Split(imageID, ":")[1][:12]
}
// no race condition since each goroutine accesses different `i`
imagePrintables[i] = composeImagePrintable{
ContainerName: containerName,
Repository: repository,
Tag: tag,
ImageID: imageID,
Size: progress.Bytes(size).String(),
}
return nil
})
}
if err := eg.Wait(); err != nil {
return err
}
if format == "json" {
outJSON, err := formatter.ToJSON(imagePrintables, "", "")
if err != nil {
return err
}
_, err = fmt.Fprint(cmd.OutOrStdout(), outJSON)
return err
}
w := tabwriter.NewWriter(cmd.OutOrStdout(), 4, 8, 4, ' ', 0)
fmt.Fprintln(w, "Container\tRepository\tTag\tImage Id\tSize")
for _, p := range imagePrintables {
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n",
p.ContainerName,
p.Repository,
p.Tag,
p.ImageID,
p.Size,
); err != nil {
return err
}
}
return w.Flush()
}