generated from cybozu-go/neco-template
/
runnerpool_webhook.go
54 lines (44 loc) · 2.3 KB
/
runnerpool_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
package v1alpha1
import (
constants "github.com/cybozu-go/meows"
apierrors "k8s.io/apimachinery/pkg/api/errors"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
func (r *RunnerPool) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
// +kubebuilder:webhook:failurePolicy=fail,matchPolicy=equivalent,groups=meows.cybozu.com,resources=runnerpools,verbs=create,versions=v1alpha1,name=runnerpool-hook.meows.cybozu.com,path=/mutate-meows-cybozu-com-v1alpha1-runnerpool,mutating=true,sideEffects=none,admissionReviewVersions=v1
var _ webhook.Defaulter = &RunnerPool{}
// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *RunnerPool) Default() {
controllerutil.AddFinalizer(r, constants.RunnerPoolFinalizer)
}
// +kubebuilder:webhook:failurePolicy=fail,matchPolicy=equivalent,groups=meows.cybozu.com,resources=runnerpools,verbs=create;update,versions=v1alpha1,name=runnerpool-hook.meows.cybozu.com,path=/validate-meows-cybozu-com-v1alpha1-runnerpool,mutating=false,sideEffects=none,admissionReviewVersions=v1
var _ webhook.Validator = &RunnerPool{}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *RunnerPool) ValidateCreate() (warnings admission.Warnings, err error) {
errs := r.Spec.validateCreate()
if len(errs) == 0 {
return nil, nil
}
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "RunnerPool"}, r.Name, errs)
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *RunnerPool) ValidateUpdate(old runtime.Object) (warnings admission.Warnings, err error) {
errs := r.Spec.validateUpdate(old.(*RunnerPool).Spec)
if len(errs) == 0 {
return nil, nil
}
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "RunnerPool"}, r.Name, errs)
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *RunnerPool) ValidateDelete() (warnings admission.Warnings, err error) {
return nil, nil
}