Skip to content

Commit

Permalink
fix: Consider ObjectMeta field for deep derivative comparison before …
Browse files Browse the repository at this point in the history
…patching
  • Loading branch information
astefanutti committed Feb 17, 2021
1 parent c742331 commit a656a8b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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

0 comments on commit a656a8b

Please sign in to comment.