@@ -12,6 +12,7 @@ import (
1212 "github.com/VictoriaMetrics/operator/controllers/factory/k8stools"
1313 "github.com/VictoriaMetrics/operator/controllers/factory/psp"
1414 "github.com/VictoriaMetrics/operator/internal/config"
15+ "golang.org/x/exp/slices"
1516 appsv1 "k8s.io/api/apps/v1"
1617 corev1 "k8s.io/api/core/v1"
1718 "k8s.io/apimachinery/pkg/api/errors"
@@ -127,41 +128,39 @@ func createOrUpdateVMAlertSecret(ctx context.Context, rclient client.Client, cr
127128
128129func CreateOrUpdateVMAlert (ctx context.Context , cr * victoriametricsv1beta1.VMAlert , rclient client.Client , c * config.BaseOperatorConf , cmNames []string ) error {
129130 l := log .WithValues ("controller" , "vmalert.crud" , "vmalert" , cr .Name )
131+
130132 // copy to avoid side effects.
131133 cr = cr .DeepCopy ()
132- var additionalNotifiers []victoriametricsv1beta1.VMAlertNotifierSpec
133134
134- if cr .Spec .Notifier != nil {
135- cr .Spec .Notifiers = append (cr .Spec .Notifiers , * cr .Spec .Notifier )
136- }
137- // trim notifiers with non-empty notifier Selector
138- var cnt int
139- for i := range cr .Spec .Notifiers {
140- n := cr .Spec .Notifiers [i ]
141- // fast path
142- if n .Selector == nil {
143- cr .Spec .Notifiers [cnt ] = n
144- cnt ++
145- continue
146- }
135+ var additionalNotifiers []victoriametricsv1beta1.VMAlertNotifierSpec
136+ for _ , selector := range cr .GetNotifierSelectors () {
147137 // discover alertmanagers
148138 var ams victoriametricsv1beta1.VMAlertmanagerList
149- amListOpts , err := n . Selector .AsListOptions ()
139+ amListOpts , err := selector .AsListOptions ()
150140 if err != nil {
151141 return fmt .Errorf ("cannot convert notifier selector as ListOptions: %w" , err )
152142 }
153143 if err := rclient .List (ctx , & ams , amListOpts , config .MustGetNamespaceListOptions ()); err != nil {
154144 return fmt .Errorf ("cannot list alertmanagers for vmalert notifier sd: %w" , err )
155145 }
156146 for _ , item := range ams .Items {
157- if ! item .DeletionTimestamp .IsZero () || (n . Selector . Namespace != nil && ! n . Selector .Namespace .IsMatch (& item )) {
147+ if ! item .DeletionTimestamp .IsZero () || (selector . Namespace != nil && ! selector .Namespace .IsMatch (& item )) {
158148 continue
159149 }
160150 dsc := item .AsNotifiers ()
161151 additionalNotifiers = append (additionalNotifiers , dsc ... )
162152 }
163153 }
164- cr .Spec .Notifiers = cr .Spec .Notifiers [:cnt ]
154+
155+ // remove notifiers with a selector as they have been consumed and the
156+ // results added to additionalNotifiers.
157+ cr .Spec .Notifiers = slices .DeleteFunc (cr .Spec .Notifiers , func (n victoriametricsv1beta1.VMAlertNotifierSpec ) bool {
158+ return n .Selector != nil
159+ })
160+
161+ if cr .Spec .Notifier != nil && cr .Spec .Notifier .Selector == nil {
162+ cr .Spec .Notifiers = append (cr .Spec .Notifiers , * cr .Spec .Notifier )
163+ }
165164
166165 if len (additionalNotifiers ) > 0 {
167166 sort .Slice (additionalNotifiers , func (i , j int ) bool {
0 commit comments