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 2d4db2e
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,14 @@ 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 +1122,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 +1443,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 +1461,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 2d4db2e

Please sign in to comment.