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

Consolidate compliance messages #323

Conversation

yiraeChristineKim
Copy link
Contributor

@yiraeChristineKim yiraeChristineKim commented Dec 6, 2024

Currently the compliance message is listed out for each object from the object selector. Ideally these messages would be consolidated for better consumption and readability.

Definition of Done for Engineering Story Owner (Checklist)
Status messages from the objectSelector are consolidated rather than listed out individually for each name.

Only combine messages for the same object-template (when it uses a resource selector, there can be multiple objects)

example of change:
configmaps [rex, woody] found but not as specified in namespace toy-story1; configmaps [buzz, potato] found but not as specified in namespace toy-story2
Ref: https://issues.redhat.com/browse/ACM-15773

@openshift-ci openshift-ci bot requested review from dhaiducek and gparvin December 6, 2024 19:05
@openshift-ci openshift-ci bot added the approved label Dec 6, 2024
@yiraeChristineKim yiraeChristineKim marked this pull request as draft December 6, 2024 19:07
@yiraeChristineKim yiraeChristineKim force-pushed the ACM-15773 branch 2 times, most recently from 89ba6b1 to 9bfe0f2 Compare December 9, 2024 18:06
@yiraeChristineKim yiraeChristineKim marked this pull request as ready for review December 9, 2024 18:44
@openshift-ci openshift-ci bot requested a review from JustinKuli December 9, 2024 18:45
@yiraeChristineKim
Copy link
Contributor Author

/cc @JustinKuli

Copy link
Member

@JustinKuli JustinKuli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some small things, but overall this is working great

Comment on lines 684 to 687
for j, nsName := range objectNameStrsToNsNames[namesStr] {
if j != 0 {
compliancyDetailsMsg = setCompliancyDetailsMsgEnd(compliancyDetailsMsg)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If j == 0, then the current message would be empty, right? So then setCompliancyDetailsMsgEnd would not add a semicolon?

I think checking the index and the message in setCompliancyDetailsMsgEnd is redundant. I'd prefer to just use the index, and add the semicolon directly:

Suggested change
for j, nsName := range objectNameStrsToNsNames[namesStr] {
if j != 0 {
compliancyDetailsMsg = setCompliancyDetailsMsgEnd(compliancyDetailsMsg)
}
for j, nsName := range objectNameStrsToNsNames[namesStr] {
if j != 0 {
compliancyDetailsMsg += "; "
}

Comment on lines 611 to 626
for _, msgTemplate := range msgTemplates {
names := msgMap[msgTemplate]
slices.Sort(names) // Sort names for consistent output

compliancyDetailsMsg = setCompliancyDetailsMsgEnd(compliancyDetailsMsg)

joinedNames := strings.Join(names, ", ")
if joinedNames != "" {
joinedNames = fmt.Sprintf(" [%s]", joinedNames)
}

compliancyDetailsMsg += fmt.Sprintf(msgTemplate, resourceName, joinedNames)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the index approach works, I think that would be more clear here as well:

Suggested change
for _, msgTemplate := range msgTemplates {
names := msgMap[msgTemplate]
slices.Sort(names) // Sort names for consistent output
compliancyDetailsMsg = setCompliancyDetailsMsgEnd(compliancyDetailsMsg)
joinedNames := strings.Join(names, ", ")
if joinedNames != "" {
joinedNames = fmt.Sprintf(" [%s]", joinedNames)
}
compliancyDetailsMsg += fmt.Sprintf(msgTemplate, resourceName, joinedNames)
}
for j, msgTemplate := range msgTemplates {
names := msgMap[msgTemplate]
slices.Sort(names) // Sort names for consistent output
if j != 0 {
compliancyDetailsMsg += "; "
}
joinedNames := strings.Join(names, ", ")
if joinedNames != "" {
joinedNames = fmt.Sprintf(" [%s]", joinedNames)
}
compliancyDetailsMsg += fmt.Sprintf(msgTemplate, resourceName, joinedNames)
}


// This prevents repeating the same reason for each unique object name list.
// Process the object name strings in order to ensure a deterministic reason and message.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: since this function returns a map (and lookups from maps in go can be in an inconsistent order), it is the getCombinedCompliancyDetailsMsg and createStatus functions which ensures they are in a deterministic order.

Also, I just want to thank you for bringing this into a separate function! It really helps readability of createStatus 🥇

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, how about move "// Process the object name strings in order to ensure a deterministic reason and message." above "getCombinedCompliancyDetailsMsg" as function comment

policy.yaml Outdated
Comment on lines 21 to 36
# - complianceType: musthave
# namespaceSelector:
# matchExpressions:
# - key: yikim
# operator: Exists
# objectSelector:
# matchExpressions:
# - key: yikim
# operator: Exists
# objectDefinition:
# apiVersion: config-policy-controller.io/v1
# kind: FakeAPI
# metadata:
# labels:
# selected: "true"
# namespace: case42-e2e-2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these commented bits necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OH my god this should be deleted

Comment on lines +987 to +989
"configmaps [rex, woody] found but not as specified in namespace toy-story1; " +
"configmaps [buzz, potato] found but not as specified in namespace toy-story2",
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love when the GH review begins with an easy-to-read test! This looks great

@openshift-ci openshift-ci bot added the lgtm label Dec 10, 2024
Copy link

openshift-ci bot commented Dec 10, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: JustinKuli, yiraeChristineKim

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [JustinKuli,yiraeChristineKim]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants