15
15
package v1
16
16
17
17
import (
18
+ "context"
19
+
18
20
"github.com/pkg/errors"
19
21
"k8s.io/apimachinery/pkg/runtime"
20
22
ctrl "sigs.k8s.io/controller-runtime"
23
+ "sigs.k8s.io/controller-runtime/pkg/client"
21
24
logf "sigs.k8s.io/controller-runtime/pkg/log"
22
25
"sigs.k8s.io/controller-runtime/pkg/webhook"
23
26
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
32
35
gpuMinVersion = controllers .ImageMinVersion
33
36
)
34
37
38
+ var cli client.Client
39
+
35
40
// SetupWebhookWithManager sets up a webhook for GpuDevicePlugin custom resources.
36
41
func (r * GpuDevicePlugin ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
42
+ cli = mgr .GetClient ()
43
+
37
44
return ctrl .NewWebhookManagedBy (mgr ).
38
45
For (r ).
39
46
Complete ()
@@ -77,6 +84,30 @@ func (r *GpuDevicePlugin) ValidateDelete() (admission.Warnings, error) {
77
84
return nil , nil
78
85
}
79
86
87
+ func (r * GpuDevicePlugin ) crossCheckResourceManagement () bool {
88
+ ctx := context .Background ()
89
+ gpuCrs := GpuDevicePluginList {}
90
+
91
+ if err := cli .List (ctx , & gpuCrs ); err != nil {
92
+ gpudevicepluginlog .Info ("unable to list GPU CRs" )
93
+
94
+ return false
95
+ }
96
+
97
+ for _ , cr := range gpuCrs .Items {
98
+ // Ignore itself.
99
+ if cr .Name == r .Name {
100
+ continue
101
+ }
102
+
103
+ if cr .Spec .ResourceManager != r .Spec .ResourceManager {
104
+ return false
105
+ }
106
+ }
107
+
108
+ return true
109
+ }
110
+
80
111
func (r * GpuDevicePlugin ) validatePlugin () error {
81
112
if r .Spec .SharedDevNum == 1 && r .Spec .PreferredAllocationPolicy != "none" {
82
113
return errors .Errorf ("PreferredAllocationPolicy is valid only when setting sharedDevNum > 1" )
@@ -86,5 +117,9 @@ func (r *GpuDevicePlugin) validatePlugin() error {
86
117
return errors .Errorf ("resourceManager is valid only when setting sharedDevNum > 1" )
87
118
}
88
119
120
+ if ! r .crossCheckResourceManagement () {
121
+ return errors .Errorf ("All GPU CRs must be with or without resource management" )
122
+ }
123
+
89
124
return validatePluginImage (r .Spec .Image , "intel-gpu-plugin" , gpuMinVersion )
90
125
}
0 commit comments