func()

in pkg/deploy/lattice/target_group_synthesizer.go [158:200]


func (t *TargetGroupSynthesizer) calculateTargetGroupsToDelete(ctx context.Context) ([]tgListOutput, error) {
	latticeTgs, err := t.targetGroupManager.List(ctx)
	if err != nil {
		return latticeTgs, fmt.Errorf("failed TargetGroupManager.List due to %s", err)
	}

	var tgsToDelete []tgListOutput

	// we check existing target groups to see if they are still in use - this is necessary as
	// some changes to existing service exports or routes will simply create new target groups,
	// for example on protocol changes
	for _, latticeTg := range latticeTgs {
		if !t.hasTags(latticeTg) || !t.vpcMatchesConfig(latticeTg) {
			continue
		}

		// TGs from earlier releases will require 1-time manual cleanup
		// this method of validation only covers TGs created by this build
		// of the controller forward
		tagFields := model.TGTagFieldsFromTags(latticeTg.tags)
		if !t.hasExpectedTags(latticeTg, tagFields) {
			continue
		}

		// most importantly, is the tg in use?
		if len(latticeTg.tgSummary.ServiceArns) > 0 {
			t.log.Debugf(ctx, "TargetGroup %s (%s) is referenced by lattice service",
				*latticeTg.tgSummary.Arn, *latticeTg.tgSummary.Name)
			continue
		}

		if tagFields.K8SSourceType == model.SourceTypeSvcExport {
			if t.shouldDeleteSvcExportTg(ctx, latticeTg, tagFields) {
				tgsToDelete = append(tgsToDelete, latticeTg)
			}
		} else {
			if t.shouldDeleteRouteTg(ctx, latticeTg, tagFields) {
				tgsToDelete = append(tgsToDelete, latticeTg)
			}
		}
	}
	return tgsToDelete, nil
}