func()

in pkg/deploy/lattice/target_group_manager.go [459:502]


func (s *defaultTargetGroupManager) ResolveRuleTgIds(ctx context.Context, ruleAction *model.RuleAction, stack core.Stack) error {
	if len(ruleAction.TargetGroups) == 0 {
		s.log.Debugf(ctx, "no target groups to resolve for rule")
		return nil
	}
	for i, ruleActionTg := range ruleAction.TargetGroups {
		if ruleActionTg.StackTargetGroupId == "" && ruleActionTg.SvcImportTG == nil && ruleActionTg.LatticeTgId == "" {
			return errors.New("rule TG is missing a required target group identifier")
		}
		if ruleActionTg.LatticeTgId != "" {
			s.log.Debugf(ctx, "Rule TG %d already resolved %s", i, ruleActionTg.LatticeTgId)
			continue
		}
		if ruleActionTg.StackTargetGroupId != "" {
			if ruleActionTg.StackTargetGroupId == model.InvalidBackendRefTgId {
				s.log.Debugf(ctx, "Rule TG has an invalid backendref, setting TG id to invalid")
				ruleActionTg.LatticeTgId = model.InvalidBackendRefTgId
				continue
			}
			s.log.Debugf(ctx, "Fetching TG %d from the stack (ID %s)", i, ruleActionTg.StackTargetGroupId)
			stackTg := &model.TargetGroup{}
			err := stack.GetResource(ruleActionTg.StackTargetGroupId, stackTg)
			if err != nil {
				return err
			}
			if stackTg.Status == nil {
				return errors.New("stack target group is missing Status field")
			}
			ruleActionTg.LatticeTgId = stackTg.Status.Id
		}
		if ruleActionTg.SvcImportTG != nil {
			s.log.Debugf(ctx, "Getting target group for service import %s %s (%s, %s)",
				ruleActionTg.SvcImportTG.K8SServiceName, ruleActionTg.SvcImportTG.K8SServiceNamespace,
				ruleActionTg.SvcImportTG.K8SClusterName, ruleActionTg.SvcImportTG.VpcId)
			tgId, err := s.findSvcExportTG(ctx, *ruleActionTg.SvcImportTG)

			if err != nil {
				return err
			}
			ruleActionTg.LatticeTgId = tgId
		}
	}
	return nil
}