forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no_host_checker.go
79 lines (71 loc) · 2.23 KB
/
no_host_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
package virtual_services
import (
"fmt"
"strings"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/models"
)
type NoHostChecker struct {
Namespace string
ServiceNames []string
VirtualService kubernetes.IstioObject
ServiceEntryHosts map[string][]string
}
func (n NoHostChecker) Check() ([]*models.IstioCheck, bool) {
validations := make([]*models.IstioCheck, 0)
routeProtocols := []string{"http", "tcp", "tls"}
countOfDefinedProtocols := 0
for _, protocol := range routeProtocols {
if prot, ok := n.VirtualService.GetSpec()[protocol]; ok {
countOfDefinedProtocols++
if aHttp, ok := prot.([]interface{}); ok {
for k, httpRoute := range aHttp {
if mHttpRoute, ok := httpRoute.(map[string]interface{}); ok {
if route, ok := mHttpRoute["route"]; ok {
if aDestinationWeight, ok := route.([]interface{}); ok {
for i, destination := range aDestinationWeight {
if !n.checkDestination(destination, protocol) {
validation := models.Build("virtualservices.nohost.hostnotfound",
fmt.Sprintf("spec/%s[%d]/route[%d]/destination/host", protocol, k, i))
validations = append(validations, &validation)
}
}
}
}
}
}
}
}
}
if countOfDefinedProtocols < 1 {
validation := models.Build("virtualservices.nohost.invalidprotocol", "")
validations = append(validations, &validation)
}
return validations, len(validations) == 0
}
func (n NoHostChecker) checkDestination(destination interface{}, protocol string) bool {
if mDestination, ok := destination.(map[string]interface{}); ok {
if destinationW, ok := mDestination["destination"]; ok {
if mDestinationW, ok := destinationW.(map[string]interface{}); ok {
if host, ok := mDestinationW["host"]; ok {
if sHost, ok := host.(string); ok {
for _, service := range n.ServiceNames {
if kubernetes.FilterByHost(sHost, service, n.Namespace) {
return true
}
}
if protocols, found := n.ServiceEntryHosts[sHost]; found {
// We have ServiceEntry to check
for _, prot := range protocols {
if prot == strings.ToLower(protocol) {
return true
}
}
}
}
}
}
}
}
return false
}