|
15 | 15 | package v1
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
18 | 21 | "github.com/pkg/errors"
|
19 | 22 | "k8s.io/apimachinery/pkg/runtime"
|
20 | 23 | ctrl "sigs.k8s.io/controller-runtime"
|
21 | 24 | logf "sigs.k8s.io/controller-runtime/pkg/log"
|
22 |
| - "sigs.k8s.io/controller-runtime/pkg/webhook" |
23 | 25 | "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
|
24 | 26 |
|
25 | 27 | "github.com/intel/intel-device-plugins-for-kubernetes/pkg/controllers"
|
26 | 28 | )
|
27 | 29 |
|
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 |
34 | 31 |
|
35 | 32 | // SetupWebhookWithManager sets up a webhook for DsaDevicePlugin custom resources.
|
36 | 33 | func (r *DsaDevicePlugin) SetupWebhookWithManager(mgr ctrl.Manager) error {
|
37 | 34 | return ctrl.NewWebhookManagedBy(mgr).
|
38 | 35 | For(r).
|
| 36 | + WithDefaulter(&dsaDevicePluginDefaulter{}). |
| 37 | + WithValidator(&dsaDevicePluginValidator{}). |
39 | 38 | Complete()
|
40 | 39 | }
|
41 | 40 |
|
42 | 41 | // +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 | 42 |
|
44 |
| -var _ webhook.Defaulter = &DsaDevicePlugin{} |
| 43 | +type dsaDevicePluginDefaulter struct{} |
45 | 44 |
|
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{} |
49 | 46 |
|
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) |
52 | 54 | }
|
| 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 |
53 | 63 | }
|
54 | 64 |
|
55 | 65 | // +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
|
56 | 66 |
|
57 |
| -var _ webhook.Validator = &DsaDevicePlugin{} |
| 67 | +type dsaDevicePluginValidator struct{} |
58 | 68 |
|
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{} |
62 | 70 |
|
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) |
64 | 83 | }
|
65 | 84 |
|
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) |
69 | 89 |
|
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) |
71 | 97 | }
|
72 | 98 |
|
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") |
76 | 104 |
|
77 | 105 | return nil, nil
|
78 | 106 | }
|
79 | 107 |
|
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 { |
82 | 110 | return err
|
83 | 111 | }
|
84 | 112 |
|
85 |
| - if len(r.Spec.ProvisioningConfig) > 0 && len(r.Spec.InitImage) == 0 { |
| 113 | + if len(cr.Spec.ProvisioningConfig) > 0 && len(cr.Spec.InitImage) == 0 { |
86 | 114 | return errors.Errorf("ProvisioningConfig is set with no InitImage")
|
87 | 115 | }
|
88 | 116 |
|
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) |
91 | 119 | }
|
92 | 120 |
|
93 | 121 | return nil
|
|
0 commit comments