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

fix: Consider ObjectMeta field for deep derivative comparison before patching #2042

Merged
merged 1 commit into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions pkg/trait/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ func (t *deployerTrait) Apply(e *Environment) error {
return err
}

if !patch.SpecEqualDeepDerivative(object, resource) {
// If both objects have a "Spec" field and it contains all expected fields
// (plus optional others), then avoid patching
// If both objects have "ObjectMeta" and "Spec" fields and they contain all the expected fields
// (plus optional others), then avoid patching.
if !patch.ObjectMetaEqualDeepDerivative(object, resource) ||
!patch.SpecEqualDeepDerivative(object, resource) {

p, err := patch.PositiveMergePatch(object, resource)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions pkg/util/patch/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ func removeNilValues(v reflect.Value, parent reflect.Value) {
}
}

func ObjectMetaEqualDeepDerivative(object runtime.Object, expected runtime.Object) (res bool) {
defer func() {
if r := recover(); r != nil {
res = false
}
}()

if expected == nil {
return true
} else if object == nil {
return false
}

objectMeta := reflect.ValueOf(object).Elem().FieldByName("ObjectMeta").Interface()
expectedMeta := reflect.ValueOf(expected).Elem().FieldByName("ObjectMeta").Interface()

return equality.Semantic.DeepDerivative(expectedMeta, objectMeta)
}

func SpecEqualDeepDerivative(object runtime.Object, expected runtime.Object) (res bool) {
defer func() {
if r := recover(); r != nil {
Expand Down