Skip to content

fix(alertmanager): reject AlertmanagerConfig missing global fields - #8904

Open
A-RYAN-1 wants to merge 2 commits into
prometheus-operator:mainfrom
A-RYAN-1:fix/8888-global-config-check
Open

A-RYAN-1 wants to merge 2 commits into
prometheus-operator:mainfrom
A-RYAN-1:fix/8888-global-config-check

Conversation

@A-RYAN-1

Copy link
Copy Markdown
Contributor

Description

Slack apiURL, OpsGenie and VictorOps apiKey, WeChat apiSecret and corpID, and the email smarthost and from are optional in a receiver because Alertmanager can take them from the global configuration. When the global configuration did not set them, the generated file was refused by Alertmanager and no AlertmanagerConfig took effect. Such an object is now rejected at selection, like any other invalid one, and the others keep working. The base configuration (secret or global AlertmanagerConfig) is now loaded before the selection so its global section can be used for the check.

Fixes #8888

AI-assisted.

Verification

Table test TestCheckGlobalConfigFields for each receiver type (missing, set on the receiver, set in the global configuration). go test ./pkg/alertmanager/ ./pkg/operator/... passes on Linux.

Open points

  • Moving the selection after the base configuration load means the selection metrics and events are no longer emitted when the base configuration itself fails to load.
  • The email check in convertEmailConfig is kept as a safety net.

@A-RYAN-1
A-RYAN-1 requested a review from a team as a code owner October 20, 2026 07:45

@simonpasquier simonpasquier left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! It's smoother than what I expected :)

Comment thread pkg/alertmanager/operator.go Outdated
// If no AlertmanagerConfig selectors and AlertmanagerConfiguration are
// configured, the user wants to manage configuration themselves.
if am.Spec.AlertmanagerConfigSelector == nil && am.Spec.AlertmanagerConfiguration == nil {
amConfigs, err := c.selectAlertmanagerConfigs(ctx, am, store, version, nil)

Copy link
Copy Markdown
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 feel right: as stated by the comment, the Alertmanager resource doesn't rely on AlertmanagetConfigs

Comment thread pkg/alertmanager/operator.go Outdated
var reason string
err := checkAlertmanagerConfigResource(ctx, amc, amVersion, store)
if err == nil {
err = checkGlobalConfigFields(amc, global)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion: have the check in checkAlertmanagerConfigResource() directly (having an if err == nil { isn't idiomatic).

Comment thread pkg/alertmanager/operator.go Outdated
}

func (c *Operator) selectAlertmanagerConfigs(ctx context.Context, am *monitoringv1.Alertmanager, store *assets.StoreBuilder, amVersion semver.Version) (operator.TypedResourcesSelection[*monitoringv1alpha1.AlertmanagerConfig], error) {
func (c *Operator) selectAlertmanagerConfigs(ctx context.Context, am *monitoringv1.Alertmanager, store *assets.StoreBuilder, amVersion semver.Version, global *globalConfig) (operator.TypedResourcesSelection[*monitoringv1alpha1.AlertmanagerConfig], error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ConfigBuilder already carries version and store, maybe we can pass it directly?

Comment thread pkg/alertmanager/operator.go Resolved
@A-RYAN-1
A-RYAN-1 force-pushed the fix/8888-global-config-check branch from cf2c764 to b3c2d06 Compare October 20, 2026 13:52
@A-RYAN-1

Copy link
Copy Markdown
Contributor Author

Thanks for the review, reworked in b3c2d06 and rebased onto main. selectAlertmanagerConfigs now takes the ConfigBuilder (it already carries the version and the store) instead of the store, version and global config, and the check runs through a builder method next to checkAlertmanagerConfigResource, so there is no if err == nil. I kept it a separate method rather than inside checkAlertmanagerConfigResource because that function also validates the base AlertmanagerConfig in initializeFromAlertmanagerConfig, where the global section is still being built. When no selector is configured the selection runs with the empty builder, so the selection metrics behave as before. A Secret reference whose data is empty now counts as unset for Slack, OpsGenie, VictorOps and WeChat, with test cases. Happy to move the check if you prefer another place.

Comment thread pkg/alertmanager/operator.go Outdated
Comment on lines +1001 to +1005
amConfigs, err := c.selectAlertmanagerConfigs(ctx, am, cfgBuilder)
if err != nil {
return nil, fmt.Errorf("failed to select alertmanager configs: %w", err)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
amConfigs, err := c.selectAlertmanagerConfigs(ctx, am, cfgBuilder)
if err != nil {
return nil, fmt.Errorf("failed to select alertmanager configs: %w", err)
}

Comment thread pkg/alertmanager/operator.go Outdated
@@ -1017,10 +1019,7 @@ func (c *Operator) provisionAlertmanagerConfiguration(ctx context.Context, am *m
return amConfigs, nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
return amConfigs, nil
return nil, nil

Comment thread pkg/alertmanager/operator.go Outdated
Comment on lines +1063 to +1064
// The base configuration is loaded first because its global section
// decides which AlertmanagerConfig objects are valid.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
// The base configuration is loaded first because its global section
// decides which AlertmanagerConfig objects are valid.

Comment thread pkg/alertmanager/operator.go Outdated
// checkAlertmanagerConfig verifies that an AlertmanagerConfig object is valid
// on its own (see checkAlertmanagerConfigResource) and together with the base
// configuration the builder holds.
func (cb *ConfigBuilder) checkAlertmanagerConfig(ctx context.Context, amc *monitoringv1alpha1.AlertmanagerConfig) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If we switch to ConfigBuilder methods, they will need to move to amcfg.go, we don't want them to be scattered across files.
For the time being it's more convenient to pass configBuilder as a func argument. We can consider refactoring as a follow-up.

Comment thread pkg/alertmanager/operator.go Outdated
}

for i, c := range receiver.SlackConfigs {
apiURL, err := secret(c.APIURL)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the secret ref has already been loaded before, let's move the empty check there. There's no good reason why someone would reference an empty secret value.

@A-RYAN-1

Copy link
Copy Markdown
Contributor Author

Thanks, all five points addressed in 3bb8f82. selectAlertmanagerConfigs now runs only after the base configuration is loaded, and the self-managed branch returns nil, nil without selecting. The checks are plain functions in operator.go that take the ConfigBuilder, and the empty-Secret check moved to where the references are loaded: validation.NotEmpty on the Slack apiURL, OpsGenie and VictorOps apiKey and WeChat apiSecret, so checkGlobalConfigFields no longer reads Secrets. One side effect to confirm: with nil, nil the selected/rejected resource metrics are no longer reset to 0 when a user removes both selectors; happy to keep a small reset there if you prefer.

Comment thread pkg/alertmanager/operator.go Outdated
for namespaceAndName, amc := range amConfigs {
var reason string
err := checkAlertmanagerConfigResource(ctx, amc, amVersion, store)
err := checkAlertmanagerConfig(ctx, cb, amc)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

checkAlertmanagerConfigResource() is also called by initializeFromAlertmanagerConfig() and it needs to be preserved. I'd recommend not renaming checkAlertmanagerConfigResource() to checkAlertmanagerConfig() but rather extend it with the checks against the global config defaults.

@A-RYAN-1

Copy link
Copy Markdown
Contributor Author

Thanks, that makes sense. I kept checkAlertmanagerConfigResource() and extended it with the global defaults check, so checkAlertmanagerConfig() is gone. It now takes the ConfigBuilder, and initializeFromAlertmanagerConfig() calls it after the global config is converted, so the base config is checked against its own global too. Pushed in the latest commit.

simonpasquier
simonpasquier previously approved these changes Oct 22, 2026

@simonpasquier simonpasquier left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thanks!

@A-RYAN-1

Copy link
Copy Markdown
Contributor Author

Thanks for the approval, @simonpasquier. CI is green and the branch is up to date with main. Is there anything else you need from me before this can be merged?

@A-RYAN-1

Copy link
Copy Markdown
Contributor Author

This is approved and every check is green. Could a maintainer merge it when convenient? Thank you @simonpasquier for the careful reviews, they made it better.

@A-RYAN-1

Copy link
Copy Markdown
Contributor Author

This is approved and CI is green on the current head. Could a maintainer merge it when convenient? Happy to adjust anything first.

@A-RYAN-1

Copy link
Copy Markdown
Contributor Author

Thanks again for the approval, @simonpasquier. The PR has not moved since, so I wanted to check that nothing else is needed from my side before it can be merged.

@simonpasquier

Copy link
Copy Markdown
Contributor

@A-RYAN-1 no need to pile up more comments on the PR. We have a long stuff to review and try to get to them 1 by 1.

@simonpasquier

Copy link
Copy Markdown
Contributor

@A-RYAN-1 can you rebase? Now that we have v0.95 out of the door, this should be good to go.

@A-RYAN-1

A-RYAN-1 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main as a single commit. The filesystem-mode check now reads the Alertmanager from the ConfigBuilder, and the pkg/alertmanager tests pass. Thanks!

@A-RYAN-1
A-RYAN-1 force-pushed the fix/8888-global-config-check branch from e8e5c52 to e5554df Compare November 6, 2026 16:13
@A-RYAN-1

Copy link
Copy Markdown
Contributor Author

@simonpasquier the rebase onto main dismissed your earlier approval; all checks are green on e5554df. Could you re-approve when you get a chance? Thanks.


for _, receiver := range amc.Spec.Receivers {
for i, c := range receiver.EmailConfigs {
if ptr.Deref(c.Smarthost, "") == "" && global.SMTPSmarthost.Host == "" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can we remove the now redundant code in pkg/alertmanager/amcfg.go?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 1c0d9ff: the now redundant smarthost and auth checks in amcfg.go are removed.

global = cb.cfg.Global
}

for _, receiver := range amc.Spec.Receivers {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we miss Telegram fields which are covered already.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 1c0d9ff: the Telegram bot token is checked up front too, with a test. The red "Generate and format" job is the stale AutoGomemlimitSpec applyconfiguration on main, which #9013 regenerates.

Slack apiURL, OpsGenie and VictorOps apiKey, WeChat apiSecret and
corpID, and the email smarthost and from are optional in a receiver
because Alertmanager can take them from the global configuration.
When the global configuration did not set them, the generated file
was refused by Alertmanager and no AlertmanagerConfig took effect.

Load the base configuration before selecting AlertmanagerConfig
objects and reject the ones whose receivers rely on a global value
that is not set, like any other invalid object. The check lives in
checkAlertmanagerConfigResource, which now takes the ConfigBuilder.

Signed-off-by: Aryan Deshmukh <[email protected]>
…checks

Add the Telegram bot token (botToken or botTokenFile, or the global
value) to checkGlobalConfigFields so every receiver that depends on a
global default is rejected in one place, before the configuration is
generated.

Remove the email smarthost and from checks and the Telegram bot token
check from the receiver conversion in amcfg.go. They can no longer be
reached from the operator, which only converts AlertmanagerConfig
objects that passed checkAlertmanagerConfigResource, and the new
checks have table tests of their own. The builder-level test cases
for them go with the code.

Signed-off-by: Aryan Deshmukh <[email protected]>
@A-RYAN-1
A-RYAN-1 force-pushed the fix/8888-global-config-check branch from e5554df to 1c0d9ff Compare November 20, 2026 20:11
@A-RYAN-1

Copy link
Copy Markdown
Contributor Author

Thanks, both points done in 1c0d9ff. The Telegram bot token (botToken, botTokenFile or the global value) is now part of checkGlobalConfigFields, and the email smarthost and from checks and the Telegram check are removed from amcfg.go, since only objects that passed checkAlertmanagerConfigResource reach the conversion. Their builder-level test cases went with them. I also rebased on main; the only conflict was in operator_test.go, next to the new SNS tests.

This branch has not been deployed

No deployments
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.

AlertmanagerConfig receivers that rely on global credentials are accepted when the global configuration does not set them

2 participants