-
Notifications
You must be signed in to change notification settings - Fork 14
/
helm_release_view.go
147 lines (124 loc) · 4.46 KB
/
helm_release_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
/*
Copyright 2019 Blood Orange
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package views
import (
"fmt"
"strings"
"time"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/octant-jx/pkg/common/helm"
"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/store"
"github.com/vmware-tanzu/octant/pkg/view/component"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/yaml"
)
// see https://github.com/vmware-tanzu/octant/issues/919
const yamlSupportedInPlugin = false
func BuildHelmReleaseView(request service.Request, pluginContext pluginctx.Context) (component.Component, error) {
paths := strings.Split(strings.TrimPrefix(request.Path(), "/"), "/")
releaseName := paths[len(paths)-1]
ctx := request.Context()
client := request.DashboardClient()
ns := pluginContext.Namespace
log.Logger().Debugf("BuildHelmReleasesView querying for secrets owned by helm with release %s\n", releaseName)
ul, err := client.List(ctx, store.Key{
APIVersion: "v1",
Kind: "Secret",
Namespace: ns,
Selector: &labels.Set{
"owner": "helm",
"status": "deployed",
"name": releaseName,
},
})
if err != nil {
log.Logger().Info(err)
return nil, err
}
log.Logger().Infof("BuildHelmReleaseView looking for release %s in namespace %s got list of secrets %d\n", releaseName, ns, len(ul.Items))
helmReleases := helm.UnstructuredListToAnyHelmReleaseList(ul)
if len(helmReleases) == 0 {
return component.NewText(fmt.Sprintf("Error: release %s not found in namespace %s", releaseName, ns)), nil
}
r := helmReleases[0]
header := component.NewMarkdownText(viewhelpers.ToBreadcrumbMarkdown(
plugin.RootBreadcrumb,
viewhelpers.ToMarkdownLink("Helm", plugin.GetHelmLink()),
releaseName))
statusSummarySections := []component.SummarySection{
{Header: "Name", Content: ToHelmName(r)},
{Header: "Status", Content: ToHelmStatus(r)},
{Header: "Last Deployed", Content: component.NewText(r.Info.LastDeployed.Format(time.ANSIC))},
{Header: "Revision", Content: component.NewText(fmt.Sprintf("%d", r.Version))},
}
statusSummary := component.NewSummary("Status", statusSummarySections...)
notesCard := viewhelpers.NewMarkdownCard("Notes", fmt.Sprintf("\n%s\n", strings.TrimSpace(r.Info.Notes)))
flexLayout := component.NewFlexLayout("")
flexLayout.AddSections(component.FlexLayoutSection{
{Width: component.WidthFull, View: header},
{Width: component.WidthHalf, View: statusSummary},
{Width: component.WidthHalf, View: notesCard},
})
if r.Config != nil {
values := r.Config
if len(values) > 0 {
data, err := yaml.Marshal(values)
if err != nil {
log.Logger().Infof("failed to marshal helm values to YAML: %s with values %#v", err.Error(), values)
} else {
var yamlView component.Component
if yamlSupportedInPlugin {
yamlView = component.NewYAML(component.TitleFromString("Values"), string(data))
} else {
yamlView = viewhelpers.NewMarkdownCard("Values", YAMLToMarkdown(string(data)))
}
flexLayout.AddSections(component.FlexLayoutSection{
{Width: component.WidthFull, View: yamlView},
})
}
}
}
return flexLayout, nil
}
// YAMLToMarkdown convert the YAML source into markdown
func YAMLToMarkdown(text string) string {
listSeparator := "- "
b := strings.Builder{}
lines := strings.Split(strings.TrimSpace(text), "\n")
for _, line := range lines {
// find first non whitespace character
trimmed := strings.TrimSpace(line)
if trimmed == "" {
continue
}
idx := strings.Index(line, trimmed)
if idx < 0 {
idx = 0
}
// lets remove list indents
separator := "* "
remaining := line[idx:]
if strings.HasPrefix(remaining, listSeparator) {
remaining = strings.TrimPrefix(line[idx:], listSeparator)
separator = " " + separator
}
line = line[0:idx] + separator + remaining
b.WriteString(line)
b.WriteString("\n")
}
return b.String()
}