Skip to content

Commit c78d5b9

Browse files
alanprotmachine424jan--f
authored
Disallowing configure AM with the v1 api (prometheus#13883)
* Stop supporting Alertmanager v1 * Disallowing configure AM with the v1 api Signed-off-by: alanprot <[email protected]> * Update config/config_test.go Co-authored-by: Ayoub Mrini <[email protected]> Signed-off-by: Alan Protasio <[email protected]> * Update config/config.go Co-authored-by: Ayoub Mrini <[email protected]> Signed-off-by: Alan Protasio <[email protected]> * Addressing coments Signed-off-by: alanprot <[email protected]> * Update notifier/notifier.go Co-authored-by: Ayoub Mrini <[email protected]> Signed-off-by: Alan Protasio <[email protected]> * Update config/config_test.go Co-authored-by: Jan Fajerski <[email protected]> Signed-off-by: Alan Protasio <[email protected]> --------- Signed-off-by: alanprot <[email protected]> Signed-off-by: Alan Protasio <[email protected]> Co-authored-by: Ayoub Mrini <[email protected]> Co-authored-by: Jan Fajerski <[email protected]>
1 parent 754c104 commit c78d5b9

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
lines changed

cmd/promtool/testdata/config_with_service_discovery_files.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ scrape_configs:
66
alerting:
77
alertmanagers:
88
- scheme: http
9-
api_version: v1
9+
api_version: v2
1010
file_sd_configs:
1111
- files:
1212
- nonexistent_file.yml

config/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@ func (a AlertmanagerConfigs) ToMap() map[string]*AlertmanagerConfig {
955955

956956
// AlertmanagerAPIVersion represents a version of the
957957
// github.com/prometheus/alertmanager/api, e.g. 'v1' or 'v2'.
958+
// 'v1' is no longer supported.
958959
type AlertmanagerAPIVersion string
959960

960961
// UnmarshalYAML implements the yaml.Unmarshaler interface.
@@ -984,7 +985,7 @@ const (
984985
)
985986

986987
var SupportedAlertmanagerAPIVersions = []AlertmanagerAPIVersion{
987-
AlertmanagerAPIVersionV1, AlertmanagerAPIVersionV2,
988+
AlertmanagerAPIVersionV2,
988989
}
989990

990991
// AlertmanagerConfig configures how Alertmanagers can be discovered and communicated with.

config/config_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,11 @@ var expectedConf = &Config{
15001500
},
15011501
}
15021502

1503+
func TestYAMLNotLongerSupportedAMApi(t *testing.T) {
1504+
_, err := LoadFile("testdata/config_with_no_longer_supported_am_api_config.yml", false, promslog.NewNopLogger())
1505+
require.Error(t, err)
1506+
}
1507+
15031508
func TestYAMLRoundtrip(t *testing.T) {
15041509
want, err := LoadFile("testdata/roundtrip.good.yml", false, promslog.NewNopLogger())
15051510
require.NoError(t, err)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
alerting:
2+
alertmanagers:
3+
- scheme: http
4+
api_version: v1
5+
file_sd_configs:
6+
- files:
7+
- nonexistent_file.yml

notifier/notifier.go

+8-21
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,10 @@ func (n *Manager) sendAll(alerts ...*Alert) bool {
542542

543543
begin := time.Now()
544544

545-
// v1Payload and v2Payload represent 'alerts' marshaled for Alertmanager API
546-
// v1 or v2. Marshaling happens below. Reference here is for caching between
545+
// cachedPayload represent 'alerts' marshaled for Alertmanager API v2.
546+
// Marshaling happens below. Reference here is for caching between
547547
// for loop iterations.
548-
var v1Payload, v2Payload []byte
548+
var cachedPayload []byte
549549

550550
n.mtx.RLock()
551551
amSets := n.alertmanagers
@@ -576,37 +576,24 @@ func (n *Manager) sendAll(alerts ...*Alert) bool {
576576
continue
577577
}
578578
// We can't use the cached values from previous iteration.
579-
v1Payload, v2Payload = nil, nil
579+
cachedPayload = nil
580580
}
581581

582582
switch ams.cfg.APIVersion {
583-
case config.AlertmanagerAPIVersionV1:
584-
{
585-
if v1Payload == nil {
586-
v1Payload, err = json.Marshal(amAlerts)
587-
if err != nil {
588-
n.logger.Error("Encoding alerts for Alertmanager API v1 failed", "err", err)
589-
ams.mtx.RUnlock()
590-
return false
591-
}
592-
}
593-
594-
payload = v1Payload
595-
}
596583
case config.AlertmanagerAPIVersionV2:
597584
{
598-
if v2Payload == nil {
585+
if cachedPayload == nil {
599586
openAPIAlerts := alertsToOpenAPIAlerts(amAlerts)
600587

601-
v2Payload, err = json.Marshal(openAPIAlerts)
588+
cachedPayload, err = json.Marshal(openAPIAlerts)
602589
if err != nil {
603590
n.logger.Error("Encoding alerts for Alertmanager API v2 failed", "err", err)
604591
ams.mtx.RUnlock()
605592
return false
606593
}
607594
}
608595

609-
payload = v2Payload
596+
payload = cachedPayload
610597
}
611598
default:
612599
{
@@ -621,7 +608,7 @@ func (n *Manager) sendAll(alerts ...*Alert) bool {
621608

622609
if len(ams.cfg.AlertRelabelConfigs) > 0 {
623610
// We can't use the cached values on the next iteration.
624-
v1Payload, v2Payload = nil, nil
611+
cachedPayload = nil
625612
}
626613

627614
for _, am := range ams.ams {

notifier/notifier_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,27 @@ func TestPostPath(t *testing.T) {
5050
}{
5151
{
5252
in: "",
53-
out: "/api/v1/alerts",
53+
out: "/api/v2/alerts",
5454
},
5555
{
5656
in: "/",
57-
out: "/api/v1/alerts",
57+
out: "/api/v2/alerts",
5858
},
5959
{
6060
in: "/prefix",
61-
out: "/prefix/api/v1/alerts",
61+
out: "/prefix/api/v2/alerts",
6262
},
6363
{
6464
in: "/prefix//",
65-
out: "/prefix/api/v1/alerts",
65+
out: "/prefix/api/v2/alerts",
6666
},
6767
{
6868
in: "prefix//",
69-
out: "/prefix/api/v1/alerts",
69+
out: "/prefix/api/v2/alerts",
7070
},
7171
}
7272
for _, c := range cases {
73-
require.Equal(t, c.out, postPath(c.in, config.AlertmanagerAPIVersionV1))
73+
require.Equal(t, c.out, postPath(c.in, config.AlertmanagerAPIVersionV2))
7474
}
7575
}
7676

0 commit comments

Comments
 (0)