func()

in pkg/controller/ingress/reconcile/builder/albconfig_manager/model_build_listener_rules.go [28:128]


func (t *defaultModelBuildTask) buildListenerRules(ctx context.Context, lsID core.StringToken, port int32, ingList []networking.Ingress) error {
	var rules []alb.ListenerRule
	carryWeight := make(map[string][]alb.ServerGroupTuple, 0)
	for _, ing := range ingList {
		if v := annotations.GetStringAnnotationMutil(annotations.NginxCanary, annotations.AlbCanary, &ing); v == "true" {
			weight := 0
			w := annotations.GetStringAnnotationMutil(annotations.NginxCanaryWeight, annotations.AlbCanaryWeight, &ing)
			wi, _ := strconv.Atoi(w)
			weight = wi
			for _, rule := range ing.Spec.Rules {
				if rule.HTTP == nil {
					continue
				}
				for _, path := range rule.HTTP.Paths {
					if _, ok := carryWeight[rule.Host+"-"+path.Path]; ok {
						carryWeight[rule.Host+"-"+path.Path] = append(carryWeight[rule.Host+"-"+path.Path], alb.ServerGroupTuple{
							ServiceName: path.Backend.Service.Name,
							ServicePort: int(path.Backend.Service.Port.Number),
							Weight:      weight,
						})
					} else {
						carryWeight[rule.Host+"-"+path.Path] = []alb.ServerGroupTuple{{
							ServiceName: path.Backend.Service.Name,
							ServicePort: int(path.Backend.Service.Port.Number),
							Weight:      weight,
						},
						}
					}

					break
				}
			}
			break
		}
	}
	for _, ing := range ingList {
		if v := annotations.GetStringAnnotationMutil(annotations.NginxCanary, annotations.AlbCanary, &ing); v == "true" {
			we := annotations.GetStringAnnotationMutil(annotations.NginxCanaryWeight, annotations.AlbCanaryWeight, &ing)
			if we != "" {
				continue
			}
		}
		for _, rule := range ing.Spec.Rules {
			if rule.HTTP == nil {
				continue
			}

			for _, path := range rule.HTTP.Paths {
				var action alb.Action
				if v := annotations.GetStringAnnotationMutil(annotations.NginxSslRedirect, annotations.AlbSslRedirect, &ing); v == "true" && port != 443 {
					action = buildActionViaHostAndPath(ctx, rule.Host, path.Path)
				} else {
					action = buildActionViaServiceAndServicePort(ctx, path.Backend.Service.Name, int(path.Backend.Service.Port.Number), 100)
					if v := annotations.GetStringAnnotationMutil(annotations.NginxCanary, annotations.AlbCanary, &ing); v != "true" {
						if canaryWeights, ok := carryWeight[rule.Host+"-"+path.Path]; ok {
							canaryWeight := 0
							for _, cw := range canaryWeights {
								canaryWeight += cw.Weight
							}
							for i := range action.ForwardConfig.ServerGroups {
								action.ForwardConfig.ServerGroups[i].Weight = 100 - canaryWeight
							}
							action.ForwardConfig.ServerGroups = append(action.ForwardConfig.ServerGroups, canaryWeights...)
						}
					}
				}

				conditions, err := t.buildRuleConditions(ctx, rule, path, ing)
				if err != nil {
					return errors.Wrapf(err, "ingress: %v", util.NamespacedName(&ing))
				}
				action2, err := t.buildAction(ctx, ing, action)
				lrs := alb.ListenerRuleSpec{
					ListenerID: lsID,
				}
				lrs.RuleActions = []alb.Action{action2}
				lrs.RuleConditions = conditions
				rules = append(rules, alb.ListenerRule{
					Spec: lrs,
				})
			}
		}
	}

	priority := 1
	for _, rule := range rules {
		ruleResID := fmt.Sprintf("%v:%v", port, priority)
		klog.Infof("ruleResID: %s", ruleResID)
		lrs := alb.ListenerRuleSpec{
			ListenerID: lsID,
		}
		lrs.Priority = priority
		lrs.RuleConditions = rule.Spec.RuleConditions
		lrs.RuleActions = rule.Spec.RuleActions
		lrs.RuleName = fmt.Sprintf("%v-%v-%v", ListenerRuleNamePrefix, port, priority)
		_ = alb.NewListenerRule(t.stack, ruleResID, lrs)
		priority += 1
	}

	return nil
}