From d5fb50fae1b7c22c0cddc6128de9df0c2a991538 Mon Sep 17 00:00:00 2001 From: Justin Kulikauskas Date: Tue, 11 Apr 2023 17:10:07 -0400 Subject: [PATCH] Remove more type assertions 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 --- controllers/configurationpolicy_controller.go | 15 +++++++---- controllers/configurationpolicy_utils.go | 26 +++++++++++-------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/controllers/configurationpolicy_controller.go b/controllers/configurationpolicy_controller.go index 77a4354b..03f12b13 100644 --- a/controllers/configurationpolicy_controller.go +++ b/controllers/configurationpolicy_controller.go @@ -1978,7 +1978,7 @@ func buildNameList( } if match { - kindNameList = append(kindNameList, uObj.Object["metadata"].(map[string]interface{})["name"].(string)) + kindNameList = append(kindNameList, uObj.GetName()) } } @@ -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 } diff --git a/controllers/configurationpolicy_utils.go b/controllers/configurationpolicy_utils.go index 64ac8ed5..34a91222 100644 --- a/controllers/configurationpolicy_utils.go +++ b/controllers/configurationpolicy_utils.go @@ -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 } @@ -113,11 +109,15 @@ 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{})) { + if oldObjMap, ok := oldObj.(map[string]interface{}); ok { + return checkFieldsWithSort(mergedObj, oldObjMap) + } else { // includes case where oldObj is nil return false } case []interface{}: - if oldObj == nil || !checkListsMatch(mergedObj, oldObj.([]interface{})) { + if oldObjList, ok := oldObj.([]interface{}); ok { + return checkListsMatch(mergedObj, oldObjList) + } else { return false } default: @@ -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: @@ -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(oVal) || !checkFieldsWithSort(mVal, oNestedVal) { return false } default: @@ -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) }