Skip to content

Commit 8405b6f

Browse files
committed
operator: rework webhooks
controller-runtime has deprecated webhook.Defaulter/Validator and they will be removed from the next release. Move deviceplugins webhooks to use admission.CustomDefaulter/Validator. Signed-off-by: Mikko Ylinen <[email protected]>
1 parent 99ef5bb commit 8405b6f

8 files changed

+411
-215
lines changed

pkg/apis/deviceplugin/v1/dlbdeviceplugin_webhook.go

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,73 +15,101 @@
1515
package v1
1616

1717
import (
18+
"context"
19+
"fmt"
20+
1821
"k8s.io/apimachinery/pkg/runtime"
1922
ctrl "sigs.k8s.io/controller-runtime"
2023
logf "sigs.k8s.io/controller-runtime/pkg/log"
21-
"sigs.k8s.io/controller-runtime/pkg/webhook"
2224
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2325

2426
"github.com/intel/intel-device-plugins-for-kubernetes/pkg/controllers"
2527
)
2628

27-
var (
28-
// dlbdevicepluginlog is for logging in this package.
29-
dlbdevicepluginlog = logf.Log.WithName("dlbdeviceplugin-resource")
30-
31-
dlbMinVersion = controllers.ImageMinVersion
32-
)
29+
var dlbMinVersion = controllers.ImageMinVersion
3330

3431
// SetupWebhookWithManager sets up a webhook for DlbDevicePlugin custom resources.
3532
func (r *DlbDevicePlugin) SetupWebhookWithManager(mgr ctrl.Manager) error {
3633
return ctrl.NewWebhookManagedBy(mgr).
3734
For(r).
35+
WithDefaulter(&dlbDevicePluginDefaulter{}).
36+
WithValidator(&dlbDevicePluginValidator{}).
3837
Complete()
3938
}
4039

4140
// +kubebuilder:webhook:path=/mutate-deviceplugin-intel-com-v1-dlbdeviceplugin,mutating=true,failurePolicy=fail,groups=deviceplugin.intel.com,resources=dlbdeviceplugins,verbs=create;update,versions=v1,name=mdlbdeviceplugin.kb.io,sideEffects=None,admissionReviewVersions=v1
4241

43-
var _ webhook.Defaulter = &DlbDevicePlugin{}
42+
type dlbDevicePluginDefaulter struct{}
43+
44+
var _ admission.CustomDefaulter = &dlbDevicePluginDefaulter{}
4445

45-
// Default implements webhook.Defaulter so a webhook will be registered for the type.
46-
func (r *DlbDevicePlugin) Default() {
47-
dlbdevicepluginlog.Info("default", "name", r.Name)
46+
// Default implements admission.CustomDefaulter so a webhook will be registered for the type.
47+
func (r *dlbDevicePluginDefaulter) Default(ctx context.Context, obj runtime.Object) error {
48+
log := logf.FromContext(ctx)
49+
cr, ok := obj.(*DlbDevicePlugin)
4850

49-
if len(r.Spec.Image) == 0 {
50-
r.Spec.Image = "intel/intel-dlb-plugin:" + dlbMinVersion.String()
51+
if !ok {
52+
return fmt.Errorf("%w: expected an DlbDevicePlugin object but got %T", objectTypeError, obj)
5153
}
54+
55+
log.Info("default")
56+
57+
if len(cr.Spec.Image) == 0 {
58+
cr.Spec.Image = "intel/intel-dlb-plugin:" + dlbMinVersion.String()
59+
}
60+
61+
return nil
5262
}
5363

5464
// +kubebuilder:webhook:verbs=create;update,path=/validate-deviceplugin-intel-com-v1-dlbdeviceplugin,mutating=false,failurePolicy=fail,groups=deviceplugin.intel.com,resources=dlbdeviceplugins,versions=v1,name=vdlbdeviceplugin.kb.io,sideEffects=None,admissionReviewVersions=v1
5565

56-
var _ webhook.Validator = &DlbDevicePlugin{}
66+
type dlbDevicePluginValidator struct{}
5767

58-
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
59-
func (r *DlbDevicePlugin) ValidateCreate() (admission.Warnings, error) {
60-
dlbdevicepluginlog.Info("validate create", "name", r.Name)
68+
var _ admission.CustomValidator = &dlbDevicePluginValidator{}
6169

62-
return nil, r.validatePlugin()
70+
// ValidateCreate implements admission.CustomValidator so a webhook will be registered for the type.
71+
func (r *dlbDevicePluginValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
72+
log := logf.FromContext(ctx)
73+
cr, ok := obj.(*DlbDevicePlugin)
74+
75+
if !ok {
76+
return nil, fmt.Errorf("%w: expected an DlbDevicePlugin object but got %T", objectTypeError, obj)
77+
}
78+
79+
log.Info("validate create")
80+
81+
return nil, r.validatePlugin(ctx, cr)
6382
}
6483

65-
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
66-
func (r *DlbDevicePlugin) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
67-
dlbdevicepluginlog.Info("validate update", "name", r.Name)
84+
// ValidateUpdate implements admission.CustomValidator so a webhook will be registered for the type.
85+
func (r *dlbDevicePluginValidator) ValidateUpdate(ctx context.Context, oldObj runtime.Object, newObj runtime.Object) (admission.Warnings, error) {
86+
log := logf.FromContext(ctx)
87+
cr, ok := oldObj.(*DlbDevicePlugin)
6888

69-
return nil, r.validatePlugin()
89+
if !ok {
90+
return nil, fmt.Errorf("%w: expected an DlbDevicePlugin object but got %T", objectTypeError, oldObj)
91+
}
92+
93+
log.Info("validate update")
94+
95+
return nil, r.validatePlugin(ctx, cr)
7096
}
7197

72-
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
73-
func (r *DlbDevicePlugin) ValidateDelete() (admission.Warnings, error) {
74-
dlbdevicepluginlog.Info("validate delete", "name", r.Name)
98+
// ValidateDelete implements admission.CustomValidator so a webhook will be registered for the type.
99+
func (r *dlbDevicePluginValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
100+
log := logf.FromContext(ctx)
101+
102+
log.Info("validate delete")
75103

76104
return nil, nil
77105
}
78106

79-
func (r *DlbDevicePlugin) validatePlugin() error {
80-
if r.Spec.InitImage != "" {
81-
if err := validatePluginImage(r.Spec.InitImage, "intel-dlb-initcontainer", dlbMinVersion); err != nil {
107+
func (r *dlbDevicePluginValidator) validatePlugin(ctx context.Context, cr *DlbDevicePlugin) error {
108+
if cr.Spec.InitImage != "" {
109+
if err := validatePluginImage(cr.Spec.InitImage, "intel-dlb-initcontainer", dlbMinVersion); err != nil {
82110
return err
83111
}
84112
}
85113

86-
return validatePluginImage(r.Spec.Image, "intel-dlb-plugin", dlbMinVersion)
114+
return validatePluginImage(cr.Spec.Image, "intel-dlb-plugin", dlbMinVersion)
87115
}

pkg/apis/deviceplugin/v1/dsadeviceplugin_webhook.go

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,79 +15,107 @@
1515
package v1
1616

1717
import (
18+
"context"
19+
"fmt"
20+
1821
"github.com/pkg/errors"
1922
"k8s.io/apimachinery/pkg/runtime"
2023
ctrl "sigs.k8s.io/controller-runtime"
2124
logf "sigs.k8s.io/controller-runtime/pkg/log"
22-
"sigs.k8s.io/controller-runtime/pkg/webhook"
2325
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2426

2527
"github.com/intel/intel-device-plugins-for-kubernetes/pkg/controllers"
2628
)
2729

28-
var (
29-
// dsadevicepluginlog is for logging in this package.
30-
dsadevicepluginlog = logf.Log.WithName("dsadeviceplugin-resource")
31-
32-
dsaMinVersion = controllers.ImageMinVersion
33-
)
30+
var dsaMinVersion = controllers.ImageMinVersion
3431

3532
// SetupWebhookWithManager sets up a webhook for DsaDevicePlugin custom resources.
3633
func (r *DsaDevicePlugin) SetupWebhookWithManager(mgr ctrl.Manager) error {
3734
return ctrl.NewWebhookManagedBy(mgr).
3835
For(r).
36+
WithDefaulter(&dsaDevicePluginDefaulter{}).
37+
WithValidator(&dsaDevicePluginValidator{}).
3938
Complete()
4039
}
4140

4241
// +kubebuilder:webhook:path=/mutate-deviceplugin-intel-com-v1-dsadeviceplugin,mutating=true,failurePolicy=fail,groups=deviceplugin.intel.com,resources=dsadeviceplugins,verbs=create;update,versions=v1,name=mdsadeviceplugin.kb.io,sideEffects=None,admissionReviewVersions=v1
4342

44-
var _ webhook.Defaulter = &DsaDevicePlugin{}
43+
type dsaDevicePluginDefaulter struct{}
4544

46-
// Default implements webhook.Defaulter so a webhook will be registered for the type.
47-
func (r *DsaDevicePlugin) Default() {
48-
dsadevicepluginlog.Info("default", "name", r.Name)
45+
var _ admission.CustomDefaulter = &dsaDevicePluginDefaulter{}
4946

50-
if len(r.Spec.Image) == 0 {
51-
r.Spec.Image = "intel/intel-dsa-plugin:" + dsaMinVersion.String()
47+
// Default implements admission.CustomDefaulter so a webhook will be registered for the type.
48+
func (r *dsaDevicePluginDefaulter) Default(ctx context.Context, obj runtime.Object) error {
49+
log := logf.FromContext(ctx)
50+
cr, ok := obj.(*DsaDevicePlugin)
51+
52+
if !ok {
53+
return fmt.Errorf("%w: expected an DsaDevicePlugin object but got %T", objectTypeError, obj)
5254
}
55+
56+
log.Info("default")
57+
58+
if len(cr.Spec.Image) == 0 {
59+
cr.Spec.Image = "intel/intel-dsa-plugin:" + dsaMinVersion.String()
60+
}
61+
62+
return nil
5363
}
5464

5565
// +kubebuilder:webhook:verbs=create;update,path=/validate-deviceplugin-intel-com-v1-dsadeviceplugin,mutating=false,failurePolicy=fail,groups=deviceplugin.intel.com,resources=dsadeviceplugins,versions=v1,name=vdsadeviceplugin.kb.io,sideEffects=None,admissionReviewVersions=v1
5666

57-
var _ webhook.Validator = &DsaDevicePlugin{}
67+
type dsaDevicePluginValidator struct{}
5868

59-
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
60-
func (r *DsaDevicePlugin) ValidateCreate() (admission.Warnings, error) {
61-
dsadevicepluginlog.Info("validate create", "name", r.Name)
69+
var _ admission.CustomValidator = &dsaDevicePluginValidator{}
6270

63-
return nil, r.validatePlugin()
71+
// ValidateCreate implements admission.CustomValidator so a webhook will be registered for the type.
72+
func (r *dsaDevicePluginValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
73+
log := logf.FromContext(ctx)
74+
cr, ok := obj.(*DsaDevicePlugin)
75+
76+
if !ok {
77+
return nil, fmt.Errorf("%w: expected an DsaDevicePlugin object but got %T", objectTypeError, obj)
78+
}
79+
80+
log.Info("validate create")
81+
82+
return nil, r.validatePlugin(ctx, cr)
6483
}
6584

66-
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
67-
func (r *DsaDevicePlugin) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
68-
dsadevicepluginlog.Info("validate update", "name", r.Name)
85+
// ValidateUpdate implements admission.CustomValidator so a webhook will be registered for the type.
86+
func (r *dsaDevicePluginValidator) ValidateUpdate(ctx context.Context, oldObj runtime.Object, newObj runtime.Object) (admission.Warnings, error) {
87+
log := logf.FromContext(ctx)
88+
cr, ok := oldObj.(*DsaDevicePlugin)
6989

70-
return nil, r.validatePlugin()
90+
if !ok {
91+
return nil, fmt.Errorf("%w: expected an DsaDevicePlugin object but got %T", objectTypeError, oldObj)
92+
}
93+
94+
log.Info("validate update")
95+
96+
return nil, r.validatePlugin(ctx, cr)
7197
}
7298

73-
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
74-
func (r *DsaDevicePlugin) ValidateDelete() (admission.Warnings, error) {
75-
dsadevicepluginlog.Info("validate delete", "name", r.Name)
99+
// ValidateDelete implements admission.CustomValidator so a webhook will be registered for the type.
100+
func (r *dsaDevicePluginValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
101+
log := logf.FromContext(ctx)
102+
103+
log.Info("validate delete")
76104

77105
return nil, nil
78106
}
79107

80-
func (r *DsaDevicePlugin) validatePlugin() error {
81-
if err := validatePluginImage(r.Spec.Image, "intel-dsa-plugin", dsaMinVersion); err != nil {
108+
func (r *dsaDevicePluginValidator) validatePlugin(ctx context.Context, cr *DsaDevicePlugin) error {
109+
if err := validatePluginImage(cr.Spec.Image, "intel-dsa-plugin", dsaMinVersion); err != nil {
82110
return err
83111
}
84112

85-
if len(r.Spec.ProvisioningConfig) > 0 && len(r.Spec.InitImage) == 0 {
113+
if len(cr.Spec.ProvisioningConfig) > 0 && len(cr.Spec.InitImage) == 0 {
86114
return errors.Errorf("ProvisioningConfig is set with no InitImage")
87115
}
88116

89-
if len(r.Spec.InitImage) > 0 {
90-
return validatePluginImage(r.Spec.InitImage, "intel-idxd-config-initcontainer", dsaMinVersion)
117+
if len(cr.Spec.InitImage) > 0 {
118+
return validatePluginImage(cr.Spec.InitImage, "intel-idxd-config-initcontainer", dsaMinVersion)
91119
}
92120

93121
return nil

0 commit comments

Comments
 (0)