@@ -16,25 +16,15 @@ package v1
16
16
17
17
import (
18
18
"context"
19
+ "fmt"
19
20
20
- "github.com/pkg/errors"
21
- "k8s.io/apimachinery/pkg/runtime"
22
21
ctrl "sigs.k8s.io/controller-runtime"
23
22
"sigs.k8s.io/controller-runtime/pkg/client"
24
23
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"
27
24
28
25
"github.com/intel/intel-device-plugins-for-kubernetes/pkg/controllers"
29
26
)
30
27
31
- var (
32
- // gpudevicepluginlog is for logging in this package.
33
- gpudevicepluginlog = logf .Log .WithName ("gpudeviceplugin-resource" )
34
-
35
- gpuMinVersion = controllers .ImageMinVersion
36
- )
37
-
38
28
var cli client.Client
39
29
40
30
// SetupWebhookWithManager sets up a webhook for GpuDevicePlugin custom resources.
@@ -43,53 +33,25 @@ func (r *GpuDevicePlugin) SetupWebhookWithManager(mgr ctrl.Manager) error {
43
33
44
34
return ctrl .NewWebhookManagedBy (mgr ).
45
35
For (r ).
36
+ WithDefaulter (& commonDevicePluginDefaulter {
37
+ defaultImage : "intel/intel-gpu-plugin:" + controllers .ImageMinVersion .String (),
38
+ }).
39
+ WithValidator (& commonDevicePluginValidator {
40
+ expectedImage : "intel-gpu-image" ,
41
+ expectedVersion : * controllers .ImageMinVersion ,
42
+ }).
46
43
Complete ()
47
44
}
48
45
49
46
// +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
-
62
47
// +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
63
48
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 )
89
51
gpuCrs := GpuDevicePluginList {}
90
52
91
53
if err := cli .List (ctx , & gpuCrs ); err != nil {
92
- gpudevicepluginlog .Info ("unable to list GPU CRs" )
54
+ log .Info ("unable to list GPU CRs" )
93
55
94
56
return false
95
57
}
@@ -108,18 +70,18 @@ func (r *GpuDevicePlugin) crossCheckResourceManagement() bool {
108
70
return true
109
71
}
110
72
111
- func (r * GpuDevicePlugin ) validatePlugin () error {
73
+ func (r * GpuDevicePlugin ) validatePlugin (ctx context. Context , ref * commonDevicePluginValidator ) error {
112
74
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 )
114
76
}
115
77
116
78
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 )
118
80
}
119
81
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 )
122
84
}
123
85
124
- return validatePluginImage (r .Spec .Image , "intel-gpu-plugin" , gpuMinVersion )
86
+ return validatePluginImage (r .Spec .Image , ref . expectedImage , & ref . expectedVersion )
125
87
}
0 commit comments