This repository has been archived by the owner on Dec 3, 2021. It is now read-only.
/
requests.go
379 lines (317 loc) 路 11.5 KB
/
requests.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
package scheduler
import (
"encoding/json"
"errors"
"fmt"
"sync"
"time"
ot "github.com/opentracing/opentracing-go"
ext "github.com/opentracing/opentracing-go/ext"
log "github.com/opentracing/opentracing-go/log"
corev1 "k8s.io/api/core/v1"
models "github.com/nre-learning/antidote-core/db/models"
"github.com/nre-learning/antidote-core/services"
)
func (s *AntidoteScheduler) handleRequestCREATE(sc ot.SpanContext, newRequest services.LessonScheduleRequest) {
span := ot.StartSpan("scheduler_lsr_create", ot.ChildOf(sc))
defer span.Finish()
nsName := generateNamespaceName(s.Config.InstanceID, newRequest.LiveLessonID)
span.LogEvent(fmt.Sprintf("Generated namespace name %s", nsName))
ll, err := s.Db.GetLiveLesson(span.Context(), newRequest.LiveLessonID)
if err != nil {
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
return
}
err = s.createK8sStuff(span.Context(), newRequest)
if err != nil {
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
_ = s.Db.UpdateLiveLessonError(span.Context(), ll.ID, true)
return
}
err = s.waitUntilReachable(span.Context(), ll)
if err != nil {
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
_ = s.Db.UpdateLiveLessonError(span.Context(), ll.ID, true)
return
}
err = s.Db.UpdateLiveLessonStatus(span.Context(), ll.ID, models.Status_CONFIGURATION)
if err != nil {
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
_ = s.Db.UpdateLiveLessonError(span.Context(), ll.ID, true)
return
}
err = s.configureStuff(span.Context(), nsName, ll, newRequest)
if err != nil {
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
_ = s.Db.UpdateLiveLessonError(span.Context(), ll.ID, true)
return
}
lesson, err := s.Db.GetLesson(span.Context(), newRequest.LessonSlug)
if err != nil {
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
return
}
span.LogEvent(fmt.Sprintf("Inserting ready delay of %d seconds", lesson.ReadyDelay))
if lesson.ReadyDelay > 0 {
time.Sleep(time.Duration(lesson.ReadyDelay) * time.Second)
}
_ = s.Db.UpdateLiveLessonStatus(span.Context(), ll.ID, models.Status_READY)
// Inject span context and send LSR into NATS
tracer := ot.GlobalTracer()
var t services.TraceMsg
if err := tracer.Inject(span.Context(), ot.Binary, &t); err != nil {
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
}
reqBytes, _ := json.Marshal(newRequest)
t.Write(reqBytes)
s.NC.Publish(services.LsrCompleted, t.Bytes())
}
func (s *AntidoteScheduler) handleRequestMODIFY(sc ot.SpanContext, newRequest services.LessonScheduleRequest) {
span := ot.StartSpan("scheduler_lsr_modify", ot.ChildOf(sc))
defer span.Finish()
nsName := generateNamespaceName(s.Config.InstanceID, newRequest.LiveLessonID)
ll, err := s.Db.GetLiveLesson(span.Context(), newRequest.LiveLessonID)
if err != nil {
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
return
}
err = s.configureStuff(span.Context(), nsName, ll, newRequest)
if err != nil {
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
_ = s.Db.UpdateLiveLessonError(span.Context(), ll.ID, true)
return
}
err = s.Db.UpdateLiveLessonStatus(span.Context(), ll.ID, models.Status_READY)
if err != nil {
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
}
err = s.boopNamespace(span.Context(), nsName)
if err != nil {
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
}
}
func (s *AntidoteScheduler) handleRequestBOOP(sc ot.SpanContext, newRequest services.LessonScheduleRequest) {
span := ot.StartSpan("scheduler_lsr_boop", ot.ChildOf(sc))
defer span.Finish()
nsName := generateNamespaceName(s.Config.InstanceID, newRequest.LiveLessonID)
err := s.boopNamespace(span.Context(), nsName)
if err != nil {
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
}
}
// handleRequestDELETE handles a livelesson deletion request by first sending a delete request
// for the corresponding namespace, and then cleaning up local state.
func (s *AntidoteScheduler) handleRequestDELETE(sc ot.SpanContext, newRequest services.LessonScheduleRequest) {
span := ot.StartSpan("scheduler_lsr_delete", ot.ChildOf(sc))
defer span.Finish()
nsName := generateNamespaceName(s.Config.InstanceID, newRequest.LiveLessonID)
err := s.deleteNamespace(span.Context(), nsName)
if err != nil {
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
}
// Make sure to not return earlier than this, so we make sure the state is cleaned up
// no matter what
_ = s.Db.DeleteLiveLesson(span.Context(), newRequest.LiveLessonID)
}
// createK8sStuff is a high-level workflow for creating all of the things necessary for a new instance
// of a livelesson. Pods, services, networks, networkpolicies, ingresses, etc to support a new running
// lesson are all created as part of this workflow.
func (s *AntidoteScheduler) createK8sStuff(sc ot.SpanContext, req services.LessonScheduleRequest) error {
span := ot.StartSpan("scheduler_k8s_create_stuff", ot.ChildOf(sc))
defer span.Finish()
ns, err := s.createNamespace(span.Context(), req)
if err != nil {
log.Error(err)
}
// Set network policy ONLY after configuration has had a chance to take place. Once this is in place,
// only config pods spawned by Jobs will have internet access, so if this takes place earlier, lessons
// won't initially come up at all.
if !s.Config.AllowEgress {
s.createNetworkPolicy(span.Context(), ns.Name)
}
// Sync TLS certificate into the lesson namespace (and optionally, docker pull credentials)
_ = s.syncSecret(span.Context(), s.Config.SecretsNamespace, ns.ObjectMeta.Name, s.Config.TLSCertName)
if s.Config.PullCredName != "" {
_ = s.syncSecret(span.Context(), s.Config.SecretsNamespace, ns.ObjectMeta.Name, s.Config.PullCredName)
}
lesson, err := s.Db.GetLesson(span.Context(), req.LessonSlug)
if err != nil {
return err
}
ll, err := s.Db.GetLiveLesson(span.Context(), req.LiveLessonID)
if err != nil {
return err
}
// The LiveEndpoints field of the LiveLesson model is a map, not a slice. This means that the original order from the lesson definition
// is lost when the API's initializeLiveEndpoints function populates this from the original slice into the unordered LiveLesson map.
// Unfortunately this map is pretty well embedded in the API at this point, and would be a lot of work on antidote-core and antidote-web
// to convert this to an ordered data structure like a slice.
//
// In most cases, this order doesn't really matter. This code however, is a huge exception - the order in which endpoint pods are created
// is extremely important, as this is what determines which pods get which IP addresses from the specified subnet.
//
// Because this is the only place I'm currently aware of that this order matters, I've inserted this simple logic to create a slice of strings that
// represent the original order in which these endpoints appeared, and the subsequent loop can use this to create pods in the original order from
// the lesson definition. HOWEVER, if we discover that other use cases exist that require consistent ordering, we should tackle the conversion
// of this field to a slice, and remove this workaround.
epOrdered := []string{}
for _, lep := range lesson.Endpoints {
epOrdered = append(epOrdered, lep.Name)
}
// Append endpoint and create ingress for jupyter lab guide if necessary
if usesJupyterLabGuide(lesson) {
jupyterEp := &models.LiveEndpoint{
Name: "jupyterlabguide",
Image: fmt.Sprintf("antidotelabs/jupyter:%s", s.BuildInfo["imageVersion"]),
Ports: []int32{8888},
}
// Add to the endpoints map as well as the ordered list, so the loop below picks it up at the end.
ll.LiveEndpoints[jupyterEp.Name] = jupyterEp
epOrdered = append(epOrdered, jupyterEp.Name)
nsName := generateNamespaceName(s.Config.InstanceID, req.LiveLessonID)
_, err := s.createIngress(
span.Context(),
ns.ObjectMeta.Name,
jupyterEp,
&models.LivePresentation{
Name: "web",
Port: 8888,
HepDomain: fmt.Sprintf("%s-jupyterlabguide-web.%s", nsName, s.Config.HEPSDomain),
},
)
if err != nil {
return fmt.Errorf("Unable to create ingress resource - %v", err)
}
}
// Create networks from connections property
for c := range lesson.Connections {
connection := lesson.Connections[c]
_, err := s.createNetwork(span.Context(), c, fmt.Sprintf("%s-%s-net", connection.A, connection.B), connection.Subnet, req)
if err != nil {
log.Error(err)
return err
}
}
createdPods := make(map[string]*corev1.Pod)
// Create pods and services
for _, epName := range epOrdered {
ep := ll.LiveEndpoints[epName]
// createPod doesn't try to ensure a certain pod status. That's done later
newPod, err := s.createPod(span.Context(),
ep,
getMemberNetworks(ep.Name, lesson.Connections),
req,
)
if err != nil {
log.Error(err)
return err
}
createdPods[newPod.ObjectMeta.Name] = newPod
// Expose via service if needed
if len(newPod.Spec.Containers[0].Ports) > 0 {
svc, err := s.createService(
span.Context(),
newPod,
req,
)
if err != nil {
log.Error(err)
return err
}
// Update livelesson liveendpoint with cluster IP
s.Db.UpdateLiveLessonEndpointIP(span.Context(), ll.ID, ep.Name, svc.Spec.ClusterIP)
if err != nil {
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
return err
}
}
// Create ingresses for http presentations
for pr := range ep.Presentations {
p := ep.Presentations[pr]
if p.Type == "http" {
_, err := s.createIngress(
span.Context(),
ns.ObjectMeta.Name,
ep,
p,
)
if err != nil {
log.Error(err)
return err
}
}
}
}
err = s.Db.UpdateLiveLessonStatus(span.Context(), ll.ID, models.Status_BOOTING)
if err != nil {
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
return err
}
// Before moving forward with network-based health checks, let's look back at the pods
// we've deployed, and wait until they're in a "Running" status. This allows us to keep a hold
// of maximum amounts of context for troubleshooting while we have it
wg := new(sync.WaitGroup)
wg.Add(len(createdPods))
cp := &sync.Mutex{}
failLesson := false
for name, pod := range createdPods {
go func(sc ot.SpanContext, name string, pod *corev1.Pod) {
span := ot.StartSpan("scheduler_pod_status", ot.ChildOf(sc))
defer span.Finish()
span.SetTag("podName", name)
defer wg.Done()
for i := 0; i < 150; i++ {
rdy, err := s.getPodStatus(pod)
if err != nil {
s.recordPodLogs(span.Context(), ll.ID, pod.Name, initContainerName)
s.recordPodLogs(span.Context(), ll.ID, pod.Name, "")
failLesson = true
return
}
if rdy {
cp.Lock()
delete(createdPods, name)
cp.Unlock()
return
}
time.Sleep(2 * time.Second)
}
// We would only get to this point if the pod failed to start in the first place.
// One potential reason for this is a failure in the init container, so we should attempt
// to gather those logs.
s.recordPodLogs(span.Context(), ll.ID, pod.Name, initContainerName)
err = fmt.Errorf("Timed out waiting for %s to start", name)
span.LogFields(log.Error(err))
ext.Error.Set(span, true)
failLesson = true
return
}(span.Context(), name, pod)
}
wg.Wait()
// At this point, the only pods left in createdPods should be ones that failed to ready
if failLesson || len(createdPods) > 0 {
failedPodNames := []string{}
for k := range createdPods {
failedPodNames = append(failedPodNames, k)
}
span.LogFields(log.Object("failedPodNames", failedPodNames))
ext.Error.Set(span, true)
return errors.New("Some pods failed to start")
}
return nil
}