func()

in pkg/gateway/model_build_rule.go [31:98]


func (t *latticeServiceModelBuildTask) buildRules(ctx context.Context, stackListenerId string) error {
	// note we only build rules for non-deleted routes
	t.log.Debugf(ctx, "Processing %d rules", len(t.route.Spec().Rules()))

	for i, rule := range t.route.Spec().Rules() {
		ruleSpec := model.RuleSpec{
			StackListenerId: stackListenerId,
			Priority:        int64(i + 1),
		}

		if len(rule.Matches()) > 1 {
			// only support 1 match today
			return errors.New(LATTICE_NO_SUPPORT_FOR_MULTIPLE_MATCHES)
		} else if len(rule.Matches()) > 0 {
			t.log.Debugf(ctx, "Processing rule match")
			match := rule.Matches()[0]

			switch m := match.(type) {
			case *core.HTTPRouteMatch:
				if err := t.updateRuleSpecForHttpRoute(m, &ruleSpec); err != nil {
					return err
				}
			case *core.GRPCRouteMatch:
				if err := t.updateRuleSpecForGrpcRoute(m, &ruleSpec); err != nil {
					return err
				}
			default:
				return fmt.Errorf("unsupported rule match: %T", m)
			}

			if err := t.updateRuleSpecWithHeaderMatches(match, &ruleSpec); err != nil {
				return err
			}
		} else {

			// Match every traffic on no matches
			ruleSpec.PathMatchValue = "/"
			ruleSpec.PathMatchPrefix = true
			if _, ok := rule.(*core.GRPCRouteRule); ok {
				ruleSpec.Method = string(gwv1.HTTPMethodPost)
			}

		}

		ruleTgList, err := t.getTargetGroupsForRuleAction(ctx, rule)
		if err != nil {
			return err
		}

		ruleSpec.Action = model.RuleAction{
			TargetGroups: ruleTgList,
		}

		// don't bother adding rules on delete, these will be removed automatically with the owning route/lattice service
		// target groups will still be present and removed as needed
		if t.route.DeletionTimestamp().IsZero() {
			stackRule, err := model.NewRule(t.stack, ruleSpec)
			if err != nil {
				return err
			}
			t.log.Debugf(ctx, "Added rule %d to the stack (ID %s)", stackRule.Spec.Priority, stackRule.ID())
		} else {
			t.log.Debugf(ctx, "Skipping adding rule %d to the stack since the route is deleted", ruleSpec.Priority)
		}
	}

	return nil
}