forked from fluxcd/flux
/
list_images_cmd.go
151 lines (129 loc) · 3.84 KB
/
list_images_cmd.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
package main
import (
"context"
"fmt"
"sort"
"time"
"github.com/spf13/cobra"
"github.com/weaveworks/flux"
"github.com/weaveworks/flux/api/v6"
"github.com/weaveworks/flux/registry"
"github.com/weaveworks/flux/update"
)
type controllerShowOpts struct {
*rootOpts
namespace string
controller string
limit int
// Deprecated
service string
}
func newControllerShow(parent *rootOpts) *controllerShowOpts {
return &controllerShowOpts{rootOpts: parent}
}
func (opts *controllerShowOpts) Command() *cobra.Command {
cmd := &cobra.Command{
Use: "list-images",
Short: "Show the deployed and available images for a controller.",
Example: makeExample("fluxctl list-images --namespace default --controller=deployment/foo"),
RunE: opts.RunE,
}
cmd.Flags().StringVarP(&opts.namespace, "namespace", "n", "default", "Controller namespace")
cmd.Flags().StringVarP(&opts.controller, "controller", "c", "", "Show images for this controller")
cmd.Flags().IntVarP(&opts.limit, "limit", "l", 10, "Number of images to show (0 for all)")
// Deprecated
cmd.Flags().StringVarP(&opts.service, "service", "s", "", "Show images for this service")
cmd.Flags().MarkHidden("service")
return cmd
}
func (opts *controllerShowOpts) RunE(cmd *cobra.Command, args []string) error {
if len(opts.service) > 0 {
return errorServiceFlagDeprecated
}
if len(args) != 0 {
return errorWantedNoArgs
}
var resourceSpec update.ResourceSpec
if len(opts.controller) == 0 {
resourceSpec = update.ResourceSpecAll
} else {
id, err := flux.ParseResourceIDOptionalNamespace(opts.namespace, opts.controller)
if err != nil {
return err
}
resourceSpec = update.MakeResourceSpec(id)
}
ctx := context.Background()
controllers, err := opts.API.ListImages(ctx, resourceSpec)
if err != nil {
return err
}
sort.Sort(imageStatusByName(controllers))
out := newTabwriter()
fmt.Fprintln(out, "CONTROLLER\tCONTAINER\tIMAGE\tCREATED")
for _, controller := range controllers {
if len(controller.Containers) == 0 {
fmt.Fprintf(out, "%s\t\t\t\n", controller.ID)
continue
}
controllerName := controller.ID.String()
for _, container := range controller.Containers {
var lineCount int
containerName := container.Name
reg, repo, currentTag := container.Current.ID.Components()
if reg != "" {
reg += "/"
}
if len(container.Available) == 0 {
availableErr := container.AvailableError
if availableErr == "" {
availableErr = registry.ErrNoImageData.Error()
}
fmt.Fprintf(out, "%s\t%s\t%s%s\t%s\n", controllerName, containerName, reg, repo, availableErr)
} else {
fmt.Fprintf(out, "%s\t%s\t%s%s\t\n", controllerName, containerName, reg, repo)
}
foundRunning := false
for _, available := range container.Available {
running := "| "
_, _, tag := available.ID.Components()
if currentTag == tag {
running = "'->"
foundRunning = true
} else if foundRunning {
running = " "
}
lineCount++
var printEllipsis, printLine bool
if opts.limit <= 0 || lineCount <= opts.limit {
printEllipsis, printLine = false, true
} else if container.Current.ID == available.ID {
printEllipsis, printLine = lineCount > (opts.limit+1), true
}
if printEllipsis {
fmt.Fprintf(out, "\t\t%s (%d image(s) omitted)\t\n", ":", lineCount-opts.limit-1)
}
if printLine {
createdAt := ""
if !available.CreatedAt.IsZero() {
createdAt = available.CreatedAt.Format(time.RFC822)
}
fmt.Fprintf(out, "\t\t%s %s\t%s\n", running, tag, createdAt)
}
}
controllerName = ""
}
}
out.Flush()
return nil
}
type imageStatusByName []v6.ImageStatus
func (s imageStatusByName) Len() int {
return len(s)
}
func (s imageStatusByName) Less(a, b int) bool {
return s[a].ID.String() < s[b].ID.String()
}
func (s imageStatusByName) Swap(a, b int) {
s[a], s[b] = s[b], s[a]
}