Skip to content

pkg/prometheus: move LabelConfigValidator to validation package#8209

Merged
simonpasquier merged 1 commit intoprometheus-operator:mainfrom
tomlin7:refactor/labelconfigvalidator-validation-package
Dec 24, 2025
Merged

pkg/prometheus: move LabelConfigValidator to validation package#8209
simonpasquier merged 1 commit intoprometheus-operator:mainfrom
tomlin7:refactor/labelconfigvalidator-validation-package

Conversation

@tomlin7
Copy link
Contributor

@tomlin7 tomlin7 commented Dec 24, 2025

Description

This move improves code organization and encapsulation by isolating validation logic from resource selection. It also resolves issues related to accessing private fields/methods across files.

The validate method was renamed to ValidateRelabelConfig to allow it to be tested and used from outside the package.

Updated references in:

  • pkg/prometheus/resource_selector.go
  • pkg/prometheus/promcfg.go
  • pkg/prometheus/server/operator.go
  • pkg/prometheus/resource_selector_test.go

Closes: #7951

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

Successfully ran go test -v -run TestValidateRelabelConfig ./pkg/prometheus - all 29 passed.

> go test -v -run TestValidateRelabelConfig ./pkg/prometheus
=== RUN   TestValidateRelabelConfig
=== RUN   TestValidateRelabelConfig/Invalid_regex
=== RUN   TestValidateRelabelConfig/invalid_target_label
=== RUN   TestValidateRelabelConfig/empty_target_label_for_replace_action
=== RUN   TestValidateRelabelConfig/empty_target_label_for_hashmod_action
=== RUN   TestValidateRelabelConfig/empty_target_label_for_uppercase_action
=== RUN   TestValidateRelabelConfig/empty_target_label_for_lowercase_action
=== RUN   TestValidateRelabelConfig/replacement_set_for_uppercase_action
=== RUN   TestValidateRelabelConfig/invalid_hashmod_config
=== RUN   TestValidateRelabelConfig/invalid_labelmap_config
=== RUN   TestValidateRelabelConfig/valid_labelmap_config
=== RUN   TestValidateRelabelConfig/valid_labelmap_config_with_replacement
=== RUN   TestValidateRelabelConfig/invalid_labelkeep_config
=== RUN   TestValidateRelabelConfig/valid_labelkeep_config
=== RUN   TestValidateRelabelConfig/valid_labeldrop_config
=== RUN   TestValidateRelabelConfig/valid_replace_config_with_empty_replacement
=== RUN   TestValidateRelabelConfig/valid_labeldrop_config_with_default_values
=== RUN   TestValidateRelabelConfig/valid_hashmod_config
=== RUN   TestValidateRelabelConfig/valid_replace_config
=== RUN   TestValidateRelabelConfig/valid_uppercase_config
=== RUN   TestValidateRelabelConfig/valid_lowercase_config
=== RUN   TestValidateRelabelConfig/uppercase_config_with_lower_prometheus_version
=== RUN   TestValidateRelabelConfig/lowercase_config_with_lower_prometheus_version
=== RUN   TestValidateRelabelConfig/keepequal_config_with_lower_prometheus_version
=== RUN   TestValidateRelabelConfig/dropequal_config_with_lower_prometheus_version
=== RUN   TestValidateRelabelConfig/valid_keepequal_config
=== RUN   TestValidateRelabelConfig/valid_dropequal_config
=== RUN   TestValidateRelabelConfig/valid_keepequal_config_with_non_default_values_for_other_fields
=== RUN   TestValidateRelabelConfig/valid_keepequal_config_with_default_values_for_other_fields
--- PASS: TestValidateRelabelConfig (0.00s)
    --- PASS: TestValidateRelabelConfig/Invalid_regex (0.00s)
    --- PASS: TestValidateRelabelConfig/invalid_target_label (0.00s)
    --- PASS: TestValidateRelabelConfig/empty_target_label_for_replace_action (0.00s)
    --- PASS: TestValidateRelabelConfig/empty_target_label_for_hashmod_action (0.00s)
    --- PASS: TestValidateRelabelConfig/empty_target_label_for_uppercase_action (0.00s)
    --- PASS: TestValidateRelabelConfig/empty_target_label_for_lowercase_action (0.00s)
    --- PASS: TestValidateRelabelConfig/replacement_set_for_uppercase_action (0.00s)
    --- PASS: TestValidateRelabelConfig/invalid_hashmod_config (0.00s)
    --- PASS: TestValidateRelabelConfig/invalid_labelmap_config (0.00s)
    --- PASS: TestValidateRelabelConfig/valid_labelmap_config (0.00s)
    --- PASS: TestValidateRelabelConfig/valid_labelmap_config_with_replacement (0.00s)
    --- PASS: TestValidateRelabelConfig/invalid_labelkeep_config (0.00s)
    --- PASS: TestValidateRelabelConfig/valid_labelkeep_config (0.00s)
    --- PASS: TestValidateRelabelConfig/valid_labeldrop_config (0.00s)
    --- PASS: TestValidateRelabelConfig/valid_replace_config_with_empty_replacement (0.00s)
    --- PASS: TestValidateRelabelConfig/valid_labeldrop_config_with_default_values (0.00s)
    --- PASS: TestValidateRelabelConfig/valid_hashmod_config (0.00s)
    --- PASS: TestValidateRelabelConfig/valid_replace_config (0.00s)
    --- PASS: TestValidateRelabelConfig/valid_uppercase_config (0.00s)
    --- PASS: TestValidateRelabelConfig/valid_lowercase_config (0.00s)
    --- PASS: TestValidateRelabelConfig/uppercase_config_with_lower_prometheus_version (0.00s)
    --- PASS: TestValidateRelabelConfig/lowercase_config_with_lower_prometheus_version (0.00s)
    --- PASS: TestValidateRelabelConfig/keepequal_config_with_lower_prometheus_version (0.00s)
    --- PASS: TestValidateRelabelConfig/dropequal_config_with_lower_prometheus_version (0.00s)
    --- PASS: TestValidateRelabelConfig/valid_keepequal_config (0.00s)
    --- PASS: TestValidateRelabelConfig/valid_dropequal_config (0.00s)
    --- PASS: TestValidateRelabelConfig/valid_keepequal_config_with_non_default_values_for_other_fields (0.00s)
    --- PASS: TestValidateRelabelConfig/valid_keepequal_config_with_default_values_for_other_fields (0.00s)
PASS
ok      github.com/prometheus-operator/prometheus-operator/pkg/prometheus       (cached)
  • Ran go test ./pkg/prometheus/validation (new package, no compilation issues).
  • Verified that the server pkg builds correctly go build ./pkg/prometheus/server.

Changelog entry

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

Move LabelConfigValidator to a dedicated validation package to improve code encapsulation.

@tomlin7 tomlin7 requested a review from a team as a code owner December 24, 2025 11:11
tomlin7 added a commit to tomlin7/prometheus-operator that referenced this pull request Dec 24, 2025
This move improves code organization and encapsulation by isolating
validation logic from resource selection. It also resolves issues
related to accessing private fields/methods across files.

The 'validate' method was renamed to 'ValidateRelabelConfig' to allow
it to be tested and used from outside the package.

Updated references in:
- pkg/prometheus/resource_selector.go
- pkg/prometheus/promcfg.go
- pkg/prometheus/server/operator.go
- pkg/prometheus/resource_selector_test.go

Fixes: prometheus-operator#8209
@tomlin7 tomlin7 force-pushed the refactor/labelconfigvalidator-validation-package branch from ac2579a to a733b75 Compare December 24, 2025 11:17
tomlin7 added a commit to tomlin7/prometheus-operator that referenced this pull request Dec 24, 2025
This move improves code organization and encapsulation by isolating
validation logic from resource selection. It also resolves issues
related to accessing private fields/methods across files.

The 'validate' method was renamed to 'ValidateRelabelConfig' to allow
it to be tested and used from outside the package.

Updated references in:
- pkg/prometheus/resource_selector.go
- pkg/prometheus/promcfg.go
- pkg/prometheus/server/operator.go
- pkg/prometheus/resource_selector_test.go

Fixes: prometheus-operator#8209
@tomlin7 tomlin7 force-pushed the refactor/labelconfigvalidator-validation-package branch from a733b75 to ce31087 Compare December 24, 2025 11:21
tomlin7 added a commit to tomlin7/prometheus-operator that referenced this pull request Dec 24, 2025
This move improves code organization and encapsulation by isolating
validation logic from resource selection. It also resolves issues
related to accessing private fields/methods across files.

The 'validate' method was renamed to 'ValidateRelabelConfig' to allow
it to be tested and used from outside the package.

Updated references in:
- pkg/prometheus/resource_selector.go
- pkg/prometheus/promcfg.go
- pkg/prometheus/server/operator.go
- pkg/prometheus/resource_selector_test.go

Fixes: prometheus-operator#8209
@tomlin7 tomlin7 force-pushed the refactor/labelconfigvalidator-validation-package branch from ce31087 to 2219ffc Compare December 24, 2025 11:38
tomlin7 added a commit to tomlin7/prometheus-operator that referenced this pull request Dec 24, 2025
This move improves code organization and encapsulation by isolating
validation logic from resource selection. It also resolves issues
related to accessing private fields/methods across files.

The 'validate' method was renamed to 'ValidateRelabelConfig' to allow
it to be tested and used from outside the package.

Updated references in:
- pkg/prometheus/resource_selector.go
- pkg/prometheus/promcfg.go
- pkg/prometheus/server/operator.go
- pkg/prometheus/resource_selector_test.go

Fixes: prometheus-operator#8209
@tomlin7 tomlin7 force-pushed the refactor/labelconfigvalidator-validation-package branch from 2219ffc to 67f1e7a Compare December 24, 2025 11:45
@simonpasquier
Copy link
Contributor

Looks good but I'm going to revise my first comment since the goal of the issue was to move to the object to a different package so let's move it pkg/prometheus/validation as you did initially (sorry about that!).

This move improves code organization and encapsulation by isolating
validation logic from resource selection. It also resolves issues
related to accessing private fields/methods across files.

The 'validate' method was renamed to 'ValidateRelabelConfig' to allow
it to be tested and used from outside the package.

Updated references in:
- pkg/prometheus/resource_selector.go
- pkg/prometheus/promcfg.go
- pkg/prometheus/server/operator.go
- pkg/prometheus/resource_selector_test.go

Fixes: prometheus-operator#8209
@tomlin7 tomlin7 force-pushed the refactor/labelconfigvalidator-validation-package branch from 67f1e7a to 9a7bbe5 Compare December 24, 2025 13:42
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.

thanks!

@simonpasquier simonpasquier enabled auto-merge (squash) December 24, 2025 13:47
@simonpasquier simonpasquier merged commit f62c9d8 into prometheus-operator:main Dec 24, 2025
22 checks passed
Arpit529Srivastava pushed a commit to Arpit529Srivastava/prometheus-operator that referenced this pull request Jan 5, 2026
…etheus-operator#8209)

This move improves code organization and encapsulation by isolating
validation logic from resource selection. It also resolves issues
related to accessing private fields/methods across files.

The 'validate' method was renamed to 'ValidateRelabelConfig' to allow
it to be tested and used from outside the package.

Updated references in:
- pkg/prometheus/resource_selector.go
- pkg/prometheus/promcfg.go
- pkg/prometheus/server/operator.go
- pkg/prometheus/resource_selector_test.go

Fixes: prometheus-operator#8209
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.

Move LabelConfigValidator to its own package (right now we have parts of the code that access private fields/methods, it's never a good idea).

2 participants