-
Notifications
You must be signed in to change notification settings - Fork 474
/
no_dest_checker.go
236 lines (209 loc) · 7.57 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
225
226
227
228
229
230
231
232
233
234
235
236
package destinationrules
import (
"strconv"
networking_v1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
"k8s.io/apimachinery/pkg/labels"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/models"
)
type NoDestinationChecker struct {
Namespaces models.Namespaces
WorkloadsPerNamespace map[string]models.WorkloadList
DestinationRule *networking_v1beta1.DestinationRule
VirtualServices []*networking_v1beta1.VirtualService
ServiceEntries []*networking_v1beta1.ServiceEntry
RegistryServices []*kubernetes.RegistryService
PolicyAllowAny bool
}
// 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)
labelValidations := make([]*models.IstioCheck, 0)
namespace := n.DestinationRule.Namespace
fqdn := kubernetes.GetHost(n.DestinationRule.Spec.Host, namespace, n.Namespaces.GetNames())
// Testing Kubernetes Services + Istio ServiceEntries + Istio Runtime Registry (cross namespace)
if !n.hasMatchingService(fqdn, namespace) {
validation := models.Build("destinationrules.nodest.matchingregistry", "spec/host")
if n.PolicyAllowAny {
validation.Severity = models.WarningSeverity
}
valid = false
validations = append(validations, &validation)
} else if len(n.DestinationRule.Spec.Subsets) > 0 {
// Check that each subset has a matching workload somewhere..
hasLabel := false
for i, subset := range n.DestinationRule.Spec.Subsets {
if len(subset.Labels) > 0 {
if !n.hasMatchingWorkload(fqdn, 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 {
hasLabel = true
}
} else {
validation := models.Build("destinationrules.nodest.subsetnolabels",
"spec/subsets["+strconv.Itoa(i)+"]")
labelValidations = append(labelValidations, &validation)
// Not changing valid value, if other subset is on error, a valid = false has priority
}
}
for _, v := range labelValidations {
if hasLabel {
v.Severity = models.Unknown
}
validations = append(validations, v)
}
}
return validations, valid
}
func (n NoDestinationChecker) hasMatchingWorkload(host kubernetes.Host, subsetLabels map[string]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)
var selectors map[string]string
// Find the correct service
for _, s := range n.RegistryServices {
if s.Attributes.Name == localSvc && s.Attributes.Namespace == localNs {
selectors = s.Attributes.LabelSelectors
break
}
}
subsetLabelSet := labels.Set(subsetLabels)
subsetSelector := labels.SelectorFromSet(subsetLabelSet)
// Check workloads
if len(selectors) != 0 {
selector := labels.SelectorFromSet(labels.Set(selectors))
for _, wl := range n.WorkloadsPerNamespace[localNs].Workloads {
wlLabelSet := labels.Set(wl.Labels)
if selector.Matches(wlLabelSet) {
if subsetSelector.Matches(wlLabelSet) {
return true
}
}
}
} else {
// Check Service Entries
for _, se := range n.ServiceEntries {
for _, ep := range se.Spec.Endpoints {
epLabelSet := labels.Set(ep.Labels)
if subsetSelector.Matches(epLabelSet) {
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.WorkloadsPerNamespace[localNs].GetLabels()); matches {
return matches
}
}
// Check ServiceEntries
if kubernetes.HasMatchingServiceEntries(host.String(), kubernetes.ServiceEntryHostnames(n.ServiceEntries)) {
return true
}
// Use RegistryService to check destinations that may not be covered with previous check
// i.e. Multi-cluster or Federation validations
if kubernetes.HasMatchingRegistryService(itemNamespace, host.String(), n.RegistryServices) {
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_v1beta1.VirtualService, bool) {
vss := make([]*networking_v1beta1.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.Namespaces.GetNames())
vsHost := kubernetes.GetHost(virtualServiceHost, virtualService.Namespace, n.Namespaces.GetNames())
// Host could be in another namespace (FQDN)
if kubernetes.FilterByHost(vsHost.String(), vsHost.Namespace, 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.Namespaces.GetNames())
vsHost := kubernetes.GetHost(virtualServiceHost, virtualService.Namespace, n.Namespaces.GetNames())
// Host could be in another namespace (FQDN)
if kubernetes.FilterByHost(vsHost.String(), vsHost.Namespace, 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.Namespaces.GetNames())
vsHost := kubernetes.GetHost(virtualServiceHost, virtualService.Namespace, n.Namespaces.GetNames())
// Host could be in another namespace (FQDN)
if kubernetes.FilterByHost(vsHost.String(), vsHost.Namespace, drHost.Service, drHost.Namespace) && subset == virtualServiceSubset {
vss = append(vss, virtualService)
}
}
}
}
}
}
return vss, len(vss) > 0
}