Skip to content

Commit

Permalink
Handle values omitted from the API server in arrays of objects
Browse files Browse the repository at this point in the history
In the example of a Pod definition in a Deployment, you could set
`stdin: false` and `tty: false` for the container in a ConfigurationPolicy,
but the API server would omit them from the return value. This would
lead to the merged map and existing map to have a different length,
and causing the comparison to end early. This was wrong in this case
since a missing boolean should be treated the same as `false` set in the
ConfigurationPolicy.

Relates:
https://issues.redhat.com/browse/ACM-8391

Signed-off-by: mprahl <mprahl@users.noreply.github.com>
  • Loading branch information
mprahl authored and openshift-ci[bot] committed Nov 2, 2023
1 parent d916208 commit e88b230
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
28 changes: 28 additions & 0 deletions controllers/configurationpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,34 @@ func TestCheckListsMatch(t *testing.T) {
assert.False(t, checkListsMatch(oneBigOneSmall, twoFullItems))
}

func TestCheckListsMatchDiffMapLength(t *testing.T) {
existingObject := []interface{}{
map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"name": "my-container",
"image": "quay.io/org/test:latest",
},
},
},
}

mergedObject := []interface{}{
map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"name": "my-container",
"image": "quay.io/org/test:latest",
"stdin": false,
"tty": false,
},
},
},
}

assert.True(t, checkListsMatch(existingObject, mergedObject))
}

func TestNestedUnsortedLists(t *testing.T) {
objDefYaml := `
kind: FakeOperator
Expand Down
2 changes: 1 addition & 1 deletion controllers/configurationpolicy_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func checkListsMatch(oldVal []interface{}, mergedVal []interface{}) (m bool) {
case map[string]interface{}:
// if list contains maps, recurse on those maps to check for a match
if mVal, ok := mVal[idx].(map[string]interface{}); ok {
if len(mVal) != len(oNestedVal) || !checkFieldsWithSort(mVal, oNestedVal) {
if !checkFieldsWithSort(mVal, oNestedVal) {
return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ spec:
env:
- name: DEMO_GREETING
value: " \t hello with tricky whitespace \n "
stdin: false
tty: false

0 comments on commit e88b230

Please sign in to comment.