-
Notifications
You must be signed in to change notification settings - Fork 473
/
no_dest_checker.go
224 lines (195 loc) · 7.18 KB
/
no_dest_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
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
package destinationrules
import (
"strconv"
"strings"
networking_v1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3"
"k8s.io/apimachinery/pkg/labels"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/models"
)
type NoDestinationChecker struct {
Namespace string
Namespaces models.Namespaces
WorkloadList models.WorkloadList
DestinationRule networking_v1alpha3.DestinationRule
VirtualServices []networking_v1alpha3.VirtualService
ServiceEntries map[string][]string
ServiceList models.ServiceList
RegistryStatus []*kubernetes.RegistryStatus
}
// Check parses the DestinationRule definitions and verifies that they point to an existing service, including any subset definitions
func (n NoDestinationChecker) Check() ([]*models.IstioCheck, bool) {
valid := true
validations := make([]*models.IstioCheck, 0)
fqdn := kubernetes.GetHost(n.DestinationRule.Spec.Host, n.DestinationRule.Namespace, n.DestinationRule.ClusterName, n.Namespaces.GetNames())
// Testing Kubernetes Services + Istio ServiceEntries + Istio Runtime Registry (cross namespace)
if !n.hasMatchingService(fqdn, n.DestinationRule.Namespace) {
validation := models.Build("destinationrules.nodest.matchingregistry", "spec/host")
valid = false
validations = append(validations, &validation)
} else if len(n.DestinationRule.Spec.Subsets) > 0 {
// Check that each subset has a matching workload somewhere..
for i, subset := range n.DestinationRule.Spec.Subsets {
if len(subset.Labels) > 0 {
if !n.hasMatchingWorkload(fqdn.Service, subset.Labels) {
validation := models.Build("destinationrules.nodest.subsetlabels",
"spec/subsets["+strconv.Itoa(i)+"]")
if n.isSubsetReferenced(n.DestinationRule.Spec.Host, subset.Name) {
valid = false
} else {
validation.Severity = models.Unknown
}
validations = append(validations, &validation)
}
} else {
validation := models.Build("destinationrules.nodest.subsetnolabels",
"spec/subsets["+strconv.Itoa(i)+"]")
validations = append(validations, &validation)
// Not changing valid value, if other subset is on error, a valid = false has priority
}
}
}
return validations, valid
}
func (n NoDestinationChecker) hasMatchingWorkload(service string, subsetLabels map[string]string) bool {
// Check wildcard hosts - needs to match "*" and "*.suffix" also..
if strings.HasPrefix(service, "*") {
return true
}
// Covering 'servicename.namespace' host format scenario
svc := service
svcParts := strings.Split(service, ".")
if len(svcParts) > 1 {
svc = svcParts[0]
}
var selectors map[string]string
// Find the correct service
for _, s := range n.ServiceList.Services {
if s.Name == svc {
selectors = s.Selector
}
}
// Check workloads
if len(selectors) == 0 {
return false
}
selector := labels.SelectorFromSet(labels.Set(selectors))
subsetLabelSet := labels.Set(subsetLabels)
subsetSelector := labels.SelectorFromSet(subsetLabelSet)
for _, wl := range n.WorkloadList.Workloads {
wlLabelSet := labels.Set(wl.Labels)
if selector.Matches(wlLabelSet) {
if subsetSelector.Matches(wlLabelSet) {
return true
}
}
}
return false
}
func (n NoDestinationChecker) hasMatchingService(host kubernetes.Host, itemNamespace string) bool {
// Check wildcard hosts - needs to match "*" and "*.suffix" also..
if host.IsWildcard() {
return true
}
// Covering 'servicename.namespace' host format scenario
localSvc, localNs := kubernetes.ParseTwoPartHost(host)
if localNs == itemNamespace {
// Check Workloads
if matches := kubernetes.HasMatchingWorkloads(localSvc, n.WorkloadList.GetLabels()); matches {
return matches
}
// Check ServiceNames
if matches := n.ServiceList.HasMatchingServices(localSvc); matches {
return matches
}
}
// Check ServiceEntries
if kubernetes.HasMatchingServiceEntries(host.String(), n.ServiceEntries) {
return true
}
// Use RegistryStatus to check destinations that may not be covered with previous check
// i.e. Multi-cluster or Federation validations
if kubernetes.HasMatchingRegistryStatus(host.String(), n.RegistryStatus) {
return true
}
return false
}
func (n NoDestinationChecker) isSubsetReferenced(host string, subset string) bool {
virtualServices, ok := n.getVirtualServices(host, subset)
if ok && len(virtualServices) > 0 {
return true
}
return false
}
func (n NoDestinationChecker) getVirtualServices(virtualServiceHost string, virtualServiceSubset string) ([]networking_v1alpha3.VirtualService, bool) {
vss := make([]networking_v1alpha3.VirtualService, 0, len(n.VirtualServices))
for _, virtualService := range n.VirtualServices {
if len(virtualService.Spec.Http) > 0 {
for _, httpRoute := range virtualService.Spec.Http {
if httpRoute == nil {
continue
}
if len(httpRoute.Route) > 0 {
for _, dest := range httpRoute.Route {
if dest == nil || dest.Destination == nil {
continue
}
host := dest.Destination.Host
subset := dest.Destination.Subset
drHost := kubernetes.GetHost(host, n.DestinationRule.Namespace, n.DestinationRule.ClusterName, n.Namespaces.GetNames())
vsHost := kubernetes.GetHost(virtualServiceHost, virtualService.Namespace, virtualService.ClusterName, n.Namespaces.GetNames())
// TODO Host could be in another namespace (FQDN)
if kubernetes.FilterByHost(vsHost.String(), drHost.Service, drHost.Namespace) && subset == virtualServiceSubset {
vss = append(vss, virtualService)
}
}
}
}
}
if len(virtualService.Spec.Tcp) > 0 {
for _, tcpRoute := range virtualService.Spec.Tcp {
if tcpRoute == nil {
continue
}
if len(tcpRoute.Route) > 0 {
for _, dest := range tcpRoute.Route {
if dest == nil || dest.Destination == nil {
continue
}
host := dest.Destination.Host
subset := dest.Destination.Subset
drHost := kubernetes.GetHost(host, n.DestinationRule.Namespace, n.DestinationRule.ClusterName, n.Namespaces.GetNames())
vsHost := kubernetes.GetHost(virtualServiceHost, virtualService.Namespace, virtualService.ClusterName, n.Namespaces.GetNames())
// TODO Host could be in another namespace (FQDN)
if kubernetes.FilterByHost(vsHost.String(), drHost.Service, drHost.Namespace) && subset == virtualServiceSubset {
vss = append(vss, virtualService)
}
}
}
}
}
if len(virtualService.Spec.Tls) > 0 {
for _, tlsRoute := range virtualService.Spec.Tls {
if tlsRoute == nil {
continue
}
if len(tlsRoute.Route) > 0 {
for _, dest := range tlsRoute.Route {
if dest == nil || dest.Destination == nil {
continue
}
host := dest.Destination.Host
subset := dest.Destination.Subset
drHost := kubernetes.GetHost(host, n.DestinationRule.Namespace, n.DestinationRule.ClusterName, n.Namespaces.GetNames())
vsHost := kubernetes.GetHost(virtualServiceHost, virtualService.Namespace, virtualService.ClusterName, n.Namespaces.GetNames())
// TODO Host could be in another namespace (FQDN)
if kubernetes.FilterByHost(vsHost.String(), drHost.Service, drHost.Namespace) && subset == virtualServiceSubset {
vss = append(vss, virtualService)
}
}
}
}
}
}
return vss, len(vss) > 0
}