Skip to content

PrometheusRule status update after reconcillation from prometheus#8005

Merged
simonpasquier merged 15 commits intoprometheus-operator:mainfrom
yp969803:issue7923
Oct 10, 2025
Merged

PrometheusRule status update after reconcillation from prometheus#8005
simonpasquier merged 15 commits intoprometheus-operator:mainfrom
yp969803:issue7923

Conversation

@yp969803
Copy link
Contributor

@yp969803 yp969803 commented Oct 9, 2025

Description

PrometheusRule status update after reconcillation from prometheus

Closes: #7923

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.)

Verification

Please check the Prometheus-Operator testing guidelines for recommendations about automated tests.

Changelog entry

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


@yp969803 yp969803 requested a review from a team as a code owner October 9, 2025 06:10
@pull-request-size pull-request-size bot added size/S and removed size/XS labels Oct 9, 2025
@pull-request-size pull-request-size bot added size/M and removed size/S labels Oct 9, 2025
@pull-request-size pull-request-size bot added size/L and removed size/M labels Oct 9, 2025
@yp969803
Copy link
Contributor Author

yp969803 commented Oct 9, 2025

@simonpasquier PTAL!

type ControllerOption func(*Operator)

// selectedConfigResources return the configuration resources (serviceMonitors, podMonitors, probes and scrapeConfig)
// selectedConfigResources return the configuration resources (serviceMonitors, podMonitors, probes, rules and scrapeConfigs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// selectedConfigResources return the configuration resources (serviceMonitors, podMonitors, probes, rules and scrapeConfigs)
// selectedConfigResources return the configuration resources (serviceMonitors, podMonitors, probes, prometheusRules and scrapeConfigs)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

// Select selects PrometheusRules by Prometheus or ThanosRuler.
// The second returned value is the number of rejected PrometheusRule objects.
func (prs *PrometheusRuleSelector) Select(namespaces []string) (map[string]string, int, error) {
func (prs *PrometheusRuleSelector) Select(namespaces []string) (TypedResourcesSelection[*monitoringv1.PrometheusRule], int, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

we don't need to return the rejected number anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

rules[ruleName] = TypedConfigurationResource[*monitoringv1.PrometheusRule]{
resource: promRule,
err: err,
reason: InvalidConfiguration,
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm? it doesn't look right, reason should be empty if no error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm

func (c *Operator) createOrUpdateRuleConfigMaps(ctx context.Context, p *monitoringv1.Prometheus) ([]string, error) {
logger := c.logger.With("prometheus", p.Name, "namespace", p.Namespace)

func (c *Operator) getSelectedPrometheusRules(p *monitoringv1.Prometheus, logger *slog.Logger) (operator.TypedResourcesSelection[*monitoringv1.PrometheusRule], error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func (c *Operator) getSelectedPrometheusRules(p *monitoringv1.Prometheus, logger *slog.Logger) (operator.TypedResourcesSelection[*monitoringv1.PrometheusRule], error) {
func (c *Operator) selectPrometheusRules(p *monitoringv1.Prometheus, logger *slog.Logger) (operator.TypedResourcesSelection[*monitoringv1.PrometheusRule], error) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

err error // Error encountered during selection or validation (nil if valid).
reason string // Reason for rejection; empty if accepted.
generation int64 // Generation of the desired state (spec).
content string // Marshalled content of the resource (only for PrometheusRules).
Copy link
Contributor

Choose a reason for hiding this comment

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

rather than extending TypedConfigurationResource with a field which is only useful for PrometheusRule what if we create a specific struct for PrometheusRule wrapping a TypedResourcesSelection + a map[string]string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@yp969803
Copy link
Contributor Author

yp969803 commented Oct 9, 2025

@simonpasquier PTAL

// TypedResourcesSelection represents a map of configuration resources selected by Prometheus or PrometheusAgent.
type TypedResourcesSelection[T ConfigurationResource] map[string]TypedConfigurationResource[T]

type SelectedPrometheusRules struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

(nit)

Suggested change
type SelectedPrometheusRules struct {
type PrometheusRuleSelection struct {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

type TypedResourcesSelection[T ConfigurationResource] map[string]TypedConfigurationResource[T]

type SelectedPrometheusRules struct {
Rules TypedResourcesSelection[*monitoringv1.PrometheusRule] // PrometheusRules selected.
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's avoid public fields.

Suggested change
Rules TypedResourcesSelection[*monitoringv1.PrometheusRule] // PrometheusRules selected.
selection TypedResourcesSelection[*monitoringv1.PrometheusRule] // PrometheusRules selected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok


type SelectedPrometheusRules struct {
Rules TypedResourcesSelection[*monitoringv1.PrometheusRule] // PrometheusRules selected.
MarshalRules map[string]string // Map of valid marshalled rules.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
MarshalRules map[string]string // Map of valid marshalled rules.
// Map of rule configuration files serialized to the Prometheus format (key=filename).
ruleFiles map[string]string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

if tKey, ok := o.accessor.MetaNamespaceKey(t); ok {
o.metrics.SetSelectedResources(tKey, monitoringv1.PrometheusRuleKind, len(rules))
o.metrics.SetRejectedResources(tKey, monitoringv1.PrometheusRuleKind, rejected)
o.metrics.SetSelectedResources(tKey, monitoringv1.PrometheusRuleKind, len(rules.MarshalRules))
Copy link
Contributor

Choose a reason for hiding this comment

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

For other resources, we report the number of selected resources, irrespective of their validation state?
I think that we should the same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

if pKey, ok := c.accessor.MetaNamespaceKey(p); ok {
c.metrics.SetSelectedResources(pKey, monitoringv1.PrometheusRuleKind, len(rules))
c.metrics.SetRejectedResources(pKey, monitoringv1.PrometheusRuleKind, rejected)
c.metrics.SetSelectedResources(pKey, monitoringv1.PrometheusRuleKind, len(rules.MarshalRules))
Copy link
Contributor

Choose a reason for hiding this comment

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

same remark as below for ThanosRuler

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok


rules, err := c.selectPrometheusRules(p, logger)
if err != nil {
return nil, err
Copy link
Contributor

Choose a reason for hiding this comment

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

(nit) to be consistent

Suggested change
return nil, err
return nil, fmt.Errorf("selecting PrometheusRule failed: %w", err)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

return rules, nil
}

func (c *Operator) createOrUpdateRuleConfigMaps(ctx context.Context, p *monitoringv1.Prometheus, rules map[string]string, logger *slog.Logger) ([]string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

(nit) it can receive a operator.SelectedPrometheusRules argument instead of an anonymous map[string]string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@yp969803
Copy link
Contributor Author

yp969803 commented Oct 9, 2025

@simonpasquier merge request

// Select selects PrometheusRules and translates them into native
// Prometheus/Thanos configurations.
// Select selects PrometheusRules by Prometheus or ThanosRuler.
// The second returned value is the number of rejected PrometheusRule objects.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// The second returned value is the number of rejected PrometheusRule objects.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

return validRes
}

type PrometheusRuleSelection struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

(nit) can we move this new code to rules.go instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok


type PrometheusRuleSelection struct {
selection TypedResourcesSelection[*monitoringv1.PrometheusRule] // PrometheusRules selected.
ruleFiles map[string]string // Map of rule configuration files serialized to the Prometheus format (key=filename).
Copy link
Contributor

Choose a reason for hiding this comment

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

(suggestion) add a field to store the number of rejected resources so callers don't have to compute it?
the number of selected resources is len(prs.Selected()).

Suggested change
ruleFiles map[string]string // Map of rule configuration files serialized to the Prometheus format (key=filename).
rejected int

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added a function to get Rejected numbers

if pKey, ok := c.accessor.MetaNamespaceKey(p); ok {
c.metrics.SetSelectedResources(pKey, monitoringv1.PrometheusRuleKind, len(rules))
c.metrics.SetRejectedResources(pKey, monitoringv1.PrometheusRuleKind, rejected)
c.metrics.SetSelectedResources(pKey, monitoringv1.PrometheusRuleKind, len(rules.RuleFiles()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
c.metrics.SetSelectedResources(pKey, monitoringv1.PrometheusRuleKind, len(rules.RuleFiles()))
c.metrics.SetSelectedResources(pKey, monitoringv1.PrometheusRuleKind, len(rules.Selected()))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

Copy link
Contributor

@simonpasquier simonpasquier left a comment

Choose a reason for hiding this comment

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

LGTM!

@simonpasquier simonpasquier enabled auto-merge (squash) October 10, 2025 09:41
@yp969803
Copy link
Contributor Author

Error Trace:	/home/runner/work/prometheus-operator/prometheus-operator/test/e2e/status_subresource_test.go:222
        	Error:      	Received unexpected error:
        	            	serviceMonitor status gatedfeatures-servicemonitorstatuswithmultipleworkload-acf63506/servicemonitor-status-multiple-workloads failed to reach expected condition: context deadline exceeded: resource "prometheuses" gatedfeatures-servicemonitorstatuswithmultipleworkload-acf63506/server1: client rate limiter Wait returned an error: context deadline exceeded

@simonpasquier this test is failing i dont think this is related to my changes, can u rerun the test, as it failed last to last time also, but last time it passed

@simonpasquier simonpasquier merged commit 4ca908c into prometheus-operator:main Oct 10, 2025
20 of 22 checks passed
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.

Update PrometheusRule's status subresource on Prometheus/ThanosRuler reconciliations

2 participants