@@ -38,13 +38,11 @@ import (
3838 apierrors "k8s.io/apimachinery/pkg/api/errors"
3939 "k8s.io/apimachinery/pkg/api/resource"
4040 "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
41- "k8s.io/apimachinery/pkg/runtime"
4241 "k8s.io/apimachinery/pkg/runtime/schema"
4342 validationutil "k8s.io/apimachinery/pkg/util/validation"
4443 "k8s.io/apimachinery/pkg/util/validation/field"
4544 "k8s.io/utils/ptr"
4645 ctrl "sigs.k8s.io/controller-runtime"
47- "sigs.k8s.io/controller-runtime/pkg/webhook"
4846 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
4947
5048 apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
@@ -59,8 +57,8 @@ var clusterLog = log.WithName("cluster-resource").WithValues("version", "v1")
5957
6058// SetupClusterWebhookWithManager registers the webhook for Cluster in the manager.
6159func SetupClusterWebhookWithManager (mgr ctrl.Manager ) error {
62- return ctrl .NewWebhookManagedBy (mgr ). For ( & apiv1.Cluster {}).
63- WithValidator (newBypassableValidator (& ClusterCustomValidator {})).
60+ return ctrl .NewWebhookManagedBy (mgr , & apiv1.Cluster {}).
61+ WithValidator (newBypassableValidator [ * apiv1. Cluster ] (& ClusterCustomValidator {})).
6462 WithDefaulter (& ClusterCustomDefaulter {}).
6563 Complete ()
6664}
@@ -73,14 +71,8 @@ func SetupClusterWebhookWithManager(mgr ctrl.Manager) error {
7371// Kind Cluster when those are created or updated.
7472type ClusterCustomDefaulter struct {}
7573
76- var _ webhook.CustomDefaulter = & ClusterCustomDefaulter {}
77-
7874// Default implements webhook.CustomDefaulter so a webhook will be registered for the Kind Cluster.
79- func (d * ClusterCustomDefaulter ) Default (_ context.Context , obj runtime.Object ) error {
80- cluster , ok := obj .(* apiv1.Cluster )
81- if ! ok {
82- return fmt .Errorf ("expected a Cluster object but got %T" , obj )
83- }
75+ func (d * ClusterCustomDefaulter ) Default (_ context.Context , cluster * apiv1.Cluster ) error {
8476 clusterLog .Info ("Defaulting for Cluster" , "name" , cluster .GetName (), "namespace" , cluster .GetNamespace ())
8577
8678 cluster .Default ()
@@ -97,14 +89,8 @@ func (d *ClusterCustomDefaulter) Default(_ context.Context, obj runtime.Object)
9789// when it is created, updated, or deleted.
9890type ClusterCustomValidator struct {}
9991
100- var _ webhook.CustomValidator = & ClusterCustomValidator {}
101-
10292// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type Cluster.
103- func (v * ClusterCustomValidator ) ValidateCreate (_ context.Context , obj runtime.Object ) (admission.Warnings , error ) {
104- cluster , ok := obj .(* apiv1.Cluster )
105- if ! ok {
106- return nil , fmt .Errorf ("expected a Cluster object but got %T" , obj )
107- }
93+ func (v * ClusterCustomValidator ) ValidateCreate (_ context.Context , cluster * apiv1.Cluster ) (admission.Warnings , error ) {
10894 clusterLog .Info ("Validation for Cluster upon creation" , "name" , cluster .GetName (), "namespace" ,
10995 cluster .GetNamespace ())
11096
@@ -123,18 +109,8 @@ func (v *ClusterCustomValidator) ValidateCreate(_ context.Context, obj runtime.O
123109// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type Cluster.
124110func (v * ClusterCustomValidator ) ValidateUpdate (
125111 _ context.Context ,
126- oldObj , newObj runtime. Object ,
112+ oldCluster * apiv1. Cluster , cluster * apiv1. Cluster ,
127113) (admission.Warnings , error ) {
128- cluster , ok := newObj .(* apiv1.Cluster )
129- if ! ok {
130- return nil , fmt .Errorf ("expected a Cluster object for the newObj but got %T" , newObj )
131- }
132-
133- oldCluster , ok := oldObj .(* apiv1.Cluster )
134- if ! ok {
135- return nil , fmt .Errorf ("expected a Cluster object for the oldObj but got %T" , oldObj )
136- }
137-
138114 clusterLog .Info ("Validation for Cluster upon update" , "name" , cluster .GetName (), "namespace" ,
139115 cluster .GetNamespace ())
140116
@@ -157,11 +133,7 @@ func (v *ClusterCustomValidator) ValidateUpdate(
157133}
158134
159135// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type Cluster.
160- func (v * ClusterCustomValidator ) ValidateDelete (_ context.Context , obj runtime.Object ) (admission.Warnings , error ) {
161- cluster , ok := obj .(* apiv1.Cluster )
162- if ! ok {
163- return nil , fmt .Errorf ("expected a Cluster object but got %T" , obj )
164- }
136+ func (v * ClusterCustomValidator ) ValidateDelete (_ context.Context , cluster * apiv1.Cluster ) (admission.Warnings , error ) {
165137 clusterLog .Info ("Validation for Cluster upon deletion" , "name" , cluster .GetName (), "namespace" ,
166138 cluster .GetNamespace ())
167139
0 commit comments