Skip to content

Commit

Permalink
Remove more type assertions
Browse files Browse the repository at this point in the history
Some of these may be bordering on paranoia, and my search was not
totally exhaustive... but this should make me feel a little bit safer.

Signed-off-by: Justin Kulikauskas <jkulikau@redhat.com>
  • Loading branch information
JustinKuli committed Apr 12, 2023
1 parent 03dfa92 commit e58df3c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
15 changes: 10 additions & 5 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1978,7 +1978,7 @@ func buildNameList(
}

if match {
kindNameList = append(kindNameList, uObj.Object["metadata"].(map[string]interface{})["name"].(string))
kindNameList = append(kindNameList, uObj.GetName())
}
}

Expand Down Expand Up @@ -2594,10 +2594,15 @@ func (r *ConfigurationPolicyReconciler) checkAndUpdateResource(

// only look at labels and annotations for metadata - configurationPolicies do not update other metadata fields
if key == "metadata" {
mergedAnnotations := mergedObj.(map[string]interface{})["annotations"]
mergedLabels := mergedObj.(map[string]interface{})["labels"]
obj.object.UnstructuredContent()["metadata"].(map[string]interface{})["annotations"] = mergedAnnotations
obj.object.UnstructuredContent()["metadata"].(map[string]interface{})["labels"] = mergedLabels
// if it's not the right type, the map will be empty
mdMap, _ := mergedObj.(map[string]interface{})

// if either isn't found, they'll just be empty
mergedAnnotations, _, _ := unstructured.NestedStringMap(mdMap, "annotations")
mergedLabels, _, _ := unstructured.NestedStringMap(mdMap, "labels")

obj.object.SetAnnotations(mergedAnnotations)
obj.object.SetLabels(mergedLabels)
} else {
obj.object.UnstructuredContent()[key] = mergedObj
}
Expand Down
30 changes: 17 additions & 13 deletions controllers/configurationpolicy_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,13 @@ func addRelatedObjects(
// unmarshalFromJSON unmarshals raw JSON data into an object
func unmarshalFromJSON(rawData []byte) (unstructured.Unstructured, error) {
var unstruct unstructured.Unstructured
var blob interface{}

if jsonErr := json.Unmarshal(rawData, &blob); jsonErr != nil {
if jsonErr := json.Unmarshal(rawData, &unstruct.Object); jsonErr != nil {
log.Error(jsonErr, "Could not unmarshal data from JSON")

return unstruct, jsonErr
}

unstruct.Object = make(map[string]interface{})
unstruct.Object = blob.(map[string]interface{})

return unstruct, nil
}

Expand Down Expand Up @@ -113,13 +109,17 @@ func updateRelatedObjectsStatus(
func equalObjWithSort(mergedObj interface{}, oldObj interface{}) (areEqual bool) {
switch mergedObj := mergedObj.(type) {
case map[string]interface{}:
if oldObj == nil || !checkFieldsWithSort(mergedObj, oldObj.(map[string]interface{})) {
return false
if oldObjMap, ok := oldObj.(map[string]interface{}); ok {
return checkFieldsWithSort(mergedObj, oldObjMap)
}
// this includes the case where oldObj is nil
return false
case []interface{}:
if oldObj == nil || !checkListsMatch(mergedObj, oldObj.([]interface{})) {
return false
if oldObjList, ok := oldObj.([]interface{}); ok {
return checkListsMatch(mergedObj, oldObjList)
}

return false
default:
// NOTE: when type is string, int, bool
var oVal interface{}
Expand Down Expand Up @@ -249,7 +249,8 @@ func checkListFieldsWithSort(mergedObj []map[string]interface{}, oldObj []map[st
}
case map[string]interface{}:
// if a map in the list contains another map, check fields for equality
if !checkFieldsWithSort(mVal, oldItem[i].(map[string]interface{})) {
oVal, ok := oldItem[i].(map[string]interface{})
if !ok || len(mVal) != len(oVal) || !checkFieldsWithSort(mVal, oVal) {
return false
}
case string:
Expand Down Expand Up @@ -308,7 +309,8 @@ func checkListsMatch(oldVal []interface{}, mergedVal []interface{}) (m bool) {
switch oNestedVal := oNestedVal.(type) {
case map[string]interface{}:
// if list contains maps, recurse on those maps to check for a match
if !checkFieldsWithSort(mVal[idx].(map[string]interface{}), oNestedVal) {
mVal, ok := mVal[idx].(map[string]interface{})
if !ok || len(mVal) != len(oNestedVal) || !checkFieldsWithSort(mVal, oNestedVal) {
return false
}
default:
Expand Down Expand Up @@ -338,8 +340,10 @@ func filterUnwantedAnnotations(input map[string]interface{}) map[string]interfac
// formatTemplate returns the value of the input key in a manner that the controller can use for comparisons.
func formatTemplate(unstruct unstructured.Unstructured, key string) (obj interface{}) {
if key == "metadata" {
//nolint:forcetypeassert
metadata := unstruct.Object[key].(map[string]interface{})
metadata, ok := unstruct.Object[key].(map[string]interface{})
if !ok {
return metadata // it will just be empty
}

return formatMetadata(metadata)
}
Expand Down

0 comments on commit e58df3c

Please sign in to comment.