forked from kiali/kiali
/
istio_validations.go
336 lines (290 loc) · 11.5 KB
/
istio_validations.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
package business
import (
"fmt"
"sync"
v1 "k8s.io/api/core/v1"
"github.com/kiali/kiali/business/checkers"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/models"
"github.com/kiali/kiali/prometheus/internalmetrics"
)
type IstioValidationsService struct {
k8s kubernetes.IstioClientInterface
businessLayer *Layer
}
type ObjectChecker interface {
Check() models.IstioValidations
}
// GetValidations returns an IstioValidations object with all the checks found when running
// all the enabled checkers. If service is "" then the whole namespace is validated.
func (in *IstioValidationsService) GetValidations(namespace, service string) (models.IstioValidations, error) {
var err error
promtimer := internalmetrics.GetGoFunctionMetric("business", "IstioValidationsService", "GetValidations")
defer promtimer.ObserveNow(&err)
// Ensure the service or namespace exists.. do we need to block with this?
if service != "" {
if _, err := in.k8s.GetService(namespace, service); err != nil {
return nil, err
}
} else {
if _, err := in.k8s.GetNamespace(namespace); err != nil {
return nil, err
}
}
wg := sync.WaitGroup{}
errChan := make(chan error, 1)
var istioDetails kubernetes.IstioDetails
var services []v1.Service
var workloads models.WorkloadList
var gatewaysPerNamespace [][]kubernetes.IstioObject
var mtlsDetails kubernetes.MTLSDetails
var rbacDetails kubernetes.RBACDetails
wg.Add(6) // We need to add these here to make sure we don't execute wg.Wait() before scheduler has started goroutines
// NoServiceChecker is not necessary if we target a single service - those components with validation errors won't show up in the query
go in.fetchServices(&services, namespace, service, errChan, &wg)
// We fetch without target service as some validations will require full-namespace details
go in.fetchDetails(&istioDetails, namespace, errChan, &wg)
go in.fetchWorkloads(&workloads, namespace, errChan, &wg)
go in.fetchGatewaysPerNamespace(&gatewaysPerNamespace, errChan, &wg)
go in.fetchNonLocalmTLSConfigs(&mtlsDetails, namespace, errChan, &wg)
go in.fetchAuthorizationDetails(&rbacDetails, namespace, errChan, &wg)
wg.Wait()
close(errChan)
for e := range errChan {
if e != nil { // Check that default value wasn't returned
return nil, err
}
}
objectCheckers := in.getAllObjectCheckers(namespace, istioDetails, services, workloads, gatewaysPerNamespace, mtlsDetails, rbacDetails)
// Get group validations for same kind istio objects
return runObjectCheckers(objectCheckers), nil
}
func (in *IstioValidationsService) getAllObjectCheckers(namespace string, istioDetails kubernetes.IstioDetails, services []v1.Service, workloads models.WorkloadList, gatewaysPerNamespace [][]kubernetes.IstioObject, mtlsDetails kubernetes.MTLSDetails, rbacDetails kubernetes.RBACDetails) []ObjectChecker {
return []ObjectChecker{
checkers.VirtualServiceChecker{Namespace: namespace, DestinationRules: istioDetails.DestinationRules, VirtualServices: istioDetails.VirtualServices},
checkers.NoServiceChecker{Namespace: namespace, IstioDetails: &istioDetails, Services: services, WorkloadList: workloads, GatewaysPerNamespace: gatewaysPerNamespace, AuthorizationDetails: &rbacDetails},
checkers.DestinationRulesChecker{DestinationRules: istioDetails.DestinationRules, MTLSDetails: mtlsDetails},
checkers.GatewayChecker{GatewaysPerNamespace: gatewaysPerNamespace, Namespace: namespace},
checkers.MeshPolicyChecker{MeshPolicies: mtlsDetails.MeshPolicies, MTLSDetails: mtlsDetails},
checkers.PolicyChecker{Policies: mtlsDetails.Policies, MTLSDetails: mtlsDetails},
checkers.ServiceEntryChecker{ServiceEntries: istioDetails.ServiceEntries},
checkers.ServiceRoleBindChecker{RBACDetails: rbacDetails},
}
}
func (in *IstioValidationsService) GetIstioObjectValidations(namespace string, objectType string, object string) (models.IstioValidations, error) {
var err error
promtimer := internalmetrics.GetGoFunctionMetric("business", "IstioValidationsService", "GetIstioObjectValidations")
defer promtimer.ObserveNow(&err)
var istioDetails kubernetes.IstioDetails
var services []v1.Service
var workloads models.WorkloadList
var gatewaysPerNamespace [][]kubernetes.IstioObject
var mtlsDetails kubernetes.MTLSDetails
var rbacDetails kubernetes.RBACDetails
var objectCheckers []ObjectChecker
wg := sync.WaitGroup{}
errChan := make(chan error, 1)
// Get all the Istio objects from a Namespace and all gateways from every namespace
wg.Add(6)
go in.fetchDetails(&istioDetails, namespace, errChan, &wg)
go in.fetchServices(&services, namespace, "", errChan, &wg)
go in.fetchWorkloads(&workloads, namespace, errChan, &wg)
go in.fetchGatewaysPerNamespace(&gatewaysPerNamespace, errChan, &wg)
go in.fetchNonLocalmTLSConfigs(&mtlsDetails, namespace, errChan, &wg)
go in.fetchAuthorizationDetails(&rbacDetails, namespace, errChan, &wg)
wg.Wait()
noServiceChecker := checkers.NoServiceChecker{Namespace: namespace, IstioDetails: &istioDetails, Services: services, WorkloadList: workloads, GatewaysPerNamespace: gatewaysPerNamespace, AuthorizationDetails: &rbacDetails}
switch objectType {
case Gateways:
objectCheckers = []ObjectChecker{
checkers.GatewayChecker{GatewaysPerNamespace: gatewaysPerNamespace, Namespace: namespace},
}
case VirtualServices:
virtualServiceChecker := checkers.VirtualServiceChecker{Namespace: namespace, VirtualServices: istioDetails.VirtualServices, DestinationRules: istioDetails.DestinationRules}
objectCheckers = []ObjectChecker{noServiceChecker, virtualServiceChecker}
case DestinationRules:
destinationRulesChecker := checkers.DestinationRulesChecker{DestinationRules: istioDetails.DestinationRules, MTLSDetails: mtlsDetails}
objectCheckers = []ObjectChecker{noServiceChecker, destinationRulesChecker}
case MeshPolicies:
meshPoliciesChecker := checkers.MeshPolicyChecker{MeshPolicies: mtlsDetails.MeshPolicies, MTLSDetails: mtlsDetails}
objectCheckers = []ObjectChecker{meshPoliciesChecker}
case Policies:
policiesChecker := checkers.PolicyChecker{Policies: mtlsDetails.Policies, MTLSDetails: mtlsDetails}
objectCheckers = []ObjectChecker{policiesChecker}
case ServiceEntries:
serviceEntryChecker := checkers.ServiceEntryChecker{ServiceEntries: istioDetails.ServiceEntries}
objectCheckers = []ObjectChecker{serviceEntryChecker}
case Rules:
// Validations on Istio Rules are not yet in place
case Templates:
// Validations on Templates are not yet in place
// TODO Support subtypes
case Adapters:
// Validations on Adapters are not yet in place
// TODO Support subtypes
case QuotaSpecs:
// Validations on QuotaSpecs are not yet in place
case QuotaSpecBindings:
// Validations on QuotaSpecBindings are not yet in place
case ClusterRbacConfigs:
// Validations on ClusterRbacConfigs are not yet in place
case RbacConfigs:
// Validations on RbacConfigs are not yet in place
case ServiceRoles:
objectCheckers = []ObjectChecker{noServiceChecker}
case ServiceRoleBindings:
roleBindChecker := checkers.ServiceRoleBindChecker{RBACDetails: rbacDetails}
objectCheckers = []ObjectChecker{roleBindChecker}
default:
err = fmt.Errorf("Object type not found: %v", objectType)
}
close(errChan)
for e := range errChan {
if e != nil { // Check that default value wasn't returned
return nil, err
}
}
if objectCheckers == nil {
return models.IstioValidations{}, err
}
return runObjectCheckers(objectCheckers).FilterByKey(models.ObjectTypeSingular[objectType], object), nil
}
func runObjectCheckers(objectCheckers []ObjectChecker) models.IstioValidations {
objectTypeValidations := models.IstioValidations{}
// Run checks for each IstioObject type
for _, objectChecker := range objectCheckers {
objectTypeValidations.MergeValidations(objectChecker.Check())
}
return objectTypeValidations
}
// The following idea is used underneath: if errChan has at least one record, we'll effectively cancel the request (if scheduled in such order). On the other hand, if we can't
// write to the buffered errChan, we just ignore the error as select does not block even if channel is full. This is because a single error is enough to cancel the whole request.
func (in *IstioValidationsService) fetchGatewaysPerNamespace(gatewaysPerNamespace *[][]kubernetes.IstioObject, errChan chan error, wg *sync.WaitGroup) {
defer wg.Done()
if nss, err := in.businessLayer.Namespace.GetNamespaces(); err == nil {
gwss := make([][]kubernetes.IstioObject, len(nss))
for i := range nss {
gwss[i] = make([]kubernetes.IstioObject, 0)
}
*gatewaysPerNamespace = gwss
wg.Add(len(nss))
for i, ns := range nss {
go fetchNoEntry(&gwss[i], ns.Name, in.k8s.GetGateways, wg, errChan)
}
} else {
select {
case errChan <- err:
default:
}
}
}
func fetchNoEntry(rValue *[]kubernetes.IstioObject, namespace string, fetcher func(string) ([]kubernetes.IstioObject, error), wg *sync.WaitGroup, errChan chan error) {
defer wg.Done()
if len(errChan) == 0 {
fetched, err := fetcher(namespace)
*rValue = append(*rValue, fetched...)
if err != nil {
select {
case errChan <- err:
default:
}
}
}
}
func (in *IstioValidationsService) fetchServices(rValue *[]v1.Service, namespace, serviceName string, errChan chan error, wg *sync.WaitGroup) {
defer wg.Done()
if len(errChan) == 0 {
services, err := in.k8s.GetServices(namespace, nil)
if err != nil {
select {
case errChan <- err:
default:
}
} else {
*rValue = services
}
}
}
func (in *IstioValidationsService) fetchWorkloads(rValue *models.WorkloadList, namespace string, errChan chan error, wg *sync.WaitGroup) {
defer wg.Done()
if len(errChan) == 0 {
workloadList, err := in.businessLayer.Workload.GetWorkloadList(namespace)
if err != nil {
select {
case errChan <- err:
default:
}
} else {
*rValue = workloadList
}
}
}
func (in *IstioValidationsService) fetchDetails(rValue *kubernetes.IstioDetails, namespace string, errChan chan error, wg *sync.WaitGroup) {
defer wg.Done()
if len(errChan) == 0 {
istioDetails, err := in.k8s.GetIstioDetails(namespace, "")
if err != nil {
select {
case errChan <- err:
default:
}
} else {
*rValue = *istioDetails
}
}
}
func (in *IstioValidationsService) fetchNonLocalmTLSConfigs(mtlsDetails *kubernetes.MTLSDetails, namespace string, errChan chan error, wg *sync.WaitGroup) {
defer wg.Done()
if len(errChan) > 0 {
return
}
wg.Add(2)
go func(details *kubernetes.MTLSDetails) {
defer wg.Done()
meshPolicies, err := in.k8s.GetMeshPolicies(config.Get().IstioNamespace)
if err != nil {
errChan <- err
} else {
details.MeshPolicies = meshPolicies
}
}(mtlsDetails)
go func(details *kubernetes.MTLSDetails) {
defer wg.Done()
policies, err := in.k8s.GetPolicies(namespace)
if err != nil {
errChan <- err
} else {
details.Policies = policies
}
}(mtlsDetails)
namespaces, err := in.businessLayer.Namespace.GetNamespaces()
if err != nil {
errChan <- err
return
}
nsNames := make([]string, 0, len(namespaces))
for _, ns := range namespaces {
nsNames = append(nsNames, ns.Name)
}
destinationRules, err := in.businessLayer.TLS.getAllDestinationRules(nsNames)
if err != nil {
errChan <- err
} else {
mtlsDetails.DestinationRules = destinationRules
}
}
func (in *IstioValidationsService) fetchAuthorizationDetails(rValue *kubernetes.RBACDetails, namespace string, errChan chan error, wg *sync.WaitGroup) {
defer wg.Done()
if len(errChan) == 0 {
authDetails, err := in.k8s.GetAuthorizationDetails(namespace)
if err != nil {
select {
case errChan <- err:
default:
}
} else {
*rValue = *authDetails
}
}
}