feat(diff): mark records deliberately kept outside the template

This commit is contained in:
2026-08-19 17:51:19 +07:00
parent 8f57a25af0
commit e675c17123
2 changed files with 102 additions and 3 deletions
+43 -3
View File
@@ -19,6 +19,10 @@ type RecordDiff struct {
Desired *model.Record // nil for Delete
Actual *model.Record // nil for Add
ReadOnly bool // NS/SOA — shown but never applied
// Custom marks a record the operator deliberately keeps outside the
// template: shown in its own section, never counted as drift, never
// applied. Set by MarkCustom, not by Diff.
Custom bool
}
// Key is the stable identifier of the RRset this diff targets, normalised the
@@ -37,7 +41,7 @@ type Changeset struct {
func (c Changeset) Actionable() []RecordDiff {
var out []RecordDiff
for _, d := range c.Diffs {
if d.ReadOnly || d.Kind == InSync {
if d.ReadOnly || d.Custom || d.Kind == InSync {
continue
}
out = append(out, d)
@@ -52,7 +56,7 @@ func (c Changeset) Actionable() []RecordDiff {
func (c Changeset) Updates() []RecordDiff {
var out []RecordDiff
for _, d := range c.Diffs {
if d.ReadOnly {
if d.ReadOnly || d.Custom {
continue
}
if d.Kind == Add || d.Kind == Update {
@@ -73,7 +77,7 @@ func (c Changeset) Updates() []RecordDiff {
func (c Changeset) Prunes() []RecordDiff {
var out []RecordDiff
for _, d := range c.Diffs {
if d.ReadOnly {
if d.ReadOnly || d.Custom {
continue
}
if d.Kind == Delete {
@@ -131,3 +135,39 @@ func index(recs []model.Record) map[string]model.Record {
}
return m
}
// MarkCustom flags the diffs whose Key() is in keys as Custom. Only
// Kind == Delete diffs are marked, and never read-only ones: the template
// wins. As soon as the template starts describing a key, its diff becomes
// Add/Update/InSync and the stored mark stops having any effect (it is not
// deleted — the operator may go back to a template without that record).
func (c *Changeset) MarkCustom(keys []string) {
if len(keys) == 0 {
return
}
set := make(map[string]bool, len(keys))
for _, k := range keys {
set[k] = true
}
for i := range c.Diffs {
d := &c.Diffs[i]
if d.ReadOnly || d.Kind != Delete {
continue
}
if set[d.Key()] {
d.Custom = true
}
}
}
// Customs returns diffs marked by MarkCustom. Disjoint from Updates() and
// Prunes(), and outside Actionable() — a custom record is never drift.
func (c Changeset) Customs() []RecordDiff {
var out []RecordDiff
for _, d := range c.Diffs {
if d.Custom {
out = append(out, d)
}
}
return out
}