func()

in pkg/deploy/lattice/target_group_synthesizer.go [263:339]


func (t *TargetGroupSynthesizer) shouldDeleteRouteTg(
	ctx context.Context, latticeTg tgListOutput, tagFields model.TargetGroupTagFields) bool {

	routeName := types.NamespacedName{
		Namespace: tagFields.K8SRouteNamespace,
		Name:      tagFields.K8SRouteName,
	}

	var err error
	var route core.Route
	if tagFields.K8SProtocolVersion == vpclattice.TargetGroupProtocolVersionGrpc {
		route, err = core.GetGRPCRoute(ctx, t.client, routeName)
	} else if *latticeTg.tgSummary.Protocol == vpclattice.TargetGroupProtocolTcp {
		route, err = core.GetTLSRoute(ctx, t.client, routeName)
	} else {
		route, err = core.GetHTTPRoute(ctx, t.client, routeName)
	}

	if err != nil {
		if apierrors.IsNotFound(err) {
			// if the route does not exist, we can safely delete
			t.log.Debugf(ctx, "Will delete TargetGroup %s (%s) - Route is not found",
				*latticeTg.tgSummary.Arn, *latticeTg.tgSummary.Name)
			return true
		} else {
			// skip if we have an unknown error
			t.log.Infof(ctx, "Received unexpected API error getting route %s", err)
			return false
		}
	}

	if !route.DeletionTimestamp().IsZero() {
		t.log.Debugf(ctx, "Will delete TargetGroup %s (%s) - Route is deleted",
			*latticeTg.tgSummary.Arn, *latticeTg.tgSummary.Name)
		return true
	}

	// basically rebuild everything for the route and see if one of the TGs matches
	routeStack, err := t.svcBuilder.Build(ctx, route)
	if err != nil {
		t.log.Infof(ctx, "Received error building route model %s", err)
		return false
	}

	var resTargetGroups []*model.TargetGroup
	err = routeStack.ListResources(&resTargetGroups)
	if err != nil {
		t.log.Infof(ctx, "Error listing stack target groups %s", err)
		return false
	}

	var matchFound bool
	for _, modelTg := range resTargetGroups {
		match, err := t.targetGroupManager.IsTargetGroupMatch(ctx, modelTg, latticeTg.tgSummary, &tagFields)
		if err != nil {
			t.log.Infof(ctx, "Received error during tg comparison %s", err)
			continue
		}

		if match {
			t.log.Debugf(ctx, "Route TargetGroup %s (%s) is up to date",
				*latticeTg.tgSummary.Arn, *latticeTg.tgSummary.Name)

			matchFound = true
			break
		}
	}

	if !matchFound {
		t.log.Debugf(ctx, "Will delete TargetGroup %s (%s) - TG is not up to date",
			*latticeTg.tgSummary.Arn, *latticeTg.tgSummary.Name)

		return true // safe to delete
	}

	return false
}