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: reject monitors with invalid relabel configs #5841

Conversation

simonpasquier
Copy link
Contributor

@simonpasquier simonpasquier commented Aug 23, 2023

Description

I noticed that probes, service monitors, pod monitors and scrape configs with invalid relabel configs were not rejected by the operator. This PR fixes it.

Type of change

What type of changes does your code introduce to the Prometheus operator? Put an x in the box that apply.

  • CHANGE (fix or feature that would cause existing functionality to not work as expected)
  • FEATURE (non-breaking change which adds functionality)
  • BUGFIX (non-breaking change which fixes an issue)
  • ENHANCEMENT (non-breaking change which improves existing functionality)
  • NONE (if none of the other choices apply. Example, tooling, build system, CI, docs, etc.)

Changelog entry

Please put a one-line changelog entry below. This will be copied to the changelog file during the release process.

reject ServiceMonitor, PodMonitor, Probe and ScrapeConfig with invalid relabel configs.

@simonpasquier simonpasquier force-pushed the fix-missed-relabel-configs-for-probes branch 2 times, most recently from 2cdd7a1 to 1836f57 Compare August 24, 2023 14:42
@simonpasquier simonpasquier changed the title fix: reject probes with invalid relabel configs fix: reject monitors with invalid relabel configs Aug 24, 2023
@simonpasquier simonpasquier marked this pull request as ready for review August 24, 2023 14:47
@simonpasquier simonpasquier requested a review from a team as a code owner August 24, 2023 14:47
minimumVersionCaseActions := version.GTE(semver.MustParse("2.36.0"))
minimumVersionEqualActions := version.GTE(semver.MustParse("2.41.0"))
if rc.Action == "" {
rc.Action = string(relabel.Replace)
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't this be already taken care since we set default as replace?

// +kubebuilder:default=replace
Action string `json:"action,omitempty"`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is for tests + environments that don't have a recent version of the CRD (e.g. one without the default value).

Copy link
Member

@ArthurSens ArthurSens left a comment

Choose a reason for hiding this comment

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

I'm struggling a little bit to understand what/where is the bug that you're fixing here 😬

Do I understand it correctly that we're allowing actions with a mixture of Lower/Upper case and then we fail to compare with relabel.<action name>?

If that's the case, what do you think about passing this validation to kubebuilder annotations?

pkg/prometheus/resource_selector_test.go Outdated Show resolved Hide resolved
@simonpasquier
Copy link
Contributor Author

I'm struggling a little bit to understand what/where is the bug that you're fixing here 😬

One issue is here:

for _, rl := range probe.Spec.MetricRelabelConfigs {
if rl.Action != "" {
if err = validateRelabelConfig(rs.p, *rl); err != nil {
rejectFn(probe, err)
continue
}
}
}
if err = validateProberURL(probe.Spec.ProberSpec.URL); err != nil {
err := errors.Wrapf(err, "%s url specified in proberSpec is invalid, it should be of the format `hostname` or `hostname:port`", probe.Spec.ProberSpec.URL)
rejectFn(probe, err)
continue
}
res[probeName] = probe

If an invalid relabel config is detected, it will be logged but it won't continue from the outer for loop so the probe will be added to the map.

Another issue here:

for _, rl := range endpoint.RelabelConfigs {
if rl.Action != "" {
if err = validateRelabelConfig(rs.p, *rl); err != nil {
break
}
}
}
for _, rl := range endpoint.MetricRelabelConfigs {
if rl.Action != "" {
if err = validateRelabelConfig(rs.p, *rl); err != nil {
break
}
}
}
}
if err != nil {

If L157 returns an error, err might be overwritten at L165 and again the service monitor isn't rejected...

TL;DR: write unit tests to assert that the code is doing what it should ;)

Do I understand it correctly that we're allowing actions with a mixture of Lower/Upper case and then we fail to compare with relabel.<action name>?

If that's the case, what do you think about passing this validation to kubebuilder annotations?

No upper/lower case actions are fine (in fact the Prometheus config supports everything but will always compare to the lower-case version).

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
@simonpasquier simonpasquier force-pushed the fix-missed-relabel-configs-for-probes branch from 1836f57 to 20fa8ef Compare August 25, 2023 08:20
@@ -152,20 +152,14 @@ func (rs *ResourceSelector) SelectServiceMonitors(ctx context.Context, listFn Li
break
}

for _, rl := range endpoint.RelabelConfigs {
if rl.Action != "" {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure why we had this test in the first place. I've modified validateRelabelConfig() to default to the replace action if the field is empty.

@ArthurSens
Copy link
Member

I'm struggling a little bit to understand what/where is the bug that you're fixing here 😬

One issue is here:

for _, rl := range probe.Spec.MetricRelabelConfigs {
if rl.Action != "" {
if err = validateRelabelConfig(rs.p, *rl); err != nil {
rejectFn(probe, err)
continue
}
}
}
if err = validateProberURL(probe.Spec.ProberSpec.URL); err != nil {
err := errors.Wrapf(err, "%s url specified in proberSpec is invalid, it should be of the format `hostname` or `hostname:port`", probe.Spec.ProberSpec.URL)
rejectFn(probe, err)
continue
}
res[probeName] = probe

If an invalid relabel config is detected, it will be logged but it won't continue from the outer for loop so the probe will be added to the map.

Another issue here:

for _, rl := range endpoint.RelabelConfigs {
if rl.Action != "" {
if err = validateRelabelConfig(rs.p, *rl); err != nil {
break
}
}
}
for _, rl := range endpoint.MetricRelabelConfigs {
if rl.Action != "" {
if err = validateRelabelConfig(rs.p, *rl); err != nil {
break
}
}
}
}
if err != nil {

If L157 returns an error, err might be overwritten at L165 and again the service monitor isn't rejected...

TL;DR: write unit tests to assert that the code is doing what it should ;)

Do I understand it correctly that we're allowing actions with a mixture of Lower/Upper case and then we fail to compare with relabel.<action name>?
If that's the case, what do you think about passing this validation to kubebuilder annotations?

No upper/lower case actions are fine (in fact the Prometheus config supports everything but will always compare to the lower-case version).

Ooooh now I get it, we're breaking the inner loop when we wanted to break the outer loop. Thanks for the explanation!

@simonpasquier simonpasquier merged commit 94b04b1 into prometheus-operator:main Aug 25, 2023
17 checks passed
@simonpasquier simonpasquier deleted the fix-missed-relabel-configs-for-probes branch August 25, 2023 12:57
rimitchell added a commit to rimitchell/prometheus-operator that referenced this pull request Aug 25, 2023
simonpasquier added a commit to simonpasquier/prometheus-operator that referenced this pull request Aug 28, 2023
This is a follow-up of prometheus-operator#5841. For the same reason that we didn't
continue from the outer loop, invalid ScrapeConfig objects were not
rejected.

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
simonpasquier added a commit to simonpasquier/prometheus-operator that referenced this pull request Aug 29, 2023
This is a follow-up of prometheus-operator#5841. For the same reason that we didn't
continue from the outer loop, invalid ScrapeConfig objects were not
rejected.

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants