Skip to content

Commit

Permalink
vmalert: remove notifier null check (#813)
Browse files Browse the repository at this point in the history
* vmalert: remove notifier null check, since `-notifier.url` is optional and is needed only if there are alerting rules.

* fix vmauth EmbeddedIngress sanity check

* update doc

* fix ut
  • Loading branch information
Haleygo authored Nov 28, 2023
1 parent f628bee commit 7965dba
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 66 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fix_crd_nulls_yaml:


doc: install-develop-tools
cat hack/doc_header.md > doc_api.md
cat hack/doc_header.md > docs/api.md
doc-print --paths=\
$(APIS_BASE_PATH)/vmalertmanager_types.go,\
$(APIS_BASE_PATH)/vmalertmanagerconfig_types.go,\
Expand All @@ -216,7 +216,7 @@ doc: install-develop-tools
$(APIS_BASE_PATH)/vmstaticscrape_types.go,\
$(APIS_BASE_PATH)/vmprobe_types.go \
--owner VictoriaMetrics \
>> doc_api.md
>> docs/api.md

operator-conf: install-develop-tools
cat hack/doc_vars_header.md > vars.md
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta1/vmalert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ type VMAlertSpec struct {
// +optional
Port string `json:"port,omitempty"`

// Notifier prometheus alertmanager endpoint spec. Required at least one of notifier or notifiers. e.g. http://127.0.0.1:9093
// 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
// If specified both notifier and notifiers, notifier will be added as last element to notifiers.
// only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
// +optional
Notifier *VMAlertNotifierSpec `json:"notifier,omitempty"`

// Notifiers prometheus alertmanager endpoints. Required at least one of notifier or notifiers. e.g. http://127.0.0.1:9093
// 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
// If specified both notifier and notifiers, notifier will be added as last element to notifiers.
// only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
// +optional
Expand Down
4 changes: 0 additions & 4 deletions api/v1beta1/vmalert_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ func (r *VMAlert) sanityCheck() error {
return fmt.Errorf("spec.datasource.url cannot be empty")
}

if r.Spec.Notifier == nil && len(r.Spec.Notifiers) == 0 && r.Spec.NotifierConfigRef == nil {
return fmt.Errorf("notifier is not defined, provide valid config with spec.notifier or spec.notifiers or spec.notifierConfigRef")
}

if r.Spec.Notifier != nil {
if r.Spec.Notifier.URL == "" && r.Spec.Notifier.Selector == nil {
return fmt.Errorf("spec.notifier.url and spec.notifier.selector cannot be empty at the same time, provide at least one setting")
Expand Down
3 changes: 1 addition & 2 deletions api/v1beta1/vmalert_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
)

func TestVMAlert_sanityCheck(t *testing.T) {

tests := []struct {
name string
spec VMAlertSpec
Expand All @@ -23,7 +22,7 @@ func TestVMAlert_sanityCheck(t *testing.T) {
{
name: "wo notifiers",
spec: VMAlertSpec{Datasource: VMAlertDatasourceSpec{URL: "http://some-url"}},
wantErr: true,
wantErr: false,
},
{
name: "wo notifier url",
Expand Down
5 changes: 4 additions & 1 deletion api/v1beta1/vmauth_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ func (r *VMAuth) SetupWebhookWithManager(mgr ctrl.Manager) error {
var _ webhook.Validator = &VMAuth{}

func (cr *VMAuth) sanityCheck() error {

if cr.Spec.Ingress != nil {
// check ingress
// TlsHosts and TlsSecretName are both needed if one of them is used
ing := cr.Spec.Ingress
if len(ing.TlsHosts) > 0 && ing.TlsSecretName == "" {
return fmt.Errorf("spec.ingress.tlsSecretName cannot be empty with non-empty spec.ingress.tlsHosts")
}
if ing.TlsSecretName != "" && len(ing.TlsHosts) == 0 {
return fmt.Errorf("spec.ingress.tlsHosts cannot be empty with non-empty spec.ingress.tlsSecretName")
}
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions api/victoriametrics/v1beta1/vmalert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ type VMAlertSpec struct {
// +optional
Port string `json:"port,omitempty"`

// Notifier prometheus alertmanager endpoint spec. Required at least one of notifier or notifiers. e.g. http://127.0.0.1:9093
// 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
// If specified both notifier and notifiers, notifier will be added as last element to notifiers.
// only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
// +optional
Notifier *VMAlertNotifierSpec `json:"notifier,omitempty"`

// Notifiers prometheus alertmanager endpoints. Required at least one of notifier or notifiers. e.g. http://127.0.0.1:9093
// 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
// If specified both notifier and notifiers, notifier will be added as last element to notifiers.
// only one of notifier options could be chosen: notifierConfigRef or notifiers + notifier
// +optional
Expand Down
4 changes: 0 additions & 4 deletions api/victoriametrics/v1beta1/vmalert_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ func (r *VMAlert) sanityCheck() error {
return fmt.Errorf("spec.datasource.url cannot be empty")
}

if r.Spec.Notifier == nil && len(r.Spec.Notifiers) == 0 && r.Spec.NotifierConfigRef == nil {
return fmt.Errorf("notifier is not defined, provide valid config with spec.notifier or spec.notifiers or spec.notifierConfigRef")
}

if r.Spec.Notifier != nil {
if r.Spec.Notifier.URL == "" && r.Spec.Notifier.Selector == nil {
return fmt.Errorf("spec.notifier.url and spec.notifier.selector cannot be empty at the same time, provide at least one setting")
Expand Down
3 changes: 1 addition & 2 deletions api/victoriametrics/v1beta1/vmalert_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
)

func TestVMAlert_sanityCheck(t *testing.T) {

tests := []struct {
name string
spec VMAlertSpec
Expand All @@ -23,7 +22,7 @@ func TestVMAlert_sanityCheck(t *testing.T) {
{
name: "wo notifiers",
spec: VMAlertSpec{Datasource: VMAlertDatasourceSpec{URL: "http://some-url"}},
wantErr: true,
wantErr: false,
},
{
name: "wo notifier url",
Expand Down
5 changes: 4 additions & 1 deletion api/victoriametrics/v1beta1/vmauth_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ func (r *VMAuth) SetupWebhookWithManager(mgr ctrl.Manager) error {
var _ webhook.Validator = &VMAuth{}

func (cr *VMAuth) sanityCheck() error {

if cr.Spec.Ingress != nil {
// check ingress
// TlsHosts and TlsSecretName are both needed if one of them is used
ing := cr.Spec.Ingress
if len(ing.TlsHosts) > 0 && ing.TlsSecretName == "" {
return fmt.Errorf("spec.ingress.tlsSecretName cannot be empty with non-empty spec.ingress.tlsHosts")
}
if ing.TlsSecretName != "" && len(ing.TlsHosts) == 0 {
return fmt.Errorf("spec.ingress.tlsHosts cannot be empty with non-empty spec.ingress.tlsSecretName")
}
}
return nil
}
Expand Down
18 changes: 10 additions & 8 deletions config/crd/bases/operator.victoriametrics.com_vmalerts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,11 @@ spec:
type: object
notifier:
description: 'Notifier prometheus alertmanager endpoint spec. Required
at least one of notifier or notifiers. e.g. http://127.0.0.1:9093
If specified both notifier and notifiers, notifier will be added
as last element to notifiers. only one of notifier options could
be chosen: notifierConfigRef or notifiers + notifier'
at least one of notifier or notifiers when there are alerting rules.
e.g. http://127.0.0.1:9093 If specified both notifier and notifiers,
notifier will be added as last element to notifiers. only one of
notifier options could be chosen: notifierConfigRef or notifiers
+ notifier'
properties:
basicAuth:
description: BasicAuth allow an endpoint to authenticate over
Expand Down Expand Up @@ -722,10 +723,11 @@ spec:
x-kubernetes-map-type: atomic
notifiers:
description: 'Notifiers prometheus alertmanager endpoints. Required
at least one of notifier or notifiers. e.g. http://127.0.0.1:9093
If specified both notifier and notifiers, notifier will be added
as last element to notifiers. only one of notifier options could
be chosen: notifierConfigRef or notifiers + notifier'
at least one of notifier or notifiers when there are alerting rules.
e.g. http://127.0.0.1:9093 If specified both notifier and notifiers,
notifier will be added as last element to notifiers. only one of
notifier options could be chosen: notifierConfigRef or notifiers
+ notifier'
items:
description: VMAlertNotifierSpec defines the notifier url for sending
information about alerts
Expand Down
4 changes: 1 addition & 3 deletions controllers/factory/vmalert.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ func CreateOrUpdateVMAlert(ctx context.Context, cr *victoriametricsv1beta1.VMAle
l.Info("additional notifiers with sd selector", "len", len(additionalNotifiers))
}
cr.Spec.Notifiers = append(cr.Spec.Notifiers, additionalNotifiers...)
if len(cr.Spec.Notifiers) == 0 && cr.Spec.NotifierConfigRef == nil {
return fmt.Errorf("cannot create or update vmalert: %s, cannot find any notifiers. At cr.spec.Notifiers, cr.spec.Notifier, cr.spec.notifierConfigRef and discovered alertmanager are empty", cr.Name)
}

if err := psp.CreateServiceAccountForCRD(ctx, cr, rclient); err != nil {
return fmt.Errorf("failed create service account: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ aliases:
## Next release

- [vmalertmanagerconfig](./api.html#vmalertmanagerconfig): add fields `entity`, `actions` and `update_alerts` for opsgenie_configs according to https://prometheus.io/docs/alerting/latest/configuration/#opsgenie_config.
- [vmoperator](./README.md): remove vmalert notifier null check, since `-notifier.url` is optional and is needed only if there are alerting rules.

<a name="v0.39.3"></a>
## [v0.39.3](https://github.com/VictoriaMetrics/operator/releases/tag/v0.39.3) - 16 Nov 2023
Expand Down
Loading

0 comments on commit 7965dba

Please sign in to comment.