1
0
Fork 0
mirror of https://github.com/prometheus-operator/prometheus-operator.git synced 2025-04-21 11:48:53 +00:00

Add APIVersion field to Prometheus.Spec.Alerting

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2019-11-26 10:54:42 +01:00
parent 53cee6376f
commit 780fb25e9e
9 changed files with 97 additions and 3 deletions
Documentation
example/prometheus-operator-crd
jsonnet/prometheus-operator
pkg

View file

@ -98,6 +98,7 @@ AlertmanagerEndpoints defines a selection of a single Endpoints object containin
| pathPrefix | Prefix for the HTTP path alerts are pushed to. | string | false |
| tlsConfig | TLS Config to use for alertmanager connection. | *[TLSConfig](#tlsconfig) | false |
| bearerTokenFile | BearerTokenFile to read from filesystem to use when authenticating to Alertmanager. | string | false |
| apiVersion | Version of the Alertmanager API that Prometheus uses to send alerts. It can be \"v1\" or \"v2\". | string | false |
[Back to TOC](#table-of-contents)

View file

@ -48,6 +48,7 @@ The versions of Prometheus compatible to be run with the Prometheus Operator are
* v2.8.1
* v2.9.2
* v2.10.0
* v2.11.0
## Alertmanager

View file

@ -699,6 +699,10 @@ spec:
Endpoints object containing alertmanager IPs to fire alerts
against.
properties:
apiVersion:
description: Version of the Alertmanager API that Prometheus
uses to send alerts. It can be "v1" or "v2".
type: string
bearerTokenFile:
description: BearerTokenFile to read from filesystem to use
when authenticating to Alertmanager.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -525,6 +525,9 @@ type AlertmanagerEndpoints struct {
// BearerTokenFile to read from filesystem to use when authenticating to
// Alertmanager.
BearerTokenFile string `json:"bearerTokenFile,omitempty"`
// Version of the Alertmanager API that Prometheus uses to send alerts. It
// can be "v1" or "v2".
APIVersion string `json:"apiVersion,omitempty"`
}
// ServiceMonitor defines monitoring for a set of services.

View file

@ -1029,6 +1029,12 @@ func (cg *configGenerator) generateAlertmanagerConfig(version semver.Version, am
cfg = append(cfg, yaml.MapItem{Key: "bearer_token_file", Value: am.BearerTokenFile})
}
if version.Major > 2 || (version.Major == 2 && version.Minor >= 11) {
if am.APIVersion == "v1" || am.APIVersion == "v2" {
cfg = append(cfg, yaml.MapItem{Key: "api_version", Value: am.APIVersion})
}
}
var relabelings []yaml.MapSlice
relabelings = append(relabelings, yaml.MapSlice{

View file

@ -477,6 +477,84 @@ alerting:
}
}
func TestAlertmanagerAPIVersion(t *testing.T) {
cg := &configGenerator{}
cfg, err := cg.generateConfig(
&monitoringv1.Prometheus{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
},
Spec: monitoringv1.PrometheusSpec{
Version: "v2.11.0",
Alerting: &monitoringv1.AlertingSpec{
Alertmanagers: []monitoringv1.AlertmanagerEndpoints{
{
Name: "alertmanager-main",
Namespace: "default",
Port: intstr.FromString("web"),
APIVersion: "v2",
},
},
},
},
},
nil,
nil,
map[string]BasicAuthCredentials{},
map[string]BearerToken{},
nil,
nil,
nil,
nil,
)
if err != nil {
t.Fatal(err)
}
// If this becomes an endless sink of maintenance, then we should just
// change this to check that just the `api_version` is set with
// something like json-path.
expected := `global:
evaluation_interval: 30s
scrape_interval: 30s
external_labels:
prometheus: default/test
prometheus_replica: $(POD_NAME)
rule_files: []
scrape_configs: []
alerting:
alert_relabel_configs:
- action: labeldrop
regex: prometheus_replica
alertmanagers:
- path_prefix: /
scheme: http
kubernetes_sd_configs:
- role: endpoints
namespaces:
names:
- default
api_version: v2
relabel_configs:
- action: keep
source_labels:
- __meta_kubernetes_service_name
regex: alertmanager-main
- action: keep
source_labels:
- __meta_kubernetes_endpoint_port_name
regex: web
`
result := string(cfg)
if expected != result {
fmt.Println(pretty.Compare(expected, result))
t.Fatal("expected Prometheus configuration and actual configuration do not match")
}
}
func TestAdditionalAlertRelabelConfigs(t *testing.T) {
cg := &configGenerator{}
cfg, err := cg.generateConfig(

View file

@ -93,6 +93,7 @@ var (
"v2.8.1",
"v2.9.2",
"v2.10.0",
"v2.11.0",
}
)