github.com/kiali/kiali@v1.84.0/business/checkers/k8shttproutes/no_k8sgateway_checker.go (about)

     1  package k8shttproutes
     2  
     3  import (
     4  	"fmt"
     5  
     6  	k8s_networking_v1 "sigs.k8s.io/gateway-api/apis/v1"
     7  
     8  	"github.com/kiali/kiali/kubernetes"
     9  	"github.com/kiali/kiali/models"
    10  )
    11  
    12  type NoK8sGatewayChecker struct {
    13  	K8sHTTPRoute *k8s_networking_v1.HTTPRoute
    14  	GatewayNames map[string]struct{}
    15  }
    16  
    17  // Check validates that the HTTPRoute is pointing to an existing Gateway
    18  func (s NoK8sGatewayChecker) Check() ([]*models.IstioCheck, bool) {
    19  	validations := make([]*models.IstioCheck, 0)
    20  
    21  	valid := s.ValidateHTTPRouteGateways(&validations)
    22  
    23  	return validations, valid
    24  }
    25  
    26  // 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
    27  func (s NoK8sGatewayChecker) ValidateHTTPRouteGateways(validations *[]*models.IstioCheck) bool {
    28  	valid := true
    29  
    30  	if len(s.K8sHTTPRoute.Spec.ParentRefs) > 0 {
    31  		for index, parentRef := range s.K8sHTTPRoute.Spec.ParentRefs {
    32  			if string(parentRef.Name) != "" && string(*parentRef.Kind) == kubernetes.K8sActualGatewayType && string(*parentRef.Group) == kubernetes.K8sNetworkingGroupVersionV1.Group {
    33  				namespace := s.K8sHTTPRoute.Namespace
    34  				if parentRef.Namespace != nil && string(*parentRef.Namespace) != "" {
    35  					namespace = string(*parentRef.Namespace)
    36  				}
    37  				valid = s.checkGateway(string(parentRef.Name), namespace, validations, fmt.Sprintf("spec/parentRefs[%d]/name/%s", index, string(parentRef.Name))) && valid
    38  			}
    39  		}
    40  	}
    41  	return valid
    42  }
    43  
    44  func (s NoK8sGatewayChecker) checkGateway(name, namespace string, validations *[]*models.IstioCheck, location string) bool {
    45  	hostname := kubernetes.ParseGatewayAsHost(name, namespace)
    46  	for gw := range s.GatewayNames {
    47  		gwHostname := kubernetes.ParseHost(gw, namespace)
    48  		if found := kubernetes.FilterByHost(hostname.String(), hostname.Namespace, gw, gwHostname.Namespace); found {
    49  			return true
    50  		}
    51  	}
    52  	validation := models.Build("k8shttproutes.nok8sgateway", location)
    53  	*validations = append(*validations, &validation)
    54  	return false
    55  }