-
Notifications
You must be signed in to change notification settings - Fork 134
/
events_util.go
423 lines (384 loc) · 11.6 KB
/
events_util.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package armadaevents
import (
"encoding/json"
"errors"
time "time"
"github.com/armadaproject/armada/pkg/api"
)
type rawES_Event struct {
Created *time.Time `json:"created"`
EventBytes json.RawMessage `json:"Event"`
}
type rawKubernetesMainObject struct {
ObjectMeta *ObjectMeta `json:"objectMeta,omitempty"`
ObjectBytes json.RawMessage
}
type rawKubernetesObject struct {
ObjectMeta *ObjectMeta `json:"objectMeta,omitempty"`
ObjectBytes json.RawMessage
}
type rawKubernetesResourceInfo struct {
ObjectMeta *ObjectMeta `json:"objectMeta,omitempty"`
InfoBytes json.RawMessage
}
func (ev *EventSequence_Event) UnmarshalJSON(data []byte) error {
if string(data) == "null" || string(data) == `""` {
return nil
}
var rawEvent rawES_Event
err := json.Unmarshal(data, &rawEvent)
if err != nil {
return err
}
ev.Created = rawEvent.Created
// Unmarshal into a map, which is temporarily used only to get the marshalled
// key that signifies the actual struct underlying this instance of the
// isEventSequence_Event_Event interface.
var mapEvent map[string]interface{}
if err = json.Unmarshal(rawEvent.EventBytes, &mapEvent); err != nil {
return err
}
for k := range mapEvent {
switch k {
case "submitJob":
var submitJob EventSequence_Event_SubmitJob
if err = json.Unmarshal(rawEvent.EventBytes, &submitJob); err != nil {
return err
}
ev.Event = &submitJob
case "reprioritiseJob":
var reprioritiseJob EventSequence_Event_ReprioritiseJob
if err = json.Unmarshal(rawEvent.EventBytes, &reprioritiseJob); err != nil {
return err
}
ev.Event = &reprioritiseJob
case "reprioritiseJobSet":
var reprioritiseJobSet EventSequence_Event_ReprioritiseJobSet
if err = json.Unmarshal(rawEvent.EventBytes, &reprioritiseJobSet); err != nil {
return err
}
ev.Event = &reprioritiseJobSet
case "reprioritisedJob":
var reprioritisedJob EventSequence_Event_ReprioritisedJob
if err = json.Unmarshal(rawEvent.EventBytes, &reprioritisedJob); err != nil {
return err
}
ev.Event = &reprioritisedJob
case "cancelJob":
var cancelJob EventSequence_Event_CancelJob
if err = json.Unmarshal(rawEvent.EventBytes, &cancelJob); err != nil {
return err
}
ev.Event = &cancelJob
case "cancelJobSet":
var cancelJobSet EventSequence_Event_CancelJobSet
if err = json.Unmarshal(rawEvent.EventBytes, &cancelJobSet); err != nil {
return err
}
ev.Event = &cancelJobSet
case "cancelledJob":
var cancelledJob EventSequence_Event_CancelledJob
if err = json.Unmarshal(rawEvent.EventBytes, &cancelledJob); err != nil {
return err
}
ev.Event = &cancelledJob
case "jobSucceeded":
var jobSucceeded EventSequence_Event_JobSucceeded
if err = json.Unmarshal(rawEvent.EventBytes, &jobSucceeded); err != nil {
return err
}
ev.Event = &jobSucceeded
case "jobErrors":
var jobErrors EventSequence_Event_JobErrors
if err = json.Unmarshal(rawEvent.EventBytes, &jobErrors); err != nil {
return err
}
ev.Event = &jobErrors
case "jobRequeued":
var jobRequeued EventSequence_Event_JobRequeued
if err = json.Unmarshal(rawEvent.EventBytes, &jobRequeued); err != nil {
return err
}
ev.Event = &jobRequeued
case "jobRunLeased":
var jobRunLeased EventSequence_Event_JobRunLeased
if err = json.Unmarshal(rawEvent.EventBytes, &jobRunLeased); err != nil {
return err
}
ev.Event = &jobRunLeased
case "jobRunAssigned":
var jobRunAssigned EventSequence_Event_JobRunAssigned
if err = json.Unmarshal(rawEvent.EventBytes, &jobRunAssigned); err != nil {
return err
}
ev.Event = &jobRunAssigned
case "jobRunRunning":
var jobRunRunning EventSequence_Event_JobRunRunning
if err = json.Unmarshal(rawEvent.EventBytes, &jobRunRunning); err != nil {
return err
}
ev.Event = &jobRunRunning
case "jobRunSucceeded":
var jobRunSucceeded EventSequence_Event_JobRunSucceeded
if err = json.Unmarshal(rawEvent.EventBytes, &jobRunSucceeded); err != nil {
return err
}
ev.Event = &jobRunSucceeded
case "jobRunErrors":
var jobRunErrors EventSequence_Event_JobRunErrors
if err = json.Unmarshal(rawEvent.EventBytes, &jobRunErrors); err != nil {
return err
}
ev.Event = &jobRunErrors
case "jobDuplicateDetected":
var jobDuplicateDetected EventSequence_Event_JobDuplicateDetected
if err = json.Unmarshal(rawEvent.EventBytes, &jobDuplicateDetected); err != nil {
return err
}
ev.Event = &jobDuplicateDetected
case "standaloneIngressInfo":
var standaloneIngressInfo EventSequence_Event_StandaloneIngressInfo
if err = json.Unmarshal(rawEvent.EventBytes, &standaloneIngressInfo); err != nil {
return err
}
ev.Event = &standaloneIngressInfo
case "resourceUtilisation":
var resourceUtilisation EventSequence_Event_ResourceUtilisation
if err = json.Unmarshal(rawEvent.EventBytes, &resourceUtilisation); err != nil {
return err
}
ev.Event = &resourceUtilisation
case "jobRunPreempted":
var jobRunPreempted EventSequence_Event_JobRunPreempted
if err = json.Unmarshal(rawEvent.EventBytes, &jobRunPreempted); err != nil {
return err
}
ev.Event = &jobRunPreempted
default:
return errors.New("could not determine EventSequence_Event.Event type for unmarshaling")
}
}
return nil
}
func (kmo *KubernetesMainObject) UnmarshalJSON(data []byte) error {
if string(data) == "null" || string(data) == `""` {
return nil
}
var rawKmo rawKubernetesMainObject
err := json.Unmarshal(data, &rawKmo)
if err != nil {
return err
}
kmo.ObjectMeta = rawKmo.ObjectMeta
if rawKmo.ObjectBytes == nil {
return nil
}
var mapObj map[string]interface{}
if err = json.Unmarshal(rawKmo.ObjectBytes, &mapObj); err != nil {
return err
}
for k := range mapObj {
switch k {
case "pod_spec":
var ps KubernetesMainObject_PodSpec
if err = json.Unmarshal(rawKmo.ObjectBytes, &ps); err != nil {
return err
}
kmo.Object = &ps
default:
return errors.New("could not determine isKubernetesMainObject_Object type for unmarshaling")
}
}
return nil
}
func (ko *KubernetesObject) UnmarshalJSON(data []byte) error {
if string(data) == "null" || string(data) == `""` {
return nil
}
var rawKo rawKubernetesObject
err := json.Unmarshal(data, &rawKo)
if err != nil {
return err
}
ko.ObjectMeta = rawKo.ObjectMeta
if rawKo.ObjectBytes == nil {
return nil
}
var mapObj map[string]interface{}
if err = json.Unmarshal(rawKo.ObjectBytes, &mapObj); err != nil {
return err
}
for k := range mapObj {
switch k {
case "pod_spec":
var ps KubernetesObject_PodSpec
if err = json.Unmarshal(rawKo.ObjectBytes, &ps); err != nil {
return err
}
ko.Object = &ps
case "ingress":
var ing KubernetesObject_Ingress
if err = json.Unmarshal(rawKo.ObjectBytes, &ing); err != nil {
return err
}
ko.Object = &ing
case "service":
var svc KubernetesObject_Service
if err = json.Unmarshal(rawKo.ObjectBytes, &svc); err != nil {
return err
}
ko.Object = &svc
case "configMap":
var cm KubernetesObject_ConfigMap
if err = json.Unmarshal(rawKo.ObjectBytes, &cm); err != nil {
return err
}
ko.Object = &cm
default:
return errors.New("could not determine isKubernetesObject_Object type for unmarshaling")
}
}
return nil
}
func (kri *KubernetesResourceInfo) UnmarshalJSON(data []byte) error {
if string(data) == "null" || string(data) == `""` {
return nil
}
var rawKri rawKubernetesResourceInfo
err := json.Unmarshal(data, &rawKri)
if err != nil {
return err
}
kri.ObjectMeta = rawKri.ObjectMeta
var mapObj map[string]interface{}
if err = json.Unmarshal(rawKri.InfoBytes, &mapObj); err != nil {
return err
}
for k := range mapObj {
switch k {
case "podInfo":
var pi KubernetesResourceInfo_PodInfo
if err = json.Unmarshal(rawKri.InfoBytes, &pi); err != nil {
return err
}
kri.Info = &pi
case "ingressInfo":
var ing KubernetesResourceInfo_IngressInfo
if err = json.Unmarshal(rawKri.InfoBytes, &ing); err != nil {
return err
}
kri.Info = &ing
default:
return errors.New("could not determine isKubernetesResourceInfo_Info type for unmarshaling")
}
}
return nil
}
// ExpectedSequenceFromJobRequestItem returns the expected event sequence for a particular job request and response.
func ExpectedSequenceFromRequestItem(armadaQueueName, armadaUserId, userNamespace, jobSetName string,
jobId *Uuid, reqi *api.JobSubmitRequestItem,
) *EventSequence {
// Any objects created for the job in addition to the main object.
// We only check that the correct number of objects of each type is created.
// Later, we may wish to also check the fields of the objects.
objects := make([]*KubernetesObject, 0)
// Set of ports associated with a service in the submitted job.
// Because Armada automatically creates services for ingresses with no corresponding service,
// we need this to create the correct number of services.
servicePorts := make(map[uint32]bool)
// One object per service + compute servicePorts
if reqi.Services != nil {
for _, service := range reqi.Services {
objects = append(objects, &KubernetesObject{Object: &KubernetesObject_Service{}})
for _, port := range service.Ports {
servicePorts[port] = true
}
}
}
// Services and ingresses created for the job.
if reqi.Ingress != nil {
for _, ingress := range reqi.Ingress {
objects = append(objects, &KubernetesObject{Object: &KubernetesObject_Ingress{}})
// Armada automatically creates services as needed by ingresses
// (each ingress needs to point to a service).
for _, port := range ingress.Ports {
if _, ok := servicePorts[port]; !ok {
objects = append(objects, &KubernetesObject{Object: &KubernetesObject_Service{}})
}
}
}
}
// Count the total number of PodSpecs in the job and add one less than that to the additional objects
// (since one PodSpec is placed into the main object).
numPodSpecs := 0
if reqi.PodSpec != nil {
numPodSpecs++
}
if reqi.PodSpecs != nil {
numPodSpecs += len(reqi.PodSpecs)
}
for i := 0; i < numPodSpecs-1; i++ {
objects = append(objects, &KubernetesObject{Object: &KubernetesObject_PodSpec{
// The submit server should add some defaults to the submitted podspec.
PodSpec: &PodSpecWithAvoidList{},
}})
}
return &EventSequence{
Queue: armadaQueueName,
JobSetName: jobSetName,
UserId: armadaUserId,
Events: []*EventSequence_Event{
{Event: &EventSequence_Event_SubmitJob{
SubmitJob: &SubmitJob{
JobId: jobId,
DeduplicationId: reqi.ClientId,
Priority: uint32(reqi.Priority),
ObjectMeta: &ObjectMeta{
Namespace: userNamespace,
Name: "",
KubernetesId: "",
Annotations: nil,
Labels: nil,
},
MainObject: &KubernetesMainObject{Object: &KubernetesMainObject_PodSpec{}},
Objects: objects,
Lifetime: 0,
AtMostOnce: false,
Preemptible: false,
ConcurrencySafe: false,
},
}},
{Event: &EventSequence_Event_JobRunLeased{
JobRunLeased: &JobRunLeased{
RunId: nil,
JobId: jobId,
ExecutorId: "",
},
}},
{Event: &EventSequence_Event_JobRunAssigned{
JobRunAssigned: &JobRunAssigned{
RunId: nil,
JobId: jobId,
},
}},
{Event: &EventSequence_Event_JobRunRunning{
JobRunRunning: &JobRunRunning{
RunId: nil,
JobId: jobId,
ResourceInfos: nil,
},
}},
{Event: &EventSequence_Event_JobRunSucceeded{
JobRunSucceeded: &JobRunSucceeded{
RunId: nil,
JobId: jobId,
},
}},
{Event: &EventSequence_Event_JobSucceeded{
JobSucceeded: &JobSucceeded{
JobId: jobId,
},
}},
},
}
}