-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfigure_kwsk_actions.go
517 lines (459 loc) · 16.2 KB
/
configure_kwsk_actions.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
package restapi
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"time"
middleware "github.com/go-openapi/runtime/middleware"
models "github.com/projectodd/kwsk/models"
"github.com/projectodd/kwsk/restapi/operations"
"github.com/projectodd/kwsk/restapi/operations/actions"
corev1 "k8s.io/api/core/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
knative "github.com/knative/serving/pkg/client/clientset/versioned"
)
// Copied from upstream Exec.scala
var base64Pattern = regexp.MustCompile("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$")
func isBase64(s string) bool {
return (len(s)%4 == 0) && base64Pattern.MatchString(s)
}
func configureActions(api *operations.KwskAPI, knativeClient *knative.Clientset, cache cache.Store) {
api.ActionsDeleteActionHandler = actions.DeleteActionHandlerFunc(deleteActionFunc(knativeClient))
api.ActionsGetActionByNameHandler = actions.GetActionByNameHandlerFunc(getActionByNameFunc(knativeClient))
api.ActionsGetAllActionsHandler = actions.GetAllActionsHandlerFunc(getAllActionsFunc(knativeClient))
api.ActionsInvokeActionHandler = actions.InvokeActionHandlerFunc(invokeActionFunc(knativeClient, cache))
api.ActionsUpdateActionHandler = actions.UpdateActionHandlerFunc(updateActionFunc(knativeClient))
}
func deleteActionFunc(knativeClient *knative.Clientset) actions.DeleteActionHandlerFunc {
return func(params actions.DeleteActionParams, principal *models.Principal) middleware.Responder {
serviceName := sanitizeObjectName(params.ActionName)
namespace := namespaceOrDefault(params.Namespace)
err := knativeClient.ServingV1alpha1().Services(namespace).Delete(serviceName, &metav1.DeleteOptions{})
if err != nil {
errorMessage := errorMessageFromErr(err)
if k8sErrors.IsNotFound(err) {
return actions.NewDeleteActionNotFound().WithPayload(errorMessage)
}
return actions.NewDeleteActionInternalServerError().WithPayload(errorMessage)
}
// Wait for the action to be deleted
deleteTimeout := 1 * time.Minute
err = wait.PollImmediate(1*time.Second, deleteTimeout, func() (bool, error) {
_, serviceErr := knativeClient.ServingV1alpha1().Services(namespace).Get(serviceName, metav1.GetOptions{})
_, routeErr := knativeClient.ServingV1alpha1().Routes(namespace).Get(serviceName, metav1.GetOptions{})
if serviceErr != nil && k8sErrors.IsNotFound(serviceErr) && routeErr != nil && k8sErrors.IsNotFound(routeErr) {
// Service and Route are gone, so that's good enough
return true, nil
}
if serviceErr != nil {
return false, err
}
return false, nil
})
if err != nil {
fmt.Printf("Error waiting on action to delete: %s\n", err)
return actions.NewDeleteActionInternalServerError()
}
return actions.NewDeleteActionOK()
}
}
func updateActionFunc(knativeClient *knative.Clientset) actions.UpdateActionHandlerFunc {
return func(params actions.UpdateActionParams, principal *models.Principal) middleware.Responder {
name := params.ActionName
serviceName := sanitizeObjectName(name)
namespace := namespaceOrDefault(params.Namespace)
kind := params.Action.Exec.Kind
version := params.Action.Version
if version == "" {
version = "0.0.1"
}
annotations := make(map[string]string)
annotations[KwskName] = name
annotations[KwskVersion] = version
for _, kv := range params.Action.Annotations {
key := fmt.Sprintf("kwsk_action_anno_%s", kv.Key)
value := fmt.Sprintf("%s", kv.Value)
annotations[key] = value
}
var image string
if params.Action.Exec != nil {
image = params.Action.Exec.Image
annotations[KwskKind] = kind
annotations[KwskImage] = image
}
configSpec := v1alpha1.ConfigurationSpec{
RevisionTemplate: v1alpha1.RevisionTemplateSpec{
ObjectMeta: metav1.ObjectMeta{},
Spec: v1alpha1.RevisionSpec{
ConcurrencyModel: v1alpha1.RevisionRequestConcurrencyModelSingle,
},
},
}
if image == "" {
prefix := imagePrefix()
tag := imageTag()
switch kind {
case "nodejs:default", "nodejs:6":
image = "kwsk-nodejs6action"
case "nodejs:8":
image = "kwsk-action-nodejs-v8"
case "python:default", "python:2":
image = "kwsk-python2action"
case "python:3":
image = "kwsk-python3action"
case "java:default", "java":
image = "kwsk-java8action"
}
image = fmt.Sprintf("%s/%s:%s", prefix, image, tag)
}
actionParamsMap := map[string]interface{}{}
for _, kv := range params.Action.Parameters {
actionParamsMap[kv.Key] = kv.Value
}
actionParamsJson, err := json.Marshal(actionParamsMap)
if err != nil {
fmt.Println("Error marshaling action parameters: ", err)
return actions.NewUpdateActionInternalServerError()
}
containerEnv := []corev1.EnvVar{
corev1.EnvVar{
Name: "KWSK_ACTION_CODE",
Value: params.Action.Exec.Code,
},
corev1.EnvVar{
Name: "KWSK_ACTION_BINARY",
Value: strconv.FormatBool(isBase64(params.Action.Exec.Code)),
},
corev1.EnvVar{
Name: "KWSK_ACTION_PARAMS",
Value: string(actionParamsJson),
},
}
if len(params.Action.Exec.Main) > 0 {
containerEnv = append(containerEnv, corev1.EnvVar{
Name: "KWSK_ACTION_MAIN",
Value: params.Action.Exec.Main,
})
}
container := corev1.Container{
Image: image,
Env: containerEnv,
}
configSpec.RevisionTemplate.Spec.Container = container
service := &v1alpha1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
Namespace: namespace,
Annotations: annotations,
},
Spec: v1alpha1.ServiceSpec{
RunLatest: &v1alpha1.RunLatestType{
Configuration: configSpec,
},
},
}
dbg := fmt.Sprintf("Creating service %+v\n", service)
fmt.Printf("%.2000s\n", dbg)
service, err = knativeClient.ServingV1alpha1().Services(namespace).Create(service)
if err != nil {
fmt.Println("Error updating action: ", err)
return actions.NewUpdateActionInternalServerError().WithPayload(errorMessageFromErr(err))
}
action, err := getActionByName(knativeClient, name, namespace, false)
if err != nil {
fmt.Println("Error retrieving updated action: ", err)
return actions.NewUpdateActionInternalServerError().WithPayload(errorMessageFromErr(err))
}
return actions.NewUpdateActionOK().WithPayload(action)
}
}
func serviceToAction(service *v1alpha1.Service) *models.Action {
objectMeta := service.ObjectMeta
name := objectMeta.Annotations[KwskName]
if name == "" {
name = objectMeta.Name
}
version := objectMeta.Annotations[KwskVersion]
kind := objectMeta.Annotations[KwskKind]
image := objectMeta.Annotations[KwskImage]
var code string
var binary bool
var main string
var actionParams map[string]interface{}
configurationSpec := service.Spec.RunLatest.Configuration
for _, env := range configurationSpec.RevisionTemplate.Spec.Container.Env {
if env.Name == "KWSK_ACTION_CODE" {
code = env.Value
}
if env.Name == "KWSK_ACTION_BINARY" {
binary, _ = strconv.ParseBool(env.Value)
}
if env.Name == "KWSK_ACTION_MAIN" {
main = env.Value
}
if env.Name == "KWSK_ACTION_PARAMS" {
err := json.Unmarshal([]byte(env.Value), &actionParams)
if err != nil {
fmt.Println("Failed to unmarshal action parameters:", err)
}
}
}
var params []*models.KeyValue
for key, value := range actionParams {
param := &models.KeyValue{
Key: key,
Value: value,
}
params = append(params, param)
}
var annotations []*models.KeyValue
for key, value := range objectMeta.Annotations {
if strings.HasPrefix(key, "kwsk_action_anno_") {
annotation := &models.KeyValue{
Key: strings.TrimPrefix(key, "kwsk_action_anno_"),
Value: value,
}
annotations = append(annotations, annotation)
}
}
publish := false
return &models.Action{
Name: &name,
Namespace: &objectMeta.Namespace,
Version: &version,
Exec: &models.ActionExec{
Image: image,
Kind: kind,
Code: code,
Binary: binary,
Main: main,
},
Parameters: params,
Annotations: annotations,
Publish: &publish,
}
}
func getActionByName(knativeClient *knative.Clientset, name string, namespace string, includeCode bool) (*models.Action, error) {
serviceName := sanitizeObjectName(name)
namespace = namespaceOrDefault(namespace)
service, err := knativeClient.ServingV1alpha1().Services(namespace).Get(serviceName, metav1.GetOptions{})
if err != nil {
return nil, err
}
action := serviceToAction(service)
if !includeCode {
action.Exec.Code = ""
}
return action, nil
}
func getActionByNameFunc(knativeClient *knative.Clientset) actions.GetActionByNameHandlerFunc {
return func(params actions.GetActionByNameParams, principal *models.Principal) middleware.Responder {
var code bool
if params.Code != nil {
code = *params.Code
}
action, err := getActionByName(knativeClient, params.ActionName, params.Namespace, code)
if err != nil {
errorMessage := errorMessageFromErr(err)
if k8sErrors.IsNotFound(err) {
return actions.NewGetActionByNameNotFound().WithPayload(errorMessage)
}
return actions.NewGetActionByNameInternalServerError().WithPayload(errorMessage)
}
return actions.NewGetActionByNameOK().WithPayload(action)
}
}
func getAllActionsFunc(knativeClient *knative.Clientset) actions.GetAllActionsHandlerFunc {
return func(params actions.GetAllActionsParams, principal *models.Principal) middleware.Responder {
namespace := namespaceOrDefault(params.Namespace)
services, err := knativeClient.ServingV1alpha1().Services(namespace).List(metav1.ListOptions{})
if err != nil {
return actions.NewGetAllActionsInternalServerError().WithPayload(errorMessageFromErr(err))
}
var payload = make([]*models.Action, len(services.Items))
for i, service := range services.Items {
payload[i] = serviceToAction(&service)
}
return actions.NewGetAllActionsOK().WithPayload(payload)
}
}
func withRoutesReady(knativeClient *knative.Clientset, service *v1alpha1.Service) (*v1alpha1.Service, error) {
// Wait for the service routes to be ready
readyTimeout := 5 * time.Minute
if !serviceRoutesReady(service) {
err := wait.Poll(1*time.Second, readyTimeout, func() (bool, error) {
newService, err := knativeClient.ServingV1alpha1().Services(service.Namespace).Get(service.Name, metav1.GetOptions{})
if err != nil {
if k8sErrors.IsNotFound(err) {
// If not found then keep trying, assuming it will
// be found later
return false, nil
}
fmt.Println("Error waiting for service route readiness: ", err)
return false, err
}
service = newService
return serviceRoutesReady(service), nil
})
if err != nil {
fmt.Printf("Error waiting on service to become ready: %s\n", err)
return service, err
}
}
return service, nil
}
func serviceRoutesReady(service *v1alpha1.Service) bool {
if c := service.Status.GetCondition(v1alpha1.ServiceConditionRoutesReady); c != nil {
return c.Status == corev1.ConditionTrue
}
return false
}
func invokeActionFunc(knativeClient *knative.Clientset, cache cache.Store) actions.InvokeActionHandlerFunc {
return func(params actions.InvokeActionParams, principal *models.Principal) middleware.Responder {
serviceName := sanitizeObjectName(params.ActionName)
namespace := namespaceOrDefault(params.Namespace)
blocking := params.Blocking != nil && *params.Blocking == "true"
result := params.Result != nil && *params.Result == "true"
service, err := knativeClient.ServingV1alpha1().Services(namespace).Get(serviceName, metav1.GetOptions{})
if err != nil {
errorMessage := errorMessageFromErr(err)
if k8sErrors.IsNotFound(err) {
return actions.NewInvokeActionNotFound().WithPayload(errorMessage)
}
return actions.NewInvokeActionInternalServerError().WithPayload(errorMessage)
}
service, err = withRoutesReady(knativeClient, service)
if err != nil {
fmt.Printf("Error waiting for action readiness: %s\n", err)
msg := "Error waiting for action readiness\n"
errorMessage := &models.ErrorMessage{
Error: &msg,
}
return actions.NewInvokeActionInternalServerError().WithPayload(errorMessage)
}
actionHost := service.Status.Domain
istioHostAndPort := istioHostAndPort()
return runAction(istioHostAndPort, actionHost, service.Name, namespace, params.Payload, blocking, result, cache)
}
}
func runAction(istioHostAndPort string, actionHost string, name string, namespace string, params interface{}, blocking bool, result bool, cache cache.Store) middleware.Responder {
start := time.Now().UnixNano() / 1000000 // milliseconds since epoch
var resStatus int
var resBody []byte
var err error
// Wait for the action to be ready
readyTimeout := 5 * time.Minute
err = wait.PollImmediate(1*time.Second, readyTimeout, func() (bool, error) {
resStatus, resBody, err = actionRequest(istioHostAndPort, actionHost, "", params)
if err != nil {
fmt.Printf("Action not yet ready: %s\n", err)
return false, err
}
return resStatus != http.StatusNotFound && resStatus != http.StatusServiceUnavailable, nil
})
if err != nil {
fmt.Printf("Error waiting on action to become ready: %s\n", err)
return actions.NewInvokeActionInternalServerError().WithPayload(errorMessageFromErr(err))
}
if resStatus != http.StatusOK {
msg := fmt.Sprintf("Error invoking action. Status: %d, Message: %s\n", resStatus, resBody)
errorMessage := &models.ErrorMessage{
Error: &msg,
}
fmt.Println(msg)
return actions.NewInvokeActionInternalServerError().WithPayload(errorMessage)
}
var resultJson interface{}
err = json.Unmarshal(resBody, &resultJson)
if err != nil {
msg := fmt.Sprintf("Action invocation result was not valid JSON. Result: %s\n", resStatus, resBody)
errorMessage := &models.ErrorMessage{
Error: &msg,
}
fmt.Println(msg)
return actions.NewInvokeActionInternalServerError().WithPayload(errorMessage)
}
activationResult := &models.ActivationResult{
Result: resultJson,
Success: true,
Status: "success",
}
activationId, err := newActivationId()
if err != nil {
fmt.Printf("Error generating activationId: %s\n", err)
return actions.NewInvokeActionInternalServerError().WithPayload(errorMessageFromErr(err))
}
logs := []string{}
annotations := []*models.KeyValue{}
end := time.Now().UnixNano() / 1000000 // milliseconds since epoch
duration := end - start
activation := models.Activation{
ActivationID: &activationId,
Name: &name,
Namespace: &namespace,
Response: activationResult,
Start: &start,
End: end,
Duration: duration,
Logs: logs,
Annotations: annotations,
}
err = cache.Add(activation)
if err != nil {
msg := fmt.Sprintf("Error storing activation record: %s\n", err)
errorMessage := &models.ErrorMessage{
Error: &msg,
}
fmt.Println(msg)
return actions.NewInvokeActionInternalServerError().WithPayload(errorMessage)
}
if result {
fmt.Printf("Returning Activation result: %+v\n", resultJson)
return actions.NewInvokeActionOK().WithPayload(resultJson)
} else if blocking {
fmt.Printf("Returning Activation: %+v\n", activation)
return actions.NewInvokeActionOK().WithPayload(activation)
} else {
payload := &models.ActivationID{
ActivationID: &activationId,
}
fmt.Printf("Returning ActivationId: %s\n", activationId)
return actions.NewInvokeActionAccepted().WithPayload(payload)
}
}
func actionRequest(istioHostAndPort string, actionHost string, path string, requestBody interface{}) (int, []byte, error) {
url := fmt.Sprintf("http://%s/%s", istioHostAndPort, path)
fmt.Printf("Sending POST to url %s with host %s\n", url, actionHost)
body, err := json.Marshal(requestBody)
if err != nil {
fmt.Printf("Error marshaling action request body: %s\n", err)
return 500, nil, err
}
fmt.Printf("Request Body: %s\n", body)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
if err != nil {
fmt.Printf("Error creating http request for action: %s\n", err)
return 500, nil, err
}
req.Host = actionHost
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("Error calling action http endpoint: %s\n", err)
return 500, nil, err
}
defer res.Body.Close()
resBody, _ := ioutil.ReadAll(res.Body)
fmt.Printf("Response: %+v\n", res)
fmt.Printf("Response Body: %s\n", string(resBody))
return res.StatusCode, resBody, nil
}