@@ -17,6 +17,7 @@ limitations under the License.
1717package v1beta1
1818
1919import (
20+ "context"
2021 "fmt"
2122 "reflect"
2223 "strings"
@@ -37,18 +38,31 @@ import (
3738var _ = logf .Log .WithName ("gcpmachine-resource" )
3839
3940func (m * GCPMachine ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
41+ w := new (gcpMachineWebhook )
4042 return ctrl .NewWebhookManagedBy (mgr ).
4143 For (m ).
44+ WithValidator (w ).
45+ WithDefaulter (w ).
4246 Complete ()
4347}
4448
4549// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-gcpmachine,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=gcpmachines,versions=v1beta1,name=validation.gcpmachine.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
4650// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-gcpmachine,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=gcpmachines,versions=v1beta1,name=default.gcpmachine.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
4751
48- var _ webhook.Validator = & GCPMachine {}
52+ type gcpMachineWebhook struct {}
53+
54+ var (
55+ _ webhook.CustomValidator = & gcpMachineWebhook {}
56+ _ webhook.CustomDefaulter = & gcpMachineWebhook {}
57+ )
4958
5059// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
51- func (m * GCPMachine ) ValidateCreate () (admission.Warnings , error ) {
60+ func (_ * gcpMachineWebhook ) ValidateCreate (_ context.Context , obj runtime.Object ) (admission.Warnings , error ) {
61+ m , ok := obj .(* GCPMachine )
62+ if ! ok {
63+ return nil , fmt .Errorf ("expected an GCPMachine object but got %T" , m )
64+ }
65+
5266 clusterlog .Info ("validate create" , "name" , m .Name )
5367
5468 if err := validateConfidentialCompute (m .Spec ); err != nil {
@@ -58,14 +72,19 @@ func (m *GCPMachine) ValidateCreate() (admission.Warnings, error) {
5872}
5973
6074// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
61- func (m * GCPMachine ) ValidateUpdate (old runtime.Object ) (admission.Warnings , error ) {
75+ func (_ * gcpMachineWebhook ) ValidateUpdate (_ context.Context , oldObj , newObj runtime.Object ) (admission.Warnings , error ) {
76+ m , ok := newObj .(* GCPMachine )
77+ if ! ok {
78+ return nil , fmt .Errorf ("expected an GCPMachine object but got %T" , m )
79+ }
80+
6281 newGCPMachine , err := runtime .DefaultUnstructuredConverter .ToUnstructured (m )
6382 if err != nil {
6483 return nil , apierrors .NewInvalid (GroupVersion .WithKind ("GCPMachine" ).GroupKind (), m .Name , field.ErrorList {
6584 field .InternalError (nil , errors .Wrap (err , "failed to convert new GCPMachine to unstructured object" )),
6685 })
6786 }
68- oldGCPMachine , err := runtime .DefaultUnstructuredConverter .ToUnstructured (old )
87+ oldGCPMachine , err := runtime .DefaultUnstructuredConverter .ToUnstructured (oldObj )
6988 if err != nil {
7089 return nil , apierrors .NewInvalid (GroupVersion .WithKind ("GCPMachine" ).GroupKind (), m .Name , field.ErrorList {
7190 field .InternalError (nil , errors .Wrap (err , "failed to convert old GCPMachine to unstructured object" )),
@@ -97,15 +116,13 @@ func (m *GCPMachine) ValidateUpdate(old runtime.Object) (admission.Warnings, err
97116}
98117
99118// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
100- func (m * GCPMachine ) ValidateDelete () (admission.Warnings , error ) {
101- clusterlog .Info ("validate delete" , "name" , m .Name )
102-
119+ func (_ * gcpMachineWebhook ) ValidateDelete (_ context.Context , _ runtime.Object ) (admission.Warnings , error ) {
103120 return nil , nil
104121}
105122
106123// Default implements webhookutil.defaulter so a webhook will be registered for the type.
107- func (m * GCPMachine ) Default () {
108- clusterlog . Info ( "default" , "name" , m . Name )
124+ func (_ * gcpMachineWebhook ) Default (_ context. Context , _ runtime. Object ) error {
125+ return nil
109126}
110127
111128func validateConfidentialCompute (spec GCPMachineSpec ) error {
0 commit comments