func()

in pkg/appgw/requestroutingrules.go [75:138]


func (c *appGwConfigBuilder) getRules(cbCtx *ConfigBuilderContext) ([]n.ApplicationGatewayRequestRoutingRule, []n.ApplicationGatewayURLPathMap) {
	if c.mem.routingRules != nil && c.mem.pathMaps != nil {
		return *c.mem.routingRules, *c.mem.pathMaps
	}
	httpListenersMap := c.groupListenersByListenerIdentifier(cbCtx)
	pathMap := []n.ApplicationGatewayURLPathMap{}
	var requestRoutingRules []n.ApplicationGatewayRequestRoutingRule
	urlPathMaps := c.getPathMaps(cbCtx)
	priorities := c.getListenerPriorities(cbCtx)
	for listenerID, urlPathMap := range urlPathMaps {
		routingRuleName := generateRequestRoutingRuleName(listenerID)
		httpListener, exists := httpListenersMap[listenerID]
		if !exists {
			klog.Errorf("Routing rule %s will not be created; listener %+v does not exist", routingRuleName, listenerID)
			continue
		}
		rule := n.ApplicationGatewayRequestRoutingRule{
			Etag: to.StringPtr("*"),
			Name: to.StringPtr(routingRuleName),
			ID:   to.StringPtr(c.appGwIdentifier.requestRoutingRuleID(routingRuleName)),
			ApplicationGatewayRequestRoutingRulePropertiesFormat: &n.ApplicationGatewayRequestRoutingRulePropertiesFormat{
				HTTPListener: &n.SubResource{ID: to.StringPtr(c.appGwIdentifier.listenerID(*httpListener.Name))},
			},
		}
		if urlPathMap.PathRules == nil || len(*urlPathMap.PathRules) == 0 {
			// Basic Rule, because we have no path-based rule
			rule.RuleType = n.ApplicationGatewayRequestRoutingRuleTypeBasic
			rule.RedirectConfiguration = urlPathMap.DefaultRedirectConfiguration

			// We setup the default backend address pools and default backend HTTP settings only if
			// this rule does not have an `ssl-redirect` configuration.
			if rule.RedirectConfiguration == nil {
				rule.BackendAddressPool = urlPathMap.DefaultBackendAddressPool
				rule.BackendHTTPSettings = urlPathMap.DefaultBackendHTTPSettings
			} else {
				rule.BackendAddressPool = nil
				rule.BackendHTTPSettings = nil
			}
			rule.RewriteRuleSet = urlPathMap.DefaultRewriteRuleSet
		} else {
			// Path-based Rule
			rule.RuleType = n.ApplicationGatewayRequestRoutingRuleTypePathBasedRouting
			rule.URLPathMap = &n.SubResource{ID: to.StringPtr(c.appGwIdentifier.urlPathMapID(*urlPathMap.Name))}
			pathMap = append(pathMap, *urlPathMap)
		}
		if rule.RuleType == n.ApplicationGatewayRequestRoutingRuleTypePathBasedRouting {
			klog.V(3).Infof("Bound path-based rule: %s to listener: %s (%s, %d) and url path map %s", *rule.Name, *httpListener.Name, listenerID.HostNames, listenerID.FrontendPort, utils.GetLastChunkOfSlashed(*rule.URLPathMap.ID))
		} else {
			if rule.RedirectConfiguration != nil {
				klog.V(3).Infof("Bound basic rule: %s to listener: %s (%s, %d) and redirect configuration %s", *rule.Name, *httpListener.Name, listenerID.HostNames, listenerID.FrontendPort, utils.GetLastChunkOfSlashed(*rule.RedirectConfiguration.ID))
			} else {
				klog.V(3).Infof("Bound basic rule: %s to listener: %s (%s, %d) for backend pool %s and backend http settings %s", *rule.Name, *httpListener.Name, listenerID.HostNames, listenerID.FrontendPort, utils.GetLastChunkOfSlashed(*rule.BackendAddressPool.ID), utils.GetLastChunkOfSlashed(*rule.BackendHTTPSettings.ID))
			}
		}

		rule.Priority = priorities[listenerID]

		requestRoutingRules = append(requestRoutingRules, rule)
	}

	c.mem.routingRules = &requestRoutingRules
	c.mem.pathMaps = &pathMap
	return requestRoutingRules, pathMap
}