This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 481
/
container.go
584 lines (493 loc) · 16.1 KB
/
container.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
Copyright (c) 2019 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package printer
import (
"context"
"fmt"
"path"
"sort"
"strings"
"github.com/vmware-tanzu/octant/internal/api"
"github.com/vmware-tanzu/octant/internal/log"
"github.com/vmware-tanzu/octant/internal/manifest"
"github.com/vmware-tanzu/octant/internal/util/json"
"github.com/vmware-tanzu/octant/internal/util/kubernetes"
"github.com/vmware-tanzu/octant/pkg/action"
"github.com/vmware-tanzu/octant/internal/octant"
"github.com/vmware-tanzu/octant/internal/portforward"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"github.com/vmware-tanzu/octant/pkg/store"
"github.com/vmware-tanzu/octant/pkg/view/component"
)
type containerActionFunc func(runtime.Object, *corev1.Container) (component.Action, error)
// ContainerConfigurationOption
type ContainerConfigurationOption func(*ContainerConfiguration)
func IsInit(b bool) ContainerConfigurationOption {
return func(cc *ContainerConfiguration) {
cc.isInit = b
}
}
func WithPrintOptions(options Options) ContainerConfigurationOption {
return func(cc *ContainerConfiguration) {
cc.options = options
}
}
func WithActions(actions ...containerActionFunc) ContainerConfigurationOption {
return func(cc *ContainerConfiguration) {
for _, action := range actions {
cc.actionGenerators = append(cc.actionGenerators, action)
}
}
}
// ContainerConfiguration generates container configuration.
type ContainerConfiguration struct {
parent runtime.Object
container *corev1.Container
portForwardService portforward.PortForwarder
isInit bool
options Options
context context.Context
actionGenerators []containerActionFunc
}
// NewContainerConfiguration creates an instance of ContainerConfiguration.
func NewContainerConfiguration(ctx context.Context, parent runtime.Object, c *corev1.Container, pfs portforward.PortForwarder, opts ...ContainerConfigurationOption) *ContainerConfiguration {
cc := &ContainerConfiguration{
parent: parent,
container: c,
portForwardService: pfs,
context: ctx,
actionGenerators: []containerActionFunc{},
}
for _, o := range opts {
o(cc)
}
return cc
}
// Create creates a deployment configuration summary.
func (cc *ContainerConfiguration) Create() (*component.Summary, error) {
if cc == nil || cc.container == nil {
return nil, errors.New("container is nil")
}
c := cc.container
title := "Container"
if cc.isInit {
title = "Init Container"
}
var containerStatus *corev1.ContainerStatus
var hostOS = "linux"
if pod, ok := cc.parent.(*corev1.Pod); ok {
var err error
containerStatus, err = findContainerStatus(pod, cc.container.Name, cc.isInit)
if err != nil {
switch err.(type) {
case *containerNotFoundError:
// no op
default:
return nil, errors.Wrapf(err, "get container status for %q", cc.container.Name)
}
}
for _, status := range pod.Status.EphemeralContainerStatuses {
if c.Name == status.Name {
title = "Ephemeral Container"
}
}
if api.IsWindowsContainer(pod) {
hostOS = "windows"
}
}
sections := component.SummarySections{}
sections.AddText("Image", c.Image)
if containerStatus != nil {
sections.AddText("Image ID", containerStatus.ImageID)
}
if !manifest.ManifestManager.HasEntry(hostOS, c.Image) {
sections.Add("Image Manifest", component.NewButton("Show Manifest", action.CreatePayload(octant.ActionGetManifest, map[string]interface{}{"host": hostOS, "image": c.Image})))
} else {
manifest, configuration, err := manifest.ManifestManager.GetImageManifest(cc.context, hostOS, c.Image)
if err == nil {
sections.Add("Image Manifest", component.NewJSONEditor(manifest, true))
sections.Add("Image Configuration", component.NewJSONEditor(configuration, true))
} else {
sections.Add("Image Manifest", component.NewText(fmt.Sprintf("Unable to load image manifest %s", err)))
}
}
hostPorts := describeContainerHostPorts(c.Ports)
if hostPorts != "" {
sections.AddText("Host Ports", hostPorts)
}
containerPorts, err := describeContainerPorts(cc.context, cc.parent, c.Ports, cc.portForwardService)
if err != nil {
return nil, errors.Wrap(err, "describe container ports")
}
if len(containerPorts) > 0 {
sections.Add("Container Ports", component.NewPorts(containerPorts))
}
if containerStatus != nil && !cc.isInit {
lastState, found := printContainerState(containerStatus.LastTerminationState)
if found {
sections.AddText("Last State", lastState)
}
currentState, found := printContainerState(containerStatus.State)
if found {
sections.AddText("Current State", currentState)
}
sections.AddText("Ready", fmt.Sprintf("%t", containerStatus.Ready))
sections.AddText("Restart Count", fmt.Sprintf("%d", containerStatus.RestartCount))
}
envTbl, err := describeContainerEnv(cc.context, cc.parent, c, cc.options)
if err != nil {
return nil, errors.Wrap(err, "describing environment")
}
if len(envTbl.Rows()) > 0 {
sections.Add("Environment", envTbl)
}
cmd := printSlice(c.Command)
if cmd != "" {
sections.AddText("Command", cmd)
}
args := printSlice(c.Args)
if args != "" {
sections.AddText("Args", args)
}
if len(c.VolumeMounts) > 0 {
sections.Add("Volume Mounts", describeVolumeMounts(c))
}
summary := component.NewSummary(fmt.Sprintf("%s %s", title, c.Name), sections...)
for _, actionFunc := range cc.actionGenerators {
action, err := actionFunc(cc.parent, c)
if err != nil {
logger := log.From(cc.context)
logger.Errorf("failed loading actions: %w", err)
continue
}
summary.AddAction(action)
}
return summary, nil
}
func printContainerState(state corev1.ContainerState) (string, bool) {
switch {
case state.Running != nil:
return fmt.Sprintf("started at %s", state.Running.StartedAt), true
case state.Waiting != nil:
return fmt.Sprintf("waiting: %s", state.Waiting.Message), true
case state.Terminated != nil:
return fmt.Sprintf("terminated with %d at %s: %s",
state.Terminated.ExitCode,
state.Terminated.FinishedAt,
state.Terminated.Reason), true
}
return "indeterminate", false
}
type containerStatus interface {
isContainerFound() bool
}
type containerNotFoundError struct {
name string
}
var _ containerStatus = (*containerNotFoundError)(nil)
func (e *containerNotFoundError) Error() string {
return fmt.Sprintf("container %q not found", e.name)
}
func (e *containerNotFoundError) isContainerFound() bool {
return false
}
func findContainerStatus(pod *corev1.Pod, name string, isInit bool) (*corev1.ContainerStatus, error) {
if pod == nil {
return nil, errors.New("pod is nil")
}
statuses := pod.Status.ContainerStatuses
if isInit {
statuses = pod.Status.InitContainerStatuses
}
if len(pod.Status.EphemeralContainerStatuses) > 0 {
statuses = pod.Status.EphemeralContainerStatuses
}
for _, status := range statuses {
if status.Name == name {
return &status, nil
}
}
return nil, &containerNotFoundError{name: name}
}
func isPodGVK(gvk schema.GroupVersionKind) bool {
return gvk.Group == "" && gvk.Version == "v1" && gvk.Kind == "Pod"
}
type notFound interface {
NotFound() bool
}
func describeContainerPorts(
ctx context.Context,
parent runtime.Object,
cPorts []corev1.ContainerPort,
portForwardService portforward.PortForwarder) ([]component.Port, error) {
var list []component.Port
var namespace string
var name string
var err error
gvk := parent.GetObjectKind().GroupVersionKind()
isPod := isPodGVK(gvk)
if isPod {
accessor := meta.NewAccessor()
namespace, err = accessor.Namespace(parent)
if err != nil {
return nil, errors.Wrap(err, "find parent namespace")
}
name, err = accessor.Name(parent)
if err != nil {
return nil, errors.Wrap(err, "find parent name")
}
}
states, err := portForwardService.FindPod(namespace, gvk, name)
if err != nil {
if _, ok := err.(notFound); !ok {
return nil, errors.Wrap(err, "query port forward service for pod")
}
}
pfLookup := make(map[int32]string, len(states))
for _, cPort := range cPorts {
for _, state := range states {
for _, forwarded := range state.Ports {
if int(forwarded.Remote) == int(cPort.ContainerPort) {
pfLookup[cPort.ContainerPort] = state.ID
}
}
}
if cPort.ContainerPort == 0 {
continue
}
pfs := component.PortForwardState{}
var port *component.Port
if isPod && cPort.Protocol == corev1.ProtocolTCP {
pfs.IsForwardable = true
}
if id, ok := pfLookup[cPort.ContainerPort]; ok {
if state, found := portForwardService.Get(id); found {
for _, forwarded := range state.Ports {
pfs.Port = int(forwarded.Local)
}
} else {
return nil, errors.Wrap(err, "id not found for port forward")
}
pfs.ID = id
pfs.IsForwarded = true
}
apiVersion, kind := gvk.ToAPIVersionAndKind()
port = component.NewPort(namespace, apiVersion, kind, name, int(cPort.ContainerPort), string(cPort.Protocol), pfs)
list = append(list, *port)
}
return list, nil
}
func describeContainerHostPorts(cPorts []corev1.ContainerPort) string {
ports := make([]string, 0, len(cPorts))
for _, cPort := range cPorts {
if cPort.HostPort == 0 {
continue
}
ports = append(ports, fmt.Sprintf("%d/%s", cPort.HostPort, cPort.Protocol))
}
return strings.Join(ports, ", ")
}
// describeContainerEnv returns a table describing a container environment
func describeContainerEnv(ctx context.Context, parent runtime.Object, c *corev1.Container, options Options) (*component.Table, error) {
if c == nil {
return nil, errors.New("container is nil")
}
var ns string
if parent != nil {
accessor := meta.NewAccessor()
ns, _ = accessor.Namespace(parent)
}
if ns == "" {
ns = "default"
}
cols := component.NewTableCols("Name", "Value", "Source")
tbl := component.NewTable("Environment", "There are no defined environment variables!", cols)
envRows, err := describeEnvRows(ctx, ns, c.Env, options)
if err != nil {
return nil, err
}
tbl.Add(envRows...)
envFromRows, err := describeEnvFromRows(ns, c.EnvFrom, options)
if err != nil {
return nil, err
}
tbl.Add(envFromRows...)
return tbl, nil
}
// describeEnvRows renders container environment variables as table rows.
// Expected columns: Name, Value, Source
func describeEnvRows(ctx context.Context, namespace string, vars []corev1.EnvVar, options Options) ([]component.TableRow, error) {
rows := make([]component.TableRow, 0)
sort.Slice(vars, func(i, j int) bool { return vars[i].Name < vars[j].Name })
for _, e := range vars {
row := component.TableRow{}
rows = append(rows, row)
// each row requires data for each defined column
row["Source"] = component.NewText("")
row["Name"] = component.NewText(e.Name)
row["Value"] = component.NewText(e.Value)
if e.Value != "" || e.ValueFrom == nil {
continue
}
switch {
case e.ValueFrom.FieldRef != nil:
ref := e.ValueFrom.FieldRef
row["Source"] = component.NewText(ref.FieldPath)
case e.ValueFrom.ResourceFieldRef != nil:
ref := e.ValueFrom.ResourceFieldRef
row["Source"] = component.NewText(ref.Resource)
case e.ValueFrom.SecretKeyRef != nil:
ref := e.ValueFrom.SecretKeyRef
source, err := options.Link.ForGVK(namespace, "v1", "Secret", ref.Name,
fmt.Sprintf("%s:%s", ref.Name, ref.Key))
if err != nil {
return nil, err
}
row["Source"] = source
case e.ValueFrom.ConfigMapKeyRef != nil:
ref := e.ValueFrom.ConfigMapKeyRef
source, err := options.Link.ForGVK(namespace, "v1", "ConfigMap", ref.Name,
fmt.Sprintf("%s:%s", ref.Name, ref.Key))
if err != nil {
return nil, err
}
objectStore := options.DashConfig.ObjectStore()
key := store.Key{
Name: ref.Name,
Namespace: namespace,
APIVersion: "v1",
Kind: "ConfigMap",
}
u, err := objectStore.Get(ctx, key)
if u == nil || err != nil {
row["Value"] = component.NewText("<none>")
row["Source"] = component.NewText(fmt.Sprintf("%s:%s", ref.Name, ref.Key))
} else {
configMap := &corev1.ConfigMap{}
if err := kubernetes.FromUnstructured(u, configMap); err != nil {
return nil, err
}
row["Value"] = component.NewText(configMap.Data[ref.Key])
row["Source"] = source
}
}
}
return rows, nil
}
// describeEnvFromRows renders container environmentFrom references as table rows.
// Expected columns: Name, Value, Source
func describeEnvFromRows(namespace string, vars []corev1.EnvFromSource, options Options) ([]component.TableRow, error) {
rows := make([]component.TableRow, 0)
for _, e := range vars {
row := component.TableRow{}
rows = append(rows, row)
switch {
case e.SecretRef != nil:
ref := e.SecretRef
source, err := options.Link.ForGVK(namespace, "v1", "Secret", ref.Name, ref.Name)
if err != nil {
return nil, err
}
row["Source"] = source
case e.ConfigMapRef != nil:
ref := e.ConfigMapRef
source, err := options.Link.ForGVK(namespace, "v1", "ConfigMap", ref.Name, ref.Name)
if err != nil {
return nil, err
}
row["Source"] = source
}
}
return rows, nil
}
// printSlice returns a string representation of a string slice, in a format similar
// to ['a', 'b', 'c']. An empty slice will be returned as an empty string, rather than [].
func printSlice(s []string) string {
if len(s) == 0 {
return ""
}
return fmt.Sprintf("['%s']", strings.Join(s, "', '"))
}
func printVolumeMountPath(mnt corev1.VolumeMount) string {
var access string
if mnt.ReadOnly {
access = "ro"
} else {
access = "rw"
}
p := path.Join(mnt.MountPath, mnt.SubPath)
return fmt.Sprintf("%s (%s)", p, access)
}
func describeVolumeMounts(c *corev1.Container) *component.Table {
cols := component.NewTableCols("Name", "Mount Path", "Propagation")
tbl := component.NewTable("Volume Mounts", "There are no volume mounts!", cols)
for _, m := range c.VolumeMounts {
row := component.TableRow{}
row["Name"] = component.NewText(m.Name)
row["Mount Path"] = component.NewText(printVolumeMountPath(m))
var prop string
if m.MountPropagation != nil {
prop = string(*m.MountPropagation)
}
row["Propagation"] = component.NewText(prop)
tbl.Add(row)
}
return tbl
}
func editContainerAction(owner runtime.Object, container *corev1.Container) (component.Action, error) {
if container == nil {
return component.Action{}, errors.New("container is nil")
}
containersPath, err := containersPathForObject(owner)
if err != nil {
return component.Action{}, err
}
containersPathData, err := json.Marshal(containersPath)
if err != nil {
return component.Action{}, err
}
form, err := component.CreateFormForObject(octant.ActionOverviewContainerEditor, owner,
component.NewFormFieldText("Image", "containerImage", container.Image),
component.NewFormFieldHidden("containersPath", string(containersPathData)),
component.NewFormFieldHidden("containerName", container.Name),
)
if err != nil {
return component.Action{}, err
}
action := component.Action{
Name: "Edit",
Title: fmt.Sprintf("Container %s Editor", container.Name),
Form: form,
}
return action, nil
}
func containersPathForObject(object runtime.Object) ([]string, error) {
if object == nil {
return nil, errors.New("object is nil")
}
g := object.GetObjectKind().GroupVersionKind()
switch {
case g.Group == "batch" && g.Kind == "CronJob":
return []string{"spec", "jobTemplate", "spec", "template", "containers"}, nil
case g.Group == "batch" && g.Kind == "Job":
return []string{"spec", "template", "containers"}, nil
case g.Group == "apps" && g.Kind == "DaemonSet":
return []string{"spec", "template", "spec", "containers"}, nil
case g.Group == "apps" && g.Kind == "Deployment":
return []string{"spec", "template", "spec", "containers"}, nil
case g.Group == "apps" && g.Kind == "ReplicaSet":
return []string{"spec", "template", "spec", "containers"}, nil
case g.Group == "" && g.Kind == "ReplicationController":
return []string{"spec", "template", "spec", "containers"}, nil
case g.Group == "apps" && g.Kind == "StatefulSet":
return []string{"spec", "template", "spec", "containers"}, nil
default:
return nil, errors.Errorf("unable to find containers location for %+v, %s, %s", g, g.Group, g.Kind)
}
}