From 05177fb233bd6f16ddd313ed8a66a029cceee960 Mon Sep 17 00:00:00 2001 From: Justin Kulikauskas Date: Thu, 7 Sep 2023 10:34:10 -0400 Subject: [PATCH] Refactor: better uid lookup In both instances, the object is already assured to be non-nil, so there are no errors that can occur here. Signed-off-by: Justin Kulikauskas --- controllers/configurationpolicy_controller.go | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/controllers/configurationpolicy_controller.go b/controllers/configurationpolicy_controller.go index 7cf55e66..f68a6daf 100644 --- a/controllers/configurationpolicy_controller.go +++ b/controllers/configurationpolicy_controller.go @@ -608,14 +608,10 @@ func (r *ConfigurationPolicyReconciler) cleanUpChildObjects(plc policyv1.Configu } else if string(plc.Spec.PruneObjectBehavior) == "DeleteIfCreated" { // if prune behavior is DeleteIfCreated, we need to check whether createdByPolicy // is true and the UID is not stale - uid, uidFound, err := unstructured.NestedString(existing.Object, "metadata", "uid") - - if !uidFound || err != nil { - log.Error(err, "Tried to pull UID from obj but the field did not exist or was not a string") - } else if object.Properties != nil && + if object.Properties != nil && object.Properties.CreatedByPolicy != nil && *object.Properties.CreatedByPolicy && - object.Properties.UID == uid { + object.Properties.UID == string(existing.GetUID()) { needsDelete = true } } @@ -2061,13 +2057,7 @@ func (r *ConfigurationPolicyReconciler) enforceByCreatingOrDeleting(obj singleOb reason = reasonWantFoundCreated msg = fmt.Sprintf("%v %v was created successfully", obj.gvr.Resource, idStr) - var uidIsString bool - uid, uidIsString, err = unstructured.NestedString(createdObj.Object, "metadata", "uid") - - if !uidIsString || err != nil { - log.Error(err, "Tried to set UID in status but the field is not a string") - } - + uid = string(createdObj.GetUID()) completed = true } } else {