@@ -16,25 +16,15 @@ package v1
1616
1717import (
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-
3828var 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