Repository navigation
Expand file tree
/
Copy pathsingle_host_checker.go
More file actions
144 lines (125 loc) · 5.04 KB
/
Copy pathsingle_host_checker.go
File metadata and controls
144 lines (125 loc) · 5.04 KB
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
package virtualservices
import (
networking_v1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/models"
)
type SingleHostChecker struct {
Namespace string
Namespaces models.Namespaces
VirtualServices []networking_v1alpha3.VirtualService
ExportedVirtualServices []networking_v1alpha3.VirtualService
}
func (s SingleHostChecker) Check() models.IstioValidations {
hostCounter := make(map[string]map[string]map[string]map[string][]*networking_v1alpha3.VirtualService)
validations := models.IstioValidations{}
for _, vs := range append(s.VirtualServices, s.ExportedVirtualServices...) {
for _, host := range s.getHosts(vs) {
storeHost(hostCounter, vs, host)
}
}
for _, gateways := range hostCounter {
for _, clusterCounter := range gateways {
for _, namespaceCounter := range clusterCounter {
for _, serviceCounter := range namespaceCounter {
isNamespaceWildcard := len(namespaceCounter["*"]) > 0
targetSameHost := len(serviceCounter) > 1
otherServiceHosts := len(namespaceCounter) > 1
for _, virtualService := range serviceCounter {
// Marking virtualService as invalid if:
// - there is more than one virtual service per a host
// - there is one virtual service with wildcard and there are other virtual services pointing
// a host for that namespace
if targetSameHost {
// Reference everything within serviceCounter
multipleVirtualServiceCheck(*virtualService, validations, serviceCounter)
}
if isNamespaceWildcard && otherServiceHosts {
// Reference the * or in case of * the other hosts inside namespace
// or other stars
refs := make([]*networking_v1alpha3.VirtualService, 0, len(namespaceCounter))
for _, serviceCounter := range namespaceCounter {
refs = append(refs, serviceCounter...)
}
multipleVirtualServiceCheck(*virtualService, validations, refs)
}
}
}
}
}
}
return validations
}
func multipleVirtualServiceCheck(virtualService networking_v1alpha3.VirtualService, validations models.IstioValidations, references []*networking_v1alpha3.VirtualService) {
virtualServiceName := virtualService.Name
key := models.IstioValidationKey{Name: virtualServiceName, Namespace: virtualService.Namespace, ObjectType: "virtualservice"}
checks := models.Build("virtualservices.singlehost", "spec/hosts")
rrValidation := &models.IstioValidation{
Name: virtualServiceName,
ObjectType: "virtualservice",
Valid: true,
Checks: []*models.IstioCheck{
&checks,
},
References: make([]models.IstioValidationKey, 0, len(references)),
}
for _, ref := range references {
ref := *ref
refKey := models.IstioValidationKey{Name: ref.Name, Namespace: ref.Namespace, ObjectType: "virtualservice"}
if refKey != key {
rrValidation.References = append(rrValidation.References, refKey)
}
}
validations.MergeValidations(models.IstioValidations{key: rrValidation})
}
func storeHost(hostCounter map[string]map[string]map[string]map[string][]*networking_v1alpha3.VirtualService, vs networking_v1alpha3.VirtualService, host kubernetes.Host) {
vsList := []*networking_v1alpha3.VirtualService{&vs}
gwList := vs.Spec.Gateways
if len(gwList) == 0 {
gwList = []string{"no-gateway"}
}
if !host.CompleteInput {
host.Cluster = config.Get().ExternalServices.Istio.IstioIdentityDomain
host.Namespace = vs.Namespace
}
for _, gw := range gwList {
if hostCounter[gw] == nil {
hostCounter[gw] = map[string]map[string]map[string][]*networking_v1alpha3.VirtualService{
host.Cluster: {
host.Namespace: {
host.Service: vsList,
},
},
}
} else if hostCounter[gw][host.Cluster] == nil {
hostCounter[gw][host.Cluster] = map[string]map[string][]*networking_v1alpha3.VirtualService{
host.Namespace: {
host.Service: vsList,
},
}
} else if hostCounter[gw][host.Cluster][host.Namespace] == nil {
hostCounter[gw][host.Cluster][host.Namespace] = map[string][]*networking_v1alpha3.VirtualService{
host.Service: vsList,
}
} else if _, ok := hostCounter[gw][host.Cluster][host.Namespace][host.Service]; !ok {
hostCounter[gw][host.Cluster][host.Namespace][host.Service] = vsList
} else {
hostCounter[gw][host.Cluster][host.Namespace][host.Service] = append(hostCounter[gw][host.Cluster][host.Namespace][host.Service], &vs)
}
}
}
func (s SingleHostChecker) getHosts(virtualService networking_v1alpha3.VirtualService) []kubernetes.Host {
namespace, clusterName := virtualService.Namespace, virtualService.ClusterName
if clusterName == "" {
clusterName = config.Get().ExternalServices.Istio.IstioIdentityDomain
}
if len(virtualService.Spec.Hosts) == 0 {
return []kubernetes.Host{}
}
targetHosts := make([]kubernetes.Host, 0, len(virtualService.Spec.Hosts))
for _, hostName := range virtualService.Spec.Hosts {
targetHosts = append(targetHosts, kubernetes.GetHost(hostName, namespace, clusterName, s.Namespaces.GetNames()))
}
return targetHosts
}