From df9ce2680b22b434af4111c81c0ec299b31fc0ac Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 21 Mar 2017 11:31:06 -0700 Subject: [PATCH 1/2] Fix output alignment and remove no-change DC Old Output: ``` +/- Job: "example" Datacenters { Datacenters: "dc1" } +/- Task Group: "cache" (1 create/destroy update) +/- RestartPolicy { +/- Attempts: "10" => "9" Delay: "25000000000" Interval: "300000000000" Mode: "delay" } +/- EphemeralDisk { Migrate: "false" +/- SizeMB: "300" => "301" Sticky: "false" } +/- Task: "redis" (forces create/destroy update) + Meta[key]: "value" +/- Config { image: "redis:3.2" +/- port_map[0][db]: "6379" => "6380" } +/- Resources { CPU: "500" DiskMB: "0" IOPS: "0" +/- MemoryMB: "256" => "257" } +/- Service { Name: "global-redis-check" PortLabel: "db" +/- Check { Command: "" InitialStatus: "" Interval: "10000000000" Name: "alive" Path: "" PortLabel: "" Protocol: "" +/- Timeout: "2000000000" => "3000000000" Type: "tcp" } } ``` New Output: ``` +/- Job: "example" +/- Task Group: "cache" (1 create/destroy update) +/- RestartPolicy { +/- Attempts: "10" => "9" Delay: "25000000000" Interval: "300000000000" Mode: "delay" } +/- EphemeralDisk { Migrate: "false" +/- SizeMB: "300" => "301" Sticky: "false" } +/- Task: "redis" (forces create/destroy update) + Meta[key]: "value" +/- Config { image: "redis:3.2" +/- port_map[0][db]: "6379" => "6380" } +/- Resources { CPU: "500" DiskMB: "0" IOPS: "0" +/- MemoryMB: "256" => "257" } +/- Service { Name: "global-redis-check" PortLabel: "db" +/- Check { Command: "" InitialStatus: "" Interval: "10000000000" Name: "alive" Path: "" PortLabel: "" Protocol: "" +/- Timeout: "2000000000" => "3000000000" Type: "tcp" } } ``` --- command/plan.go | 8 +++++--- nomad/structs/diff.go | 2 +- nomad/structs/diff_test.go | 20 -------------------- 3 files changed, 6 insertions(+), 24 deletions(-) diff --git a/command/plan.go b/command/plan.go index a9fab5fde..7526eaf4a 100644 --- a/command/plan.go +++ b/command/plan.go @@ -353,15 +353,17 @@ func formatTaskDiff(task *api.TaskDiff, startPrefix, taskPrefix int, verbose boo // of spaces to put between the marker and object name output. func formatObjectDiff(diff *api.ObjectDiff, startPrefix, keyPrefix int) string { start := strings.Repeat(" ", startPrefix) - marker, _ := getDiffString(diff.Type) + marker, markerLen := getDiffString(diff.Type) out := fmt.Sprintf("%s%s%s%s {\n", start, marker, strings.Repeat(" ", keyPrefix), diff.Name) // Determine the length of the longest name and longest diff marker to // properly align names and values longestField, longestMarker := getLongestPrefixes(diff.Fields, diff.Objects) - subStartPrefix := startPrefix + 2 + subStartPrefix := startPrefix + keyPrefix + 2 out += alignedFieldAndObjects(diff.Fields, diff.Objects, subStartPrefix, longestField, longestMarker) - return fmt.Sprintf("%s\n%s}", out, start) + + endprefix := strings.Repeat(" ", startPrefix+markerLen+keyPrefix) + return fmt.Sprintf("%s\n%s}", out, endprefix) } // formatFieldDiff produces an annotated diff of a field. startPrefix is the diff --git a/nomad/structs/diff.go b/nomad/structs/diff.go index 2e3751cfe..0c2d943c7 100644 --- a/nomad/structs/diff.go +++ b/nomad/structs/diff.go @@ -98,7 +98,7 @@ func (j *Job) Diff(other *Job, contextual bool) (*JobDiff, error) { diff.Fields = fieldDiffs(oldPrimitiveFlat, newPrimitiveFlat, false) // Datacenters diff - if setDiff := stringSetDiff(j.Datacenters, other.Datacenters, "Datacenters", contextual); setDiff != nil { + if setDiff := stringSetDiff(j.Datacenters, other.Datacenters, "Datacenters", false); setDiff != nil { diff.Objects = append(diff.Objects, setDiff) } diff --git a/nomad/structs/diff_test.go b/nomad/structs/diff_test.go index 3f3c35042..10c589e31 100644 --- a/nomad/structs/diff_test.go +++ b/nomad/structs/diff_test.go @@ -407,26 +407,6 @@ func TestJobDiff(t *testing.T) { }, Expected: &JobDiff{ Type: DiffTypeNone, - Objects: []*ObjectDiff{ - { - Type: DiffTypeNone, - Name: "Datacenters", - Fields: []*FieldDiff{ - { - Type: DiffTypeNone, - Name: "Datacenters", - Old: "bar", - New: "bar", - }, - { - Type: DiffTypeNone, - Name: "Datacenters", - Old: "foo", - New: "foo", - }, - }, - }, - }, }, }, { From 3b323603b52abd36c3ec7f2c3f9d903211b2f314 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 21 Mar 2017 11:42:10 -0700 Subject: [PATCH 2/2] Fix diff alignment and remove no change DC Old Output: ``` +/- Job: "example" Datacenters { Datacenters: "dc1" } +/- Task Group: "cache" (1 create/destroy update) +/- RestartPolicy { +/- Attempts: "10" => "9" Delay: "25000000000" Interval: "300000000000" Mode: "delay" } +/- EphemeralDisk { Migrate: "false" +/- SizeMB: "300" => "301" Sticky: "false" } +/- Task: "redis" (forces create/destroy update) + Meta[key]: "value" +/- Config { image: "redis:3.2" +/- port_map[0][db]: "6379" => "6380" } +/- Resources { CPU: "500" DiskMB: "0" IOPS: "0" +/- MemoryMB: "256" => "257" } +/- Service { Name: "global-redis-check" PortLabel: "db" +/- Check { Command: "" InitialStatus: "" Interval: "10000000000" Name: "alive" Path: "" PortLabel: "" Protocol: "" +/- Timeout: "2000000000" => "3000000000" Type: "tcp" } } ``` New Output: ``` +/- Job: "example" +/- Task Group: "cache" (1 create/destroy update) +/- RestartPolicy { +/- Attempts: "10" => "9" Delay: "25000000000" Interval: "300000000000" Mode: "delay" } +/- EphemeralDisk { Migrate: "false" +/- SizeMB: "300" => "301" Sticky: "false" } +/- Task: "redis" (forces create/destroy update) + Meta[key]: "value" +/- Config { image: "redis:3.2" +/- port_map[0][db]: "6379" => "6380" } +/- Resources { CPU: "500" DiskMB: "0" IOPS: "0" +/- MemoryMB: "256" => "257" } +/- Service { Name: "global-redis-check" PortLabel: "db" +/- Check { Command: "" InitialStatus: "" Interval: "10000000000" Name: "alive" Path: "" PortLabel: "" Protocol: "" +/- Timeout: "2000000000" => "3000000000" Type: "tcp" } } ``` --- nomad/structs/diff.go | 2 +- nomad/structs/diff_test.go | 41 +++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/nomad/structs/diff.go b/nomad/structs/diff.go index 0c2d943c7..1d057b824 100644 --- a/nomad/structs/diff.go +++ b/nomad/structs/diff.go @@ -98,7 +98,7 @@ func (j *Job) Diff(other *Job, contextual bool) (*JobDiff, error) { diff.Fields = fieldDiffs(oldPrimitiveFlat, newPrimitiveFlat, false) // Datacenters diff - if setDiff := stringSetDiff(j.Datacenters, other.Datacenters, "Datacenters", false); setDiff != nil { + if setDiff := stringSetDiff(j.Datacenters, other.Datacenters, "Datacenters", contextual); setDiff != nil && setDiff.Type != DiffTypeNone { diff.Objects = append(diff.Objects, setDiff) } diff --git a/nomad/structs/diff_test.go b/nomad/structs/diff_test.go index 10c589e31..1b8f7a946 100644 --- a/nomad/structs/diff_test.go +++ b/nomad/structs/diff_test.go @@ -397,7 +397,7 @@ func TestJobDiff(t *testing.T) { }, }, { - // Datacenter contextual + // Datacenter contextual no change Contextual: true, Old: &Job{ Datacenters: []string{"foo", "bar"}, @@ -409,6 +409,45 @@ func TestJobDiff(t *testing.T) { Type: DiffTypeNone, }, }, + { + // Datacenter contextual + Contextual: true, + Old: &Job{ + Datacenters: []string{"foo", "bar"}, + }, + New: &Job{ + Datacenters: []string{"foo", "bar", "baz"}, + }, + Expected: &JobDiff{ + Type: DiffTypeEdited, + Objects: []*ObjectDiff{ + { + Type: DiffTypeAdded, + Name: "Datacenters", + Fields: []*FieldDiff{ + { + Type: DiffTypeAdded, + Name: "Datacenters", + Old: "", + New: "baz", + }, + { + Type: DiffTypeNone, + Name: "Datacenters", + Old: "bar", + New: "bar", + }, + { + Type: DiffTypeNone, + Name: "Datacenters", + Old: "foo", + New: "foo", + }, + }, + }, + }, + }, + }, { // Update strategy edited Old: &Job{