Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the copying when evaluating a policy #156

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2166,32 +2166,22 @@ func deleteObject(res dynamic.ResourceInterface, name, namespace string) (delete
return true, nil
}

// mergeSpecs is a wrapper for the recursive function to merge 2 maps. It marshals the objects into JSON
// to make sure they are valid objects before calling the merge function
// mergeSpecs is a wrapper for the recursive function to merge 2 maps.
func mergeSpecs(templateVal, existingVal interface{}, ctype string) (interface{}, error) {
// Copy templateVal since it will be modified in mergeSpecsHelper
data1, err := json.Marshal(templateVal)
if err != nil {
return nil, err
}

data2, err := json.Marshal(existingVal)
if err != nil {
return nil, err
}

var j1, j2 interface{}
var j1 interface{}

err = json.Unmarshal(data1, &j1)
if err != nil {
return nil, err
}

err = json.Unmarshal(data2, &j2)
if err != nil {
return nil, err
}

return mergeSpecsHelper(j1, j2, ctype), nil
return mergeSpecsHelper(j1, existingVal, ctype), nil
}

// mergeSpecsHelper is a helper function that takes an object from the existing object and merges in
Expand Down Expand Up @@ -2570,6 +2560,8 @@ func (r *ConfigurationPolicyReconciler) checkAndUpdateResource(
}

updateSucceeded = false
// Use a copy since some values can be directly assigned to mergedObj in handleSingleKey.
existingObjectCopy := obj.object.DeepCopy()

for key := range obj.unstruct.Object {
isStatus := key == "status"
Expand All @@ -2582,7 +2574,7 @@ func (r *ConfigurationPolicyReconciler) checkAndUpdateResource(

// check key for mismatch
errorMsg, keyUpdateNeeded, mergedObj, skipped := handleSingleKey(
key, obj.unstruct, obj.object, keyComplianceType,
key, obj.unstruct, existingObjectCopy, keyComplianceType,
)
if errorMsg != "" {
log.Info(errorMsg)
Expand Down