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:
@@ -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
|
||||
|
||||
@@ -28,13 +28,19 @@ type Deduplicator struct {
|
||||
// caseInsensitive determines whether the string comparison should be case-insensitive.
|
||||
// maxLen determines the maximum length of deduplicated strings. If the deduplicated string would be longer than
|
||||
// maxLen, it will be truncated.
|
||||
func NewDeduplicator(caseInsensitive bool, maxLen int) *Deduplicator {
|
||||
return &Deduplicator{
|
||||
set: make(map[string]int),
|
||||
func NewDeduplicator(caseInsensitive bool, maxLen int, initial ...string) *Deduplicator {
|
||||
d := &Deduplicator{
|
||||
set: make(map[string]int, len(initial)),
|
||||
caseInsensitive: caseInsensitive,
|
||||
maxLen: maxLen,
|
||||
uidGenerator: util.GenerateShortUID,
|
||||
}
|
||||
if len(initial) > 0 {
|
||||
for _, u := range initial {
|
||||
d.add(u, 0)
|
||||
}
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// Deduplicate returns a unique string based on the given base string. If the base string has not already been seen by
|
||||
|
||||
@@ -1,15 +1,69 @@
|
||||
package models
|
||||
|
||||
// OrgMigrationState contains information about the state of an org migration.
|
||||
type OrgMigrationState struct {
|
||||
OrgID int64 `json:"orgId"`
|
||||
CreatedFolders []string `json:"createdFolders"`
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
legacymodels "github.com/grafana/grafana/pkg/services/alerting/models"
|
||||
apiModels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
||||
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
)
|
||||
|
||||
type DashboardUpgrade struct {
|
||||
ID int64
|
||||
UID string
|
||||
Title string
|
||||
FolderID int64
|
||||
MigratedAlerts map[int64]*AlertPair
|
||||
}
|
||||
|
||||
// DashboardUpgradeInfo contains information about a dashboard that was upgraded.
|
||||
type DashboardUpgradeInfo struct {
|
||||
DashboardUID string
|
||||
DashboardName string
|
||||
NewFolderUID string
|
||||
NewFolderName string
|
||||
type AlertPair struct {
|
||||
LegacyRule *legacymodels.Alert
|
||||
Rule *ngmodels.AlertRule
|
||||
Error error
|
||||
}
|
||||
|
||||
type ContactPair struct {
|
||||
Channel *legacymodels.AlertNotification
|
||||
ContactPoint *apiModels.PostableGrafanaReceiver
|
||||
Route *apiModels.Route
|
||||
Error error
|
||||
}
|
||||
|
||||
func NewAlertPair(da *legacymodels.Alert, err error) *AlertPair {
|
||||
return &AlertPair{
|
||||
LegacyRule: da,
|
||||
Error: err,
|
||||
}
|
||||
}
|
||||
|
||||
func NewDashboardUpgrade(id int64) *DashboardUpgrade {
|
||||
return &DashboardUpgrade{
|
||||
ID: id,
|
||||
MigratedAlerts: make(map[int64]*AlertPair),
|
||||
}
|
||||
}
|
||||
|
||||
func (du *DashboardUpgrade) AddAlertErrors(err error, alerts ...*legacymodels.Alert) {
|
||||
for _, da := range alerts {
|
||||
pair := NewAlertPair(da, err)
|
||||
du.MigratedAlerts[da.PanelID] = pair
|
||||
}
|
||||
}
|
||||
|
||||
// ExtractErrors extracts errors from migrated dashboards and channels.
|
||||
func ExtractErrors(dus []*DashboardUpgrade, contactPairs []*ContactPair) []error {
|
||||
errs := make([]error, 0)
|
||||
for _, du := range dus {
|
||||
for _, pair := range du.MigratedAlerts {
|
||||
if pair.Error != nil {
|
||||
errs = append(errs, fmt.Errorf("migrate alert '%s': %w", pair.LegacyRule.Name, pair.Error))
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, pair := range contactPairs {
|
||||
if pair.Error != nil {
|
||||
errs = append(errs, fmt.Errorf("migrate channel '%s': %w", pair.Channel.Name, pair.Error))
|
||||
}
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user