func()

in internal/controllers/symphony/controller.go [106:154]


func (c *symphonyController) reconcileReverse(ctx context.Context, symph *apiv1.Symphony, comps *apiv1.CompositionList) (bool, error) {
	logger := logr.FromContextOrDiscard(ctx)

	expectedSynths := map[string]struct{}{}
	for _, variation := range symph.Spec.Variations {
		expectedSynths[variation.Synthesizer.Name] = struct{}{}
	}

	// Delete compositions when their synth has been removed from the symphony
	existingBySynthName := map[string][]*apiv1.Composition{}
	for _, comp := range comps.Items {
		comp := comp
		existingBySynthName[comp.Spec.Synthesizer.Name] = append(existingBySynthName[comp.Spec.Synthesizer.Name], &comp)

		hasVariation := slices.ContainsFunc(symph.Spec.Variations, func(vrn apiv1.Variation) bool {
			return vrn.Synthesizer.Name == comp.Spec.Synthesizer.Name
		})
		if (hasVariation && symph.DeletionTimestamp == nil) || comp.DeletionTimestamp != nil {
			continue
		}

		err := c.client.Delete(ctx, &comp)
		if err != nil {
			return false, fmt.Errorf("cleaning up composition: %w", err)
		}

		logger.V(0).Info("deleted composition because its variation was removed from the symphony", "compositionName", comp.Name, "compositionNamespace", comp.Namespace)
		return true, nil
	}

	// Delete any duplicates we may have created in the past - leave the oldest one
	for _, comps := range existingBySynthName {
		if len(comps) < 2 {
			continue
		}

		sort.Slice(comps, func(i, j int) bool { return comps[i].CreationTimestamp.Before(&comps[j].CreationTimestamp) })

		err := c.client.Delete(ctx, comps[0])
		if err != nil {
			return false, fmt.Errorf("deleting duplicate composition: %w", err)
		}

		logger.V(0).Info("deleted composition because it's a duplicate", "compositionName", comps[0].Name, "compositionNamespace", comps[0].Namespace)
		return true, nil
	}

	return false, nil
}