Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AV-206859 Setting ResolvedRef Condition in HTTProute #1558

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ako-gateway-api/nodes/avi_model_l4_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func (o *AviObjectGraph) ProcessL4Routes(key string, routeModel RouteModel, parentNsName string) {
for _, rule := range routeModel.ParseRouteConfig().Rules {
for _, rule := range routeModel.ParseRouteConfig(key).Rules {
parentNode := o.GetAviEvhVS()
// create L4 policyset per rule
o.BuildL4PolicySet(key, parentNode[0], routeModel, rule)
Expand Down
3 changes: 2 additions & 1 deletion ako-gateway-api/nodes/avi_model_l7_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (o *AviObjectGraph) AddDefaultHTTPPolicySet(key string) {
}

func (o *AviObjectGraph) ProcessL7Routes(key string, routeModel RouteModel, parentNsName string, childVSes map[string]struct{}, fullsync bool) {
httpRouteConfig := routeModel.ParseRouteConfig()
httpRouteConfig := routeModel.ParseRouteConfig(key)
noHostsOnRoute := len(httpRouteConfig.Hosts) == 0
httpRouteRules := httpRouteConfig.Rules
if noHostsOnRoute {
Expand Down Expand Up @@ -244,6 +244,7 @@ func (o *AviObjectGraph) BuildPGPool(key, parentNsName string, childVsNode *node
Name: PGName,
Tenant: lib.GetTenant(),
}

for _, httpbackend := range rule.Backends {
poolName := akogatewayapilib.GetPoolName(parentNs, parentName,
routeModel.GetNamespace(), routeModel.GetName(),
Expand Down
24 changes: 20 additions & 4 deletions ako-gateway-api/nodes/route_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"

akogatewayapilib "github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/ako-gateway-api/lib"
akogatewayapistatus "github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/ako-gateway-api/status"
"github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/internal/lib"
)

Expand All @@ -30,7 +31,7 @@ type RouteModel interface {
GetNamespace() string
GetType() string
GetSpec() interface{}
ParseRouteConfig() *RouteConfig
ParseRouteConfig(key string) *RouteConfig
Exists() bool
GetParents() sets.Set[string]
}
Expand Down Expand Up @@ -105,6 +106,7 @@ type Backend struct {
Namespace string
Port int32
Weight int32
Kind string
}

type HTTPBackend struct {
Expand Down Expand Up @@ -162,7 +164,7 @@ func (hr *httpRoute) GetSpec() interface{} {
return hr.spec
}

func (hr *httpRoute) ParseRouteConfig() *RouteConfig {
func (hr *httpRoute) ParseRouteConfig(key string) *RouteConfig {
if hr.routeConfig != nil {
return hr.routeConfig
}
Expand All @@ -172,7 +174,7 @@ func (hr *httpRoute) ParseRouteConfig() *RouteConfig {
for i := range hr.spec.Hostnames {
routeConfig.Hosts[i] = string(hr.spec.Hostnames[i])
}

var resolvedRefCondition akogatewayapistatus.Condition
routeConfig.Rules = make([]*Rule, 0, len(hr.spec.Rules))
for _, rule := range hr.spec.Rules {
routeConfigRule := &Rule{}
Expand Down Expand Up @@ -282,6 +284,7 @@ func (hr *httpRoute) ParseRouteConfig() *RouteConfig {
}
routeConfigRule.Filters = append(routeConfigRule.Filters, filter)
}
hasInvalidBackend := false
for _, ruleBackend := range rule.BackendRefs {
httpBackend := &HTTPBackend{}
backend := &Backend{}
Expand All @@ -295,16 +298,29 @@ func (hr *httpRoute) ParseRouteConfig() *RouteConfig {
//Default 0
backend.Port = int32(*ruleBackend.Port)
}
if ruleBackend.BackendRef.Kind != nil {
backend.Kind = string(*ruleBackend.Kind)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we by default add Services?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do not add anything for Kind, as in if we keep kind as "", by default Service gets added as kind

backend.Weight = 1
if ruleBackend.Weight != nil {
backend.Weight = *ruleBackend.Weight
}
httpBackend.Backend = backend
routeConfigRule.Backends = append(routeConfigRule.Backends, httpBackend)
isValidBackend, resolvedRefConditionforBackend := validateBackendReference(key, *backend, hr)
if isValidBackend {
routeConfigRule.Backends = append(routeConfigRule.Backends, httpBackend)
if !hasInvalidBackend {
resolvedRefCondition = resolvedRefConditionforBackend
}
} else {
hasInvalidBackend = true
resolvedRefCondition = resolvedRefConditionforBackend
}
}
routeConfig.Rules = append(routeConfig.Rules, routeConfigRule)
}
hr.routeConfig = routeConfig
setResolvedRefConditionInHTTPRouteStatus(key, resolvedRefCondition, lib.HTTPRoute+"/"+hr.GetNamespace()+"/"+hr.GetName())
return hr.routeConfig
}

Expand Down
39 changes: 39 additions & 0 deletions ako-gateway-api/nodes/route_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,45 @@ func IsHTTPRouteValid(key string, obj *gatewayv1.HTTPRoute) bool {
return true
}

func validateBackendReference(key string, backend Backend, httpRoute *httpRoute) (bool, akogatewayapistatus.Condition) {
routeConditionResolvedRef := akogatewayapistatus.NewCondition().
Type(string(gatewayv1.RouteConditionResolvedRefs)).
Status(metav1.ConditionFalse)
if backend.Kind != "" && backend.Kind != "Service" {
utils.AviLog.Errorf("key: %s, msg: BackendRef %s has invalid kind %s.", key, backend.Name, backend.Kind)
err := fmt.Errorf("BackendRef %s has invalid kind %s.", backend.Name, backend.Kind)
routeConditionResolvedRef.
Reason(string(gatewayv1.RouteReasonInvalidKind)).
Message(err.Error())
return false, routeConditionResolvedRef
}
//TODO: other backendRef related conditions should go here

// Valid route case
routeConditionResolvedRef.
Status(metav1.ConditionTrue).
Reason(string(gatewayv1.RouteReasonResolvedRefs))
return true, routeConditionResolvedRef
}

func setResolvedRefConditionInHTTPRouteStatus(key string, routeConditionResolvedRef akogatewayapistatus.Condition, routeTypeNamespaceName string) {
if routeConditionResolvedRef == nil {
return
}
httpRouteStatus := akogatewayapiobjects.GatewayApiLister().GetRouteToRouteStatusMapping(routeTypeNamespaceName)
routeConditionResolvedRef.ObservedGeneration(httpRouteStatus.Parents[0].Conditions[0].ObservedGeneration)
for parentRefIndex := range httpRouteStatus.Parents {
routeConditionResolvedRef.SetIn(&httpRouteStatus.Parents[parentRefIndex].Conditions)
}
_, namespace, name := lib.ExtractTypeNameNamespace(routeTypeNamespaceName)
httpRoute, err := akogatewayapilib.AKOControlConfig().GatewayApiInformers().HTTPRouteInformer.Lister().HTTPRoutes(namespace).Get(name)
if err != nil {
utils.AviLog.Warnf("key: %s, msg: Unable to extract the HTTPRoute object %s for BackendRef validation", key, name)
return
}
akogatewayapistatus.Record(key, httpRoute, &status.Status{HTTPRouteStatus: httpRouteStatus})
}

func validateParentReference(key string, httpRoute *gatewayv1.HTTPRoute, httpRouteStatus *gatewayv1.HTTPRouteStatus, parentRefIndexFromSpec int, parentRefIndexInHttpRouteStatus *int) error {

name := string(httpRoute.Spec.ParentRefs[parentRefIndexFromSpec].Name)
Expand Down
6 changes: 3 additions & 3 deletions ako-gateway-api/objects/gateway_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (g *GWLister) GetGatewayToGatewayStatusMapping(gwName string) *gatewayv1.Ga
return gatewayList.(*gatewayv1.GatewayStatus)
}

func (g *GWLister) UpdateRouteToRouteStatusMapping(routeTypeNamespaceName string, routeStatus *gatewayv1.RouteStatus) {
func (g *GWLister) UpdateRouteToRouteStatusMapping(routeTypeNamespaceName string, routeStatus *gatewayv1.HTTPRouteStatus) {
g.gwLock.Lock()
defer g.gwLock.Unlock()
g.routeToStatus.AddOrUpdate(routeTypeNamespaceName, routeStatus)
Expand All @@ -275,14 +275,14 @@ func (g *GWLister) DeleteRouteToRouteStatusMapping(routeTypeNamespaceName string
g.routeToStatus.Delete(routeTypeNamespaceName)
}

func (g *GWLister) GetRouteToRouteStatusMapping(routeTypeNamespaceName string) *gatewayv1.RouteStatus {
func (g *GWLister) GetRouteToRouteStatusMapping(routeTypeNamespaceName string) *gatewayv1.HTTPRouteStatus {
g.gwLock.RLock()
defer g.gwLock.RUnlock()
found, routeList := g.routeToStatus.Get(routeTypeNamespaceName)
if !found {
return nil
}
return routeList.(*gatewayv1.RouteStatus)
return routeList.(*gatewayv1.HTTPRouteStatus)
}

//=====All route <-> gateway mappings go here.
Expand Down
2 changes: 1 addition & 1 deletion ako-gateway-api/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func Record(key string, obj runtime.Object, objStatus *status.Status) {
objectType = lib.HTTPRoute
serviceMetadata.HTTPRoute = gwObject.Namespace + "/" + gwObject.Name
key = serviceMetadata.HTTPRoute
akogatewayapiobjects.GatewayApiLister().UpdateRouteToRouteStatusMapping(objectType+"/"+serviceMetadata.HTTPRoute, &objStatus.RouteStatus)
akogatewayapiobjects.GatewayApiLister().UpdateRouteToRouteStatusMapping(objectType+"/"+serviceMetadata.HTTPRoute, objStatus.HTTPRouteStatus)
default:
utils.AviLog.Warnf("key %s, msg: Unsupported object received at the status layer, %T", key, obj)
return
Expand Down
4 changes: 2 additions & 2 deletions tests/gatewayapitests/graphlayer/httproute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ func TestHTTPRouteWithValidConfig(t *testing.T) {
return false
}
nodes := aviModel.(*avinodes.AviObjectGraph).GetAviEvhVS()
return len(nodes[0].VSVIPRefs[0].FQDNs) > 0
return len(nodes[0].VSVIPRefs[0].FQDNs) > 0 && len(nodes[0].EvhNodes[0].PoolGroupRefs) > 0

}, 25*time.Second).Should(gomega.Equal(true))

Expand Down Expand Up @@ -766,7 +766,7 @@ func TestHTTPRouteBackendServiceCDC(t *testing.T) {
}
nodes := aviModel.(*avinodes.AviObjectGraph).GetAviEvhVS()
return len(nodes[0].EvhNodes[0].PoolGroupRefs[0].Members)
}, 25*time.Second).Should(gomega.Equal(1))
}, 30*time.Second).Should(gomega.Equal(1))

_, aviModel = objects.SharedAviGraphLister().Get(modelName)
nodes = aviModel.(*avinodes.AviObjectGraph).GetAviEvhVS()
Expand Down
Loading
Loading