-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathkubectl_images.go
411 lines (360 loc) · 10.2 KB
/
kubectl_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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package kubeimage
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path"
"regexp"
"strconv"
"strings"
"github.com/dustin/go-humanize"
"github.com/olekukonko/tablewriter"
"gopkg.in/yaml.v2"
)
const (
podTemplate = `go-template={{range .items}} {{.metadata.namespace}} {{","}} {{.metadata.name}} {{","}} {{range .spec.containers}} {{.name}} {{","}} {{.image}} {{","}} {{.imagePullPolicy}} {{"\n"}} {{end}} {{range .spec.initContainers}} {{"(init)"}} {{.name}} {{","}} {{.image}} {{","}} {{.imagePullPolicy}} {{"\n"}} {{end}} {{end}}`
nodeTemplate = `go-template={{range .items}} {{range .status.images}} {{range .names}} {{.}} {{","}} {{end}} {{.sizeBytes}} {{"\n"}} {{end}} {{end}}`
labelNamespace = "Namespace"
labelPod = "Pod"
labelContainer = "Container"
labelImage = "Image"
labelImagePullPolicy = "ImagePullPolicy"
labelImageSize = "ImageSize"
)
type Parameters struct {
AllNamespace bool
Namespace string
Columns string
KubeConfig string
Context string
Unique bool
}
// KubeImage is the representation of a container image used in the cluster.
type KubeImage struct {
entities []*ImageEntity
columns []string
regx *regexp.Regexp
params Parameters
imageSize map[string]int
needNodeInfo bool
}
// NewKubeImage creates a new KubeImage instance.
func NewKubeImage(regx *regexp.Regexp, params Parameters) *KubeImage {
var needNodeInfo bool
names := make([]string, 0)
for _, c := range stringSplit(params.Columns, ",") {
switch c {
case "0":
names = append(names, labelNamespace)
case "1":
names = append(names, labelPod)
case "2":
names = append(names, labelContainer)
case "3":
names = append(names, labelImage)
case "4":
names = append(names, labelImagePullPolicy)
case "5":
names = append(names, labelImageSize)
needNodeInfo = true
}
}
return &KubeImage{
columns: names,
params: params,
regx: regx,
imageSize: make(map[string]int),
needNodeInfo: needNodeInfo,
}
}
// ImageEntity is the representation of an entity to be displayed.
type ImageEntity struct {
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
Pod string `json:"pod,omitempty" yaml:"pod,omitempty"`
Container string `json:"container,omitempty" yaml:"container,omitempty"`
Image string `json:"image,omitempty" yaml:"image,omitempty"`
ImagePullPolicy string `json:"imagePullPolicy,omitempty" yaml:"imagePullPolicy,omitempty"`
ImageSize string `json:"imageSize,omitempty" yaml:"imageSize,omitempty"`
}
func (ie *ImageEntity) selectBy(columns []string) []string {
result := make([]string, 0)
for _, c := range columns {
switch c {
case labelNamespace:
result = append(result, ie.Namespace)
case labelPod:
result = append(result, ie.Pod)
case labelContainer:
result = append(result, ie.Container)
case labelImage:
result = append(result, ie.Image)
case labelImagePullPolicy:
result = append(result, ie.ImagePullPolicy)
case labelImageSize:
result = append(result, ie.ImageSize)
}
}
return result
}
func (ie *ImageEntity) filterBy(columns []string) ImageEntity {
var entity ImageEntity
for _, c := range columns {
switch c {
case labelNamespace:
entity.Namespace = ie.Namespace
case labelPod:
entity.Pod = ie.Pod
case labelContainer:
entity.Container = ie.Container
case labelImage:
entity.Image = ie.Image
case labelImagePullPolicy:
entity.ImagePullPolicy = ie.ImagePullPolicy
case labelImageSize:
entity.ImageSize = ie.ImageSize
}
}
return entity
}
// Counter is a simple counter.
type Counter struct {
cnt int
items map[string]bool
}
// NewCounter creates a new Counter instance.
func NewCounter() *Counter {
return &Counter{items: make(map[string]bool)}
}
func (c *Counter) add(obj string) {
if !c.items[obj] {
c.cnt += 1
c.items[obj] = true
}
}
// Count returns current counter reading.
func (c *Counter) Count() int {
return c.cnt
}
func stringSplit(in, sep string) []string {
out := make([]string, 0)
for _, s := range strings.Split(in, sep) {
out = append(out, strings.TrimSpace(s))
}
return out
}
// podCommands builds the command to be executed based on user input.
func (ki *KubeImage) podCommands() []string {
kubecfg := make([]string, 0)
if ki.params.KubeConfig != "" {
kubecfg = append(kubecfg, "--kubeconfig", ki.params.KubeConfig)
}
if ki.params.Context != "" {
kubecfg = append(kubecfg, "--context", ki.params.Context)
}
if ki.params.AllNamespace {
return append([]string{"get", "pods", "--all-namespaces", "-o", podTemplate}, kubecfg...)
} else if ki.params.Namespace != "" {
return append([]string{"get", "pods", "-n", ki.params.Namespace, "-o", podTemplate}, kubecfg...)
}
return append([]string{"get", "pods", "-o", podTemplate}, kubecfg...)
}
func (ki *KubeImage) nodeCommands() []string {
kubecfg := make([]string, 0)
if ki.params.KubeConfig != "" {
kubecfg = append(kubecfg, "--kubeconfig", ki.params.KubeConfig)
}
if ki.params.Context != "" {
kubecfg = append(kubecfg, "--context", ki.params.Context)
}
return append([]string{"get", "nodes", "-o", nodeTemplate}, kubecfg...)
}
func (ki *KubeImage) recordImageSize(image string, size int) {
ki.imageSize[image] = size
ki.imageSize[path.Base(image)] = size
}
func (ki *KubeImage) execNodeCommand() {
process := exec.Command("kubectl", ki.nodeCommands()...)
bs, err := process.CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "[Oh...] Execute nodes command error: %v, %s", err, string(bs))
os.Exit(1)
}
for _, line := range stringSplit(string(bs), "\n") {
items := stringSplit(line, ",")
switch len(items) {
case 2:
size, err := strconv.Atoi(items[1])
if err != nil {
continue
}
ki.recordImageSize(items[0], size)
parts := strings.Split(items[0], ":")
if len(parts) == 2 && parts[1] == "latest" {
ki.recordImageSize(parts[0], size)
}
case 3:
size, err := strconv.Atoi(items[2])
if err != nil {
continue
}
ki.recordImageSize(items[0], size)
ki.recordImageSize(items[1], size)
parts := strings.Split(items[1], ":")
if len(parts) == 2 && parts[1] == "latest" {
ki.recordImageSize(parts[0], size)
}
}
}
for _, entity := range ki.entities {
size, ok := ki.imageSize[entity.Image]
if ok {
entity.ImageSize = humanize.IBytes(uint64(size))
}
}
}
func (ki *KubeImage) execPodCommand() {
process := exec.Command("kubectl", ki.podCommands()...)
bs, err := process.CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "[Oh...] Execute pods command error: %v, %s", err, string(bs))
os.Exit(1)
}
entities := make([]*ImageEntity, 0)
for _, line := range stringSplit(string(bs), "\n") {
items := stringSplit(line, ",")
entity := &ImageEntity{}
switch len(items) {
case 1:
continue
case 3:
entity.Container = items[0]
entity.Image = items[1]
entity.ImagePullPolicy = items[2]
case 5:
entity.Namespace = items[0]
entity.Pod = items[1]
entity.Container = items[2]
entity.Image = items[3]
entity.ImagePullPolicy = items[4]
}
entities = append(entities, entity)
}
for i := 0; i < len(entities); i++ {
if entities[i].Pod == "" && i > 0 {
entities[i].Namespace = entities[i-1].Namespace
entities[i].Pod = entities[i-1].Pod
}
}
for i := 0; i < len(entities); i++ {
if ki.regx == nil {
ki.entities = append(ki.entities, entities[i])
continue
}
if ki.regx.Match([]byte(entities[i].Pod)) {
ki.entities = append(ki.entities, entities[i])
}
}
}
func (ki *KubeImage) groupBy() []*ImageEntity {
if !ki.params.Unique {
return ki.entities
}
set := make(map[string]struct{})
entities := make([]*ImageEntity, 0)
for i, entity := range ki.entities {
k := fmt.Sprintf("%s/%s/%s/%s", entity.Namespace, entity.Container, entity.Image, entity.ImagePullPolicy)
if _, ok := set[k]; ok {
continue
}
set[k] = struct{}{}
entities = append(entities, ki.entities[i])
}
return entities
}
func (ki *KubeImage) summary() {
namespaceCnt := NewCounter()
podCnt := NewCounter()
imageCnt := NewCounter()
containerCnt := 0
for i := 0; i < len(ki.entities); i++ {
namespaceCnt.add(ki.entities[i].Namespace)
podCnt.add(ki.entities[i].Pod)
imageCnt.add(ki.entities[i].Image)
containerCnt += 1
}
fmt.Fprintf(os.Stdout, "[Summary]: %d namespaces, %d pods, %d containers and %d different images\n",
namespaceCnt.Count(), podCnt.Count(), containerCnt, imageCnt.Count(),
)
}
func (ki *KubeImage) tableRender() {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(ki.columns)
table.SetAutoFormatHeaders(false)
table.SetAutoMergeCells(true)
table.SetRowLine(true)
entities := ki.getGroupByEntities()
for _, entity := range entities {
table.Append(entity)
}
table.Render()
}
func (ki *KubeImage) getGroupByEntities() [][]string {
set := make(map[string]struct{})
dst := make([][]string, 0)
for _, entity := range ki.groupBy() {
line := strings.Join(entity.selectBy(ki.columns), "||")
_, ok := set[line]
if !ok {
set[line] = struct{}{}
dst = append(dst, strings.Split(line, "||"))
}
}
return dst
}
func (ki *KubeImage) jsonRender() {
entities := ki.groupBy()
records := make([]ImageEntity, 0, len(entities))
for _, entity := range entities {
records = append(records, entity.filterBy(ki.columns))
}
output, err := json.MarshalIndent(records, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "[Oh...] Failed to marshal JSON data, error: %v", err)
os.Exit(1)
}
fmt.Fprintln(os.Stdout, string(output))
}
func (ki *KubeImage) yamlRender() {
entities := ki.groupBy()
records := make([]ImageEntity, 0, len(entities))
for _, entity := range entities {
records = append(records, entity.filterBy(ki.columns))
}
output, err := yaml.Marshal(records)
if err != nil {
fmt.Fprintf(os.Stderr, "[Oh...] Failed to marshal YAML data, error: %v", err)
os.Exit(1)
}
fmt.Fprintln(os.Stdout, string(output))
}
// Render renders and displays the table output.
func (ki *KubeImage) Render(format string) {
ki.execPodCommand()
if ki.needNodeInfo {
ki.execNodeCommand()
}
if len(ki.entities) == 0 {
fmt.Fprintln(os.Stdout, "[Oh...] No images matched!")
return
}
switch format {
case "json", "j":
ki.jsonRender()
case "yaml", "y":
ki.yamlRender()
default: // table
ki.summary()
ki.tableRender()
}
}