Skip to content

Commit 8dd66f5

Browse files
authored
Merge pull request #1906 from mythi/PR-2024-024
operator: rework webhooks
2 parents 76b5118 + c30e80b commit 8dd66f5

8 files changed

+219
-365
lines changed

pkg/apis/deviceplugin/v1/dlbdeviceplugin_webhook.go

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,73 +15,35 @@
1515
package v1
1616

1717
import (
18-
"k8s.io/apimachinery/pkg/runtime"
1918
ctrl "sigs.k8s.io/controller-runtime"
20-
logf "sigs.k8s.io/controller-runtime/pkg/log"
21-
"sigs.k8s.io/controller-runtime/pkg/webhook"
22-
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2319

2420
"github.com/intel/intel-device-plugins-for-kubernetes/pkg/controllers"
2521
)
2622

27-
var (
28-
// dlbdevicepluginlog is for logging in this package.
29-
dlbdevicepluginlog = logf.Log.WithName("dlbdeviceplugin-resource")
30-
31-
dlbMinVersion = controllers.ImageMinVersion
32-
)
33-
3423
// SetupWebhookWithManager sets up a webhook for DlbDevicePlugin custom resources.
3524
func (r *DlbDevicePlugin) SetupWebhookWithManager(mgr ctrl.Manager) error {
3625
return ctrl.NewWebhookManagedBy(mgr).
3726
For(r).
27+
WithDefaulter(&commonDevicePluginDefaulter{
28+
defaultImage: "intel/intel-dlb-plugin:" + controllers.ImageMinVersion.String(),
29+
}).
30+
WithValidator(&commonDevicePluginValidator{
31+
expectedImage: "intel-dlb-plugin",
32+
expectedInitImage: "intel-dlb-initimage",
33+
expectedVersion: *controllers.ImageMinVersion,
34+
}).
3835
Complete()
3936
}
4037

4138
// +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
42-
43-
var _ webhook.Defaulter = &DlbDevicePlugin{}
44-
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)
48-
49-
if len(r.Spec.Image) == 0 {
50-
r.Spec.Image = "intel/intel-dlb-plugin:" + dlbMinVersion.String()
51-
}
52-
}
53-
5439
// +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
5540

56-
var _ webhook.Validator = &DlbDevicePlugin{}
57-
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)
61-
62-
return nil, r.validatePlugin()
63-
}
64-
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)
68-
69-
return nil, r.validatePlugin()
70-
}
71-
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)
75-
76-
return nil, nil
77-
}
78-
79-
func (r *DlbDevicePlugin) validatePlugin() error {
41+
func (r *DlbDevicePlugin) validatePlugin(ref *commonDevicePluginValidator) error {
8042
if r.Spec.InitImage != "" {
81-
if err := validatePluginImage(r.Spec.InitImage, "intel-dlb-initcontainer", dlbMinVersion); err != nil {
43+
if err := validatePluginImage(r.Spec.InitImage, ref.expectedInitImage, &ref.expectedVersion); err != nil {
8244
return err
8345
}
8446
}
8547

86-
return validatePluginImage(r.Spec.Image, "intel-dlb-plugin", dlbMinVersion)
48+
return validatePluginImage(r.Spec.Image, ref.expectedImage, &ref.expectedVersion)
8749
}

pkg/apis/deviceplugin/v1/dsadeviceplugin_webhook.go

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,79 +15,42 @@
1515
package v1
1616

1717
import (
18-
"github.com/pkg/errors"
19-
"k8s.io/apimachinery/pkg/runtime"
18+
"fmt"
19+
2020
ctrl "sigs.k8s.io/controller-runtime"
21-
logf "sigs.k8s.io/controller-runtime/pkg/log"
22-
"sigs.k8s.io/controller-runtime/pkg/webhook"
23-
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2421

2522
"github.com/intel/intel-device-plugins-for-kubernetes/pkg/controllers"
2623
)
2724

28-
var (
29-
// dsadevicepluginlog is for logging in this package.
30-
dsadevicepluginlog = logf.Log.WithName("dsadeviceplugin-resource")
31-
32-
dsaMinVersion = controllers.ImageMinVersion
33-
)
34-
3525
// SetupWebhookWithManager sets up a webhook for DsaDevicePlugin custom resources.
3626
func (r *DsaDevicePlugin) SetupWebhookWithManager(mgr ctrl.Manager) error {
3727
return ctrl.NewWebhookManagedBy(mgr).
3828
For(r).
29+
WithDefaulter(&commonDevicePluginDefaulter{
30+
defaultImage: "intel/intel-dsa-plugin:" + controllers.ImageMinVersion.String(),
31+
}).
32+
WithValidator(&commonDevicePluginValidator{
33+
expectedImage: "intel-dsa-plugin",
34+
expectedInitImage: "intel-idxd-config-initcontainer",
35+
expectedVersion: *controllers.ImageMinVersion,
36+
}).
3937
Complete()
4038
}
4139

4240
// +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
43-
44-
var _ webhook.Defaulter = &DsaDevicePlugin{}
45-
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)
49-
50-
if len(r.Spec.Image) == 0 {
51-
r.Spec.Image = "intel/intel-dsa-plugin:" + dsaMinVersion.String()
52-
}
53-
}
54-
5541
// +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
5642

57-
var _ webhook.Validator = &DsaDevicePlugin{}
58-
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)
62-
63-
return nil, r.validatePlugin()
64-
}
65-
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)
69-
70-
return nil, r.validatePlugin()
71-
}
72-
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)
76-
77-
return nil, nil
78-
}
79-
80-
func (r *DsaDevicePlugin) validatePlugin() error {
81-
if err := validatePluginImage(r.Spec.Image, "intel-dsa-plugin", dsaMinVersion); err != nil {
43+
func (r *DsaDevicePlugin) validatePlugin(ref *commonDevicePluginValidator) error {
44+
if err := validatePluginImage(r.Spec.Image, ref.expectedImage, &ref.expectedVersion); err != nil {
8245
return err
8346
}
8447

8548
if len(r.Spec.ProvisioningConfig) > 0 && len(r.Spec.InitImage) == 0 {
86-
return errors.Errorf("ProvisioningConfig is set with no InitImage")
49+
return fmt.Errorf("%w: ProvisioningConfig is set with no InitImage", errValidation)
8750
}
8851

8952
if len(r.Spec.InitImage) > 0 {
90-
return validatePluginImage(r.Spec.InitImage, "intel-idxd-config-initcontainer", dsaMinVersion)
53+
return validatePluginImage(r.Spec.InitImage, ref.expectedInitImage, &ref.expectedVersion)
9154
}
9255

9356
return nil

pkg/apis/deviceplugin/v1/fpgadeviceplugin_webhook.go

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,33 @@
1515
package v1
1616

1717
import (
18-
"k8s.io/apimachinery/pkg/runtime"
1918
ctrl "sigs.k8s.io/controller-runtime"
20-
logf "sigs.k8s.io/controller-runtime/pkg/log"
21-
"sigs.k8s.io/controller-runtime/pkg/webhook"
22-
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2319

2420
"github.com/intel/intel-device-plugins-for-kubernetes/pkg/controllers"
2521
)
2622

27-
var (
28-
// fpgadevicepluginlog is for logging in this package.
29-
fpgadevicepluginlog = logf.Log.WithName("fpgadeviceplugin-resource")
30-
31-
fpgaMinVersion = controllers.ImageMinVersion
32-
)
33-
3423
// SetupWebhookWithManager sets up a webhook for FpgaDevicePlugin custom resources.
3524
func (r *FpgaDevicePlugin) SetupWebhookWithManager(mgr ctrl.Manager) error {
3625
return ctrl.NewWebhookManagedBy(mgr).
3726
For(r).
27+
WithDefaulter(&commonDevicePluginDefaulter{
28+
defaultImage: "intel/intel-fpga-plugin:" + controllers.ImageMinVersion.String(),
29+
}).
30+
WithValidator(&commonDevicePluginValidator{
31+
expectedImage: "intel-fpga-plugin",
32+
expectedInitImage: "intel-fpga-initimage",
33+
expectedVersion: *controllers.ImageMinVersion,
34+
}).
3835
Complete()
3936
}
4037

4138
// +kubebuilder:webhook:path=/mutate-deviceplugin-intel-com-v1-fpgadeviceplugin,mutating=true,failurePolicy=fail,groups=deviceplugin.intel.com,resources=fpgadeviceplugins,verbs=create;update,versions=v1,name=mfpgadeviceplugin.kb.io,sideEffects=None,admissionReviewVersions=v1
42-
43-
var _ webhook.Defaulter = &FpgaDevicePlugin{}
44-
45-
// Default implements webhook.Defaulter so a webhook will be registered for the type.
46-
func (r *FpgaDevicePlugin) Default() {
47-
fpgadevicepluginlog.Info("default", "name", r.Name)
48-
49-
if len(r.Spec.Image) == 0 {
50-
r.Spec.Image = "intel/intel-fpga-plugin:" + fpgaMinVersion.String()
51-
}
52-
53-
if len(r.Spec.InitImage) == 0 {
54-
r.Spec.InitImage = "intel/intel-fpga-initcontainer:" + fpgaMinVersion.String()
55-
}
56-
}
57-
5839
// +kubebuilder:webhook:verbs=create;update,path=/validate-deviceplugin-intel-com-v1-fpgadeviceplugin,mutating=false,failurePolicy=fail,groups=deviceplugin.intel.com,resources=fpgadeviceplugins,versions=v1,name=vfpgadeviceplugin.kb.io,sideEffects=None,admissionReviewVersions=v1
5940

60-
var _ webhook.Validator = &FpgaDevicePlugin{}
61-
62-
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
63-
func (r *FpgaDevicePlugin) ValidateCreate() (admission.Warnings, error) {
64-
fpgadevicepluginlog.Info("validate create", "name", r.Name)
65-
66-
return nil, r.validatePlugin()
67-
}
68-
69-
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
70-
func (r *FpgaDevicePlugin) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
71-
fpgadevicepluginlog.Info("validate update", "name", r.Name)
72-
73-
return nil, r.validatePlugin()
74-
}
75-
76-
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
77-
func (r *FpgaDevicePlugin) ValidateDelete() (admission.Warnings, error) {
78-
fpgadevicepluginlog.Info("validate delete", "name", r.Name)
79-
80-
return nil, nil
81-
}
82-
83-
func (r *FpgaDevicePlugin) validatePlugin() error {
84-
if err := validatePluginImage(r.Spec.Image, "intel-fpga-plugin", fpgaMinVersion); err != nil {
41+
func (r *FpgaDevicePlugin) validatePlugin(ref *commonDevicePluginValidator) error {
42+
if err := validatePluginImage(r.Spec.Image, ref.expectedImage, &ref.expectedVersion); err != nil {
8543
return err
8644
}
8745

88-
return validatePluginImage(r.Spec.InitImage, "intel-fpga-initcontainer", fpgaMinVersion)
46+
return validatePluginImage(r.Spec.InitImage, ref.expectedInitImage, &ref.expectedVersion)
8947
}

pkg/apis/deviceplugin/v1/gpudeviceplugin_webhook.go

Lines changed: 17 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,15 @@ package v1
1616

1717
import (
1818
"context"
19+
"fmt"
1920

20-
"github.com/pkg/errors"
21-
"k8s.io/apimachinery/pkg/runtime"
2221
ctrl "sigs.k8s.io/controller-runtime"
2322
"sigs.k8s.io/controller-runtime/pkg/client"
2423
logf "sigs.k8s.io/controller-runtime/pkg/log"
25-
"sigs.k8s.io/controller-runtime/pkg/webhook"
26-
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2724

2825
"github.com/intel/intel-device-plugins-for-kubernetes/pkg/controllers"
2926
)
3027

31-
var (
32-
// gpudevicepluginlog is for logging in this package.
33-
gpudevicepluginlog = logf.Log.WithName("gpudeviceplugin-resource")
34-
35-
gpuMinVersion = controllers.ImageMinVersion
36-
)
37-
3828
var cli client.Client
3929

4030
// SetupWebhookWithManager sets up a webhook for GpuDevicePlugin custom resources.
@@ -43,53 +33,25 @@ func (r *GpuDevicePlugin) SetupWebhookWithManager(mgr ctrl.Manager) error {
4333

4434
return ctrl.NewWebhookManagedBy(mgr).
4535
For(r).
36+
WithDefaulter(&commonDevicePluginDefaulter{
37+
defaultImage: "intel/intel-gpu-plugin:" + controllers.ImageMinVersion.String(),
38+
}).
39+
WithValidator(&commonDevicePluginValidator{
40+
expectedImage: "intel-gpu-plugin",
41+
expectedVersion: *controllers.ImageMinVersion,
42+
}).
4643
Complete()
4744
}
4845

4946
// +kubebuilder:webhook:path=/mutate-deviceplugin-intel-com-v1-gpudeviceplugin,mutating=true,failurePolicy=fail,groups=deviceplugin.intel.com,resources=gpudeviceplugins,verbs=create;update,versions=v1,name=mgpudeviceplugin.kb.io,sideEffects=None,admissionReviewVersions=v1
50-
51-
var _ webhook.Defaulter = &GpuDevicePlugin{}
52-
53-
// Default implements webhook.Defaulter so a webhook will be registered for the type.
54-
func (r *GpuDevicePlugin) Default() {
55-
gpudevicepluginlog.Info("default", "name", r.Name)
56-
57-
if len(r.Spec.Image) == 0 {
58-
r.Spec.Image = "intel/intel-gpu-plugin:" + gpuMinVersion.String()
59-
}
60-
}
61-
6247
// +kubebuilder:webhook:verbs=create;update,path=/validate-deviceplugin-intel-com-v1-gpudeviceplugin,mutating=false,failurePolicy=fail,groups=deviceplugin.intel.com,resources=gpudeviceplugins,versions=v1,name=vgpudeviceplugin.kb.io,sideEffects=None,admissionReviewVersions=v1
6348

64-
var _ webhook.Validator = &GpuDevicePlugin{}
65-
66-
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
67-
func (r *GpuDevicePlugin) ValidateCreate() (admission.Warnings, error) {
68-
gpudevicepluginlog.Info("validate create", "name", r.Name)
69-
70-
return nil, r.validatePlugin()
71-
}
72-
73-
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
74-
func (r *GpuDevicePlugin) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
75-
gpudevicepluginlog.Info("validate update", "name", r.Name)
76-
77-
return nil, r.validatePlugin()
78-
}
79-
80-
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
81-
func (r *GpuDevicePlugin) ValidateDelete() (admission.Warnings, error) {
82-
gpudevicepluginlog.Info("validate delete", "name", r.Name)
83-
84-
return nil, nil
85-
}
86-
87-
func (r *GpuDevicePlugin) crossCheckResourceManagement() bool {
88-
ctx := context.Background()
49+
func (r *GpuDevicePlugin) crossCheckResourceManagement(ctx context.Context) bool {
50+
log := logf.FromContext(ctx)
8951
gpuCrs := GpuDevicePluginList{}
9052

9153
if err := cli.List(ctx, &gpuCrs); err != nil {
92-
gpudevicepluginlog.Info("unable to list GPU CRs")
54+
log.Info("unable to list GPU CRs")
9355

9456
return false
9557
}
@@ -108,18 +70,18 @@ func (r *GpuDevicePlugin) crossCheckResourceManagement() bool {
10870
return true
10971
}
11072

111-
func (r *GpuDevicePlugin) validatePlugin() error {
73+
func (r *GpuDevicePlugin) validatePlugin(ctx context.Context, ref *commonDevicePluginValidator) error {
11274
if r.Spec.SharedDevNum == 1 && r.Spec.PreferredAllocationPolicy != "none" {
113-
return errors.Errorf("PreferredAllocationPolicy is valid only when setting sharedDevNum > 1")
75+
return fmt.Errorf("%w: PreferredAllocationPolicy is valid only when setting sharedDevNum > 1", errValidation)
11476
}
11577

11678
if r.Spec.SharedDevNum == 1 && r.Spec.ResourceManager {
117-
return errors.Errorf("resourceManager is valid only when setting sharedDevNum > 1")
79+
return fmt.Errorf("%w: resourceManager is valid only when setting sharedDevNum > 1", errValidation)
11880
}
11981

120-
if !r.crossCheckResourceManagement() {
121-
return errors.Errorf("All GPU CRs must be with or without resource management")
82+
if !r.crossCheckResourceManagement(ctx) {
83+
return fmt.Errorf("%w: All GPU CRs must be with or without resource management", errValidation)
12284
}
12385

124-
return validatePluginImage(r.Spec.Image, "intel-gpu-plugin", gpuMinVersion)
86+
return validatePluginImage(r.Spec.Image, ref.expectedImage, &ref.expectedVersion)
12587
}

0 commit comments

Comments
 (0)