Disallowing configure AM with the v1 api (#13883)

* Stop supporting Alertmanager v1

* Disallowing configure AM with the v1 api

Signed-off-by: alanprot <alanprot@gmail.com>

* Update config/config_test.go

Co-authored-by: Ayoub Mrini <ayoubmrini424@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Update config/config.go

Co-authored-by: Ayoub Mrini <ayoubmrini424@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Addressing coments

Signed-off-by: alanprot <alanprot@gmail.com>

* Update notifier/notifier.go

Co-authored-by: Ayoub Mrini <ayoubmrini424@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Update config/config_test.go

Co-authored-by: Jan Fajerski <jan--f@users.noreply.github.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>

---------

Signed-off-by: alanprot <alanprot@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
Co-authored-by: Ayoub Mrini <ayoubmrini424@gmail.com>
Co-authored-by: Jan Fajerski <jan--f@users.noreply.github.com>
This commit is contained in:
Alan Protasio 2024-10-18 06:23:14 -07:00 committed by GitHub
parent 754c104a3e
commit c78d5b94af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 29 additions and 29 deletions

View File

@ -6,7 +6,7 @@ scrape_configs:
alerting: alerting:
alertmanagers: alertmanagers:
- scheme: http - scheme: http
api_version: v1 api_version: v2
file_sd_configs: file_sd_configs:
- files: - files:
- nonexistent_file.yml - nonexistent_file.yml

View File

@ -955,6 +955,7 @@ func (a AlertmanagerConfigs) ToMap() map[string]*AlertmanagerConfig {
// AlertmanagerAPIVersion represents a version of the // AlertmanagerAPIVersion represents a version of the
// github.com/prometheus/alertmanager/api, e.g. 'v1' or 'v2'. // github.com/prometheus/alertmanager/api, e.g. 'v1' or 'v2'.
// 'v1' is no longer supported.
type AlertmanagerAPIVersion string type AlertmanagerAPIVersion string
// UnmarshalYAML implements the yaml.Unmarshaler interface. // UnmarshalYAML implements the yaml.Unmarshaler interface.
@ -984,7 +985,7 @@ const (
) )
var SupportedAlertmanagerAPIVersions = []AlertmanagerAPIVersion{ var SupportedAlertmanagerAPIVersions = []AlertmanagerAPIVersion{
AlertmanagerAPIVersionV1, AlertmanagerAPIVersionV2, AlertmanagerAPIVersionV2,
} }
// AlertmanagerConfig configures how Alertmanagers can be discovered and communicated with. // AlertmanagerConfig configures how Alertmanagers can be discovered and communicated with.

View File

@ -1500,6 +1500,11 @@ var expectedConf = &Config{
}, },
} }
func TestYAMLNotLongerSupportedAMApi(t *testing.T) {
_, err := LoadFile("testdata/config_with_no_longer_supported_am_api_config.yml", false, promslog.NewNopLogger())
require.Error(t, err)
}
func TestYAMLRoundtrip(t *testing.T) { func TestYAMLRoundtrip(t *testing.T) {
want, err := LoadFile("testdata/roundtrip.good.yml", false, promslog.NewNopLogger()) want, err := LoadFile("testdata/roundtrip.good.yml", false, promslog.NewNopLogger())
require.NoError(t, err) require.NoError(t, err)

View File

@ -0,0 +1,7 @@
alerting:
alertmanagers:
- scheme: http
api_version: v1
file_sd_configs:
- files:
- nonexistent_file.yml

View File

@ -542,10 +542,10 @@ func (n *Manager) sendAll(alerts ...*Alert) bool {
begin := time.Now() begin := time.Now()
// v1Payload and v2Payload represent 'alerts' marshaled for Alertmanager API // cachedPayload represent 'alerts' marshaled for Alertmanager API v2.
// v1 or v2. Marshaling happens below. Reference here is for caching between // Marshaling happens below. Reference here is for caching between
// for loop iterations. // for loop iterations.
var v1Payload, v2Payload []byte var cachedPayload []byte
n.mtx.RLock() n.mtx.RLock()
amSets := n.alertmanagers amSets := n.alertmanagers
@ -576,29 +576,16 @@ func (n *Manager) sendAll(alerts ...*Alert) bool {
continue continue
} }
// We can't use the cached values from previous iteration. // We can't use the cached values from previous iteration.
v1Payload, v2Payload = nil, nil cachedPayload = nil
} }
switch ams.cfg.APIVersion { switch ams.cfg.APIVersion {
case config.AlertmanagerAPIVersionV1:
{
if v1Payload == nil {
v1Payload, err = json.Marshal(amAlerts)
if err != nil {
n.logger.Error("Encoding alerts for Alertmanager API v1 failed", "err", err)
ams.mtx.RUnlock()
return false
}
}
payload = v1Payload
}
case config.AlertmanagerAPIVersionV2: case config.AlertmanagerAPIVersionV2:
{ {
if v2Payload == nil { if cachedPayload == nil {
openAPIAlerts := alertsToOpenAPIAlerts(amAlerts) openAPIAlerts := alertsToOpenAPIAlerts(amAlerts)
v2Payload, err = json.Marshal(openAPIAlerts) cachedPayload, err = json.Marshal(openAPIAlerts)
if err != nil { if err != nil {
n.logger.Error("Encoding alerts for Alertmanager API v2 failed", "err", err) n.logger.Error("Encoding alerts for Alertmanager API v2 failed", "err", err)
ams.mtx.RUnlock() ams.mtx.RUnlock()
@ -606,7 +593,7 @@ func (n *Manager) sendAll(alerts ...*Alert) bool {
} }
} }
payload = v2Payload payload = cachedPayload
} }
default: default:
{ {
@ -621,7 +608,7 @@ func (n *Manager) sendAll(alerts ...*Alert) bool {
if len(ams.cfg.AlertRelabelConfigs) > 0 { if len(ams.cfg.AlertRelabelConfigs) > 0 {
// We can't use the cached values on the next iteration. // We can't use the cached values on the next iteration.
v1Payload, v2Payload = nil, nil cachedPayload = nil
} }
for _, am := range ams.ams { for _, am := range ams.ams {

View File

@ -50,27 +50,27 @@ func TestPostPath(t *testing.T) {
}{ }{
{ {
in: "", in: "",
out: "/api/v1/alerts", out: "/api/v2/alerts",
}, },
{ {
in: "/", in: "/",
out: "/api/v1/alerts", out: "/api/v2/alerts",
}, },
{ {
in: "/prefix", in: "/prefix",
out: "/prefix/api/v1/alerts", out: "/prefix/api/v2/alerts",
}, },
{ {
in: "/prefix//", in: "/prefix//",
out: "/prefix/api/v1/alerts", out: "/prefix/api/v2/alerts",
}, },
{ {
in: "prefix//", in: "prefix//",
out: "/prefix/api/v1/alerts", out: "/prefix/api/v2/alerts",
}, },
} }
for _, c := range cases { for _, c := range cases {
require.Equal(t, c.out, postPath(c.in, config.AlertmanagerAPIVersionV1)) require.Equal(t, c.out, postPath(c.in, config.AlertmanagerAPIVersionV2))
} }
} }