-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This Commit, - enhances update event notifications with details of what Spec is getting changed - adds custom `SpecDiffReporter` and `utils.Diff()` method. - adds update events in all config.yaml files - adds unit-test for utils.Diff() - adds update events for Deployment, Statefulset and Job by default.
- Loading branch information
Showing
11 changed files
with
150 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
// SpecDiffReporter is a simple custom reporter that only records differences | ||
// detected in Object Spec during comparison. | ||
type SpecDiffReporter struct { | ||
path cmp.Path | ||
diffs []string | ||
} | ||
|
||
// PushStep custom implements Reporter interface | ||
func (r *SpecDiffReporter) PushStep(ps cmp.PathStep) { | ||
r.path = append(r.path, ps) | ||
} | ||
|
||
// Report custom implements Reporter interface | ||
func (r *SpecDiffReporter) Report(rs cmp.Result) { | ||
if !rs.Equal() { | ||
vx, vy := r.path.Last().Values() | ||
path := fmt.Sprintf("%#v", r.path) | ||
if ok := strings.Contains(path, ".Spec."); ok { | ||
r.diffs = append(r.diffs, fmt.Sprintf("%#v:\n\t-: %+v\n\t+: %+v\n", r.path, vx, vy)) | ||
} | ||
} | ||
} | ||
|
||
// PopStep custom implements Reporter interface | ||
func (r *SpecDiffReporter) PopStep() { | ||
r.path = r.path[:len(r.path)-1] | ||
} | ||
|
||
// String custom implements Reporter interface | ||
func (r *SpecDiffReporter) String() string { | ||
return strings.Join(r.diffs, "\n") | ||
} | ||
|
||
// Diff provides differences between two objects spec | ||
func Diff(x, y interface{}) string { | ||
var r SpecDiffReporter | ||
cmp.Equal(x, y, cmp.Reporter(&r)) | ||
return r.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
// Object mocks kubernetes objects | ||
type Object struct { | ||
Spec Spec | ||
Other Other | ||
} | ||
|
||
// Other mocks fileds like MetaData, Status etc in kubernetes objects | ||
type Other struct { | ||
Foo string | ||
} | ||
|
||
// Spec mocks ObjectSpec field in kubernetes object | ||
type Spec struct { | ||
Port int | ||
} | ||
|
||
// ExpectedDiff struct to generate expected diff | ||
type ExpectedDiff struct { | ||
Path string | ||
X string | ||
Y string | ||
} | ||
|
||
func TestDiff(t *testing.T) { | ||
tests := map[string]struct { | ||
old Object | ||
new Object | ||
expected ExpectedDiff | ||
}{ | ||
`Spec Diff`: { | ||
old: Object{Spec: Spec{Port: 81}, Other: Other{Foo: "bar"}}, | ||
new: Object{Spec: Spec{Port: 83}, Other: Other{Foo: "bar"}}, | ||
expected: ExpectedDiff{ | ||
Path: "{utils.Object}.Spec.Port", | ||
X: "81", | ||
Y: "83", | ||
}, | ||
}, | ||
`Non Spec Diff`: { | ||
old: Object{Spec: Spec{Port: 81}, Other: Other{Foo: "bar"}}, | ||
new: Object{Spec: Spec{Port: 81}, Other: Other{Foo: "boo"}}, | ||
expected: ExpectedDiff{}, | ||
}, | ||
} | ||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
if actual := Diff(test.old, test.new); actual != test.expected.MockDiff() { | ||
t.Errorf("expected: %+v != actual: %+v\n", test.expected.MockDiff(), actual) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// MockDiff mocks utils.Diff | ||
func (e *ExpectedDiff) MockDiff() string { | ||
if e.Path == "" { | ||
return "" | ||
} | ||
return fmt.Sprintf("%+v:\n\t-: %+v\n\t+: %+v\n", e.Path, e.X, e.Y) | ||
} |