Skip to content

Commit

Permalink
change unit test
Browse files Browse the repository at this point in the history
Objects managed by ConfigurationPolicies are left behind if they are renamed

The pruneObjectBehavior of a ConfigurationPolicy does not help clean up objects when the policy is edited such that the managed object's name is changed. Ideally, editing the policy would result in deleting the old object, if the pruneObjectBehavior field was set.

add Dale's suggestion

ref: https://issues.redhat.com/browse/ACM-3050

Signed-off-by: Yi Rae Kim <yikim@redhat.com>
  • Loading branch information
yiraeChristineKim committed Mar 30, 2023
1 parent e868a2b commit 13b1d54
Show file tree
Hide file tree
Showing 6 changed files with 562 additions and 20 deletions.
42 changes: 28 additions & 14 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,14 +1136,15 @@ func (r *ConfigurationPolicyReconciler) handleObjectTemplates(plc policyv1.Confi
func (r *ConfigurationPolicyReconciler) checkRelatedAndUpdate(
plc policyv1.ConfigurationPolicy, related, oldRelated []policyv1.RelatedObject, sendEvent bool,
) {
sortRelatedObjectsAndUpdate(&plc, related, oldRelated, r.EnableMetrics)
r.sortRelatedObjectsAndUpdate(&plc, related, oldRelated, r.EnableMetrics)
// An update always occurs to account for the lastEvaluated status field
r.addForUpdate(&plc, sendEvent)
}

// helper function to check whether related objects has changed
func sortRelatedObjectsAndUpdate(
plc *policyv1.ConfigurationPolicy, related, oldRelated []policyv1.RelatedObject, collectMetrics bool,
func (r *ConfigurationPolicyReconciler) sortRelatedObjectsAndUpdate(
plc *policyv1.ConfigurationPolicy, related, oldRelated []policyv1.RelatedObject,
collectMetrics bool,
) {
sort.SliceStable(related, func(i, j int) bool {
if related[i].Object.Kind != related[j].Object.Kind {
Expand All @@ -1156,8 +1157,6 @@ func sortRelatedObjectsAndUpdate(
return related[i].Object.Metadata.Name < related[j].Object.Metadata.Name
})

update := false

// Instantiate found objects for the related object metric
found := map[string]bool{}

Expand Down Expand Up @@ -1215,19 +1214,34 @@ func sortRelatedObjectsAndUpdate(
}
}

if len(oldRelated) == len(related) {
for i, entry := range oldRelated {
if !gocmp.Equal(entry, related[i]) {
update = true
}
if !gocmp.Equal(related, oldRelated) {
r.deleteDetachedObj(*plc, related, oldRelated)
plc.Status.RelatedObjects = related
}
}

// helper function to delete unconnected objs
func (r *ConfigurationPolicyReconciler) deleteDetachedObj(plc policyv1.ConfigurationPolicy,
related, oldRelated []policyv1.RelatedObject,
) []policyv1.RelatedObject {
objShouldRemoved := []policyv1.RelatedObject{}
// Pick out only obj should be removed in oldRelated
for _, oldR := range oldRelated {
isContain := containRelated(related, oldR)

if !isContain {
objShouldRemoved = append(objShouldRemoved, oldR)
}
} else {
update = true
}

if update {
plc.Status.RelatedObjects = related
plc.Status.RelatedObjects = objShouldRemoved

// removed objs which are not related(detached) anymore
if r != nil {
r.cleanUpChildObjects(plc)
}
// For now this is for unit test
return objShouldRemoved
}

// helper function that appends a condition (violation or compliant) to the status of a configurationpolicy
Expand Down
Loading

0 comments on commit 13b1d54

Please sign in to comment.