1515package v1
1616
1717import (
18+ "context"
19+
1820 "github.com/pkg/errors"
1921 "k8s.io/apimachinery/pkg/runtime"
2022 ctrl "sigs.k8s.io/controller-runtime"
23+ "sigs.k8s.io/controller-runtime/pkg/client"
2124 logf "sigs.k8s.io/controller-runtime/pkg/log"
2225 "sigs.k8s.io/controller-runtime/pkg/webhook"
2326 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3235 gpuMinVersion = controllers .ImageMinVersion
3336)
3437
38+ var cli client.Client
39+
3540// SetupWebhookWithManager sets up a webhook for GpuDevicePlugin custom resources.
3641func (r * GpuDevicePlugin ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
42+ cli = mgr .GetClient ()
43+
3744 return ctrl .NewWebhookManagedBy (mgr ).
3845 For (r ).
3946 Complete ()
@@ -77,6 +84,30 @@ func (r *GpuDevicePlugin) ValidateDelete() (admission.Warnings, error) {
7784 return nil , nil
7885}
7986
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+
80111func (r * GpuDevicePlugin ) validatePlugin () error {
81112 if r .Spec .SharedDevNum == 1 && r .Spec .PreferredAllocationPolicy != "none" {
82113 return errors .Errorf ("PreferredAllocationPolicy is valid only when setting sharedDevNum > 1" )
@@ -86,5 +117,9 @@ func (r *GpuDevicePlugin) validatePlugin() error {
86117 return errors .Errorf ("resourceManager is valid only when setting sharedDevNum > 1" )
87118 }
88119
120+ if ! r .crossCheckResourceManagement () {
121+ return errors .Errorf ("All GPU CRs must be with or without resource management" )
122+ }
123+
89124 return validatePluginImage (r .Spec .Image , "intel-gpu-plugin" , gpuMinVersion )
90125}
0 commit comments