Alerting: Refactor migration to return pairs of legacy and upgraded structs (#79719)

Some refactoring that will simplify next changes for dry-run PRs. This should be no-op as far as the created ngalert resources and database state, though it does change some logs.

The key change here is to modify migrateOrg to return pairs of legacy struct + ngalert struct instead of actually persisting the alerts and alertmanager config. This will allow us to capture error information during dry-run migration.

It also moves most persistence-related operations such as title deduplication and folder creation to the right before we persist. This will simplify eventual partial migrations (individual alerts, dashboards, channels, ...).

Additionally it changes channel code to deal with PostableGrafanaReceiver instead of PostableApiReceiver (integration instead of contact point).
This commit is contained in:
Matthew Jacobson
2024-01-05 05:37:13 -05:00
committed by GitHub
parent 359b118e6a
commit 3537c5440f
18 changed files with 952 additions and 526 deletions

View File

@@ -26,12 +26,25 @@ func NewAlertmanager() *Alertmanager {
// AddRoute adds a route to the alertmanager config.
func (am *Alertmanager) AddRoute(route *apiModels.Route) {
if route == nil {
return
}
am.legacyRoute.Routes = append(am.legacyRoute.Routes, route)
}
// AddReceiver adds a receiver to the alertmanager config.
func (am *Alertmanager) AddReceiver(recv *apiModels.PostableApiReceiver) {
am.Config.AlertmanagerConfig.Receivers = append(am.Config.AlertmanagerConfig.Receivers, recv)
func (am *Alertmanager) AddReceiver(recv *apiModels.PostableGrafanaReceiver) {
if recv == nil {
return
}
am.Config.AlertmanagerConfig.Receivers = append(am.Config.AlertmanagerConfig.Receivers, &apiModels.PostableApiReceiver{
Receiver: config.Receiver{
Name: recv.Name, // Channel name is unique within an Org.
},
PostableGrafanaReceivers: apiModels.PostableGrafanaReceivers{
GrafanaManagedReceivers: []*apiModels.PostableGrafanaReceiver{recv},
},
})
}
// createBaseConfig creates an alertmanager config with the root-level route, default receiver, and nested route