1
0
Fork 0
mirror of https://github.com/prometheus-operator/prometheus-operator.git synced 2025-04-21 03:38:43 +00:00

chore: optimize AM APIVersion validation ()

* optimize am APIVersion


---------

Signed-off-by: dongjiang1989 <dongjiang1989@126.com>
This commit is contained in:
dongjiang 2024-11-27 18:56:20 +08:00 committed by GitHub
parent f3d6be116a
commit c5de052771
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 206 additions and 28 deletions

27
Documentation/api.md generated
View file

@ -4893,6 +4893,26 @@ in clear-text. Prefer using <code>authorization</code>.</em></p>
</tr>
</tbody>
</table>
<h3 id="monitoring.coreos.com/v1.AlertmanagerAPIVersion">AlertmanagerAPIVersion
(<code>string</code> alias)</h3>
<p>
(<em>Appears on:</em><a href="#monitoring.coreos.com/v1.AlertmanagerEndpoints">AlertmanagerEndpoints</a>)
</p>
<div>
</div>
<table>
<thead>
<tr>
<th>Value</th>
<th>Description</th>
</tr>
</thead>
<tbody><tr><td><p>&#34;V1&#34;</p></td>
<td></td>
</tr><tr><td><p>&#34;V2&#34;</p></td>
<td></td>
</tr></tbody>
</table>
<h3 id="monitoring.coreos.com/v1.AlertmanagerConfigMatcherStrategy">AlertmanagerConfigMatcherStrategy
</h3>
<p>
@ -5164,12 +5184,15 @@ Sigv4
<td>
<code>apiVersion</code><br/>
<em>
string
<a href="#monitoring.coreos.com/v1.AlertmanagerAPIVersion">
AlertmanagerAPIVersion
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>Version of the Alertmanager API that Prometheus uses to send alerts.
It can be &ldquo;v1&rdquo; or &ldquo;v2&rdquo;.
It can be &ldquo;V1&rdquo; or &ldquo;V2&rdquo;.
The field has no effect for Prometheus &gt;= v3.0.0 because only the v2 API is supported.</p>
</td>
</tr>

7
bundle.yaml generated
View file

@ -32945,8 +32945,13 @@ spec:
apiVersion:
description: |-
Version of the Alertmanager API that Prometheus uses to send alerts.
It can be "v1" or "v2".
It can be "V1" or "V2".
The field has no effect for Prometheus >= v3.0.0 because only the v2 API is supported.
enum:
- v1
- V1
- v2
- V2
type: string
authorization:
description: |-

View file

@ -1237,8 +1237,13 @@ spec:
apiVersion:
description: |-
Version of the Alertmanager API that Prometheus uses to send alerts.
It can be "v1" or "v2".
It can be "V1" or "V2".
The field has no effect for Prometheus >= v3.0.0 because only the v2 API is supported.
enum:
- v1
- V1
- v2
- V2
type: string
authorization:
description: |-

View file

@ -1238,8 +1238,13 @@ spec:
apiVersion:
description: |-
Version of the Alertmanager API that Prometheus uses to send alerts.
It can be "v1" or "v2".
It can be "V1" or "V2".
The field has no effect for Prometheus >= v3.0.0 because only the v2 API is supported.
enum:
- v1
- V1
- v2
- V2
type: string
authorization:
description: |-

View file

@ -1010,7 +1010,13 @@
"type": "array"
},
"apiVersion": {
"description": "Version of the Alertmanager API that Prometheus uses to send alerts.\nIt can be \"v1\" or \"v2\".\nThe field has no effect for Prometheus >= v3.0.0 because only the v2 API is supported.",
"description": "Version of the Alertmanager API that Prometheus uses to send alerts.\nIt can be \"V1\" or \"V2\".\nThe field has no effect for Prometheus >= v3.0.0 because only the v2 API is supported.",
"enum": [
"v1",
"V1",
"v2",
"V2"
],
"type": "string"
},
"authorization": {

View file

@ -1860,6 +1860,14 @@ type APIServerConfig struct {
BearerToken string `json:"bearerToken,omitempty"`
}
// +kubebuilder:validation:Enum=v1;V1;v2;V2
type AlertmanagerAPIVersion string
const (
AlertmanagerAPIVersion1 = AlertmanagerAPIVersion("V1")
AlertmanagerAPIVersion2 = AlertmanagerAPIVersion("V2")
)
// AlertmanagerEndpoints defines a selection of a single Endpoints object
// containing Alertmanager IPs to fire alerts against.
// +k8s:openapi-gen=true
@ -1924,9 +1932,11 @@ type AlertmanagerEndpoints struct {
Sigv4 *Sigv4 `json:"sigv4,omitempty"`
// Version of the Alertmanager API that Prometheus uses to send alerts.
// It can be "v1" or "v2".
// It can be "V1" or "V2".
// The field has no effect for Prometheus >= v3.0.0 because only the v2 API is supported.
APIVersion string `json:"apiVersion,omitempty"`
//
// +optional
APIVersion *AlertmanagerAPIVersion `json:"apiVersion,omitempty"`
// Timeout is a per-target Alertmanager timeout when pushing alerts.
//

View file

@ -167,6 +167,11 @@ func (in *AlertmanagerEndpoints) DeepCopyInto(out *AlertmanagerEndpoints) {
*out = new(Sigv4)
(*in).DeepCopyInto(*out)
}
if in.APIVersion != nil {
in, out := &in.APIVersion, &out.APIVersion
*out = new(AlertmanagerAPIVersion)
**out = **in
}
if in.Timeout != nil {
in, out := &in.Timeout, &out.Timeout
*out = new(Duration)

View file

@ -34,7 +34,7 @@ type AlertmanagerEndpointsApplyConfiguration struct {
BearerTokenFile *string `json:"bearerTokenFile,omitempty"`
Authorization *SafeAuthorizationApplyConfiguration `json:"authorization,omitempty"`
Sigv4 *Sigv4ApplyConfiguration `json:"sigv4,omitempty"`
APIVersion *string `json:"apiVersion,omitempty"`
APIVersion *monitoringv1.AlertmanagerAPIVersion `json:"apiVersion,omitempty"`
Timeout *monitoringv1.Duration `json:"timeout,omitempty"`
EnableHttp2 *bool `json:"enableHttp2,omitempty"`
RelabelConfigs []RelabelConfigApplyConfiguration `json:"relabelings,omitempty"`
@ -130,7 +130,7 @@ func (b *AlertmanagerEndpointsApplyConfiguration) WithSigv4(value *Sigv4ApplyCon
// WithAPIVersion sets the APIVersion field in the declarative configuration to the given value
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
// If called multiple times, the APIVersion field is set to the value of the last call.
func (b *AlertmanagerEndpointsApplyConfiguration) WithAPIVersion(value string) *AlertmanagerEndpointsApplyConfiguration {
func (b *AlertmanagerEndpointsApplyConfiguration) WithAPIVersion(value monitoringv1.AlertmanagerAPIVersion) *AlertmanagerEndpointsApplyConfiguration {
b.APIVersion = &value
return b
}

View file

@ -2182,12 +2182,16 @@ func (cg *ConfigGenerator) generateAlertmanagerConfig(alerting *monitoringv1.Ale
cfg = cg.WithMinimumVersion("2.48.0").addSigv4ToYaml(cfg, fmt.Sprintf("alertmanager/auth/%d", i), store, am.Sigv4)
apiVersionCg := cg.WithMinimumVersion("2.11.0")
switch am.APIVersion {
case "v1":
if am.APIVersion != nil {
switch monitoringv1.AlertmanagerAPIVersion(strings.ToUpper(string(*am.APIVersion))) {
// API v1 isn't supported anymore by Prometheus v3.
cfg = apiVersionCg.WithMaximumVersion("2.999.0").AppendMapItem(cfg, "api_version", am.APIVersion)
case "v2":
cfg = apiVersionCg.AppendMapItem(cfg, "api_version", am.APIVersion)
case monitoringv1.AlertmanagerAPIVersion1:
if cg.version.Major <= 2 {
cfg = apiVersionCg.AppendMapItem(cfg, "api_version", strings.ToLower(string(*am.APIVersion)))
}
case monitoringv1.AlertmanagerAPIVersion2:
cfg = apiVersionCg.AppendMapItem(cfg, "api_version", strings.ToLower(string(*am.APIVersion)))
}
}
var relabelings []yaml.MapSlice

View file

@ -1184,7 +1184,7 @@ func TestAlertmanagerAPIVersion(t *testing.T) {
Name: "alertmanager-main",
Namespace: ptr.To("default"),
Port: intstr.FromString("web"),
APIVersion: "v1",
APIVersion: ptr.To(monitoringv1.AlertmanagerAPIVersion1),
},
},
},
@ -1199,7 +1199,7 @@ func TestAlertmanagerAPIVersion(t *testing.T) {
Name: "alertmanager-main",
Namespace: ptr.To("default"),
Port: intstr.FromString("web"),
APIVersion: "v2",
APIVersion: ptr.To(monitoringv1.AlertmanagerAPIVersion2),
},
},
},
@ -1214,11 +1214,26 @@ func TestAlertmanagerAPIVersion(t *testing.T) {
Name: "alertmanager-main",
Namespace: ptr.To("default"),
Port: intstr.FromString("web"),
APIVersion: "v2",
APIVersion: ptr.To(monitoringv1.AlertmanagerAPIVersion2),
},
},
},
golden: "AlertmanagerAPIVersion.golden",
golden: "AlertmanagerAPIVersionPrometheusV3.golden",
},
{
name: "Alertmanager APIv1 Incompatible with Prometheus V3",
version: "3.0.0-rc.0",
alerting: &monitoringv1.AlertingSpec{
Alertmanagers: []monitoringv1.AlertmanagerEndpoints{
{
Name: "alertmanager-main",
Namespace: ptr.To("default"),
Port: intstr.FromString("web"),
APIVersion: ptr.To(monitoringv1.AlertmanagerAPIVersion1),
},
},
},
golden: "AlertmanagerAPIVersionV1LowerCasePrometheusV3.golden",
},
{
name: "Alertmanager APIV1 Incompatible with Prometheus V3",
@ -1229,11 +1244,26 @@ func TestAlertmanagerAPIVersion(t *testing.T) {
Name: "alertmanager-main",
Namespace: ptr.To("default"),
Port: intstr.FromString("web"),
APIVersion: "v1",
APIVersion: ptr.To(monitoringv1.AlertmanagerAPIVersion1),
},
},
},
golden: "AlertmanagerAPIVersionV1PrometheusV3.golden",
golden: "AlertmanagerAPIVersionV1UpperCasePrometheusV3.golden",
},
{
name: "Alertmanager APIV2 Incompatible with Prometheus V3",
version: "3.0.0",
alerting: &monitoringv1.AlertingSpec{
Alertmanagers: []monitoringv1.AlertmanagerEndpoints{
{
Name: "alertmanager-main",
Namespace: ptr.To("default"),
Port: intstr.FromString("web"),
APIVersion: ptr.To(monitoringv1.AlertmanagerAPIVersion1),
},
},
},
golden: "AlertmanagerAPIVersionV2UpperCasePrometheusV3.golden",
},
}
for _, tc := range testCases {
@ -1271,7 +1301,7 @@ func TestAlertmanagerTimeoutConfig(t *testing.T) {
Name: "alertmanager-main",
Namespace: ptr.To("default"),
Port: intstr.FromString("web"),
APIVersion: "v2",
APIVersion: ptr.To(monitoringv1.AlertmanagerAPIVersion2),
Timeout: ptr.To(monitoringv1.Duration("60s")),
},
},
@ -1329,7 +1359,7 @@ func TestAlertmanagerEnableHttp2(t *testing.T) {
Name: "alertmanager-main",
Namespace: ptr.To("default"),
Port: intstr.FromString("web"),
APIVersion: "v2",
APIVersion: ptr.To(monitoringv1.AlertmanagerAPIVersion2),
EnableHttp2: ptr.To(tc.enableHTTP2),
},
},
@ -1363,7 +1393,7 @@ func TestAlertmanagerRelabelConfigs(t *testing.T) {
Name: "alertmanager-main",
Namespace: ptr.To("default"),
Port: intstr.FromString("web"),
APIVersion: "v2",
APIVersion: ptr.To(monitoringv1.AlertmanagerAPIVersion2),
RelabelConfigs: []monitoringv1.RelabelConfig{
{
TargetLabel: "namespace",
@ -1430,7 +1460,7 @@ func TestAlertmanagerAlertRelabelConfigs(t *testing.T) {
Name: "alertmanager-main",
Namespace: ptr.To("default"),
Port: intstr.FromString("web"),
APIVersion: "v2",
APIVersion: ptr.To(monitoringv1.AlertmanagerAPIVersion2),
AlertRelabelConfigs: []monitoringv1.RelabelConfig{
{
TargetLabel: "namespace",

View file

@ -0,0 +1,29 @@
global:
scrape_interval: 30s
external_labels:
prometheus: default/test
prometheus_replica: $(POD_NAME)
evaluation_interval: 30s
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

View file

@ -0,0 +1,28 @@
global:
scrape_interval: 30s
external_labels:
prometheus: default/test
prometheus_replica: $(POD_NAME)
evaluation_interval: 30s
scrape_configs: []
alerting:
alert_relabel_configs:
- action: labeldrop
regex: prometheus_replica
alertmanagers:
- path_prefix: /
scheme: http
kubernetes_sd_configs:
- role: endpoints
namespaces:
names:
- default
relabel_configs:
- action: keep
source_labels:
- __meta_kubernetes_service_name
regex: alertmanager-main
- action: keep
source_labels:
- __meta_kubernetes_endpoint_port_name
regex: web

View file

@ -0,0 +1,28 @@
global:
scrape_interval: 30s
external_labels:
prometheus: default/test
prometheus_replica: $(POD_NAME)
evaluation_interval: 30s
scrape_configs: []
alerting:
alert_relabel_configs:
- action: labeldrop
regex: prometheus_replica
alertmanagers:
- path_prefix: /
scheme: http
kubernetes_sd_configs:
- role: endpoints
namespaces:
names:
- default
relabel_configs:
- action: keep
source_labels:
- __meta_kubernetes_service_name
regex: alertmanager-main
- action: keep
source_labels:
- __meta_kubernetes_endpoint_port_name
regex: web

View file

@ -4718,7 +4718,7 @@ func testPrometheusCRDValidation(t *testing.T) {
Scheme: "https",
PathPrefix: "/alerts",
BearerTokenFile: "/file",
APIVersion: "v1",
APIVersion: ptr.To(monitoringv1.AlertmanagerAPIVersion1),
},
},
},
@ -4747,7 +4747,7 @@ func testPrometheusCRDValidation(t *testing.T) {
Scheme: "https",
PathPrefix: "/alerts",
BearerTokenFile: "/file",
APIVersion: "v1",
APIVersion: ptr.To(monitoringv1.AlertmanagerAPIVersion1),
},
},
},
@ -4775,7 +4775,7 @@ func testPrometheusCRDValidation(t *testing.T) {
Scheme: "https",
PathPrefix: "/alerts",
BearerTokenFile: "/file",
APIVersion: "v1",
APIVersion: ptr.To(monitoringv1.AlertmanagerAPIVersion1),
},
},
},