Skip to content

Commit 0d0eb6e

Browse files
committed
webhook: fix webhook after generating code
operator-sdk generates the webhook code that targets sigs.k8s.io/controller-runtime v0.19 or older. As such, it contains webhook.Validator and webhook.Defaulter. This API was removed in v0.20, see [1]. To make this work, we patched the code out while generating it. But now we have to fix it. Fix it now for the new API. [1] kubernetes-sigs/controller-runtime#2877
1 parent 93d7fc0 commit 0d0eb6e

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

api/v1/dpuoperatorconfig_webhook.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ limitations under the License.
1717
package v1
1818

1919
import (
20+
"context"
2021
"k8s.io/apimachinery/pkg/runtime"
2122
ctrl "sigs.k8s.io/controller-runtime"
2223
logf "sigs.k8s.io/controller-runtime/pkg/log"
23-
//"sigs.k8s.io/controller-runtime/pkg/webhook"
24+
"sigs.k8s.io/controller-runtime/pkg/webhook"
2425
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2526
)
2627

@@ -31,6 +32,7 @@ var dpuoperatorconfiglog = logf.Log.WithName("dpuoperatorconfig-resource")
3132
func (r *DpuOperatorConfig) SetupWebhookWithManager(mgr ctrl.Manager) error {
3233
return ctrl.NewWebhookManagedBy(mgr).
3334
For(r).
35+
WithValidator(r).
3436
Complete()
3537
}
3638

@@ -41,26 +43,28 @@ func (r *DpuOperatorConfig) SetupWebhookWithManager(mgr ctrl.Manager) error {
4143
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
4244
//+kubebuilder:webhook:path=/validate-config-openshift-io-v1-dpuoperatorconfig,mutating=false,failurePolicy=fail,sideEffects=None,groups=config.openshift.io,resources=dpuoperatorconfigs,verbs=create;update,versions=v1,name=vdpuoperatorconfig.kb.io,admissionReviewVersions=v1
4345

44-
//var _ webhook.Validator = &DpuOperatorConfig{}
46+
var _ webhook.CustomValidator = &DpuOperatorConfig{}
4547

4648
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
47-
func (r *DpuOperatorConfig) ValidateCreate() (admission.Warnings, error) {
49+
func (r *DpuOperatorConfig) ValidateCreate(tx context.Context, obj runtime.Object) (admission.Warnings, error) {
50+
r = obj.(*DpuOperatorConfig)
4851
dpuoperatorconfiglog.Info("validate create", "name", r.Name)
4952

5053
// TODO(user): fill in your validation logic upon object creation.
5154
return nil, nil
5255
}
5356

5457
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
55-
func (r *DpuOperatorConfig) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
58+
func (r *DpuOperatorConfig) ValidateUpdate(ctx context.Context, oldObj runtime.Object, newObj runtime.Object) (admission.Warnings, error) {
59+
r = newObj.(*DpuOperatorConfig)
5660
dpuoperatorconfiglog.Info("validate update", "name", r.Name)
5761

5862
// TODO(user): fill in your validation logic upon object update.
5963
return nil, nil
6064
}
6165

6266
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
63-
func (r *DpuOperatorConfig) ValidateDelete() (admission.Warnings, error) {
67+
func (r *DpuOperatorConfig) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
6468
dpuoperatorconfiglog.Info("validate delete", "name", r.Name)
6569

6670
// TODO(user): fill in your validation logic upon object deletion.

vendor/github.com/openshift/dpu-operator/api/v1/dpuoperatorconfig_webhook.go

+9-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)