func()

in pkg/providers/gateway/validator.go [180:257]


func (v *Validator) validateParentRef(r *commonRoute) ([]*types.ListenerConf, error) {
	var matchedListeners []*types.ListenerConf
	for _, parentRef := range r.parentRefs {
		listeners, err := v.getListenersConf(r, parentRef)
		if err != nil {
			return nil, err
		}

		// filter listener by ParentRef
		for _, listenerConf := range listeners {

			if listenerConf.Protocol != r.routeProtocol {
				continue
			}
			if !listenerConf.IsAllowedKind(r.routeGroupKind) {
				// TODO: set the “ResolvedRefs” condition to False for this Listener with the “InvalidRouteKinds” reason.
				continue
			}
			if r.isHTTPProtocol() || r.isHTTPSProtocol() {
				if listenerConf.HasHostname() && len(r.routeHostnames) != 0 {
					if !listenerConf.IsHostnameMatch(r.routeHostnames) {
						continue
					}
				}
			}

			// match listener by AllowRoute.Namespaces
			switch *listenerConf.RouteNamespace.From {
			case gatewayv1beta1.NamespacesFromSame:
				if r.routeNamespace != listenerConf.Namespace {
					continue
				}
			case gatewayv1beta1.NamespacesFromSelector:
				// get listener namespace with selector labeled namespace
				selector, err := metav1.LabelSelectorAsSelector(listenerConf.RouteNamespace.Selector)
				if err != nil {
					log.Errorw("convert Selector failed",
						zap.Error(err),
						zap.Any("Object", listenerConf.RouteNamespace.Selector),
					)
					return nil, err
				}
				allowedNamespaces := &corev1.NamespaceList{}
				err = v.provider.runtimeClient.List(
					context.TODO(), allowedNamespaces,
					&runtimeclient.ListOptions{LabelSelector: selector},
				)
				if err != nil {
					log.Errorw("list parent namespace failed",
						zap.Error(err),
						zap.Any("Object", *listenerConf.RouteNamespace.From),
					)
					return nil, err
				}
				namespaceMatched := false
				for _, allowedNamespace := range allowedNamespaces.Items {
					if string(allowedNamespace.Name) == r.routeNamespace {
						namespaceMatched = true
						break
					}
				}
				if !namespaceMatched {
					continue
				}
			}

			matchedListeners = append(matchedListeners, listenerConf)
		}
	}
	if len(matchedListeners) == 0 {
		log.Errorw("no listeners referenced by ParentRefs",
			zap.Any("listeners", v.provider.listeners),
			zap.Any("route", r),
		)
		return nil, fmt.Errorf("no listeners referenced by ParentRefs")
	}
	return matchedListeners, nil
}