Skip to content

feat: support Service discovery for Alertmanager - #2060

Open
hugosmoreira wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
hugosmoreira:agent/alertmanager-service-discovery
Open

feat: support Service discovery for Alertmanager#2060
hugosmoreira wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
hugosmoreira:agent/alertmanager-service-discovery

Conversation

@hugosmoreira

Copy link
Copy Markdown

What this change does

  • Adds an opt-in Service discovery mode to AlertmanagerEndpoints while preserving existing Endpoints discovery as the default.
  • Generates a static Alertmanager target using the Kubernetes Service DNS name (<name>.<namespace>.svc:<port>), so portless ExternalName Services work without an Endpoints object.
  • Requires a numeric port for Service discovery and retains the existing authorization, TLS, scheme, path prefix, API version, timeout, redirect, and HTTP/2 configuration.
  • Regenerates the OperatorConfig CRDs, combined setup manifest, and API documentation.
  • Adds validation, configuration-generation, and rule-evaluator golden coverage for the new mode.

Why

The current Kubernetes discovery path requires an Endpoints object. Services without Endpoints, including the ExternalName setup described in #595, therefore cannot be used as Alertmanager targets without manually maintaining an Endpoints object.

Fixes #595.

Validation

  • go test ./pkg/operator/... -count=1 in Go 1.25
  • Repository-wide ./hack/presubmit.sh test in the repository's hermetic Go 1.26.5 image
  • Uncached ./hack/presubmit.sh all diff in the repository's hermetic image
  • Generated CRD, manifest, API documentation, and rule-evaluator golden output verified by the presubmit

Assistance disclosure

This contribution was prepared with Codex assistance and the resulting code, generated artifacts, and tests were reviewed and validated locally before publication.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces support for discovering Alertmanager targets via Kubernetes Service DNS names in addition to the default Endpoints-based discovery. It adds a new discoveryType field to the AlertmanagerEndpoints specification, updates the validation logic to require a numeric port when using Service discovery, and updates the configuration generation to construct the appropriate Service DNS name. Unit and end-to-end tests have been added to verify this new functionality. There are no review comments, and I have no additional feedback to provide.

@bwplotka

bwplotka commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

Thanks! I think this change is reasonable. Do you self-deploy operator or do you use managed GMP?

@hugosmoreira

Copy link
Copy Markdown
Author

Thanks! I don't currently run either setup in production. I picked this up from #595 and validated the change against the open-source operator's configuration-generation path and repository tests, but I haven't tested it with managed GMP. The use case I had in mind is self-deployed operators that need to target an ExternalName Service without maintaining an Endpoints object. If managed GMP has additional API or compatibility constraints, I'm happy to adjust the scope.

@bwplotka

Copy link
Copy Markdown
Collaborator

Cool, that's ok - let me know once it's ready for review!

@hugosmoreira
hugosmoreira marked this pull request as ready for review August 10, 2026 16:22
@hugosmoreira

Copy link
Copy Markdown
Author

Thanks! It’s ready for review now. The implementation and relevant automated repository tests are complete and passing.

@bwplotka bwplotka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

One top level question first.

Just to understand the expectations again, is this something you need or just helping us with a tech debt?

require.Len(t, staticConfig[0].Targets, 1)
require.Equal(t,
prommodel.LabelValue("external-alertmanager.monitoring.svc:9093"),
staticConfig[0].Targets[0][prommodel.AddressLabel],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It looks like we added a test for Service discovery here, but it might be worth adding a regression test to ensure that the default Endpoints SD behaviour remains backwards compatible too.

},
},
},
{

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Consider adding a test case to verify that an unknown DiscoveryType safely falls back and returns the expected unknown type validation error.

func validateAlertManagerEndpoint(alertManagerEndpoint *AlertmanagerEndpoints) error {
switch alertManagerEndpoint.DiscoveryType {
case "", AlertmanagerDiscoveryTypeEndpoints:
case AlertmanagerDiscoveryTypeService:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since Service discovery bypasses standard endpoints validation, an empty Name or Namespace could yield a broken svcDNSName (e.g. ..svc:9093). Consider adding an explicit sanity rejection for empty strings here.

cfg.ServiceDiscoveryConfigs = discovery.Configs{
discovery.StaticConfig{
&targetgroup.Group{
Targets: []prommodel.LabelSet{{prommodel.AddressLabel: prommodel.LabelValue(svcDNSName)}},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Technically this is a DNS SD not a "service" discovery through Kubernetes API. Why not using Kube API for this (other than more code?)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Also if we need DNS we could use DNS SD too

@bwplotka
bwplotka self-requested a review August 12, 2026 12:29

@bwplotka bwplotka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sorry I didn't meant to approve yet, just comment

@hugosmoreira

Copy link
Copy Markdown
Author

Thanks for checking. This is a community contribution addressing #595 rather than something I personally require in production. The issue describes a real user need for routing managed rule evaluation to an external Alertmanager through an ExternalName Service.

You are right that the current implementation constructs a static DNS target rather than using Kubernetes Service discovery. Since the rule evaluator already has Kubernetes API access and Service permissions, kubernetes_sd_config with role: service, filtered by namespace, Service name, and port, seems like the better fit. I can revise the implementation that way and add the requested compatibility, unknown-type, and empty-field tests. Would you prefer that over dns_sd_config?

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.

Support non-Endpoint-backed Services for AlertmanagerEndpoints

2 participants