@@ -17,63 +17,59 @@ limitations under the License.
1717package v1beta2
1818
1919import (
20+ "context"
2021 "fmt"
2122 "reflect"
2223
2324 apierrors "k8s.io/apimachinery/pkg/api/errors"
2425 "k8s.io/apimachinery/pkg/runtime"
2526
2627 ctrl "sigs.k8s.io/controller-runtime"
27- logf "sigs.k8s.io/controller-runtime/pkg/log"
2828 "sigs.k8s.io/controller-runtime/pkg/webhook"
2929 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3030)
3131
32- // log is for logging in this package.
33- var ibmpowervsclustertemplatelog = logf . Log . WithName ( "ibmpowervsclustertemplate-resource" )
32+ //+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsclustertemplate,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclustertemplates,verbs=create;update,versions=v1beta2,name=mibmpowervsclustertemplate.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
33+ //+kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsclustertemplate,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclustertemplates,versions=v1beta2,name=vibmpowervsclustertemplate.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
3434
3535func (r * IBMPowerVSClusterTemplate ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
3636 return ctrl .NewWebhookManagedBy (mgr ).
37- For (r ).
37+ For (& IBMPowerVSClusterTemplate {}).
38+ WithValidator (r ).
39+ WithDefaulter (r ).
3840 Complete ()
3941}
4042
41- //+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsclustertemplate,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclustertemplates,verbs=create;update,versions=v1beta2,name=mibmpowervsclustertemplate.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
42-
43- var _ webhook.Defaulter = & IBMPowerVSClusterTemplate {}
43+ var _ webhook.CustomDefaulter = & IBMPowerVSClusterTemplate {}
44+ var _ webhook.CustomValidator = & IBMPowerVSClusterTemplate {}
4445
45- // Default implements webhook.Defaulter so a webhook will be registered for the type.
46- func (r * IBMPowerVSClusterTemplate ) Default () {
47- ibmpowervsclustertemplatelog . Info ( "default" , "name" , r . Name )
46+ // Default implements webhook.CustomDefaulter so a webhook will be registered for the type.
47+ func (r * IBMPowerVSClusterTemplate ) Default (_ context. Context , _ runtime. Object ) error {
48+ return nil
4849}
4950
50- //+kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsclustertemplate,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclustertemplates,versions=v1beta2,name=vibmpowervsclustertemplate.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
51-
52- var _ webhook.Validator = & IBMPowerVSClusterTemplate {}
53-
54- // ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
55- func (r * IBMPowerVSClusterTemplate ) ValidateCreate () (admission.Warnings , error ) {
56- ibmpowervsclustertemplatelog .Info ("validate create" , "name" , r .Name )
57-
51+ // ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type.
52+ func (r * IBMPowerVSClusterTemplate ) ValidateCreate (_ context.Context , _ runtime.Object ) (admission.Warnings , error ) {
5853 return nil , nil
5954}
6055
61- // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
62- func (r * IBMPowerVSClusterTemplate ) ValidateUpdate (oldRaw runtime.Object ) (admission.Warnings , error ) {
63- ibmpowervsclustertemplatelog .Info ("validate update" , "name" , r .Name )
64- old , ok := oldRaw .(* IBMPowerVSClusterTemplate )
56+ // ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type.
57+ func (r * IBMPowerVSClusterTemplate ) ValidateUpdate (_ context.Context , oldObj , newObj runtime.Object ) (warnings admission.Warnings , err error ) {
58+ oldObjValue , ok := oldObj .(* IBMPowerVSClusterTemplate )
6559 if ! ok {
66- return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected an IBMPowerVSClusterTemplate but got a %T" , oldRaw ))
60+ return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected an IBMPowerVSClusterTemplate but got a %T" , oldObj ))
6761 }
68- if ! reflect .DeepEqual (r .Spec , old .Spec ) {
62+ newObjValue , ok := newObj .(* IBMPowerVSClusterTemplate )
63+ if ! ok {
64+ return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected an IBMPowerVSClusterTemplate but got a %T" , newObj ))
65+ }
66+ if ! reflect .DeepEqual (newObjValue .Spec , oldObjValue .Spec ) {
6967 return nil , apierrors .NewBadRequest ("IBMPowerVSClusterTemplate.Spec is immutable" )
7068 }
7169 return nil , nil
7270}
7371
74- // ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
75- func (r * IBMPowerVSClusterTemplate ) ValidateDelete () (admission.Warnings , error ) {
76- ibmpowervsclustertemplatelog .Info ("validate delete" , "name" , r .Name )
77-
72+ // ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
73+ func (r * IBMPowerVSClusterTemplate ) ValidateDelete (_ context.Context , _ runtime.Object ) (admission.Warnings , error ) {
7874 return nil , nil
7975}
0 commit comments