-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathlist.go
More file actions
362 lines (298 loc) · 8.71 KB
/
Copy pathlist.go
File metadata and controls
362 lines (298 loc) · 8.71 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2023, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package list
import (
"context"
"fmt"
"slices"
"strings"
"github.com/MakeNowJust/heredoc"
"github.com/dustin/go-humanize"
gcrname "github.com/google/go-containerregistry/pkg/name"
"github.com/spf13/cobra"
"unikraft.com/cloud/sdk/controlplane"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
"kraftkit.sh/internal/tableprinter"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
)
type ListOptions struct {
All bool `long:"all" usage:"Also show available official images"`
Output string `long:"output" short:"o" usage:"Set output format. Options: table,yaml,json,list,raw" default:"table"`
token string
allowInsecure bool
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&ListOptions{}, cobra.Command{
Short: "List all images for your account",
Use: "list",
Args: cobra.NoArgs,
Aliases: []string{"ls"},
Example: heredoc.Doc(`
# List images in your account.
$ kraft cloud image list
# List images in your account along with available official images.
$ kraft cloud image list --all
# List all images in your account in JSON format.
$ kraft cloud image list -o json
`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "kraftcloud-image",
},
})
if err != nil {
panic(err)
}
return cmd
}
func (opts *ListOptions) Pre(cmd *cobra.Command, _ []string) error {
err := utils.PopulateMetroToken(cmd, nil, &opts.token, &opts.allowInsecure)
if err != nil {
return fmt.Errorf("could not populate token: %w", err)
}
if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}
return nil
}
func (opts *ListOptions) Run(ctx context.Context, args []string) error {
return opts.runControlPlane(ctx)
}
func (opts *ListOptions) runControlPlane(ctx context.Context) error {
auth, err := config.GetKraftCloudAuthConfig(ctx, strings.TrimSpace(opts.token))
if err != nil {
return fmt.Errorf("could not retrieve credentials: %w", err)
}
clientOpts := []controlplane.ClientOption{
controlplane.WithAllowInsecure(opts.allowInsecure),
controlplane.WithToken(config.GetKraftCloudTokenAuthConfig(*auth)),
}
client := controlplane.NewClient(clientOpts...)
details := true
resp, err := client.ListImages(ctx, controlplane.ListImagesOpts{Details: &details})
if err != nil {
return fmt.Errorf("could not list images: %w", err)
}
if opts.Output == "raw" || opts.Output == "json" {
fmt.Fprintln(iostreams.G(ctx).Out, string(resp.RawBody()))
return nil
}
images := collectImageItems(resp)
if opts.All {
respOfficial, err := client.ListImages(ctx, controlplane.ListImagesOpts{Details: &details, Namespace: []string{"official"}})
if err != nil {
return fmt.Errorf("could not list official images: %w", err)
}
images = append(images, collectImageItems(respOfficial)...)
}
return opts.renderImages(ctx, images)
}
type imageRow struct {
Digest string
Tags []string
SizeInBytes int64
}
func collectImageItems(resp *controlplane.Response[controlplane.ListImagesResponseData]) []imageRow {
if resp == nil || resp.Data == nil {
return nil
}
imagesByKey := make(map[string]*imageRow, len(resp.Data.Images))
for _, image := range resp.Data.Images {
name := ""
if image.Name != nil {
name = strings.TrimSpace(*image.Name)
}
if name == "" {
continue
}
for _, tag := range image.Tags {
tagName := ""
if tag.Name != nil {
tagName = strings.TrimSpace(*tag.Name)
}
if tagName == "" {
continue
}
ref := fmt.Sprintf("%s:%s", name, tagName)
digest := strings.TrimSpace(derefString(tag.Digest))
key := name + "|" + digest
if digest == "" {
key = name + "|" + tagName + "|nodigest"
}
item, ok := imagesByKey[key]
if !ok {
item = &imageRow{Digest: digest}
imagesByKey[key] = item
}
sizeInBytes := int64(0)
if tag.Size != nil {
sizeInBytes = int64(*tag.Size)
}
if sizeInBytes > item.SizeInBytes {
item.SizeInBytes = sizeInBytes
}
item.Tags = append(item.Tags, ref)
}
}
images := make([]imageRow, 0, len(imagesByKey))
for _, item := range imagesByKey {
if len(item.Tags) == 0 {
continue
}
images = append(images, *item)
}
return images
}
func derefString(value *string) string {
if value == nil {
return ""
}
return *value
}
func (opts *ListOptions) renderImages(ctx context.Context, images []imageRow) error {
err := iostreams.G(ctx).StartPager()
if err != nil {
log.G(ctx).Errorf("error starting pager: %v", err)
}
defer iostreams.G(ctx).StopPager()
cs := iostreams.G(ctx).ColorScheme()
table, err := tableprinter.NewTablePrinter(ctx,
tableprinter.WithMaxWidth(iostreams.G(ctx).TerminalWidth()),
tableprinter.WithOutputFormatFromString(opts.Output),
)
if err != nil {
return err
}
// Sort by namespace (official last), then repo, then tag.
slices.SortFunc(images, func(left, right imageRow) int {
leftRepo, leftTag := repoAndTag(left)
rightRepo, rightTag := repoAndTag(right)
leftOfficial := opts.All && isOfficialNamespace(leftRepo)
rightOfficial := opts.All && isOfficialNamespace(rightRepo)
if leftOfficial != rightOfficial {
if leftOfficial {
return 1
}
return -1
}
if leftRepo != rightRepo {
return strings.Compare(leftRepo, rightRepo)
}
return strings.Compare(leftTag, rightTag)
})
// Header row
table.AddField("NAME", cs.Bold)
table.AddField("VERSION", cs.Bold)
if opts.Output != "table" {
table.AddField("DIGEST", cs.Bold)
}
table.AddField("SIZE", cs.Bold)
table.EndRow()
imgloop:
for _, image := range images {
if len(image.Tags) == 0 {
continue
}
var name string
versions := make([]string, 0, len(image.Tags))
for _, taggedImgRef := range image.Tags {
tag, err := parseTagReference(taggedImgRef)
if err != nil {
log.G(ctx).Warn("Invalid tagged image reference: ", err)
continue
}
repo := tag.RepositoryStr()
if isOfficial(repo) && !opts.All {
continue imgloop
}
if name == "" {
name = repo
if isOfficialNamespace(name) {
name = strings.TrimPrefix(name, "official/")
if name == "official" {
name = ""
}
}
}
versions = append(versions, tag.TagStr())
}
if len(versions) == 0 || name == "" {
continue
}
slices.Sort(versions)
table.AddField(name, nil)
table.AddField(strings.Join(versions, ", "), nil)
if opts.Output != "table" {
table.AddField(image.Digest, nil)
}
table.AddField(humanize.Bytes(uint64(image.SizeInBytes)), nil)
table.EndRow()
}
return table.Render(iostreams.G(ctx).Out)
}
// parseTagReference parses the given tagged image reference into a name.Tage.
// The input is expected to be in the format "[namespace/]image:tag" without
// the registry host.
func parseTagReference(tref string) (gcrname.Tag, error) {
// Shortest possible string that can be parseable as a registry domain.
// https://github.com/google/go-containerregistry/blob/v0.19.0/pkg/name/repository.go#L81-L91
const sentinelRegHost = "::"
t, err := gcrname.NewTag(tref, gcrname.WithDefaultRegistry(sentinelRegHost))
if err != nil {
return gcrname.Tag{}, fmt.Errorf("parsing image reference: %w", err)
}
// Special case: the ref string is namespaced with a valid hostname,
// resulting in that namespace being parsed as the image's registry.
//
// Example:
//
// (no registry/)user.unikraft.io/myapp -> {reg:user.unikraft.io, img:myapp}
// │ └── image
// └── image namespace
//
if t.RegistryStr() != sentinelRegHost {
return parseTagReference(sentinelRegHost + "/" + tref)
}
return t, nil
}
// isOfficial naively differentiates between official and non-official images.
func isOfficial(repo string) bool {
return !isNamespacedRepository(repo)
}
// isNamespacedRepository returns whether the given image repository is
// namespaced.
func isNamespacedRepository(repo string) bool {
const regNsDelimiter = '/'
return strings.ContainsRune(repo, regNsDelimiter)
}
func isOfficialNamespace(repo string) bool {
return repo == "official" || strings.HasPrefix(repo, "official/")
}
func repoAndTag(image imageRow) (string, string) {
var (
bestRepo string
bestTag string
found bool
)
for _, ref := range image.Tags {
parsed, err := parseTagReference(ref)
if err != nil {
continue
}
repo := parsed.RepositoryStr()
tag := parsed.TagStr()
if !found || repo < bestRepo || (repo == bestRepo && tag < bestTag) {
bestRepo = repo
bestTag = tag
found = true
}
}
if !found {
return "", ""
}
return bestRepo, bestTag
}