forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threescale.go
478 lines (413 loc) · 15.5 KB
/
threescale.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
package business
import (
"encoding/json"
"fmt"
"strings"
"sync"
"k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/models"
"github.com/kiali/kiali/prometheus/internalmetrics"
)
// ThreeScaleService is Maistra-Only. Maistra does not allow Istio multi-namespace deployment, use
// the single Istio namespace.
type ThreeScaleService struct {
k8s kubernetes.IstioClientInterface
}
// GetThreeScaleInfo is Maistra-Only. Maistra does not allow Istio multi-namespace deployment, use
// the single Istio namespace.
func (in *ThreeScaleService) GetThreeScaleInfo() (models.ThreeScaleInfo, error) {
var err error
promtimer := internalmetrics.GetGoFunctionMetric("business", "ThreeScaleService", "GetThreeScaleInfo")
defer promtimer.ObserveNow(&err)
conf := config.Get()
_, err2 := in.k8s.GetAdapter(conf.IstioNamespace, "adapters", conf.Extensions.ThreeScale.AdapterName)
if err2 != nil {
if errors.IsNotFound(err2) {
return models.ThreeScaleInfo{}, nil
} else {
return models.ThreeScaleInfo{}, err2
}
}
canCreate, canUpdate, canDelete := getPermissions(in.k8s, conf.IstioNamespace, "adapters", "adapters")
return models.ThreeScaleInfo{
Enabled: true,
Permissions: models.ResourcePermissions{
Create: canCreate,
Update: canUpdate,
Delete: canDelete,
}}, nil
}
// GetThreeScaleHandlers is Maistra-Only. Maistra does not allow Istio multi-namespace deployment, use
// the single Istio namespace.
func (in *ThreeScaleService) GetThreeScaleHandlers() (models.ThreeScaleHandlers, error) {
var err error
promtimer := internalmetrics.GetGoFunctionMetric("business", "ThreeScaleService", "GetThreeScaleHandlers")
defer promtimer.ObserveNow(&err)
return in.getThreeScaleHandlers()
}
// CreateThreeScaleHandler is Maistra-Only. Maistra does not allow Istio multi-namespace deployment, use
// the single Istio namespace.
func (in *ThreeScaleService) CreateThreeScaleHandler(body []byte) (models.ThreeScaleHandlers, error) {
var err error
promtimer := internalmetrics.GetGoFunctionMetric("business", "ThreeScaleService", "CreateThreeScaleHandler")
defer promtimer.ObserveNow(&err)
conf := config.Get()
threeScaleHandler := &models.ThreeScaleHandler{}
err2 := json.Unmarshal(body, threeScaleHandler)
if err2 != nil {
log.Errorf("JSON: %s shows error: %s", string(body), err2)
err = fmt.Errorf(models.BadThreeScaleHandlerJson)
return nil, err
}
jsonHandler, jsonInstance, err2 := generateJsonHandlerInstance(*threeScaleHandler)
if err2 != nil {
log.Error(err2)
err = fmt.Errorf(models.BadThreeScaleHandlerJson)
return nil, err
}
var wg sync.WaitGroup
wg.Add(1)
var errHandler, errInstance error
go func() {
defer wg.Done()
_, errInstance = in.k8s.CreateIstioObject(resourceTypesToAPI["templates"], conf.IstioNamespace, "instances", jsonInstance)
}()
// Create handler on main goroutine
_, errHandler = in.k8s.CreateIstioObject(resourceTypesToAPI["adapters"], conf.IstioNamespace, "handlers", jsonHandler)
wg.Wait()
if errHandler != nil {
return nil, errHandler
}
if errInstance != nil {
return nil, errHandler
}
return in.getThreeScaleHandlers()
}
// getThreeScaleHandlers is to be reused for several public methods
func (in *ThreeScaleService) getThreeScaleHandlers() (models.ThreeScaleHandlers, error) {
conf := config.Get()
// Istio config generated from Kiali will be labeled as kiali_wizard
tsh, err2 := in.k8s.GetAdapters(conf.IstioNamespace, "kiali_wizard")
if err2 != nil {
return models.ThreeScaleHandlers{}, err2
}
return models.CastThreeScaleHandlers(tsh), nil
}
// generateJsonHandlerInstance will generate the JSON representing the Handler and Instance that will be used for the ThreeScale Handler
func generateJsonHandlerInstance(handler models.ThreeScaleHandler) (string, string, error) {
conf := config.Get()
newHandler := kubernetes.GenericIstioObject{
TypeMeta: meta_v1.TypeMeta{
APIVersion: "config.istio.io/v1alpha2",
Kind: "handler",
},
ObjectMeta: meta_v1.ObjectMeta{
Name: handler.Name,
Namespace: conf.IstioNamespace,
Labels: map[string]string{
"kiali_wizard": "threescale-handler",
},
},
Spec: map[string]interface{}{
"adapter": conf.Extensions.ThreeScale.AdapterName,
"params": map[string]interface{}{
"service_id": handler.ServiceId,
"system_url": handler.SystemUrl,
"access_token": handler.AccessToken,
},
"connection": map[string]interface{}{
"address": conf.Extensions.ThreeScale.AdapterService + ":" + conf.Extensions.ThreeScale.AdapterPort,
},
},
}
newInstance := kubernetes.GenericIstioObject{
TypeMeta: meta_v1.TypeMeta{
APIVersion: "config.istio.io/v1alpha2",
Kind: "instance",
},
ObjectMeta: meta_v1.ObjectMeta{
Name: "threescale-authorization-" + handler.Name,
Namespace: conf.IstioNamespace,
Labels: map[string]string{
"kiali_wizard": "threescale-handler",
},
},
Spec: map[string]interface{}{
"template": "threescale-authorization",
"params": map[string]interface{}{
"subject": map[string]interface{}{
"user": "request.query_params[\"user_key\"] | request.headers[\"user-key\"] | \"\"",
"properties": map[string]interface{}{
"app_id": "request.query_params[\"app_id\"] | request.headers[\"app-id\"] | \"\"",
"app_key": "request.query_params[\"app_key\"] | request.headers[\"app-key\"] | \"\"",
},
},
"action": map[string]interface{}{
"path": "request.url_path",
"method": "request.method | \"get\"",
},
},
},
}
bHandler, err := json.Marshal(newHandler)
if err != nil {
return "", "", err
}
bInstance, err := json.Marshal(newInstance)
if err != nil {
return "", "", err
}
return string(bHandler), string(bInstance), nil
}
// UpdateThreeScaleHandler is Maistra-Only. Maistra does not allow Istio multi-namespace deployment, use
// the single Istio namespace.
func (in *ThreeScaleService) UpdateThreeScaleHandler(handlerName string, body []byte) (models.ThreeScaleHandlers, error) {
var err error
promtimer := internalmetrics.GetGoFunctionMetric("business", "ThreeScaleService", "UpdateThreeScaleHandler")
defer promtimer.ObserveNow(&err)
threeScaleHandler := &models.ThreeScaleHandler{}
err2 := json.Unmarshal(body, threeScaleHandler)
if err2 != nil {
log.Errorf("JSON: %s shows error: %s", string(body), err2)
err = fmt.Errorf(models.BadThreeScaleHandlerJson)
return nil, err
}
// Be sure that name inside body is same as used as parameter
(*threeScaleHandler).Name = handlerName
// We need the handler structure generated from the ThreeScaleHandler to update it
jsonUpdatedHandler, _, err2 := generateJsonHandlerInstance(*threeScaleHandler)
if err2 != nil {
return nil, err
}
conf := config.Get()
_, err2 = in.k8s.UpdateIstioObject(resourceTypesToAPI["adapters"], conf.IstioNamespace, "handlers", handlerName, jsonUpdatedHandler)
if err2 != nil {
return nil, err2
}
return in.getThreeScaleHandlers()
}
func checkHandler(istioObject kubernetes.IstioObject, handlerName string) bool {
conf := config.Get()
fullHandlerName := handlerName + "." + conf.IstioNamespace
if istioObject.GetSpec() != nil {
if actions, actionsFound := istioObject.GetSpec()["actions"]; actionsFound {
if actionArray, actionCast := actions.([]interface{}); actionCast {
if len(actionArray) == 1 {
action := actionArray[0]
if actionMap, actionMapCast := action.(map[string]interface{}); actionMapCast {
if handler, ok := actionMap["handler"]; ok {
if handlerValue, handlerCast := handler.(string); handlerCast && handlerValue == fullHandlerName {
return true
}
}
}
}
}
}
}
return false
}
// DeleteThreeScaleHandler is Maistra-Only. Maistra does not allow Istio multi-namespace deployment, use
// the single Istio namespace.
func (in *ThreeScaleService) DeleteThreeScaleHandler(handlerName string) (models.ThreeScaleHandlers, error) {
var err error
promtimer := internalmetrics.GetGoFunctionMetric("business", "ThreeScaleService", "DeleteThreeScaleHandler")
defer promtimer.ObserveNow(&err)
conf := config.Get()
err = in.k8s.DeleteIstioObject(resourceTypesToAPI["adapters"], conf.IstioNamespace, "handlers", handlerName)
if err != nil {
return nil, err
}
instanceName := "threescale-authorization-" + handlerName
err = in.k8s.DeleteIstioObject(resourceTypesToAPI["templates"], conf.IstioNamespace, "instances", instanceName)
if err != nil {
return nil, err
}
// It should delete all Rules generated by Wizard that used the deleted handler
rules, err := in.k8s.GetIstioRules(conf.IstioNamespace, "kiali_wizard")
if err != nil {
return nil, err
}
ruleNamesToDelete := make([]string, 0)
for _, rule := range rules {
if checkHandler(rule, handlerName) {
ruleNamesToDelete = append(ruleNamesToDelete, rule.GetObjectMeta().Name)
}
}
wg := sync.WaitGroup{}
wg.Add(len(ruleNamesToDelete))
errChan := make(chan error, 1)
for _, ruleName := range ruleNamesToDelete {
go func(ruleNameToDelete string) {
defer wg.Done()
if len(errChan) == 0 {
err = in.k8s.DeleteIstioObject(resourceTypesToAPI["rules"], conf.IstioNamespace, "rules", ruleNameToDelete)
if err != nil {
errChan <- err
}
}
}(ruleName)
}
wg.Wait()
close(errChan)
for e := range errChan {
if e != nil {
return nil, err
}
}
return in.getThreeScaleHandlers()
}
func getThreeScaleRuleDetails(rule kubernetes.IstioObject) string {
conf := config.Get()
threeScaleHandlerName := ""
if rule.GetSpec() != nil {
if actions, actionsFound := rule.GetSpec()["actions"]; actionsFound {
if actionsCast, actionInterface := actions.([]interface{}); actionInterface {
if len(actionsCast) == 1 {
action := actionsCast[0]
if actionCast, actionInterface := action.(map[string]interface{}); actionInterface {
if handler, handlerFound := actionCast["handler"]; handlerFound {
if handlerCast, handlerString := handler.(string); handlerString {
suffix := "." + conf.IstioNamespace
if strings.HasSuffix(handlerCast, "."+conf.IstioNamespace) {
threeScaleHandlerName = handlerCast[0 : len(handlerCast)-len(suffix)]
}
}
}
}
}
}
}
}
return threeScaleHandlerName
}
// GetThreeScaleRule is Maistra-Only. Maistra does not allow Istio multi-namespace deployment, use
// the single Istio namespace.
func (in *ThreeScaleService) GetThreeScaleRule(namespace, service string) (models.ThreeScaleServiceRule, error) {
var err error
promtimer := internalmetrics.GetGoFunctionMetric("business", "ThreeScaleService", "GetThreeScaleRule")
defer promtimer.ObserveNow(&err)
conf := config.Get()
ruleName := "threescale-" + namespace + "-" + service
rule, err := in.k8s.GetIstioRule(conf.IstioNamespace, ruleName)
if err != nil {
return models.ThreeScaleServiceRule{}, err
}
threeScaleHandlerName := getThreeScaleRuleDetails(rule)
threeScaleServiceRule := models.ThreeScaleServiceRule{
ServiceName: service,
ServiceNamespace: namespace,
ThreeScaleHandlerName: threeScaleHandlerName,
}
return threeScaleServiceRule, nil
}
func generateMatch(threeScaleServiceRule models.ThreeScaleServiceRule) string {
// Match granularity is set at service level so no need to use versions labels
match := "context.reporter.kind == \"inbound\" && "
match += "destination.service.namespace == \"" + threeScaleServiceRule.ServiceNamespace + "\" && "
match += "destination.service.name == \"" + threeScaleServiceRule.ServiceName + "\""
return match
}
func generateJsonRule(threeScaleServiceRule models.ThreeScaleServiceRule) (string, error) {
conf := config.Get()
newRule := kubernetes.GenericIstioObject{
TypeMeta: meta_v1.TypeMeta{
APIVersion: "config.istio.io/v1alpha2",
Kind: "rule",
},
ObjectMeta: meta_v1.ObjectMeta{
Name: "threescale-" + threeScaleServiceRule.ServiceNamespace + "-" + threeScaleServiceRule.ServiceName,
Namespace: conf.IstioNamespace,
Labels: map[string]string{
"kiali_wizard": threeScaleServiceRule.ServiceNamespace + "-" + threeScaleServiceRule.ServiceName,
},
},
Spec: map[string]interface{}{
"match": generateMatch(threeScaleServiceRule),
"actions": []interface{}{
map[string]interface{}{
"handler": threeScaleServiceRule.ThreeScaleHandlerName + "." + conf.IstioNamespace,
"instances": []interface{}{
"threescale-authorization-" + threeScaleServiceRule.ThreeScaleHandlerName,
},
},
},
},
}
bRule, err := json.Marshal(newRule)
if err != nil {
return "", err
}
return string(bRule), nil
}
// CreateThreeScaleRule is Maistra-Only. Maistra does not allow Istio multi-namespace deployment, use
// the single Istio namespace.
func (in *ThreeScaleService) CreateThreeScaleRule(namespace string, body []byte) (models.ThreeScaleServiceRule, error) {
var err error
promtimer := internalmetrics.GetGoFunctionMetric("business", "ThreeScaleService", "CreateThreeScaleRule")
defer promtimer.ObserveNow(&err)
threeScaleServiceRule := &models.ThreeScaleServiceRule{}
err2 := json.Unmarshal(body, threeScaleServiceRule)
if err2 != nil {
log.Errorf("JSON: %s shows error: %s", string(body), err2)
err = fmt.Errorf(models.BadThreeScaleRuleJson)
return models.ThreeScaleServiceRule{}, err
}
jsonRule, err2 := generateJsonRule(*threeScaleServiceRule)
if err2 != nil {
log.Error(err2)
err = fmt.Errorf(models.BadThreeScaleRuleJson)
return models.ThreeScaleServiceRule{}, err
}
conf := config.Get()
_, errRule := in.k8s.CreateIstioObject(resourceTypesToAPI["rules"], conf.IstioNamespace, "rules", jsonRule)
if errRule != nil {
return models.ThreeScaleServiceRule{}, errRule
}
return *threeScaleServiceRule, nil
}
// UpdateThreeScaleHandler is Maistra-Only. Maistra does not allow Istio multi-namespace deployment, use
// the single Istio namespace.
func (in *ThreeScaleService) UpdateThreeScaleRule(namespace, service string, body []byte) (models.ThreeScaleServiceRule, error) {
var err error
promtimer := internalmetrics.GetGoFunctionMetric("business", "ThreeScaleService", "UpdateThreeScaleRule")
defer promtimer.ObserveNow(&err)
threeScaleServiceRule := &models.ThreeScaleServiceRule{}
err2 := json.Unmarshal(body, threeScaleServiceRule)
if err2 != nil {
log.Errorf("JSON: %s shows error: %s", string(body), err2)
err = fmt.Errorf(models.BadThreeScaleRuleJson)
return models.ThreeScaleServiceRule{}, err
}
// Be sure that parameters are used in the rule
(*threeScaleServiceRule).ServiceNamespace = namespace
(*threeScaleServiceRule).ServiceName = service
jsonRule, err2 := generateJsonRule(*threeScaleServiceRule)
if err2 != nil {
log.Error(err2)
err = fmt.Errorf(models.BadThreeScaleRuleJson)
return models.ThreeScaleServiceRule{}, err
}
ruleName := "threescale-" + namespace + "-" + service
conf := config.Get()
_, errRule := in.k8s.UpdateIstioObject(resourceTypesToAPI["rules"], conf.IstioNamespace, "rules", ruleName, jsonRule)
if errRule != nil {
return models.ThreeScaleServiceRule{}, errRule
}
return *threeScaleServiceRule, nil
}
// DeleteThreeScaleHandler is Maistra-Only. Maistra does not allow Istio multi-namespace deployment, use
// the single Istio namespace.
func (in *ThreeScaleService) DeleteThreeScaleRule(namespace, service string) error {
var err error
promtimer := internalmetrics.GetGoFunctionMetric("business", "ThreeScaleService", "DeleteThreeScaleRule")
defer promtimer.ObserveNow(&err)
conf := config.Get()
ruleName := "threescale-" + namespace + "-" + service
err = in.k8s.DeleteIstioObject(resourceTypesToAPI["rules"], conf.IstioNamespace, "rules", ruleName)
return err
}