-
Notifications
You must be signed in to change notification settings - Fork 14
/
pipeline_steps_render.go
264 lines (235 loc) · 7.37 KB
/
pipeline_steps_render.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
package views
import (
"fmt"
"strings"
"github.com/jenkins-x/jx-logging/pkg/log"
v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/octant-jx/pkg/common/viewhelpers"
"github.com/jenkins-x/octant-jx/pkg/plugin"
"github.com/vmware-tanzu/octant/pkg/view/component"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
const (
indentation = " "
)
type PipelineStepRenderer struct {
Writer strings.Builder
PipelineActivity *v1.PipelineActivity
Pod *unstructured.Unstructured
}
// ToStepsView renders a markdown description of the pipeline
func ToStepsView(pa *v1.PipelineActivity, pod *unstructured.Unstructured) *component.Text {
r := &PipelineStepRenderer{
PipelineActivity: pa,
Pod: pod,
}
w := &r.Writer
if pa != nil {
steps := pa.Spec.Steps
for k := range steps {
if addStepRow(r, &steps[k], "") {
break
}
}
}
text := w.String()
return component.NewMarkdownText(text)
}
func addStepRow(w *PipelineStepRenderer, parent *v1.PipelineActivityStep, indent string) bool {
stage := parent.Stage
preview := parent.Preview
promote := parent.Promote
pending := false
if stage != nil {
if addStageRow(w, stage, indent) {
pending = true
}
} else if preview != nil {
if addPreviewRow(w, stage, preview, indent) {
pending = true
}
} else if promote != nil {
if addPromoteRow(w, stage, promote, indent) {
pending = true
}
} else {
log.Logger().Infof("Unknown step kind %#v", parent)
}
return pending
}
func addStageRow(w *PipelineStepRenderer, stage *v1.StageActivityStep, indent string) bool {
name := "Stage"
if stage.Name != "" {
name = ""
}
pending := addStepRowItem(w, stage, &stage.CoreActivityStep, indent, name, "")
indent += indentation
steps := stage.Steps
for k := range steps {
if addStepRowItem(w, stage, &steps[k], indent, "", "") {
pending = true
break
}
}
return pending
}
func addPreviewRow(w *PipelineStepRenderer, stage *v1.StageActivityStep, parent *v1.PreviewActivityStep, indent string) bool {
pullRequestURL := parent.PullRequestURL
if pullRequestURL == "" {
pullRequestURL = parent.Environment
}
pending := addStepRowItem(w, stage, &parent.CoreActivityStep, indent, "Preview", colorInfo(pullRequestURL))
indent += indentation
appURL := parent.ApplicationURL
if appURL != "" {
if addStepRowItem(w, stage, &parent.CoreActivityStep, indent, "Preview Application", colorInfo(appURL)) {
pending = true
}
}
return pending
}
func addPromoteRow(w *PipelineStepRenderer, stage *v1.StageActivityStep, parent *v1.PromoteActivityStep, indent string) bool {
pending := addStepRowItem(w, stage, &parent.CoreActivityStep, indent, "Promote: "+parent.Environment, "")
indent += indentation
pullRequest := parent.PullRequest
update := parent.Update
if pullRequest != nil {
if addStepRowItem(w, stage, &pullRequest.CoreActivityStep, indent, "PullRequest", describePromotePullRequest(pullRequest)) {
pending = true
}
}
if update != nil {
if addStepRowItem(w, stage, &update.CoreActivityStep, indent, "Update", describePromoteUpdate(update)) {
pending = true
}
}
appURL := parent.ApplicationURL
if appURL != "" {
if addStepRowItem(w, stage, &update.CoreActivityStep, indent, "Promoted", " Application is at: "+colorInfo(appURL)) {
pending = true
}
}
return pending
}
func addStepRowItem(w *PipelineStepRenderer, stage *v1.StageActivityStep, step *v1.CoreActivityStep, indent, name, description string) bool {
textName := step.Name
if textName == "" {
textName = name
} else if name != "" {
textName = name + ":" + textName
}
icon := ToPipelineStatusMarkup(step.Status)
status := ""
durationText := durationMarkup(step.StartedTimestamp, step.CompletedTimestamp)
if durationText != "" {
status = " : " + durationText
}
podName := ""
if w.Pod != nil {
podName = w.Pod.GetName()
}
containerName := FindContainerName(step, w.Pod)
if containerName != "" {
paName := w.PipelineActivity.Name
ns := w.PipelineActivity.Namespace
if podName != "" {
status += fmt.Sprintf(` <a href="%s" title="View Step details"><clr-icon shape="details"></clr-icon></a>`, plugin.GetPipelineContainerLink(ns, paName, podName, containerName))
}
w.Writer.WriteString(fmt.Sprintf("%s* %s [%s](%s) %s\n", indent, icon, textName, plugin.GetPipelineContainerLogLink(paName, containerName), status))
} else {
log.Logger().Infof("failed to find container name for step %s and pod %s", step.Name, podName)
w.Writer.WriteString(fmt.Sprintf("%s* %s %s %s\n", indent, icon, textName, status))
}
return step.Status == v1.ActivityStatusTypePending
}
func durationMarkup(start, end *metav1.Time) string {
if start == nil || end == nil {
return ""
}
return end.Sub(start.Time).String()
}
func FindContainerName(step *v1.CoreActivityStep, u *unstructured.Unstructured) string {
if u != nil && step != nil {
pod, err := viewhelpers.ToPod(u)
if err != nil {
log.Logger().Info(fmt.Sprintf("failed to convert to Pod: %s", err.Error()))
return ""
}
names := []string{step.Name}
return FindContainerNameForStepName(pod, names)
}
return ""
}
func FindContainerNameForStepName(pod *corev1.Pod, pipelineActivityStepNames []string) string {
name := "step-" + strings.ToLower(strings.Join(pipelineActivityStepNames, "-"))
name = strings.ReplaceAll(name, " ", "-")
name2 := "step-" + name
containers := pod.Spec.Containers
for k := range containers {
c := containers[k]
if c.Name == name || c.Name == name2 {
return c.Name
}
}
return ""
}
func ToPipelineStatus(pa *v1.PipelineActivity) component.Component {
if pa == nil || pa.Spec.Status == v1.ActivityStatusTypeNone {
return component.NewText("")
}
return component.NewMarkdownText(ToPipelineStatusMarkup(pa.Spec.Status))
}
func ToPipelineStatusMarkup(statusType v1.ActivityStatusType) string {
text := statusType.String()
switch statusType {
case v1.ActivityStatusTypeFailed, v1.ActivityStatusTypeError:
return `<clr-icon shape="warning-standard" class="is-solid is-danger" title="Failed"></clr-icon>`
case v1.ActivityStatusTypeSucceeded:
return `<clr-icon shape="check-circle" class="is-solid is-success" title="Succeeded"></clr-icon>`
case v1.ActivityStatusTypePending:
return `<clr-icon shape="clock" title="Pending"></clr-icon>`
case v1.ActivityStatusTypeRunning:
return `<span class="spinner spinner-inline" title="Running"></span>`
}
return text
}
func describePromotePullRequest(promote *v1.PromotePullRequestStep) string {
description := ""
if promote.PullRequestURL != "" {
description += " PullRequest: " + colorInfo(promote.PullRequestURL)
}
if promote.MergeCommitSHA != "" {
description += " Merge SHA: " + colorInfo(promote.MergeCommitSHA)
}
return description
}
func describePromoteUpdate(promote *v1.PromoteUpdateStep) string {
description := ""
for _, status := range promote.Statuses {
url := status.URL
state := status.Status
if url != "" && state != "" {
description += " Status: " + pullRequestStatusString(state) + " at: " + colorInfo(url)
}
}
return description
}
func colorInfo(text string) string {
return text
}
func colorError(text string) string {
return text
}
func pullRequestStatusString(text string) string {
title := strings.Title(text)
switch text {
case "success":
return colorInfo(title)
case "error", "failed":
return colorError(title)
default:
return text
}
}