Skip to content

Commit 05ef46b

Browse files
vmalert: do not modify notifiers in CR
1 parent 060281c commit 05ef46b

File tree

5 files changed

+652
-562
lines changed

5 files changed

+652
-562
lines changed

api/operator/v1beta1/vmalert_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,18 @@ type VMAlertSpec struct {
7272

7373
// Notifier prometheus alertmanager endpoint spec. Required at least one of notifier or notifiers when there are alerting rules. e.g. http://127.0.0.1:9093
7474
// If specified both notifier and notifiers, notifier will be added as last element to notifiers.
75-
// only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
75+
// only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
7676
// +optional
7777
Notifier *VMAlertNotifierSpec `json:"notifier,omitempty"`
7878

7979
// Notifiers prometheus alertmanager endpoints. Required at least one of notifier or notifiers when there are alerting rules. e.g. http://127.0.0.1:9093
8080
// If specified both notifier and notifiers, notifier will be added as last element to notifiers.
81-
// only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
81+
// only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
8282
// +optional
8383
Notifiers []VMAlertNotifierSpec `json:"notifiers,omitempty"`
8484

8585
// NotifierConfigRef reference for secret with notifier configuration for vmalert
86-
// only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
86+
// only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
8787
// +optional
8888
NotifierConfigRef *corev1.SecretKeySelector `json:"notifierConfigRef,omitempty"`
8989

config/crd/overlay/crd.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19582,7 +19582,7 @@ spec:
1958219582
description: |-
1958319583
Notifier prometheus alertmanager endpoint spec. Required at least one of notifier or notifiers when there are alerting rules. e.g. http://127.0.0.1:9093
1958419584
If specified both notifier and notifiers, notifier will be added as last element to notifiers.
19585-
only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
19585+
only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
1958619586
properties:
1958719587
basicAuth:
1958819588
description: BasicAuth allow an endpoint to authenticate over
@@ -19773,7 +19773,7 @@ spec:
1977319773
notifierConfigRef:
1977419774
description: |-
1977519775
NotifierConfigRef reference for secret with notifier configuration for vmalert
19776-
only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
19776+
only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
1977719777
properties:
1977819778
key:
1977919779
description: The key of the secret to select from. Must be a
@@ -19799,7 +19799,7 @@ spec:
1979919799
description: |-
1980019800
Notifiers prometheus alertmanager endpoints. Required at least one of notifier or notifiers when there are alerting rules. e.g. http://127.0.0.1:9093
1980119801
If specified both notifier and notifiers, notifier will be added as last element to notifiers.
19802-
only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
19802+
only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
1980319803
items:
1980419804
description: VMAlertNotifierSpec defines the notifier url for sending
1980519805
information about alerts

docs/api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3642,9 +3642,9 @@ Appears in: [VMAlert](#vmalert)
36423642
| managedMetadata<a href="#vmalertspec-managedmetadata" id="vmalertspec-managedmetadata">#</a><br/>_[ManagedObjectsMetadata](#managedobjectsmetadata)_ | _(Required)_<br/>ManagedMetadata defines metadata that will be added to the all objects<br />created by operator for the given CustomResource |
36433643
| minReadySeconds<a href="#vmalertspec-minreadyseconds" id="vmalertspec-minreadyseconds">#</a><br/>_integer_ | _(Optional)_<br/>MinReadySeconds defines a minimum number of seconds to wait before starting update next pod<br />if previous in healthy state<br />Has no effect for VLogs and VMSingle |
36443644
| nodeSelector<a href="#vmalertspec-nodeselector" id="vmalertspec-nodeselector">#</a><br/>_object (keys:string, values:string)_ | _(Optional)_<br/>NodeSelector Define which Nodes the Pods are scheduled on. |
3645-
| notifier<a href="#vmalertspec-notifier" id="vmalertspec-notifier">#</a><br/>_[VMAlertNotifierSpec](#vmalertnotifierspec)_ | _(Optional)_<br/>Notifier prometheus alertmanager endpoint spec. Required at least one of notifier or notifiers when there are alerting rules. e.g. http://127.0.0.1:9093<br />If specified both notifier and notifiers, notifier will be added as last element to notifiers.<br />only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier |
3646-
| notifierConfigRef<a href="#vmalertspec-notifierconfigref" id="vmalertspec-notifierconfigref">#</a><br/>_[SecretKeySelector](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.30/#secretkeyselector-v1-core)_ | _(Optional)_<br/>NotifierConfigRef reference for secret with notifier configuration for vmalert<br />only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier |
3647-
| notifiers<a href="#vmalertspec-notifiers" id="vmalertspec-notifiers">#</a><br/>_[VMAlertNotifierSpec](#vmalertnotifierspec) array_ | _(Optional)_<br/>Notifiers prometheus alertmanager endpoints. Required at least one of notifier or notifiers when there are alerting rules. e.g. http://127.0.0.1:9093<br />If specified both notifier and notifiers, notifier will be added as last element to notifiers.<br />only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier |
3645+
| notifier<a href="#vmalertspec-notifier" id="vmalertspec-notifier">#</a><br/>_[VMAlertNotifierSpec](#vmalertnotifierspec)_ | _(Optional)_<br/>Notifier prometheus alertmanager endpoint spec. Required at least one of notifier or notifiers when there are alerting rules. e.g. http://127.0.0.1:9093<br />If specified both notifier and notifiers, notifier will be added as last element to notifiers.<br />only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier |
3646+
| notifierConfigRef<a href="#vmalertspec-notifierconfigref" id="vmalertspec-notifierconfigref">#</a><br/>_[SecretKeySelector](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.30/#secretkeyselector-v1-core)_ | _(Optional)_<br/>NotifierConfigRef reference for secret with notifier configuration for vmalert<br />only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier |
3647+
| notifiers<a href="#vmalertspec-notifiers" id="vmalertspec-notifiers">#</a><br/>_[VMAlertNotifierSpec](#vmalertnotifierspec) array_ | _(Optional)_<br/>Notifiers prometheus alertmanager endpoints. Required at least one of notifier or notifiers when there are alerting rules. e.g. http://127.0.0.1:9093<br />If specified both notifier and notifiers, notifier will be added as last element to notifiers.<br />only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier |
36483648
| paused<a href="#vmalertspec-paused" id="vmalertspec-paused">#</a><br/>_boolean_ | _(Optional)_<br/>Paused If set to true all actions on the underlying managed objects are not<br />going to be performed, except for delete actions. |
36493649
| podDisruptionBudget<a href="#vmalertspec-poddisruptionbudget" id="vmalertspec-poddisruptionbudget">#</a><br/>_[EmbeddedPodDisruptionBudgetSpec](#embeddedpoddisruptionbudgetspec)_ | _(Optional)_<br/>PodDisruptionBudget created by operator |
36503650
| podMetadata<a href="#vmalertspec-podmetadata" id="vmalertspec-podmetadata">#</a><br/>_[EmbeddedObjectMetadata](#embeddedobjectmetadata)_ | _(Required)_<br/>PodMetadata configures Labels and Annotations which are propagated to the VMAlert pods. |

internal/controller/operator/factory/vmalert/vmalert.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/VictoriaMetrics/operator/internal/controller/operator/factory/build"
2222
"github.com/VictoriaMetrics/operator/internal/controller/operator/factory/finalize"
2323
"github.com/VictoriaMetrics/operator/internal/controller/operator/factory/k8stools"
24-
"github.com/VictoriaMetrics/operator/internal/controller/operator/factory/logger"
2524
"github.com/VictoriaMetrics/operator/internal/controller/operator/factory/reconcile"
2625
)
2726

@@ -85,8 +84,8 @@ func CreateOrUpdate(ctx context.Context, cr *vmv1beta1.VMAlert, rclient client.C
8584
if cr.ParsedLastAppliedSpec != nil {
8685
prevCR = cr.DeepCopy()
8786
prevCR.Spec = *cr.ParsedLastAppliedSpec
88-
if err := discoverNotifierIfNeeded(ctx, rclient, prevCR); err != nil {
89-
logger.WithContext(ctx).Error(err, "cannot discover notifiers for prev spec")
87+
if err := discoverNotifiersIfNeeded(ctx, rclient, prevCR); err != nil {
88+
return fmt.Errorf("cannot discover notifiers for prev spec: %w", err)
9089
}
9190

9291
}
@@ -102,8 +101,8 @@ func CreateOrUpdate(ctx context.Context, cr *vmv1beta1.VMAlert, rclient client.C
102101
return fmt.Errorf("failed create service account: %w", err)
103102
}
104103
}
105-
if err := discoverNotifierIfNeeded(ctx, rclient, cr); err != nil {
106-
return fmt.Errorf("cannot discover additional notifiers: %w", err)
104+
if err := discoverNotifiersIfNeeded(ctx, rclient, cr); err != nil {
105+
return fmt.Errorf("cannot discover notifiers for new spec: %w", err)
107106
}
108107

109108
ac := getAssetsCache(ctx, rclient, cr)
@@ -710,7 +709,7 @@ func buildConfigReloaderContainer(cr *vmv1beta1.VMAlert, ruleConfigMapNames []st
710709
return configReloaderContainer
711710
}
712711

713-
func discoverNotifierIfNeeded(ctx context.Context, rclient client.Client, cr *vmv1beta1.VMAlert) error {
712+
func discoverNotifiersIfNeeded(ctx context.Context, rclient client.Client, cr *vmv1beta1.VMAlert) error {
714713
var additionalNotifiers []vmv1beta1.VMAlertNotifierSpec
715714

716715
if cr.Spec.Notifier != nil {
@@ -727,23 +726,19 @@ func discoverNotifierIfNeeded(ctx context.Context, rclient client.Client, cr *vm
727726
continue
728727
}
729728
// discover alertmanagers
730-
var ams vmv1beta1.VMAlertmanagerList
731-
amListOpts, err := n.Selector.AsListOptions()
729+
o, err := n.Selector.AsListOptions()
732730
if err != nil {
733731
return fmt.Errorf("cannot convert notifier selector as ListOptions: %w", err)
734732
}
735-
if err := k8stools.ListObjectsByNamespace(ctx, rclient, config.MustGetWatchNamespaces(), func(objects *vmv1beta1.VMAlertmanagerList) {
736-
ams.Items = append(ams.Items, objects.Items...)
737-
}, amListOpts); err != nil {
738-
return fmt.Errorf("cannot list alertmanager with discovery selector: %w", err)
739-
}
740-
741-
for _, item := range ams.Items {
742-
if !item.DeletionTimestamp.IsZero() || (n.Selector.Namespace != nil && !n.Selector.Namespace.IsMatch(&item)) {
743-
continue
733+
if err := k8stools.ListObjectsByNamespace(ctx, rclient, config.MustGetWatchNamespaces(), func(l *vmv1beta1.VMAlertmanagerList) {
734+
for _, item := range l.Items {
735+
if !item.DeletionTimestamp.IsZero() || (n.Selector.Namespace != nil && !n.Selector.Namespace.IsMatch(&item)) {
736+
continue
737+
}
738+
additionalNotifiers = append(additionalNotifiers, item.AsNotifiers()...)
744739
}
745-
dsc := item.AsNotifiers()
746-
additionalNotifiers = append(additionalNotifiers, dsc...)
740+
}, o); err != nil {
741+
return fmt.Errorf("cannot list alertmanager with discovery selector: %w", err)
747742
}
748743
}
749744
cr.Spec.Notifiers = cr.Spec.Notifiers[:cnt]

0 commit comments

Comments
 (0)