@@ -17,6 +17,7 @@ limitations under the License.
1717package v1alpha1
1818
1919import (
20+ "context"
2021 "fmt"
2122 "reflect"
2223
@@ -26,6 +27,7 @@ import (
2627 ctrl "sigs.k8s.io/controller-runtime"
2728 logf "sigs.k8s.io/controller-runtime/pkg/log"
2829 "sigs.k8s.io/controller-runtime/pkg/webhook"
30+ "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2931)
3032
3133var (
@@ -44,19 +46,24 @@ var (
4446 webhookLog = logf .Log .WithName ("webhooks" )
4547
4648 // this just ensures that we've implemented the interface
47- _ webhook.Defaulter = & CrdbCluster {}
48- _ webhook.Validator = & CrdbCluster {}
49+ _ webhook.CustomDefaulter = & CrdbCluster {}
50+ _ webhook.CustomValidator = & CrdbCluster {}
4951)
5052
5153// SetupWebhookWithManager ensures webhooks are enabled for the CrdbCluster resource.
5254func (r * CrdbCluster ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
53- return ctrl .NewWebhookManagedBy (mgr ).For (r ).Complete ()
55+ return ctrl .NewWebhookManagedBy (mgr ).
56+ For (r ).
57+ WithDefaulter (r ).
58+ WithValidator (r ).
59+ Complete ()
5460}
5561
5662// +kubebuilder:webhook:path=/mutate-crdb-cockroachlabs-com-v1alpha1-crdbcluster,mutating=true,failurePolicy=fail,groups=crdb.cockroachlabs.com,resources=crdbclusters,verbs=create;update,versions=v1alpha1,name=mcrdbcluster.kb.io,sideEffects=None,admissionReviewVersions=v1
5763
5864// Default implements webhook.Defaulter so a webhook will be registered for the type.
59- func (r * CrdbCluster ) Default () {
65+ func (r * CrdbCluster ) Default (ctx context.Context , obj runtime.Object ) error {
66+ r = obj .(* CrdbCluster )
6067 webhookLog .Info ("default" , "name" , r .Name )
6168
6269 if r .Spec .GRPCPort == nil {
@@ -79,12 +86,15 @@ func (r *CrdbCluster) Default() {
7986 policy := v1 .PullIfNotPresent
8087 r .Spec .Image .PullPolicyName = & policy
8188 }
89+
90+ return nil
8291}
8392
8493// +kubebuilder:webhook:path=/validate-crdb-cockroachlabs-com-v1alpha1-crdbcluster,mutating=false,failurePolicy=fail,groups=crdb.cockroachlabs.com,resources=crdbclusters,verbs=create;update,versions=v1alpha1,name=vcrdbcluster.kb.io,sideEffects=None,admissionReviewVersions=v1
8594
8695// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
87- func (r * CrdbCluster ) ValidateCreate () error {
96+ func (r * CrdbCluster ) ValidateCreate (ctx context.Context , obj runtime.Object ) (admission.Warnings , error ) {
97+ r = obj .(* CrdbCluster )
8898 webhookLog .Info ("validate create" , "name" , r .Name )
8999 var errors []error
90100 if r .Spec .Ingress != nil {
@@ -102,20 +112,21 @@ func (r *CrdbCluster) ValidateCreate() error {
102112 }
103113
104114 if len (errors ) != 0 {
105- return kerrors .NewAggregate (errors )
115+ return nil , kerrors .NewAggregate (errors )
106116 }
107117
108- return nil
118+ return nil , nil
109119}
110120
111121// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
112- func (r * CrdbCluster ) ValidateUpdate (old runtime.Object ) error {
122+ func (r * CrdbCluster ) ValidateUpdate (ctx context.Context , oldObj runtime.Object , newObj runtime.Object ) (admission.Warnings , error ) {
123+ r = newObj .(* CrdbCluster )
113124 webhookLog .Info ("validate update" , "name" , r .Name )
114125 var errors []error
115126
116- oldCluster , ok := old .(* CrdbCluster )
127+ oldCluster , ok := oldObj .(* CrdbCluster )
117128 if ! ok {
118- webhookLog .Info (fmt .Sprintf ("unexpected old cluster type %T" , old ))
129+ webhookLog .Info (fmt .Sprintf ("unexpected old cluster type %T" , oldObj ))
119130 } else {
120131 // Validate if labels changed.
121132 // k8s does not support changing selector/labels on sts:
@@ -136,18 +147,19 @@ func (r *CrdbCluster) ValidateUpdate(old runtime.Object) error {
136147 }
137148
138149 if len (errors ) != 0 {
139- return kerrors .NewAggregate (errors )
150+ return nil , kerrors .NewAggregate (errors )
140151 }
141152
142- return nil
153+ return nil , nil
143154}
144155
145156// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
146- func (r * CrdbCluster ) ValidateDelete () error {
157+ func (r * CrdbCluster ) ValidateDelete (ctx context.Context , obj runtime.Object ) (admission.Warnings , error ) {
158+ r = obj .(* CrdbCluster )
147159 webhookLog .Info ("validate delete" , "name" , r .Name )
148160
149161 // we're not validating anything on delete. This is just a placeholder for now to satisfy the Validator interface
150- return nil
162+ return nil , nil
151163}
152164
153165// ValidateIngress validates the ingress configuration used to create ingress resource
0 commit comments