diff --git a/nomad/structs/diff.go b/nomad/structs/diff.go index b8ced9180..34069a786 100644 --- a/nomad/structs/diff.go +++ b/nomad/structs/diff.go @@ -598,6 +598,11 @@ func serviceCheckDiff(old, new *ServiceCheck, contextual bool) *ObjectDiff { diff.Objects = append(diff.Objects, headerDiff) } + // Diff check_restart + if crDiff := checkRestartDiff(old.CheckRestart, new.CheckRestart, contextual); crDiff != nil { + diff.Objects = append(diff.Objects, crDiff) + } + return diff } @@ -621,6 +626,26 @@ func checkHeaderDiff(old, new map[string][]string, contextual bool) *ObjectDiff return diff } +// checkRestartDiff returns the diff of two service check check_restart +// objects. If contextual diff is enabled, all fields will be returned, even if +// no diff occurred. +func checkRestartDiff(old, new *CheckRestart, contextual bool) *ObjectDiff { + diff := &ObjectDiff{Type: DiffTypeNone, Name: "CheckRestart"} + if reflect.DeepEqual(old, new) { + return nil + } else if old == nil { + diff.Type = DiffTypeAdded + } else if new == nil { + diff.Type = DiffTypeDeleted + } else { + diff.Type = DiffTypeEdited + } + oldFlat := flatmap.Flatten(old, nil, false) + newFlat := flatmap.Flatten(new, nil, false) + diff.Fields = fieldDiffs(oldFlat, newFlat, contextual) + return diff +} + // serviceCheckDiffs diffs a set of service checks. If contextual diff is // enabled, unchanged fields within objects nested in the tasks will be // returned. diff --git a/nomad/structs/diff_test.go b/nomad/structs/diff_test.go index f41692ba3..eff0d2fca 100644 --- a/nomad/structs/diff_test.go +++ b/nomad/structs/diff_test.go @@ -3573,6 +3573,192 @@ func TestTaskDiff(t *testing.T) { }, }, }, + { + Name: "CheckRestart edited", + Old: &Task{ + Services: []*Service{ + { + Name: "foo", + Checks: []*ServiceCheck{ + { + Name: "foo", + Type: "http", + Command: "foo", + Args: []string{"foo"}, + Path: "foo", + Protocol: "http", + Interval: 1 * time.Second, + Timeout: 1 * time.Second, + }, + { + Name: "bar", + Type: "http", + Command: "foo", + Args: []string{"foo"}, + Path: "foo", + Protocol: "http", + Interval: 1 * time.Second, + Timeout: 1 * time.Second, + CheckRestart: &CheckRestart{ + Limit: 2, + Grace: 2 * time.Second, + }, + }, + { + Name: "baz", + Type: "http", + Command: "foo", + Args: []string{"foo"}, + Path: "foo", + Protocol: "http", + Interval: 1 * time.Second, + Timeout: 1 * time.Second, + CheckRestart: &CheckRestart{ + Limit: 3, + Grace: 3 * time.Second, + }, + }, + }, + }, + }, + }, + New: &Task{ + Services: []*Service{ + { + Name: "foo", + Checks: []*ServiceCheck{ + { + Name: "foo", + Type: "http", + Command: "foo", + Args: []string{"foo"}, + Path: "foo", + Protocol: "http", + Interval: 1 * time.Second, + Timeout: 1 * time.Second, + CheckRestart: &CheckRestart{ + Limit: 1, + Grace: 1 * time.Second, + }, + }, + { + Name: "bar", + Type: "http", + Command: "foo", + Args: []string{"foo"}, + Path: "foo", + Protocol: "http", + Interval: 1 * time.Second, + Timeout: 1 * time.Second, + }, + { + Name: "baz", + Type: "http", + Command: "foo", + Args: []string{"foo"}, + Path: "foo", + Protocol: "http", + Interval: 1 * time.Second, + Timeout: 1 * time.Second, + CheckRestart: &CheckRestart{ + Limit: 4, + Grace: 4 * time.Second, + }, + }, + }, + }, + }, + }, + Expected: &TaskDiff{ + Type: DiffTypeEdited, + Objects: []*ObjectDiff{ + { + Type: DiffTypeEdited, + Name: "Service", + Objects: []*ObjectDiff{ + { + Type: DiffTypeEdited, + Name: "Check", + Objects: []*ObjectDiff{ + { + Type: DiffTypeEdited, + Name: "CheckRestart", + Fields: []*FieldDiff{ + { + Type: DiffTypeEdited, + Name: "Grace", + Old: "3000000000", + New: "4000000000", + }, + { + Type: DiffTypeEdited, + Name: "Limit", + Old: "3", + New: "4", + }, + }, + }, + }, + }, + { + Type: DiffTypeEdited, + Name: "Check", + Objects: []*ObjectDiff{ + { + Type: DiffTypeAdded, + Name: "CheckRestart", + Fields: []*FieldDiff{ + { + Type: DiffTypeAdded, + Name: "Grace", + New: "1000000000", + }, + { + Type: DiffTypeAdded, + Name: "IgnoreWarnings", + New: "false", + }, + { + Type: DiffTypeAdded, + Name: "Limit", + New: "1", + }, + }, + }, + }, + }, + { + Type: DiffTypeEdited, + Name: "Check", + Objects: []*ObjectDiff{ + { + Type: DiffTypeDeleted, + Name: "CheckRestart", + Fields: []*FieldDiff{ + { + Type: DiffTypeDeleted, + Name: "Grace", + Old: "2000000000", + }, + { + Type: DiffTypeDeleted, + Name: "IgnoreWarnings", + Old: "false", + }, + { + Type: DiffTypeDeleted, + Name: "Limit", + Old: "2", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, { Name: "Vault added", Old: &Task{},