-
Notifications
You must be signed in to change notification settings - Fork 474
/
route_checker.go
192 lines (158 loc) · 5.67 KB
/
route_checker.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
package virtualservices
import (
"fmt"
api_networking_v1beta1 "istio.io/api/networking/v1beta1"
networking_v1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/models"
)
type RouteChecker struct {
Namespaces []string
VirtualService *networking_v1beta1.VirtualService
}
// Check returns both an array of IstioCheck and a boolean indicating if the current route rule is valid.
// The array of IstioChecks contains the result of running the following validations:
// 1. All weights with a numeric number.
// 2. All weights have value between 0 and 100.
// 3. Sum of all weights are 100 (if only one weight, then it assumes that is 100).
// 4. All the route has to have weight label.
func (route RouteChecker) Check() ([]*models.IstioCheck, bool) {
checks, valid := make([]*models.IstioCheck, 0), true
cs, v := route.checkHttpRoutes()
checks = append(checks, cs...)
valid = valid && v
cs, v = route.checkTcpRoutes()
checks = append(checks, cs...)
valid = valid && v
cs, v = route.checkTlsRoutes()
checks = append(checks, cs...)
valid = valid && v
return checks, valid
}
func (route RouteChecker) checkHttpRoutes() ([]*models.IstioCheck, bool) {
validations := make([]*models.IstioCheck, 0)
valid := true
for routeIdx, httpRoute := range route.VirtualService.Spec.Http {
if httpRoute == nil {
continue
}
// Getting a []DestinationWeight
destinationWeights := httpRoute.Route
if len(destinationWeights) == 1 {
if destinationWeights[0] == nil {
continue
}
weight := destinationWeights[0].Weight
// We can't rely on nil value as Weight is an integer that will be always present
if weight > 0 && weight < 100 {
valid = true
path := fmt.Sprintf("spec/http[%d]/route[%d]/weight", routeIdx, 0)
validation := models.Build("virtualservices.route.singleweight", path)
validations = append(validations, &validation)
}
}
route.trackHttpSubset(routeIdx, "http", destinationWeights, &validations)
}
return validations, valid
}
func (route RouteChecker) checkTcpRoutes() ([]*models.IstioCheck, bool) {
validations := make([]*models.IstioCheck, 0)
valid := true
for routeIdx, tcpRoute := range route.VirtualService.Spec.Tcp {
if tcpRoute == nil {
continue
}
// Getting a []DestinationWeight
destinationWeights := tcpRoute.Route
if len(destinationWeights) == 1 {
if destinationWeights[0] == nil {
continue
}
weight := destinationWeights[0].Weight
if weight < 100 {
valid = true
path := fmt.Sprintf("spec/tcp[%d]/route[%d]/weight", routeIdx, 0)
validation := models.Build("virtualservices.route.singleweight", path)
validations = append(validations, &validation)
}
}
route.trackTcpTlsSubset(routeIdx, "tcp", destinationWeights, &validations)
}
return validations, valid
}
func (route RouteChecker) checkTlsRoutes() ([]*models.IstioCheck, bool) {
validations := make([]*models.IstioCheck, 0)
valid := true
for routeIdx, tlsRoute := range route.VirtualService.Spec.Tls {
if tlsRoute == nil {
continue
}
// Getting a []DestinationWeight
destinationWeights := tlsRoute.Route
if len(destinationWeights) == 1 {
if destinationWeights[0] == nil {
continue
}
weight := destinationWeights[0].Weight
if weight < 100 {
valid = true
path := fmt.Sprintf("spec/tls[%d]/route[%d]/weight", routeIdx, 0)
validation := models.Build("virtualservices.route.singleweight", path)
validations = append(validations, &validation)
}
}
route.trackTcpTlsSubset(routeIdx, "tls", destinationWeights, &validations)
}
return validations, valid
}
func (route RouteChecker) trackHttpSubset(routeIdx int, kind string, destinationWeights []*api_networking_v1beta1.HTTPRouteDestination, checks *[]*models.IstioCheck) {
subsetCollitions := map[string][]int{}
for destWeightIdx, destinationWeight := range destinationWeights {
if destinationWeight == nil {
continue
}
if destinationWeight.Destination == nil {
return
}
fqdn := kubernetes.GetHost(destinationWeight.Destination.Host, route.VirtualService.Namespace, route.Namespaces)
subset := destinationWeight.Destination.Subset
key := fmt.Sprintf("%s%s", fqdn.String(), subset)
collisions := subsetCollitions[key]
if collisions == nil {
collisions = make([]int, 0, len(destinationWeights))
}
subsetCollitions[key] = append(collisions, destWeightIdx)
}
appendSubsetDuplicity(routeIdx, kind, subsetCollitions, checks)
}
func (route RouteChecker) trackTcpTlsSubset(routeIdx int, kind string, destinationWeights []*api_networking_v1beta1.RouteDestination, checks *[]*models.IstioCheck) {
subsetCollitions := map[string][]int{}
for destWeightIdx, destinationWeight := range destinationWeights {
if destinationWeight == nil {
continue
}
if destinationWeight.Destination == nil {
return
}
fqdn := kubernetes.GetHost(destinationWeight.Destination.Host, route.VirtualService.Namespace, route.Namespaces)
subset := destinationWeight.Destination.Subset
key := fmt.Sprintf("%s%s", fqdn.String(), subset)
collisions := subsetCollitions[key]
if collisions == nil {
collisions = make([]int, 0, len(destinationWeights))
}
subsetCollitions[key] = append(collisions, destWeightIdx)
}
appendSubsetDuplicity(routeIdx, kind, subsetCollitions, checks)
}
func appendSubsetDuplicity(routeIdx int, kind string, collistionsMap map[string][]int, checks *[]*models.IstioCheck) {
for _, dups := range collistionsMap {
if len(dups) > 1 {
for _, dup := range dups {
path := fmt.Sprintf("spec/%s[%d]/route[%d]/host", kind, routeIdx, dup)
validation := models.Build("virtualservices.route.repeatedsubset", path)
*checks = append(*checks, &validation)
}
}
}
}