Skip to content

Commit 7b74ad6

Browse files
committed
operator: gpu: prevent scenario where CRs both enable and disable resource management
Signed-off-by: Tuomas Katila <[email protected]>
1 parent eba7d3f commit 7b74ad6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

pkg/apis/deviceplugin/v1/gpudeviceplugin_webhook.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
package v1
1616

1717
import (
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"
@@ -32,8 +35,12 @@ var (
3235
gpuMinVersion = controllers.ImageMinVersion
3336
)
3437

38+
var cli client.Client
39+
3540
// SetupWebhookWithManager sets up a webhook for GpuDevicePlugin custom resources.
3641
func (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+
80111
func (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

Comments
 (0)