Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using specific object ID on diff #11400

Merged
merged 3 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/11400.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
cli: Improve `nomad job plan` output for `artifact` and `template` changes
```
34 changes: 26 additions & 8 deletions nomad/structs/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import (
"github.com/mitchellh/hashstructure"
)

// DiffableWithID defines an object that have an unique and stable field that
// can be used as an identifier when generating a diff.
type DiffableWithID interface {
// DiffID returns the value to use to match entities between the old and
// the new input.
DiffID() string
}

// DiffType denotes the type of a diff object.
type DiffType string

Expand Down Expand Up @@ -2419,11 +2427,20 @@ func primitiveObjectSetDiff(old, new []interface{}, filter []string, name string
makeSet := func(objects []interface{}) map[string]interface{} {
objMap := make(map[string]interface{}, len(objects))
for _, obj := range objects {
hash, err := hashstructure.Hash(obj, nil)
if err != nil {
panic(err)
var key string

if diffable, ok := obj.(DiffableWithID); ok {
key = diffable.DiffID()
}
objMap[fmt.Sprintf("%d", hash)] = obj

if key == "" {
hash, err := hashstructure.Hash(obj, nil)
if err != nil {
panic(err)
}
key = fmt.Sprintf("%d", hash)
}
objMap[key] = obj
}

return objMap
Expand All @@ -2433,10 +2450,11 @@ func primitiveObjectSetDiff(old, new []interface{}, filter []string, name string
newSet := makeSet(new)

var diffs []*ObjectDiff
for k, v := range oldSet {
// Deleted
if _, ok := newSet[k]; !ok {
diffs = append(diffs, primitiveObjectDiff(v, nil, filter, name, contextual))
for k, oldObj := range oldSet {
newObj := newSet[k]
diff := primitiveObjectDiff(oldObj, newObj, filter, name, contextual)
if diff != nil {
diffs = append(diffs, diff)
}
}
for k, v := range newSet {
Expand Down
28 changes: 26 additions & 2 deletions nomad/structs/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4459,7 +4459,7 @@ func TestTaskDiff(t *testing.T) {
New: &Task{
Artifacts: []*TaskArtifact{
{
GetterSource: "foo",
GetterSource: "foo/bar",
GetterOptions: map[string]string{
"foo": "bar",
},
Expand All @@ -4482,6 +4482,18 @@ func TestTaskDiff(t *testing.T) {
Expected: &TaskDiff{
Type: DiffTypeEdited,
Objects: []*ObjectDiff{
{
Type: DiffTypeEdited,
Name: "Artifact",
Fields: []*FieldDiff{
{
Type: DiffTypeEdited,
Name: "GetterSource",
Old: "foo",
New: "foo/bar",
},
},
},
{
Type: DiffTypeAdded,
Name: "Artifact",
Expand Down Expand Up @@ -6914,7 +6926,7 @@ func TestTaskDiff(t *testing.T) {
{
SourcePath: "foo",
DestPath: "bar",
EmbeddedTmpl: "baz",
EmbeddedTmpl: "baz new",
ChangeMode: "bam",
ChangeSignal: "SIGHUP",
Splay: 1,
Expand All @@ -6934,6 +6946,18 @@ func TestTaskDiff(t *testing.T) {
Expected: &TaskDiff{
Type: DiffTypeEdited,
Objects: []*ObjectDiff{
{
Type: DiffTypeEdited,
Name: "Template",
Fields: []*FieldDiff{
{
Type: DiffTypeEdited,
Name: "EmbeddedTmpl",
Old: "baz",
New: "baz new",
},
},
},
{
Type: DiffTypeAdded,
Name: "Template",
Expand Down
10 changes: 10 additions & 0 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7479,6 +7479,11 @@ func (t *Template) Warnings() error {
return mErr.ErrorOrNil()
}

// DiffID fulfills the DiffableWithID interface.
func (t *Template) DiffID() string {
return t.DestPath
}

// AllocState records a single event that changes the state of the whole allocation
type AllocStateField uint8

Expand Down Expand Up @@ -8120,6 +8125,11 @@ func (ta *TaskArtifact) GoString() string {
return fmt.Sprintf("%+v", ta)
}

// DiffID fulfills the DiffableWithID interface.
func (ta *TaskArtifact) DiffID() string {
return ta.RelativeDest
}

// hashStringMap appends a deterministic hash of m onto h.
func hashStringMap(h hash.Hash, m map[string]string) {
keys := make([]string, 0, len(m))
Expand Down