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

fix: validate unique names for monitors #666

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft

Conversation

andygodish
Copy link

Description

Added a new piece to the pepr operator's validator function. The new functionality loops through the Package CR's monitor array, adding each description field (or a dummy string in the event of no provided description) to a set, failing given a duplicate.

The sanitizeResourceName() function was used in the validator since its also used downstream to create the ServiceMonitor resource name based on the string from the monitor.description field.

...

Related Issue

Fixes #656

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Other (security config, docs update, etc)

Checklist before merging

@andygodish andygodish requested a review from a team as a code owner August 13, 2024 16:51
Copy link
Contributor

@mjnagel mjnagel left a comment

Choose a reason for hiding this comment

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

Haven't had a chance to fully review the code yet but to fix the one CI failure you'll want to rename your PR title to follow a conventional commit syntax, something like fix: validate unique names for monitors

@andygodish andygodish changed the title Monitor validate fix: validate unique names for monitors Aug 21, 2024
Comment on lines +150 to +152
const monitorName = sanitizeResourceName(
monitor.description || `${Object.values(monitor.selector)}-${monitor.portName}`,
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Realized in retrospect we could just import generateMonitorName and use that here to ensure we keep this logic in sync with our name generation rather than duplicating.

// Ensure serviceMonitors use a unique description or selector/portName used for generating the resource name
const monitorNames = new Set<string>();

for (const monitor of monitors) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Didn't think about this before but I think there's another edge case where podmonitors and servicemonitors should be allowed to have overlapping names, but this would deny them. We may need separate sets per type and check the kind in this loop.

Comment on lines +155 to +157
return req.Deny(
`A serviceMonitor resource generated from the Package, ${pkg.metadata?.name} contains a duplicate name, please provide a unique description for each item in the monitor array`,
);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: might be good to align this with the wording we use for the VirtualService check, and make sure we are capturing kind accurately here (pod vs svc monitor).

@andygodish andygodish marked this pull request as draft August 26, 2024 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Package CR fails to properly create servicemonitor resources when looping through monitor array
2 participants