-
Notifications
You must be signed in to change notification settings - Fork 14
/
pipeline_container_view.go
162 lines (141 loc) · 5.01 KB
/
pipeline_container_view.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
package views // import "github.com/jenkins-x/octant-jx/pkg/plugin/views"
import (
"fmt"
"log"
"strings"
"github.com/jenkins-x/octant-jx/pkg/common/links"
"github.com/jenkins-x/octant-jx/pkg/common/pluginctx"
"github.com/jenkins-x/octant-jx/pkg/common/viewhelpers"
"github.com/jenkins-x/octant-jx/pkg/plugin"
"github.com/vmware-tanzu/octant/pkg/plugin/service"
"github.com/vmware-tanzu/octant/pkg/view/component"
corev1 "k8s.io/api/core/v1"
)
func BuildPipelineContainerView(request service.Request, pluginContext pluginctx.Context) (component.Component, error) {
paths := strings.Split(strings.TrimSuffix(request.Path(), "/"), "/")
pipelineName := ""
pl := len(paths)
if len(paths) < 3 {
return component.NewText("not enough values in the path"), nil
}
pipelineName = paths[pl-3]
name := paths[pl-2]
step := paths[pl-1]
ctx := request.Context()
client := request.DashboardClient()
ns := pluginContext.Namespace
//log.Printf("BuildPipelineContainersView querying for Pod %s in namespace %s\n", name, ns)
u, err := viewhelpers.GetResourceByName(ctx, client, "v1", "Pod", name, ns)
if err != nil {
return nil, err
}
if u == nil {
return component.NewText(fmt.Sprintf("Error: Pod %s not found in namespace %s", name, ns)), nil
}
pod := &corev1.Pod{}
err = viewhelpers.ToStructured(u, &pod)
if err != nil {
log.Println(err)
return component.NewText(fmt.Sprintf("Error: failed to load Pod %s not found in namespace %s", name, ns)), nil
}
breadcrumbs := []string{
plugin.RootBreadcrumb,
viewhelpers.ToMarkdownLink("Pipelines", plugin.GetPipelinesLink()),
}
if pipelineName != "" {
breadcrumbs = append(breadcrumbs, viewhelpers.ToMarkdownLink("Pipeline", plugin.GetPipelineLink(pipelineName)))
}
breadcrumbs = append(breadcrumbs, viewhelpers.ToMarkdownLink("Pod", links.GetPodLink(ns, name)))
if pipelineName != "" {
breadcrumbs = append(breadcrumbs, viewhelpers.ToMarkdownLink("Steps", plugin.GetPipelineContainersLink(ns, pipelineName, name)))
}
breadcrumbs = append(breadcrumbs, step)
header := component.NewMarkdownText(viewhelpers.ToBreadcrumbMarkdown(breadcrumbs...))
notesCard := component.NewCard(nil)
vc := containersViewContext{
Request: request,
Namespace: ns,
PipelineName: pipelineName,
PodName: name,
}
found := false
for i, container := range pod.Spec.Containers {
if container.Name == step {
return ToPipelinePodContainerView(header, vc, pod, i, &container), nil
found = true
break
}
}
if !found {
notesCard.SetBody(component.NewMarkdownText(fmt.Sprintf("Pod %s does not have a container called %s", pod, step)))
}
flexLayout := component.NewFlexLayout("")
flexLayout.AddSections(component.FlexLayoutSection{
{Width: component.WidthFull, View: header},
{Width: component.WidthFull, View: notesCard},
})
return flexLayout, nil
}
func ToPipelinePodContainerView(header component.Component, vc containersViewContext, pod *corev1.Pod, index int, c *corev1.Container) component.Component {
image := ToImage(c)
commandLine := ToCommandLine(index, c)
statusSummarySections := []component.SummarySection{
{"Name", component.NewText(c.Name)},
{"Image", component.NewMarkdownText(image)},
{"Working Dir", component.NewText(c.WorkingDir)},
{"Command", component.NewMarkdownText(fmt.Sprintf("```%s```", commandLine))},
}
statusSummary := component.NewSummary("Container", statusSummarySections...)
volumesSections := []component.SummarySection{}
if len(c.VolumeMounts) > 0 {
for _, v := range c.VolumeMounts {
volumesSections = append(volumesSections, ToVolumeMountSection(pod, c, v))
}
}
viewhelpers.SortSummarySection(volumesSections)
volumesSummary := component.NewSummary("Volume Mounts", volumesSections...)
envSections := []component.SummarySection{}
for _, e := range c.Env {
envSections = append(envSections, ToEnvVarSection(e))
}
viewhelpers.SortSummarySection(envSections)
envSummary := component.NewSummary("Environment Variables", envSections...)
flexLayout := component.NewFlexLayout("")
flexLayout.AddSections(component.FlexLayoutSection{
{Width: component.WidthFull, View: header},
{Width: component.WidthHalf, View: statusSummary},
{Width: component.WidthHalf, View: volumesSummary},
{Width: component.WidthFull, View: envSummary},
})
return flexLayout
}
func ToEnvVarSection(e corev1.EnvVar) component.SummarySection {
name := e.Name
value := e.Value
f := e.ValueFrom
if value == "" && f != nil {
cm := f.ConfigMapKeyRef
if cm != nil {
value = fmt.Sprintf("from ConfigMap %s %s", cm.Name, cm.Key)
}
sec := f.SecretKeyRef
if sec != nil {
value = fmt.Sprintf("from Secret %s %s", sec.Name, sec.Key)
}
}
return component.SummarySection{
Header: name,
Content: component.NewMarkdownText(value),
}
}
func ToVolumeMountSection(pod *corev1.Pod, c *corev1.Container, v corev1.VolumeMount) component.SummarySection {
mountPath := v.MountPath
subPath := v.SubPath
if subPath != "" {
mountPath += subPath
}
return component.SummarySection{
Header: v.Name,
Content: component.NewMarkdownText(mountPath),
}
}