func()

in pkg/deploy/lattice/target_group_synthesizer.go [202:261]


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

	svcExportName := types.NamespacedName{
		Namespace: tagFields.K8SServiceNamespace,
		Name:      tagFields.K8SServiceName,
	}

	t.log.Debugf(ctx, "TargetGroup %s (%s) is referenced by ServiceExport",
		*latticeTg.tgSummary.Arn, *latticeTg.tgSummary.Name)

	svcExport := &anv1alpha1.ServiceExport{}
	err := t.client.Get(ctx, svcExportName, svcExport)
	if err != nil {
		if apierrors.IsNotFound(err) {
			// if the service export does not exist, we can safely delete
			t.log.Infof(ctx, "Will delete TargetGroup %s (%s) - ServiceExport 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 service export %s", err)
			return false
		}
	}

	if !svcExport.DeletionTimestamp.IsZero() {
		// backing object is deleted, we can delete too
		t.log.Infof(ctx, "Will delete TargetGroup %s (%s) - ServiceExport has been deleted",
			*latticeTg.tgSummary.Arn, *latticeTg.tgSummary.Name)
		return true
	}

	// now we get to the tricky business of seeing if our unused target group actually matches
	// the current state of the service and service export - the most correct way to do this is to
	// reconstruct the target group spec from the service export itself, then compare fields
	modelTg, err := t.svcExportTgBuilder.BuildTargetGroup(ctx, svcExport)
	if err != nil {
		t.log.Infof(ctx, "Received error building svc export target group model %s", err)
		return false
	}

	// the main identifiers are validated, just need to check the other essentials.
	// protocolVersion is not in TG summary so we are bringing it from tags.
	if int64(modelTg.Spec.Port) != aws.Int64Value(latticeTg.tgSummary.Port) ||
		modelTg.Spec.Protocol != aws.StringValue(latticeTg.tgSummary.Protocol) ||
		modelTg.Spec.ProtocolVersion != tagFields.K8SProtocolVersion ||
		modelTg.Spec.IpAddressType != aws.StringValue(latticeTg.tgSummary.IpAddressType) {

		// one or more immutable fields differ from the source, so the TG is out of date
		t.log.Infof(ctx, "Will delete TargetGroup %s (%s) - fields differ from source service/service export",
			*latticeTg.tgSummary.Arn, *latticeTg.tgSummary.Name)
		return true
	}

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

	return false
}