From 2f41bf007f8fb50f2cf261856a436443be303c8d Mon Sep 17 00:00:00 2001 From: Craig Peterson Date: Wed, 9 Oct 2019 09:48:25 -0400 Subject: [PATCH] pull out records with identical content first. Saves from ordering issues. --- providers/diff/diff.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/providers/diff/diff.go b/providers/diff/diff.go index e87252475..e36b592ee 100644 --- a/providers/diff/diff.go +++ b/providers/diff/diff.go @@ -117,17 +117,28 @@ func (d *differ) IncrementalDiff(existing []*models.RecordConfig) (unchanged, cr // Each iteration is only for a single type/name record set for key, existingRecords := range existingByNameAndType { desiredRecords := desiredByNameAndType[key] - // first look through records that are the same target on both sides. Those are either modifications or unchanged + + // Very first, get rid of any identical records. Easy. + for i := len(existingRecords) - 1; i >= 0; i-- { + ex := existingRecords[i] + for j, de := range desiredRecords { + if d.content(de) != d.content(ex) { + continue + } + unchanged = append(unchanged, Correlation{d, ex, de}) + existingRecords = existingRecords[:i+copy(existingRecords[i:], existingRecords[i+1:])] + desiredRecords = desiredRecords[:j+copy(desiredRecords[j:], desiredRecords[j+1:])] + break + } + } + + // Next, match by target. This will give the most natural modifications. for i := len(existingRecords) - 1; i >= 0; i-- { ex := existingRecords[i] for j, de := range desiredRecords { if de.GetTargetField() == ex.GetTargetField() { - // they're either identical or should be a modification of each other (ttl or metadata changes) - if d.content(de) == d.content(ex) { - unchanged = append(unchanged, Correlation{d, ex, de}) - } else { - modify = append(modify, Correlation{d, ex, de}) - } + // two records share a target, but different content (ttl or metadata changes) + modify = append(modify, Correlation{d, ex, de}) // remove from both slices by index existingRecords = existingRecords[:i+copy(existingRecords[i:], existingRecords[i+1:])] desiredRecords = desiredRecords[:j+copy(desiredRecords[j:], desiredRecords[j+1:])]