-
Notifications
You must be signed in to change notification settings - Fork 474
/
no_k8sgateway_checker.go
55 lines (45 loc) · 2.02 KB
/
no_k8sgateway_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
package k8shttproutes
import (
"fmt"
k8s_networking_v1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/models"
)
type NoK8sGatewayChecker struct {
K8sHTTPRoute *k8s_networking_v1beta1.HTTPRoute
GatewayNames map[string]struct{}
}
// Check validates that the HTTPRoute is pointing to an existing Gateway
func (s NoK8sGatewayChecker) Check() ([]*models.IstioCheck, bool) {
validations := make([]*models.IstioCheck, 0)
valid := s.ValidateHTTPRouteGateways(&validations)
return validations, valid
}
// ValidateHTTPRouteGateways checks all HTTPRoute gateways and checks that they're found from the given list of gatewayNames. Also return index of missing gatways to show clearer error path in editor
func (s NoK8sGatewayChecker) ValidateHTTPRouteGateways(validations *[]*models.IstioCheck) bool {
valid := true
if len(s.K8sHTTPRoute.Spec.ParentRefs) > 0 {
for index, parentRef := range s.K8sHTTPRoute.Spec.ParentRefs {
if string(parentRef.Name) != "" && string(*parentRef.Kind) == kubernetes.K8sActualGatewayType && string(*parentRef.Group) == kubernetes.K8sNetworkingGroupVersionV1Beta1.Group {
namespace := s.K8sHTTPRoute.Namespace
if parentRef.Namespace != nil && string(*parentRef.Namespace) != "" {
namespace = string(*parentRef.Namespace)
}
valid = s.checkGateway(string(parentRef.Name), namespace, validations, fmt.Sprintf("spec/parentRefs[%d]/name/%s", index, string(parentRef.Name))) && valid
}
}
}
return valid
}
func (s NoK8sGatewayChecker) checkGateway(name, namespace string, validations *[]*models.IstioCheck, location string) bool {
hostname := kubernetes.ParseGatewayAsHost(name, namespace)
for gw := range s.GatewayNames {
gwHostname := kubernetes.ParseHost(gw, namespace)
if found := kubernetes.FilterByHost(hostname.String(), hostname.Namespace, gw, gwHostname.Namespace); found {
return true
}
}
validation := models.Build("k8shttproutes.nok8sgateway", location)
*validations = append(*validations, &validation)
return false
}