-
Notifications
You must be signed in to change notification settings - Fork 505
/
Copy pathno_host_checker.go
72 lines (61 loc) · 2.97 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
package k8shttproutes
import (
"fmt"
"strings"
k8s_networking_v1 "sigs.k8s.io/gateway-api/apis/v1"
k8s_networking_v1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/models"
)
type NoHostChecker struct {
K8sHTTPRoute *k8s_networking_v1.HTTPRoute
K8sReferenceGrants []*k8s_networking_v1beta1.ReferenceGrant
Namespaces models.Namespaces
RegistryServices []*kubernetes.RegistryService
}
func (n NoHostChecker) Check() ([]*models.IstioCheck, bool) {
validations := make([]*models.IstioCheck, 0)
valid := true
for i, ref := range n.K8sHTTPRoute.Spec.ParentRefs {
if ref.Kind == nil || string(*ref.Kind) != kubernetes.ServiceType {
continue
}
valid = n.checkReference(ref.Namespace, ref.Name, &validations, fmt.Sprintf("spec/parentRefs[%d]/name", i)) && valid
}
for k, httpRoute := range n.K8sHTTPRoute.Spec.Rules {
for i, ref := range httpRoute.BackendRefs {
if ref.Kind == nil || string(*ref.Kind) != kubernetes.ServiceType {
continue
}
valid = n.checkReference(ref.Namespace, ref.Name, &validations, fmt.Sprintf("spec/rules[%d]/backendRefs[%d]/name", k, i)) && valid
}
}
return validations, valid
}
func (n NoHostChecker) checkReference(refNamespace *k8s_networking_v1.Namespace, refName k8s_networking_v1.ObjectName, validations *[]*models.IstioCheck, location string) bool {
namespace := n.K8sHTTPRoute.Namespace
if refNamespace != nil && string(*refNamespace) != "" {
namespace = string(*refNamespace)
}
fqdn := kubernetes.GetHost(string(refName), namespace, n.Namespaces.GetNames())
//service name should not be set in fqdn format
// if the http route is referencing to a service from the same namespace, then service should exist there
// if the http route is referencing to a service from other namespace, then a ReferenceGrant should exist to cross namespace reference, and the service should exist in remote namespace
if strings.Contains(string(refName), ".") ||
(namespace == n.K8sHTTPRoute.Namespace && !n.checkDestination(fqdn.String(), namespace)) ||
(namespace != n.K8sHTTPRoute.Namespace && (!n.checkReferenceGrant(n.K8sHTTPRoute.Namespace, namespace) || !n.checkDestination(fqdn.String(), namespace))) {
validation := models.Build("k8sroutes.nohost.namenotfound", location)
*validations = append(*validations, &validation)
return false
}
return true
}
func (n NoHostChecker) checkDestination(sHost string, itemNamespace string) bool {
// Use RegistryService to check destinations that may not be covered with previous check
// i.e. Multi-cluster or Federation validations
return kubernetes.HasMatchingRegistryService(itemNamespace, sHost, n.RegistryServices)
}
func (n NoHostChecker) checkReferenceGrant(fromNamespace string, toNamespace string) bool {
// Use ReferenceGrant objects to check if cross namespace reference exists
return kubernetes.HasMatchingReferenceGrant(fromNamespace, toNamespace, kubernetes.K8sHTTPRouteType, kubernetes.ServiceType, n.K8sReferenceGrants)
}