-
Notifications
You must be signed in to change notification settings - Fork 14
/
pipeline_terminal_view.go
87 lines (76 loc) · 2.83 KB
/
pipeline_terminal_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
package views // import "github.com/jenkins-x/octant-jx/pkg/plugin/views"
import (
"fmt"
"strings"
"time"
"github.com/jenkins-x/jx-logging/pkg/log"
"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"
)
const useTerminal = true
func BuildPipelineTerminalView(request service.Request, pluginContext pluginctx.Context) (component.Component, error) {
paths := strings.Split(strings.TrimSuffix(request.Path(), "/"), "/")
pipelineName := ""
pl := len(paths)
if pl > 1 {
pipelineName = paths[pl-2]
}
name := paths[pl-1]
ctx := request.Context()
client := request.DashboardClient()
ns := pluginContext.Namespace
log.Logger().Infof("BuildPipelineTerminalView querying for Pipeline %s Pod %s in namespace %s\n", pipelineName, 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.Logger().Info(err)
return component.NewText(fmt.Sprintf("Error: failed to load Pod %s not found in namespace %s", name, ns)), nil
}
containers := pod.Spec.Containers
if len(containers) == 0 {
return component.NewText(fmt.Sprintf("Error: no containers for Pod %s found in namespace %s", name, ns)), nil
}
lastContainer := containers[len(containers)-1]
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)), "Terminal")
header := component.NewMarkdownText(viewhelpers.ToBreadcrumbMarkdown(breadcrumbs...))
var terminal component.Component
containerName := lastContainer.Name
if useTerminal {
details := component.TerminalDetails{
Container: containerName,
Command: "/bin/sh",
CreatedAt: time.Now(),
Active: true,
//UUID: name + "-" + containerName,
}
terminal = component.NewTerminal(ns, name, name, details)
return terminal, nil
} else {
terminal = component.NewMarkdownText(fmt.Sprintf("this would be a terminal for pod %s container %s", name, containerName))
}
flexLayout := component.NewFlexLayout("")
flexLayout.AddSections(component.FlexLayoutSection{
{Width: component.WidthFull, View: header},
{Width: component.WidthFull, View: terminal},
})
return flexLayout, nil
}