Skip to content

Commit

Permalink
Handle objects that have omitted empty values (#244)
Browse files Browse the repository at this point in the history
If a policy specifies an empty array or map in its object definition
and the property is set to `omitEmpty`, then the controller will
mark the policy as noncompliant when it compares the policy with the
object retrieved from the API since the property wouldn't be returned.

This commit makes it so that an omitted array or map value is equal to
it being empty.

Signed-off-by: mprahl <mprahl@users.noreply.github.com>
  • Loading branch information
mprahl committed Mar 30, 2022
1 parent fe7bc31 commit a663462
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions controllers/configurationpolicy_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,15 @@ func checkFieldsWithSort(mergedObj map[string]interface{}, oldObj map[string]int
case map[string]interface{}:
// if field is a map, recurse to check for a match
oVal, ok := oldObj[i].(map[string]interface{})
if !ok || !checkFieldsWithSort(mVal, oVal) {
if !ok {
if len(mVal) == 0 {
break
}

return false
}

if !checkFieldsWithSort(mVal, oVal) {
return false
}
case []map[string]interface{}:
Expand All @@ -127,7 +135,15 @@ func checkFieldsWithSort(mergedObj map[string]interface{}, oldObj map[string]int
case []interface{}:
// if field is a generic list, sort and iterate through them to make sure each value matches
oVal, ok := oldObj[i].([]interface{})
if !ok || len(mVal) != len(oVal) || !checkListsMatch(oVal, mVal) {
if !ok {
if len(mVal) == 0 {
break
}

return false
}

if len(mVal) != len(oVal) || !checkListsMatch(oVal, mVal) {
return false
}
case string:
Expand Down

0 comments on commit a663462

Please sign in to comment.