forked from kiali/kiali
/
istio_rules_service.go
189 lines (164 loc) · 5.78 KB
/
istio_rules_service.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
package kubernetes
import (
"fmt"
"strings"
"github.com/kiali/kiali/log"
)
// GetIstioRules returns a list of mixer rules for a given namespace.
func (in *IstioClient) GetIstioRules(namespace string) (*IstioRules, error) {
result, err := in.istioConfigApi.Get().Namespace(namespace).Resource(rules).Do().Get()
if err != nil {
return nil, err
}
ruleList, ok := result.(*ruleList)
if !ok {
return nil, fmt.Errorf("%s doesn't return a rules list", namespace)
}
istioRules := IstioRules{}
istioRules.Rules = make([]IstioObject, 0)
for _, rule := range ruleList.Items {
istioRules.Rules = append(istioRules.Rules, rule.DeepCopyIstioObject())
}
return &istioRules, nil
}
// GetIstioRuleDetails returns the handlers and instances details for a given mixer rule.
// On this version, the following handlers and instances are supported:
// - listchecker
// - listentry
// - denier
// - checknothing
func (in *IstioClient) GetIstioRuleDetails(namespace string, istiorule string) (*IstioRuleDetails, error) {
result, err := in.istioConfigApi.Get().Namespace(namespace).Resource(rules).SubResource(istiorule).Do().Get()
if err != nil {
return nil, err
}
mRule, ok := result.(*rule)
if !ok {
return nil, fmt.Errorf("%s/%s doesn't return a Rule", namespace, istiorule)
}
istioRuleDetails := IstioRuleDetails{}
istioRuleDetails.Rule = mRule.DeepCopyIstioObject()
actions, ok := istioRuleDetails.Rule.GetSpec()["actions"].(actionsType)
if !ok {
return nil, fmt.Errorf("%s/%s doesn't return an Action", namespace, istiorule)
}
istioRuleDetails.Actions = make([]*IstioRuleAction, 0)
for _, rawAction := range actions {
action, ok := rawAction.(actionType)
if !ok {
return nil, fmt.Errorf("%s doesn't follow a map format", action)
}
actionDetails, err := in.getActionDetails(namespace, istiorule, action)
if err != nil {
return nil, err
}
istioRuleDetails.Actions = append(istioRuleDetails.Actions, actionDetails)
}
return &istioRuleDetails, nil
}
func (in *IstioClient) getActionDetails(namespace string, istiorule string, action actionType) (*IstioRuleAction, error) {
handler := action["handler"]
hName, hType, hNamespace := parseFQN(handler.(string))
if hNamespace != "" && hNamespace != namespace {
return nil, fmt.Errorf("Istio Rule %s/%s has a handler for a different namespace", namespace, istiorule)
}
handlerChan, instancesChan := make(chan istioResponse), make(chan istioResponse)
go in.getActionHandler(namespace, hType, hName, handlerChan)
instances := action["instances"]
for _, instance := range instances.(instancesType) {
iName, iType, iNamespace := parseFQN(instance.(string))
go in.getActionInstance(namespace, istiorule, iNamespace, iType, iName, instancesChan)
}
istioRuleAction := IstioRuleAction{}
handlerResponse := <-handlerChan
if handlerResponse.err != nil {
return nil, handlerResponse.err
}
istioRuleAction.Handler = handlerResponse.result
istioRuleAction.Instances = make([]IstioObject, 0)
for i := 0; i < len(instances.(instancesType)); i++ {
instanceResponse := <-instancesChan
if instanceResponse.err != nil {
return nil, instanceResponse.err
}
istioRuleAction.Instances = append(istioRuleAction.Instances, instanceResponse.result)
}
return &istioRuleAction, nil
}
func (in *IstioClient) getActionHandler(namespace string, handlerType string, handlerName string, handlerChan chan<- istioResponse) {
handlerTypePlural, ok := istioTypePlurals[handlerType]
if !ok {
log.Warningf("Handler type %s is not supported", handlerType)
handlerChan <- istioResponse{}
return
}
result, err := in.istioConfigApi.Get().Namespace(namespace).Resource(handlerTypePlural).SubResource(handlerName).Do().Get()
istioObject, ok := result.(IstioObject)
if !ok {
istioObject = nil
if err == nil {
err = fmt.Errorf("%s/%s doesn't return a valid IstioObject", handlerType, handlerName)
}
}
if istioObject != nil {
if istioObject.GetSpec() != nil {
istioObject.GetSpec()["adapter"] = handlerType
} else {
istioObject.SetSpec(map[string]interface{}{
"adapter": handlerType,
})
}
}
handlerChan <- istioResponse{result: istioObject, err: err}
}
func (in *IstioClient) getActionInstance(namespace string, istiorule string, instanceNamespace string, instanceType string, instanceName string, instancesChan chan<- istioResponse) {
if instanceNamespace != "" && instanceNamespace != namespace {
err := fmt.Errorf("Istio Rule %s/%s has an instance for a different namespace", namespace, istiorule)
instancesChan <- istioResponse{err: err}
return
}
istioTypePlural, ok := istioTypePlurals[instanceType]
if !ok {
log.Warningf("Instance type %s is not supported", instanceType)
instancesChan <- istioResponse{}
return
}
result, err := in.istioConfigApi.Get().Namespace(namespace).Resource(istioTypePlural).SubResource(instanceName).Do().Get()
istioObject, ok := result.(IstioObject)
if !ok {
istioObject = nil
if err == nil {
err = fmt.Errorf("%s/%s doesn't return a valid IstioObject", instanceType, instanceName)
}
}
if istioObject != nil {
if istioObject.GetSpec() != nil {
istioObject.GetSpec()["template"] = instanceType
} else {
istioObject.SetSpec(map[string]interface{}{
"template": instanceType,
})
}
}
instancesChan <- istioResponse{result: istioObject, err: err}
}
func parseFQN(fqn string) (fqnName string, fqnType string, fqnNamespace string) {
if fqn == "" {
return "", "", ""
}
iName := strings.Index(fqn, ".")
if iName == -1 {
return fqn, "", ""
}
runesHandler := []rune(fqn)
fName := string(runesHandler[0:iName])
fType := string(runesHandler[iName+1:])
fNamespace := ""
iNamespace := strings.Index(fType, ".")
if iNamespace != -1 {
runesType := []rune(fType)
fType = string(runesType[0:iNamespace])
fNamespace = string(runesType[iNamespace+1:])
}
return fName, fType, fNamespace
}