/
grafana_webhook.go
70 lines (50 loc) · 2.42 KB
/
grafana_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
// Copyright (c) 2022 Aiven, Helsinki, Finland. https://aiven.io/
package v1alpha1
import (
"errors"
"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"
)
// log is for logging in this package.
var grafanalog = logf.Log.WithName("grafana-resource")
func (in *Grafana) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(in).
Complete()
}
//+kubebuilder:webhook:path=/mutate-aiven-io-v1alpha1-grafana,mutating=true,failurePolicy=fail,sideEffects=None,groups=aiven.io,resources=grafanas,verbs=create;update,versions=v1alpha1,name=mgrafana.kb.io,admissionReviewVersions=v1
var _ webhook.Defaulter = &Grafana{}
func (in *Grafana) Default() {
grafanalog.Info("default", "name", in.Name)
}
//+kubebuilder:webhook:verbs=create;update;delete,path=/validate-aiven-io-v1alpha1-grafana,mutating=false,failurePolicy=fail,groups=aiven.io,resources=grafanas,versions=v1alpha1,name=vgrafana.kb.io,sideEffects=none,admissionReviewVersions=v1
var _ webhook.Validator = &Grafana{}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (in *Grafana) ValidateCreate() error {
grafanalog.Info("validate create", "name", in.Name)
return in.Spec.Validate()
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (in *Grafana) ValidateUpdate(old runtime.Object) error {
grafanalog.Info("validate update", "name", in.Name)
if in.Spec.Project != old.(*Grafana).Spec.Project {
return errors.New("cannot update a Grafana service, project field is immutable and cannot be updated")
}
if in.Spec.ConnInfoSecretTarget.Name != old.(*Grafana).Spec.ConnInfoSecretTarget.Name {
return errors.New("cannot update a Grafana service, connInfoSecretTarget.name field is immutable and cannot be updated")
}
return in.Spec.Validate()
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (in *Grafana) ValidateDelete() error {
grafanalog.Info("validate delete", "name", in.Name)
if in.Spec.TerminationProtection != nil && *in.Spec.TerminationProtection {
return errors.New("cannot delete Grafana service, termination protection is on")
}
if in.Spec.ProjectVPCID != "" && in.Spec.ProjectVPCRef != nil {
return errors.New("cannot use both projectVpcId and projectVPCRef")
}
return nil
}