-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_activity.go
370 lines (333 loc) · 9.55 KB
/
get_activity.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
package cmd
import (
"io"
"strings"
"time"
"github.com/ghodss/yaml"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/client/clientset/versioned"
tbl "github.com/jenkins-x/jx/pkg/jx/cmd/table"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/tools/cache"
)
const (
indentation = " "
)
// GetActivityOptions containers the CLI options
type GetActivityOptions struct {
CommonOptions
Filter string
BuildNumber string
Watch bool
}
var (
get_activity_long = templates.LongDesc(`
Display the current activities for one more more projects.
`)
get_activity_example = templates.Examples(`
# List the current activities for all applications in the current team
jx get activities
# List the current activities for application 'foo'
jx get act -f foo
# Watch the activities for application 'foo'
jx get act -f foo -w
`)
)
// NewCmdGetActivity creates the new command for: jx get version
func NewCmdGetActivity(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &GetActivityOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "activities",
Short: "Display one or many Activities on projects",
Aliases: []string{"activity", "act"},
Long: get_activity_long,
Example: get_activity_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
cmdutil.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Filter, "filter", "f", "", "Text to filter the pipeline names")
cmd.Flags().StringVarP(&options.BuildNumber, "build", "b", "", "The build number to filter on")
cmd.Flags().BoolVarP(&options.Watch, "watch", "w", false, "Whether to watch the activities for changes")
return cmd
}
// Run implements this command
func (o *GetActivityOptions) Run() error {
f := o.Factory
client, currentNs, err := f.CreateJXClient()
if err != nil {
return err
}
kubeClient, _, err := o.KubeClient()
if err != nil {
return err
}
ns, _, err := kube.GetDevNamespace(kubeClient, currentNs)
if err != nil {
return err
}
envList, err := client.JenkinsV1().Environments(ns).List(metav1.ListOptions{})
if err != nil {
return err
}
kube.SortEnvironments(envList.Items)
apisClient, err := f.CreateApiExtensionsClient()
if err != nil {
return err
}
err = kube.RegisterPipelineActivityCRD(apisClient)
if err != nil {
return err
}
table := o.CreateTable()
table.SetColumnAlign(1, util.ALIGN_RIGHT)
table.SetColumnAlign(2, util.ALIGN_RIGHT)
table.AddRow("STEP", "STARTED AGO", "DURATION", "STATUS")
if o.Watch {
return o.WatchActivities(&table, client, ns)
}
list, err := client.JenkinsV1().PipelineActivities(ns).List(metav1.ListOptions{})
if err != nil {
return err
}
for _, activity := range list.Items {
o.addTableRow(&table, &activity)
}
table.Render()
return nil
}
func (o *GetActivityOptions) addTableRow(table *tbl.Table, activity *v1.PipelineActivity) bool {
if o.matches(activity) {
spec := &activity.Spec
text := ""
version := activity.Spec.Version
if version != "" {
text = "Version: " + util.ColorInfo(version)
}
statusText := statusString(activity.Spec.Status)
if statusText == "" {
statusText = text
} else {
statusText += " " + text
}
table.AddRow(spec.Pipeline+" #"+spec.Build,
timeToString(spec.StartedTimestamp),
durationString(spec.StartedTimestamp, spec.CompletedTimestamp),
statusText)
indent := indentation
for _, step := range spec.Steps {
o.addStepRow(table, &step, indent)
}
return true
}
return false
}
func (o *GetActivityOptions) WatchActivities(table *tbl.Table, jxClient versioned.Interface, ns string) error {
yamlSpecMap := map[string]string{}
activity := &v1.PipelineActivity{}
listWatch := cache.NewListWatchFromClient(jxClient.JenkinsV1().RESTClient(), "pipelineactivities", ns, fields.Everything())
kube.SortListWatchByName(listWatch)
_, controller := cache.NewInformer(
listWatch,
activity,
time.Minute*10,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
o.onActivity(table, obj, yamlSpecMap)
},
UpdateFunc: func(oldObj, newObj interface{}) {
o.onActivity(table, newObj, yamlSpecMap)
},
DeleteFunc: func(obj interface{}) {
},
},
)
stop := make(chan struct{})
go controller.Run(stop)
// Wait forever
select {}
}
func (o *GetActivityOptions) onActivity(table *tbl.Table, obj interface{}, yamlSpecMap map[string]string) {
activity, ok := obj.(*v1.PipelineActivity)
if !ok {
log.Infof("Object is not a PipelineActivity %#v\n", obj)
return
}
data, err := yaml.Marshal(&activity.Spec)
if err != nil {
log.Infof("Failed to marshal Activity.Spec to YAML: %s", err)
} else {
text := string(data)
name := activity.Name
old := yamlSpecMap[name]
if old == "" || old != text {
yamlSpecMap[name] = text
if o.addTableRow(table, activity) {
table.Render()
table.Clear()
}
}
}
}
func (o *CommonOptions) addStepRow(table *tbl.Table, parent *v1.PipelineActivityStep, indent string) {
stage := parent.Stage
preview := parent.Preview
promote := parent.Promote
if stage != nil {
addStageRow(table, stage, indent)
} else if preview != nil {
addPreviewRow(table, preview, indent)
} else if promote != nil {
addPromoteRow(table, promote, indent)
} else {
log.Warnf("Unknown step kind %#v\n", parent)
}
}
func addStageRow(table *tbl.Table, stage *v1.StageActivityStep, indent string) {
name := "Stage"
if stage.Name != "" {
name = ""
}
addStepRowItem(table, &stage.CoreActivityStep, indent, name, "")
indent += indentation
for _, step := range stage.Steps {
addStepRowItem(table, &step, indent, "", "")
}
}
func addPreviewRow(table *tbl.Table, parent *v1.PreviewActivityStep, indent string) {
pullRequestURL := parent.PullRequestURL
if pullRequestURL == "" {
pullRequestURL = parent.Environment
}
addStepRowItem(table, &parent.CoreActivityStep, indent, "Preview", util.ColorInfo(pullRequestURL))
indent += indentation
appURL := parent.ApplicationURL
if appURL != "" {
addStepRowItem(table, &parent.CoreActivityStep, indent, "Preview Application", util.ColorInfo(appURL))
}
}
func addPromoteRow(table *tbl.Table, parent *v1.PromoteActivityStep, indent string) {
addStepRowItem(table, &parent.CoreActivityStep, indent, "Promote: "+parent.Environment, "")
indent += indentation
pullRequest := parent.PullRequest
update := parent.Update
if pullRequest != nil {
addStepRowItem(table, &pullRequest.CoreActivityStep, indent, "PullRequest", describePromotePullRequest(pullRequest))
}
if update != nil {
addStepRowItem(table, &update.CoreActivityStep, indent, "Update", describePromoteUpdate(update))
}
appURL := parent.ApplicationURL
if appURL != "" {
addStepRowItem(table, &update.CoreActivityStep, indent, "Promoted", " Application is at: "+util.ColorInfo(appURL))
}
}
func addStepRowItem(table *tbl.Table, step *v1.CoreActivityStep, indent string, name string, description string) {
text := step.Description
if description != "" {
if text == "" {
text = description
} else {
text += description
}
}
textName := step.Name
if textName == "" {
textName = name
} else {
if name != "" {
textName = name + ":" + textName
}
}
table.AddRow(indent+textName,
timeToString(step.StartedTimestamp),
durationString(step.StartedTimestamp, step.CompletedTimestamp),
statusString(step.Status)+text)
}
func statusString(statusType v1.ActivityStatusType) string {
text := statusType.String()
switch statusType {
case v1.ActivityStatusTypeFailed, v1.ActivityStatusTypeError:
return util.ColorError(text)
case v1.ActivityStatusTypeSucceeded:
return util.ColorInfo(text)
case v1.ActivityStatusTypeRunning:
return util.ColorStatus(text)
}
return text
}
func describePromotePullRequest(promote *v1.PromotePullRequestStep) string {
description := ""
if promote.PullRequestURL != "" {
description += " PullRequest: " + util.ColorInfo(promote.PullRequestURL)
}
if promote.MergeCommitSHA != "" {
description += " Merge SHA: " + util.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: " + util.ColorInfo(url)
}
}
return description
}
func pullRequestStatusString(text string) string {
title := strings.Title(text)
switch text {
case "success":
return util.ColorInfo(title)
case "error", "failed":
return util.ColorError(title)
default:
return util.ColorStatus(title)
}
}
func durationString(start *metav1.Time, end *metav1.Time) string {
if start == nil || end == nil {
return ""
}
return end.Sub(start.Time).Round(time.Second).String()
}
func timeToString(t *metav1.Time) string {
if t == nil {
return ""
}
now := &metav1.Time{
Time: time.Now(),
}
return durationString(t, now)
}
func (o *GetActivityOptions) matches(activity *v1.PipelineActivity) bool {
answer := true
filter := o.Filter
if filter != "" {
answer = strings.Contains(activity.Name, filter) || strings.Contains(activity.Spec.Pipeline, filter)
}
build := o.BuildNumber
if answer && build != "" {
answer = activity.Spec.Build == build
}
return answer
}