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

Bug : Objects managed by ConfigurationPolicies are left behind if they are renamed #115

Merged
Show file tree
Hide file tree
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
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