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

Add support for custom compliance messages in ConfigurationPolicy #176

Closed
wants to merge 1 commit into from

Conversation

zyjjay
Copy link
Contributor

@zyjjay zyjjay commented Aug 21, 2024

The new .spec.customMesssage.Compliant and spec.customMessage.noncompliant fields of ConfigurationPolicy are now supported. Note that when consolidateManifests is set to true, the custom message configuration should be set at the policy level and not at the manifest level.

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

@mprahl
Copy link
Member

mprahl commented Aug 21, 2024

@zyjjay it seems the inheritance isn't taken into account (e.g. if set on policyDefaults but on the policy, policyDefaults is used). Take a look at the applyDefaults function and account for this new field.

@zyjjay
Copy link
Contributor Author

zyjjay commented Aug 21, 2024

@mprahl Actually I was thinking if the customMessages field is unset in the generated policy, the default message templating will be handled by the config policy controller.

Copy link
Member

@dhaiducek dhaiducek left a comment

Choose a reason for hiding this comment

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

The custom message can also be set at the policyDefaults or policies[] levels, which should propagate down using the applyDefaults() function. And the compliant and noncompliant message overrides should be handled separately. It's more complex but brings greater configurability for users.

I just noticed you can see this (and even reuse some/all of the code logic) for evaluationInterval if you search the repo for that:
https://github.com/search?q=repo%3Aopen-cluster-management-io%2Fpolicy-generator-plugin+evaluationinterval+path%3A%2F%5Einternal%5C%2Fplugin.go%2F&type=code

@@ -1079,6 +1079,10 @@ func (p *Plugin) assertValidConfig() error {
return fmt.Errorf(errorMsgFmt, "severity")
}

if manifest.CustomMessage != nil {
Copy link
Member

Choose a reason for hiding this comment

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

This is fine if the messages are the same.

Suggested change
if manifest.CustomMessage != nil {
if !reflect.DeepEqual(manifest.CustomMessage, policy.CustomMessage) {

@@ -45,6 +45,7 @@ type ConfigurationPolicyOptions struct {
PruneObjectBehavior string `json:"pruneObjectBehavior,omitempty" yaml:"pruneObjectBehavior,omitempty"`
RecordDiff string `json:"recordDiff,omitempty" yaml:"recordDiff,omitempty"`
RecreateOption string `json:"recreateOption,omitempty" yaml:"recreateOption,omitempty"`
CustomMessage *CustomMessage `json:"customMessage,omitempty" yaml:"customMessage,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

Similar to EvaluationInterval, I'm not sure there's a benefit to making this a pointer--unless there was a reason to do so?

Suggested change
CustomMessage *CustomMessage `json:"customMessage,omitempty" yaml:"customMessage,omitempty"`
CustomMessage CustomMessage `json:"customMessage,omitempty" yaml:"customMessage,omitempty"`

Comment on lines 490 to 496
var customMessage *types.CustomMessage
if policyConf.ConsolidateManifests {
customMessage = policyConf.CustomMessage
} else {
customMessage = configPolicyOptionsOverrides.CustomMessage
}

Copy link
Member

Choose a reason for hiding this comment

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

This logic should live in the applyDefaults() function.

Comment on lines 502 to 505
if customMessage != nil {
policySpec["customMessage"] = customMessage
}

Copy link
Member

Choose a reason for hiding this comment

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

This is oversimplified. It should look more like the evaluationInterval conditional further down.

@zyjjay
Copy link
Contributor Author

zyjjay commented Aug 21, 2024

The custom message can also be set at the policyDefaults or policies[] levels, which should propagate down using the applyDefaults() function. And the compliant and noncompliant message overrides should be handled separately. It's more complex but brings greater configurability for users.

I just noticed you can see this (and even reuse some/all of the code logic) for evaluationInterval if you search the repo for that: https://github.com/search?q=repo%3Aopen-cluster-management-io%2Fpolicy-generator-plugin+evaluationinterval+path%3A%2F%5Einternal%5C%2Fplugin.go%2F&type=code

I see, it does seem like a lot of code can be reused. I'm wondering if I should just combine some helper functions together to avoid adding functions with minimal differences (i.e., checking the evaluationInterval.compliant vs. customMessage.compliant fields). Having it separate seems more readable though, and reduces the complexity of each individual function. Any preferences? 🤔

Edit: I went with the latter and kept them separate

The new `.spec.customMesssage.Compliant` and `spec.customMessage.noncompliant`
fields of ConfigurationPolicy are now supported. Note that when consolidateManifests
is set to true, the custom message configuration should be set at the policy level
and not at the manifest level.

ref: https://issues.redhat.com/browse/ACM-13134
Signed-off-by: Jason Zhang <[email protected]>
Copy link

openshift-ci bot commented Aug 22, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: zyjjay
Once this PR has been reviewed and has the lgtm label, please ask for approval from dhaiducek. For more information see the Kubernetes Code Review Process.

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

Needs approval from an approver in each of these files:

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

@mprahl
Copy link
Member

mprahl commented Aug 28, 2024

Superseded by #178

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.

3 participants