Skip to content

Commit

Permalink
Optimize handleObjects method
Browse files Browse the repository at this point in the history
Signed-off-by: clyang82 <chuyang@redhat.com>
  • Loading branch information
clyang82 committed May 25, 2023
1 parent 0499944 commit 04636ba
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,13 @@ func (r *ConfigurationPolicyReconciler) handleObjectTemplates(plc policyv1.Confi
numCompliant := 0
numNonCompliant := 0
handled := false
// map raw object to a resource, generate a violation if resource cannot be found
mapping, statusUpdateNeeded := r.getMapping(objectT.ObjectDefinition, &plc, indx)
unstruct, err := unmarshalFromJSON(objectT.ObjectDefinition.Raw)
if err != nil {
os.Exit(1)
}

// iterate through all namespaces the configurationpolicy is set on
for _, ns := range relevantNamespaces {
log.Info(
Expand All @@ -1114,10 +1121,16 @@ func (r *ConfigurationPolicyReconciler) handleObjectTemplates(plc policyv1.Confi
"desiredName", templateObjs[indx].name,
"index", indx,
)

names, compliant, reason, objKind, related, statusUpdateNeeded := r.handleObjects(
objectT, ns, templateObjs[indx], indx, &plc,
)
var names []string
var compliant bool
var reason, objKind string
var related []policyv1.RelatedObject

if mapping != nil {
names, compliant, reason, objKind, related, statusUpdateNeeded = r.handleObjects(
objectT, ns, templateObjs[indx], indx, &plc, mapping, unstruct,
)
}

if statusUpdateNeeded {
parentStatusUpdateNeeded = true
Expand Down Expand Up @@ -1429,6 +1442,8 @@ func (r *ConfigurationPolicyReconciler) handleObjects(
objDetails objectTemplateDetails,
index int,
policy *policyv1.ConfigurationPolicy,
mapping *meta.RESTMapping,
unstruct unstructured.Unstructured,
) (
objNameList []string,
compliant bool,
Expand All @@ -1445,19 +1460,6 @@ func (r *ConfigurationPolicyReconciler) handleObjects(
log.V(2).Info("Handling object template, no namespace specified")
}

ext := objectT.ObjectDefinition

// map raw object to a resource, generate a violation if resource cannot be found
mapping, statusUpdateNeeded := r.getMapping(ext, policy, index)
if mapping == nil {
return nil, false, "", "", nil, statusUpdateNeeded
}

unstruct, err := unmarshalFromJSON(ext.Raw)
if err != nil {
os.Exit(1)
}

exists := true
objNames := []string{}
remediation := policy.Spec.RemediationAction
Expand Down

0 comments on commit 04636ba

Please sign in to comment.