Skip to content

Commit

Permalink
staging
Browse files Browse the repository at this point in the history
  • Loading branch information
barney-s committed Feb 11, 2025
1 parent 7ea746c commit dc12732
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/controller/instance/delta/delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ func cleanMetadata(obj *unstructured.Unstructured) {
//
// Records a Difference if values don't match or are of different types.
func walkCompare(desired, observed interface{}, path string, differences *[]Difference) {
// Special case: treat empty array and nil as equivalent
if isEmptyArrayOrNil(desired) && isEmptyArrayOrNil(observed) {
return
}

switch d := desired.(type) {
case map[string]interface{}:
e, ok := observed.(map[string]interface{})
Expand All @@ -116,6 +121,10 @@ func walkCompare(desired, observed interface{}, path string, differences *[]Diff
case []interface{}:
e, ok := observed.([]interface{})
if !ok {
// Special case: if desired is empty array and observed is nil, treat as equal
if len(d) == 0 && observed == nil {
return
}
*differences = append(*differences, Difference{
Path: path,
Observed: observed,
Expand All @@ -136,6 +145,17 @@ func walkCompare(desired, observed interface{}, path string, differences *[]Diff
}
}

// isEmptyArrayOrNil returns true if the value is nil or an empty array
func isEmptyArrayOrNil(v interface{}) bool {
if v == nil {
return true
}
if arr, ok := v.([]interface{}); ok && len(arr) == 0 {
return true
}
return false
}

// walkMap compares two maps recursively. For each key in desired:
//
// - If key missing in observed: records a difference
Expand Down

0 comments on commit dc12732

Please sign in to comment.