2016-08-23 08:31:50 +08:00
|
|
|
package diff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
2017-01-12 03:38:07 +08:00
|
|
|
|
|
|
|
"github.com/StackExchange/dnscontrol/models"
|
2016-08-23 08:31:50 +08:00
|
|
|
)
|
|
|
|
|
2017-01-12 03:38:07 +08:00
|
|
|
type Correlation struct {
|
|
|
|
d *differ
|
|
|
|
Existing *models.RecordConfig
|
|
|
|
Desired *models.RecordConfig
|
|
|
|
}
|
|
|
|
type Changeset []Correlation
|
2016-08-23 08:31:50 +08:00
|
|
|
|
2017-01-12 03:38:07 +08:00
|
|
|
type Differ interface {
|
|
|
|
IncrementalDiff(existing []*models.RecordConfig) (unchanged, create, toDelete, modify Changeset)
|
2016-08-23 08:31:50 +08:00
|
|
|
}
|
|
|
|
|
2017-01-12 03:38:07 +08:00
|
|
|
func New(dc *models.DomainConfig, extraValues ...func(*models.RecordConfig) map[string]string) Differ {
|
|
|
|
return &differ{
|
|
|
|
dc: dc,
|
|
|
|
extraValues: extraValues,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type differ struct {
|
|
|
|
dc *models.DomainConfig
|
|
|
|
extraValues []func(*models.RecordConfig) map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// get normalized content for record. target, ttl, mxprio, and specified metadata
|
|
|
|
func (d *differ) content(r *models.RecordConfig) string {
|
|
|
|
content := fmt.Sprintf("%s %d", r.Target, r.TTL)
|
|
|
|
if r.Type == "MX" {
|
|
|
|
content += fmt.Sprintf(" priority=%d", r.Priority)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range d.extraValues {
|
|
|
|
for k, v := range f(r) {
|
|
|
|
content += fmt.Sprintf(" %s=%s", k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return content
|
2016-08-23 08:31:50 +08:00
|
|
|
}
|
|
|
|
|
2017-01-12 03:38:07 +08:00
|
|
|
func (d *differ) IncrementalDiff(existing []*models.RecordConfig) (unchanged, create, toDelete, modify Changeset) {
|
2016-08-23 08:31:50 +08:00
|
|
|
unchanged = Changeset{}
|
|
|
|
create = Changeset{}
|
|
|
|
toDelete = Changeset{}
|
|
|
|
modify = Changeset{}
|
2017-01-12 03:38:07 +08:00
|
|
|
desired := d.dc.Records
|
2016-08-23 08:31:50 +08:00
|
|
|
|
|
|
|
//sort existing and desired by name
|
|
|
|
type key struct {
|
|
|
|
name, rType string
|
|
|
|
}
|
2017-01-12 03:38:07 +08:00
|
|
|
existingByNameAndType := map[key][]*models.RecordConfig{}
|
|
|
|
desiredByNameAndType := map[key][]*models.RecordConfig{}
|
2016-08-23 08:31:50 +08:00
|
|
|
for _, e := range existing {
|
2017-01-12 03:38:07 +08:00
|
|
|
k := key{e.NameFQDN, e.Type}
|
2016-08-23 08:31:50 +08:00
|
|
|
existingByNameAndType[k] = append(existingByNameAndType[k], e)
|
|
|
|
}
|
|
|
|
for _, d := range desired {
|
2017-01-12 03:38:07 +08:00
|
|
|
k := key{d.NameFQDN, d.Type}
|
2016-08-23 08:31:50 +08:00
|
|
|
desiredByNameAndType[k] = append(desiredByNameAndType[k], d)
|
|
|
|
}
|
2017-01-12 03:38:07 +08:00
|
|
|
// Look through existing records. This will give us changes and deletions and some additions.
|
|
|
|
// Each iteration is only for a single type/name record set
|
2016-08-23 08:31:50 +08:00
|
|
|
for key, existingRecords := range existingByNameAndType {
|
|
|
|
desiredRecords := desiredByNameAndType[key]
|
2017-01-12 03:38:07 +08:00
|
|
|
//first look through records that are the same target on both sides. Those are either modifications or unchanged
|
2016-08-23 08:31:50 +08:00
|
|
|
for i := len(existingRecords) - 1; i >= 0; i-- {
|
|
|
|
ex := existingRecords[i]
|
|
|
|
for j, de := range desiredRecords {
|
2017-01-12 03:38:07 +08:00
|
|
|
if de.Target == ex.Target {
|
|
|
|
//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})
|
2016-08-23 08:31:50 +08:00
|
|
|
} else {
|
2017-01-12 03:38:07 +08:00
|
|
|
modify = append(modify, Correlation{d, ex, de})
|
2016-08-23 08:31:50 +08:00
|
|
|
}
|
|
|
|
// remove from both slices by index
|
|
|
|
existingRecords = existingRecords[:i+copy(existingRecords[i:], existingRecords[i+1:])]
|
|
|
|
desiredRecords = desiredRecords[:j+copy(desiredRecords[j:], desiredRecords[j+1:])]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 03:38:07 +08:00
|
|
|
desiredLookup := map[string]*models.RecordConfig{}
|
|
|
|
existingLookup := map[string]*models.RecordConfig{}
|
|
|
|
// build index based on normalized content data
|
2016-08-23 08:31:50 +08:00
|
|
|
for _, ex := range existingRecords {
|
2017-01-12 03:38:07 +08:00
|
|
|
normalized := d.content(ex)
|
2016-08-23 08:31:50 +08:00
|
|
|
if existingLookup[normalized] != nil {
|
|
|
|
panic(fmt.Sprintf("DUPLICATE E_RECORD FOUND: %s %s", key, normalized))
|
|
|
|
}
|
|
|
|
existingLookup[normalized] = ex
|
|
|
|
}
|
|
|
|
for _, de := range desiredRecords {
|
2017-01-12 03:38:07 +08:00
|
|
|
normalized := d.content(de)
|
2016-08-23 08:31:50 +08:00
|
|
|
if desiredLookup[normalized] != nil {
|
|
|
|
panic(fmt.Sprintf("DUPLICATE D_RECORD FOUND: %s %s", key, normalized))
|
|
|
|
}
|
|
|
|
desiredLookup[normalized] = de
|
|
|
|
}
|
|
|
|
// if a record is in both, it is unchanged
|
|
|
|
for norm, ex := range existingLookup {
|
|
|
|
if de, ok := desiredLookup[norm]; ok {
|
2017-01-12 03:38:07 +08:00
|
|
|
unchanged = append(unchanged, Correlation{d, ex, de})
|
2016-08-23 08:31:50 +08:00
|
|
|
delete(existingLookup, norm)
|
|
|
|
delete(desiredLookup, norm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//sort records by normalized text. Keeps behaviour deterministic
|
2017-01-12 03:38:07 +08:00
|
|
|
existingStrings, desiredStrings := sortedKeys(existingLookup), sortedKeys(desiredLookup)
|
2016-08-23 08:31:50 +08:00
|
|
|
// Modifications. Take 1 from each side.
|
|
|
|
for len(desiredStrings) > 0 && len(existingStrings) > 0 {
|
2017-01-12 03:38:07 +08:00
|
|
|
modify = append(modify, Correlation{d, existingLookup[existingStrings[0]], desiredLookup[desiredStrings[0]]})
|
2016-08-23 08:31:50 +08:00
|
|
|
existingStrings = existingStrings[1:]
|
|
|
|
desiredStrings = desiredStrings[1:]
|
|
|
|
}
|
|
|
|
// If desired still has things they are additions
|
|
|
|
for _, norm := range desiredStrings {
|
|
|
|
rec := desiredLookup[norm]
|
2017-01-12 03:38:07 +08:00
|
|
|
create = append(create, Correlation{d, nil, rec})
|
2016-08-23 08:31:50 +08:00
|
|
|
}
|
|
|
|
// if found , but not desired, delete it
|
|
|
|
for _, norm := range existingStrings {
|
|
|
|
rec := existingLookup[norm]
|
2017-01-12 03:38:07 +08:00
|
|
|
toDelete = append(toDelete, Correlation{d, rec, nil})
|
2016-08-23 08:31:50 +08:00
|
|
|
}
|
|
|
|
// remove this set from the desired list to indicate we have processed it.
|
|
|
|
delete(desiredByNameAndType, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
//any name/type sets not already processed are pure additions
|
|
|
|
for name := range existingByNameAndType {
|
|
|
|
delete(desiredByNameAndType, name)
|
|
|
|
}
|
|
|
|
for _, desiredList := range desiredByNameAndType {
|
|
|
|
for _, rec := range desiredList {
|
2017-01-12 03:38:07 +08:00
|
|
|
create = append(create, Correlation{d, nil, rec})
|
2016-08-23 08:31:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Correlation) String() string {
|
|
|
|
if c.Existing == nil {
|
2017-01-12 03:38:07 +08:00
|
|
|
return fmt.Sprintf("CREATE %s %s %s", c.Desired.Type, c.Desired.NameFQDN, c.d.content(c.Desired))
|
2016-08-23 08:31:50 +08:00
|
|
|
}
|
|
|
|
if c.Desired == nil {
|
2017-01-12 03:38:07 +08:00
|
|
|
return fmt.Sprintf("DELETE %s %s %s", c.Existing.Type, c.Existing.NameFQDN, c.d.content(c.Existing))
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("MODIFY %s %s: (%s) -> (%s)", c.Existing.Type, c.Existing.NameFQDN, c.d.content(c.Existing), c.d.content(c.Desired))
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortedKeys(m map[string]*models.RecordConfig) []string {
|
|
|
|
s := []string{}
|
|
|
|
for v := range m {
|
|
|
|
s = append(s, v)
|
2016-08-23 08:31:50 +08:00
|
|
|
}
|
2017-01-12 03:38:07 +08:00
|
|
|
sort.Strings(s)
|
|
|
|
return s
|
2016-08-23 08:31:50 +08:00
|
|
|
}
|