diff --git a/controllers/configurationpolicy_utils.go b/controllers/configurationpolicy_utils.go index ed38d476..c8acf096 100644 --- a/controllers/configurationpolicy_utils.go +++ b/controllers/configurationpolicy_utils.go @@ -300,13 +300,18 @@ func checkListFieldsWithSort(mergedObj []map[string]interface{}, oldObj []map[st // checkListsMatch is a generic list check that uses an arbitrary sort to ensure it is comparing the right values func checkListsMatch(oldVal []interface{}, mergedVal []interface{}) (m bool) { - oVal := append([]interface{}{}, oldVal...) - mVal := append([]interface{}{}, mergedVal...) + if (oldVal == nil && mergedVal != nil) || (oldVal != nil && mergedVal == nil) { + return false + } - if (oVal == nil && mVal != nil) || (oVal != nil && mVal == nil) { + if len(mergedVal) != len(oldVal) { return false } + // Make copies of the lists, so we can sort them without mutating this function's inputs + oVal := append([]interface{}{}, oldVal...) + mVal := append([]interface{}{}, mergedVal...) + sort.Slice(oVal, func(i, j int) bool { return fmt.Sprintf("%v", oVal[i]) < fmt.Sprintf("%v", oVal[j]) }) @@ -314,10 +319,6 @@ func checkListsMatch(oldVal []interface{}, mergedVal []interface{}) (m bool) { return fmt.Sprintf("%v", mVal[x]) < fmt.Sprintf("%v", mVal[y]) }) - if len(mVal) != len(oVal) { - return false - } - for idx, oNestedVal := range oVal { switch oNestedVal := oNestedVal.(type) { case map[string]interface{}: