/
vmalert_webhook.go
78 lines (65 loc) · 2.27 KB
/
vmalert_webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package v1beta1
import (
"fmt"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
// log is for logging in this package.
var vmalertlog = logf.Log.WithName("vmalert-resource")
func (r *VMAlert) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
// +kubebuilder:webhook:verbs=create;update,admissionReviewVersions=v1,sideEffects=none,path=/validate-operator-victoriametrics-com-v1beta1-vmalert,mutating=false,failurePolicy=fail,groups=operator.victoriametrics.com,resources=vmalerts,versions=v1beta1,name=vvmalert.kb.io
var _ webhook.Validator = &VMAlert{}
func (r *VMAlert) sanityCheck() error {
if r.Spec.Datasource.URL == "" {
return fmt.Errorf("spec.datasource.url cannot be empty")
}
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")
}
}
for idx, nt := range r.Spec.Notifiers {
if nt.URL == "" && nt.Selector == nil {
return fmt.Errorf("notifier.url is empty and selector is not set, provide at least once for spec.notifiers at idx: %d", idx)
}
}
return nil
}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *VMAlert) ValidateCreate() (aw admission.Warnings, err error) {
if r.Spec.ParsingError != "" {
return aw, fmt.Errorf(r.Spec.ParsingError)
}
if mustSkipValidation(r) {
return
}
if err := r.sanityCheck(); err != nil {
return aw, err
}
return
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *VMAlert) ValidateUpdate(old runtime.Object) (aw admission.Warnings, err error) {
if r.Spec.ParsingError != "" {
return aw, fmt.Errorf(r.Spec.ParsingError)
}
if mustSkipValidation(r) {
return
}
if err := r.sanityCheck(); err != nil {
return aw, err
}
return
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *VMAlert) ValidateDelete() (aw admission.Warnings, err error) {
// no-op
return
}