-
Notifications
You must be signed in to change notification settings - Fork 132
/
event_util.go
214 lines (206 loc) · 4.8 KB
/
event_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
package api
import (
"encoding/json"
"reflect"
"time"
"github.com/pkg/errors"
)
type Event interface {
GetJobId() string
GetJobSetId() string
GetQueue() string
GetCreated() time.Time
}
type KubernetesEvent interface {
Event
GetKubernetesId() string
GetClusterId() string
GetPodNumber() int32
GetPodName() string
GetPodNamespace() string
}
// customise oneof serialisation
func (message *EventMessage) MarshalJSON() ([]byte, error) {
return json.Marshal(message.Events)
}
func (message *EventMessage) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, message.Events)
}
func UnwrapEvent(message *EventMessage) (Event, error) {
switch event := message.Events.(type) {
case *EventMessage_Submitted:
return event.Submitted, nil
case *EventMessage_Queued:
return event.Queued, nil
case *EventMessage_DuplicateFound:
return event.DuplicateFound, nil
case *EventMessage_Leased:
return event.Leased, nil
case *EventMessage_LeaseReturned:
return event.LeaseReturned, nil
case *EventMessage_LeaseExpired:
return event.LeaseExpired, nil
case *EventMessage_Pending:
return event.Pending, nil
case *EventMessage_Running:
return event.Running, nil
case *EventMessage_UnableToSchedule:
return event.UnableToSchedule, nil
case *EventMessage_Failed:
return event.Failed, nil
case *EventMessage_Succeeded:
return event.Succeeded, nil
case *EventMessage_Reprioritizing:
return event.Reprioritizing, nil
case *EventMessage_Reprioritized:
return event.Reprioritized, nil
case *EventMessage_Cancelling:
return event.Cancelling, nil
case *EventMessage_Cancelled:
return event.Cancelled, nil
case *EventMessage_Terminated:
return event.Terminated, nil
case *EventMessage_Utilisation:
return event.Utilisation, nil
case *EventMessage_IngressInfo:
return event.IngressInfo, nil
case *EventMessage_Updated:
return event.Updated, nil
case *EventMessage_Preempting:
return event.Preempting, nil
case *EventMessage_Preempted:
return event.Preempted, nil
}
return nil, errors.Errorf("unknown event type: %s", reflect.TypeOf(message.Events))
}
func Wrap(event Event) (*EventMessage, error) {
switch typed := event.(type) {
case *JobSubmittedEvent:
return &EventMessage{
Events: &EventMessage_Submitted{
Submitted: typed,
},
}, nil
case *JobQueuedEvent:
return &EventMessage{
Events: &EventMessage_Queued{
Queued: typed,
},
}, nil
case *JobDuplicateFoundEvent:
return &EventMessage{
Events: &EventMessage_DuplicateFound{
DuplicateFound: typed,
},
}, nil
case *JobLeasedEvent:
return &EventMessage{
Events: &EventMessage_Leased{
Leased: typed,
},
}, nil
case *JobLeaseReturnedEvent:
return &EventMessage{
Events: &EventMessage_LeaseReturned{
LeaseReturned: typed,
},
}, nil
case *JobLeaseExpiredEvent:
return &EventMessage{
Events: &EventMessage_LeaseExpired{
LeaseExpired: typed,
},
}, nil
case *JobPendingEvent:
return &EventMessage{
Events: &EventMessage_Pending{
Pending: typed,
},
}, nil
case *JobRunningEvent:
return &EventMessage{
Events: &EventMessage_Running{
Running: typed,
},
}, nil
case *JobUnableToScheduleEvent:
return &EventMessage{
Events: &EventMessage_UnableToSchedule{
UnableToSchedule: typed,
},
}, nil
case *JobFailedEvent:
return &EventMessage{
Events: &EventMessage_Failed{
Failed: typed,
},
}, nil
case *JobSucceededEvent:
return &EventMessage{
Events: &EventMessage_Succeeded{
Succeeded: typed,
},
}, nil
case *JobReprioritizingEvent:
return &EventMessage{
Events: &EventMessage_Reprioritizing{
Reprioritizing: typed,
},
}, nil
case *JobReprioritizedEvent:
return &EventMessage{
Events: &EventMessage_Reprioritized{
Reprioritized: typed,
},
}, nil
case *JobCancellingEvent:
return &EventMessage{
Events: &EventMessage_Cancelling{
Cancelling: typed,
},
}, nil
case *JobCancelledEvent:
return &EventMessage{
Events: &EventMessage_Cancelled{
Cancelled: typed,
},
}, nil
case *JobTerminatedEvent:
return &EventMessage{
Events: &EventMessage_Terminated{
Terminated: typed,
},
}, nil
case *JobUtilisationEvent:
return &EventMessage{
Events: &EventMessage_Utilisation{
Utilisation: typed,
},
}, nil
case *JobIngressInfoEvent:
return &EventMessage{
Events: &EventMessage_IngressInfo{
IngressInfo: typed,
},
}, nil
case *JobUpdatedEvent:
return &EventMessage{
Events: &EventMessage_Updated{
Updated: typed,
},
}, nil
case *JobPreemptingEvent:
return &EventMessage{
Events: &EventMessage_Preempting{
Preempting: typed,
},
}, nil
case *JobPreemptedEvent:
return &EventMessage{
Events: &EventMessage_Preempted{
Preempted: typed,
},
}, nil
}
return nil, errors.Errorf("unknown event type: %s", reflect.TypeOf(event))
}