-
Notifications
You must be signed in to change notification settings - Fork 134
/
watch.go
64 lines (58 loc) · 2.18 KB
/
watch.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
package armadactl
import (
"encoding/json"
"fmt"
"reflect"
"time"
"github.com/armadaproject/armada/internal/common/armadacontext"
"github.com/armadaproject/armada/pkg/api"
"github.com/armadaproject/armada/pkg/client"
"github.com/armadaproject/armada/pkg/client/domain"
)
// Watch prints events associated with a particular job set.
func (a *App) Watch(queue string, jobSetId string, raw bool, exitOnInactive bool, forceNewEvents bool, forceLegacyEvents bool) error {
fmt.Fprintf(a.Out, "Watching job set %s\n", jobSetId)
return client.WithEventClient(a.Params.ApiConnectionDetails, func(c api.EventClient) error {
client.WatchJobSet(c, queue, jobSetId, true, true, forceNewEvents, forceLegacyEvents, armadacontext.Background(), func(state *domain.WatchContext, event api.Event) bool {
if raw {
data, err := json.Marshal(event)
if err != nil {
fmt.Fprintf(a.Out, "error parsing event %s: %s\n", event, err)
} else {
fmt.Fprintf(a.Out, "%s %s\n", reflect.TypeOf(event), string(data))
}
} else {
switch event2 := event.(type) {
case *api.JobUtilisationEvent:
// no print
case *api.JobFailedEvent:
a.printSummary(state, event)
fmt.Fprintf(a.Out, "Job failed: %s\n", event2.Reason)
jobInfo := state.GetJobInfo(event2.JobId)
if jobInfo != nil && jobInfo.ClusterId != "" && jobInfo.Job != nil {
fmt.Fprintf(
a.Out, "Found no logs for job; try '%s --tail=50\n",
client.GetKubectlCommand(jobInfo.ClusterId, jobInfo.Job.Namespace, event2.JobId, int(event2.PodNumber), "logs"),
)
}
default:
a.printSummary(state, event)
}
}
if exitOnInactive && state.GetNumberOfJobs() == state.GetNumberOfFinishedJobs() {
return true
}
return false
})
return nil
})
}
func (a *App) printSummary(state *domain.WatchContext, e api.Event) {
summary := fmt.Sprintf("%s | ", e.GetCreated().Format(time.Stamp))
summary += state.GetCurrentStateSummary()
summary += fmt.Sprintf(" | %s, job id: %s", reflect.TypeOf(e).String()[5:], e.GetJobId())
if kubernetesEvent, ok := e.(api.KubernetesEvent); ok {
summary += fmt.Sprintf(" pod: %d", kubernetesEvent.GetPodNumber())
}
fmt.Fprintf(a.Out, "%s\n", summary)
}