-
Notifications
You must be signed in to change notification settings - Fork 54
/
restore_webhook.go
85 lines (71 loc) · 2.95 KB
/
restore_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
79
80
81
82
83
84
85
package v2alpha1
import (
"reflect"
"github.com/infinispan/infinispan-operator/controllers/constants"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
func (r *Restore) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
// +kubebuilder:webhook:path=/mutate-infinispan-org-v2alpha1-restore,mutating=true,failurePolicy=fail,sideEffects=None,groups=infinispan.org,resources=restores,verbs=create;update,versions=v2alpha1,name=mrestore.kb.io,admissionReviewVersions={v1,v1beta1}
var _ webhook.Defaulter = &Restore{}
// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *Restore) Default() {
if r.Spec.Container.Memory == "" {
r.Spec.Container.Memory = constants.DefaultMemorySize.String()
}
resources := r.Spec.Resources
if resources == nil {
return
}
if len(resources.CacheConfigs) > 0 {
resources.Templates = resources.CacheConfigs
resources.CacheConfigs = nil
}
if len(resources.Scripts) > 0 {
resources.Tasks = resources.Scripts
resources.Scripts = nil
}
}
// +kubebuilder:webhook:path=/validate-infinispan-org-v2alpha1-restore,mutating=false,failurePolicy=fail,sideEffects=None,groups=infinispan.org,resources=restores,verbs=create;update,versions=v2alpha1,name=vrestore.kb.io,admissionReviewVersions={v1,v1beta1}
var _ webhook.Validator = &Restore{}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (b *Restore) ValidateCreate() error {
var allErrs field.ErrorList
if b.Spec.Cluster == "" {
allErrs = append(allErrs, field.Required(field.NewPath("spec").Child("cluster"), "'spec.cluster' must be configured"))
}
if b.Spec.Backup == "" {
allErrs = append(allErrs, field.Required(field.NewPath("spec").Child("backup"), "'spec.backup' must be configured"))
}
return b.StatusError(allErrs)
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (b *Restore) ValidateUpdate(old runtime.Object) error {
var allErrs field.ErrorList
oldRestore := old.(*Restore)
if !reflect.DeepEqual(b.Spec, oldRestore.Spec) {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec"), "The Restore spec is immutable and cannot be updated after initial Restore creation"))
}
return b.StatusError(allErrs)
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (b *Restore) ValidateDelete() error {
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
return nil
}
func (b *Restore) StatusError(allErrs field.ErrorList) error {
if len(allErrs) != 0 {
return apierrors.NewInvalid(
schema.GroupKind{Group: GroupVersion.Group, Kind: "Restore"},
b.Name, allErrs)
}
return nil
}