mirror of
https://github.com/grafana/grafana.git
synced 2026-02-03 20:49:50 -05:00
Use new helper methods to rename references in imported route
This commit is contained in:
parent
22adb5f82d
commit
0113d53902
3 changed files with 16 additions and 40 deletions
|
|
@ -141,28 +141,14 @@ func (e ImportedConfigRevision) GetManagedRoute() (*ManagedRoute, error) {
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
// We use a stub config so that calling definition.Merge doesn't modify the original Route.
|
||||
// We leave out parts that are irrelevant to constructing the correctly imported route with renamed receivers and
|
||||
// time intervals.
|
||||
mergeResult, err := definition.Merge(definitions.PostableApiAlertingConfig{
|
||||
Config: definitions.Config{
|
||||
Route: &definitions.Route{},
|
||||
MuteTimeIntervals: e.rev.Config.AlertmanagerConfig.MuteTimeIntervals,
|
||||
TimeIntervals: e.rev.Config.AlertmanagerConfig.TimeIntervals,
|
||||
},
|
||||
Receivers: e.rev.Config.AlertmanagerConfig.Receivers,
|
||||
}, *e.importedConfig, e.opts)
|
||||
renamed, err := definition.DeduplicateResources(e.rev.Config.AlertmanagerConfig, *e.importedConfig, e.opts)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to merge imported config: %w", err)
|
||||
return nil, fmt.Errorf("failed to deduplicate imported config resources: %w", err)
|
||||
}
|
||||
// The imported route should have been injected into the stub and be the only subroute.
|
||||
if len(mergeResult.Config.Route.Routes) != 1 {
|
||||
// This really shouldn't happen, but just in case.
|
||||
return nil, fmt.Errorf("failed to merge imported config: missing imported route")
|
||||
}
|
||||
importedRoute := mergeResult.Config.Route.Routes[0]
|
||||
|
||||
mr := NewManagedRoute(e.identifier, importedRoute)
|
||||
definition.RenameResourceUsagesInRoutes([]*definition.Route{e.importedConfig.Route}, renamed)
|
||||
|
||||
mr := NewManagedRoute(e.identifier, e.importedConfig.Route)
|
||||
mr.Provenance = models.ProvenanceConvertedPrometheus
|
||||
mr.Origin = models.ResourceOriginImported
|
||||
return mr, nil
|
||||
|
|
|
|||
|
|
@ -5,15 +5,12 @@ import (
|
|||
|
||||
"github.com/grafana/alerting/definition"
|
||||
"github.com/prometheus/alertmanager/config"
|
||||
"github.com/prometheus/alertmanager/dispatch"
|
||||
"github.com/prometheus/alertmanager/pkg/labels"
|
||||
"github.com/prometheus/common/model"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
func TestConfigRevisionImported(t *testing.T) {
|
||||
|
|
@ -210,12 +207,9 @@ receivers:
|
|||
assert.NotEmpty(t, route.Version) // Test separately so we don't couple this test to version consistency.
|
||||
route.Version = ""
|
||||
require.Equal(t, &ManagedRoute{
|
||||
Name: extra.Identifier,
|
||||
Version: "",
|
||||
Receiver: "r1",
|
||||
GroupWait: util.Pointer(model.Duration(dispatch.DefaultRouteOpts.GroupWait)),
|
||||
GroupInterval: util.Pointer(model.Duration(dispatch.DefaultRouteOpts.GroupInterval)),
|
||||
RepeatInterval: util.Pointer(model.Duration(dispatch.DefaultRouteOpts.RepeatInterval)),
|
||||
Name: extra.Identifier,
|
||||
Version: "",
|
||||
Receiver: "r1",
|
||||
Routes: []*definitions.Route{
|
||||
{Receiver: "r2", Routes: []*definitions.Route{{Receiver: "r1", Routes: make([]*definition.Route, 0)}}},
|
||||
{Receiver: "", Routes: []*definitions.Route{{Receiver: "r2", Routes: make([]*definition.Route, 0)}}},
|
||||
|
|
@ -256,12 +250,9 @@ mute_time_intervals:
|
|||
assert.NotEmpty(t, route.Version) // Test separately so we don't couple this test to version consistency.
|
||||
route.Version = ""
|
||||
require.Equal(t, &ManagedRoute{
|
||||
Name: extra.Identifier,
|
||||
Version: "",
|
||||
Receiver: "receiver1" + expectedDedupSuffix,
|
||||
GroupWait: util.Pointer(model.Duration(dispatch.DefaultRouteOpts.GroupWait)),
|
||||
GroupInterval: util.Pointer(model.Duration(dispatch.DefaultRouteOpts.GroupInterval)),
|
||||
RepeatInterval: util.Pointer(model.Duration(dispatch.DefaultRouteOpts.RepeatInterval)),
|
||||
Name: extra.Identifier,
|
||||
Version: "",
|
||||
Receiver: "receiver1" + expectedDedupSuffix,
|
||||
Routes: []*definitions.Route{
|
||||
{Receiver: "dupe-receiver" + expectedDedupSuffix, MuteTimeIntervals: []string{"mute-interval-1" + expectedDedupSuffix}, Routes: make([]*definition.Route, 0)},
|
||||
{Receiver: "r1", ActiveTimeIntervals: []string{"time-interval-1" + expectedDedupSuffix}, Routes: make([]*definition.Route, 0)},
|
||||
|
|
|
|||
|
|
@ -75,15 +75,14 @@ func TestIntegrationReadImported_Snapshot(t *testing.T) {
|
|||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: identifier,
|
||||
Namespace: apis.DefaultNamespace,
|
||||
ResourceVersion: "60514828ecf72916",
|
||||
ResourceVersion: "624f4696d803bc64",
|
||||
},
|
||||
Spec: v0alpha1.RoutingTreeSpec{
|
||||
Defaults: v0alpha1.RoutingTreeRouteDefaults{
|
||||
GroupBy: []string{"alertname", "cluster"},
|
||||
GroupWait: util.Pointer("1s"),
|
||||
GroupInterval: util.Pointer("5s"),
|
||||
RepeatInterval: util.Pointer("4h"),
|
||||
Receiver: "noop",
|
||||
GroupBy: []string{"alertname", "cluster"},
|
||||
GroupWait: util.Pointer("1s"),
|
||||
GroupInterval: util.Pointer("5s"),
|
||||
Receiver: "noop",
|
||||
},
|
||||
Routes: []v0alpha1.RoutingTreeRoute{
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue