Skip to content

Commit 07790fb

Browse files
committed
Set driver name based on device profile
1 parent 15653a6 commit 07790fb

File tree

14 files changed

+69
-57
lines changed

14 files changed

+69
-57
lines changed

cmd/dra-example-kubeletplugin/cdi.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
cdispec "tags.cncf.io/container-device-interface/specs-go"
2828

2929
"sigs.k8s.io/dra-example-driver/internal/profiles"
30-
"sigs.k8s.io/dra-example-driver/pkg/consts"
3130
)
3231

3332
const cdiCommonDeviceName = "common"
@@ -65,7 +64,7 @@ func (cdi *CDIHandler) CreateCommonSpecFile() error {
6564
ContainerEdits: cdispec.ContainerEdits{
6665
Env: []string{
6766
fmt.Sprintf("KUBERNETES_NODE_NAME=%s", os.Getenv("NODE_NAME")),
68-
fmt.Sprintf("DRA_RESOURCE_DRIVER_NAME=%s", consts.DriverName),
67+
fmt.Sprintf("DRA_RESOURCE_DRIVER_NAME=%s", cdi.driverName),
6968
},
7069
},
7170
},

cmd/dra-example-kubeletplugin/driver.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ import (
2727
coreclientset "k8s.io/client-go/kubernetes"
2828
"k8s.io/dynamic-resource-allocation/kubeletplugin"
2929
"k8s.io/klog/v2"
30-
31-
"sigs.k8s.io/dra-example-driver/pkg/consts"
3230
)
3331

3432
type driver struct {
@@ -56,7 +54,7 @@ func NewDriver(ctx context.Context, config *Config) (*driver, error) {
5654
driver,
5755
kubeletplugin.KubeClient(config.coreclient),
5856
kubeletplugin.NodeName(config.flags.nodeName),
59-
kubeletplugin.DriverName(consts.DriverName),
57+
kubeletplugin.DriverName(config.flags.driverName),
6058
kubeletplugin.RegistrarDirectoryPath(config.flags.kubeletRegistrarDirectoryPath),
6159
kubeletplugin.PluginDataDirectoryPath(config.DriverPluginPath()),
6260
)

cmd/dra-example-kubeletplugin/health.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ import (
3333
"k8s.io/klog/v2"
3434
drapb "k8s.io/kubelet/pkg/apis/dra/v1"
3535
registerapi "k8s.io/kubelet/pkg/apis/pluginregistration/v1"
36-
37-
"sigs.k8s.io/dra-example-driver/pkg/consts"
3836
)
3937

4038
type healthcheck struct {
@@ -65,7 +63,7 @@ func startHealthcheck(ctx context.Context, config *Config) (*healthcheck, error)
6563
Scheme: "unix",
6664
// TODO: this needs to adapt when seamless upgrades
6765
// are enabled and the filename includes a uid.
68-
Path: path.Join(config.flags.kubeletRegistrarDirectoryPath, consts.DriverName+"-reg.sock"),
66+
Path: path.Join(config.flags.kubeletRegistrarDirectoryPath, config.flags.driverName+"-reg.sock"),
6967
}).String()
7068
log.Info("connecting to registration socket", "path", regSockPath)
7169
regConn, err := grpc.NewClient(

cmd/dra-example-kubeletplugin/main.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"k8s.io/klog/v2"
3535

3636
"sigs.k8s.io/dra-example-driver/internal/profiles/gpu"
37-
"sigs.k8s.io/dra-example-driver/pkg/consts"
3837
"sigs.k8s.io/dra-example-driver/pkg/flags"
3938
)
4039

@@ -53,6 +52,7 @@ type Flags struct {
5352
kubeletPluginsDirectoryPath string
5453
healthcheckPort int
5554
profile string
55+
driverName string
5656
}
5757

5858
type Config struct {
@@ -71,7 +71,7 @@ var validProfiles = []string{
7171
}
7272

7373
func (c Config) DriverPluginPath() string {
74-
return filepath.Join(c.flags.kubeletPluginsDirectoryPath, consts.DriverName)
74+
return filepath.Join(c.flags.kubeletPluginsDirectoryPath, c.flags.driverName)
7575
}
7676

7777
func main() {
@@ -135,6 +135,12 @@ func newApp() *cli.App {
135135
Destination: &flags.profile,
136136
EnvVars: []string{"DEVICE_PROFILE"},
137137
},
138+
&cli.StringFlag{
139+
Name: "driver-name",
140+
Usage: "Name of the DRA driver. Its default is derived from the device profile.",
141+
Destination: &flags.driverName,
142+
EnvVars: []string{"DRIVER_NAME"},
143+
},
138144
}
139145
cliFlags = append(cliFlags, flags.kubeClientConfig.Flags()...)
140146
cliFlags = append(cliFlags, flags.loggingConfig.Flags()...)
@@ -158,6 +164,10 @@ func newApp() *cli.App {
158164
return fmt.Errorf("create client: %w", err)
159165
}
160166

167+
if flags.driverName == "" {
168+
flags.driverName = flags.profile + ".example.com"
169+
}
170+
161171
var (
162172
sb runtime.SchemeBuilder
163173
applyConfigFunc ApplyConfigFunc

cmd/dra-example-kubeletplugin/state.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
3030

3131
"sigs.k8s.io/dra-example-driver/internal/profiles"
32-
"sigs.k8s.io/dra-example-driver/pkg/consts"
3332
)
3433

3534
type AllocatableDevices map[string]resourceapi.Device
@@ -44,6 +43,7 @@ type OpaqueDeviceConfig struct {
4443

4544
type DeviceState struct {
4645
sync.Mutex
46+
driverName string
4747
cdi *CDIHandler
4848
driverResources resourceslice.DriverResources
4949
allocatable AllocatableDevices
@@ -58,7 +58,7 @@ func NewDeviceState(config *Config) (*DeviceState, error) {
5858
return nil, fmt.Errorf("error enumerating all possible devices: %v", err)
5959
}
6060

61-
cdi, err := NewCDIHandler(config.flags.cdiRoot, consts.DriverName, config.cdiClass)
61+
cdi, err := NewCDIHandler(config.flags.cdiRoot, config.flags.driverName, config.cdiClass)
6262
if err != nil {
6363
return nil, fmt.Errorf("unable to create CDI handler: %v", err)
6464
}
@@ -91,6 +91,7 @@ func NewDeviceState(config *Config) (*DeviceState, error) {
9191
}
9292

9393
state := &DeviceState{
94+
driverName: config.flags.driverName,
9495
cdi: cdi,
9596
driverResources: driverResources,
9697
allocatable: allocatable,
@@ -190,7 +191,7 @@ func (s *DeviceState) prepareDevices(claim *resourceapi.ResourceClaim) (profiles
190191
// Retrieve the full set of device configs for the driver.
191192
configs, err := GetOpaqueDeviceConfigs(
192193
s.configDecoder,
193-
consts.DriverName,
194+
s.driverName,
194195
claim.Status.Allocation.Devices.Config,
195196
)
196197
if err != nil {
@@ -206,8 +207,12 @@ func (s *DeviceState) prepareDevices(claim *resourceapi.ResourceClaim) (profiles
206207
// each device allocation result based on their order of precedence.
207208
configResultsMap := make(map[runtime.Object][]*resourceapi.DeviceRequestAllocationResult)
208209
for _, result := range claim.Status.Allocation.Devices.Results {
210+
// The claim may include allocations meant for other drivers.
211+
if result.Driver != s.driverName {
212+
continue
213+
}
209214
if _, exists := s.allocatable[result.Device]; !exists {
210-
return nil, fmt.Errorf("requested GPU is not allocatable: %v", result.Device)
215+
return nil, fmt.Errorf("requested device is not allocatable: %v", result.Device)
211216
}
212217
for _, c := range slices.Backward(configs) {
213218
if len(c.Requests) == 0 || slices.Contains(c.Requests, result.Request) {

cmd/dra-example-webhook/main.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ import (
3434
"k8s.io/klog/v2"
3535

3636
"sigs.k8s.io/dra-example-driver/internal/profiles/gpu"
37-
"sigs.k8s.io/dra-example-driver/pkg/consts"
3837
"sigs.k8s.io/dra-example-driver/pkg/flags"
3938
)
4039

4140
type Flags struct {
4241
loggingConfig *flags.LoggingConfig
4342

44-
certFile string
45-
keyFile string
46-
port int
47-
profile string
43+
certFile string
44+
keyFile string
45+
port int
46+
profile string
47+
driverName string
4848
}
4949

5050
var configScheme = runtime.NewScheme()
@@ -92,6 +92,12 @@ func newApp() *cli.App {
9292
Destination: &flags.profile,
9393
EnvVars: []string{"DEVICE_PROFILE"},
9494
},
95+
&cli.StringFlag{
96+
Name: "driver-name",
97+
Usage: "Name of the DRA driver. Its default is derived from the device profile.",
98+
Destination: &flags.driverName,
99+
EnvVars: []string{"DRIVER_NAME"},
100+
},
95101
}
96102
cliFlags = append(cliFlags, flags.loggingConfig.Flags()...)
97103

@@ -120,12 +126,16 @@ func newApp() *cli.App {
120126
return fmt.Errorf("invalid device profile %q, valid profiles are %q", flags.profile, validProfiles)
121127
}
122128

129+
if flags.driverName == "" {
130+
flags.driverName = flags.profile + ".example.com"
131+
}
132+
123133
if err := sb.AddToScheme(configScheme); err != nil {
124134
return fmt.Errorf("create config scheme: %w", err)
125135
}
126136

127137
server := &http.Server{
128-
Handler: newMux(newConfigDecoder(), validate),
138+
Handler: newMux(newConfigDecoder(), validate, flags.driverName),
129139
Addr: fmt.Sprintf(":%d", flags.port),
130140
}
131141
klog.Info("starting webhook server on", server.Addr)
@@ -148,9 +158,9 @@ func newConfigDecoder() runtime.Decoder {
148158
)
149159
}
150160

151-
func newMux(configDecoder runtime.Decoder, validate validator) *http.ServeMux {
161+
func newMux(configDecoder runtime.Decoder, validate validator, driverName string) *http.ServeMux {
152162
mux := http.NewServeMux()
153-
mux.HandleFunc("/validate-resource-claim-parameters", serveResourceClaim(configDecoder, validate))
163+
mux.HandleFunc("/validate-resource-claim-parameters", serveResourceClaim(configDecoder, validate, driverName))
154164
mux.HandleFunc("/readyz", func(w http.ResponseWriter, req *http.Request) {
155165
_, err := w.Write([]byte("ok"))
156166
if err != nil {
@@ -161,9 +171,9 @@ func newMux(configDecoder runtime.Decoder, validate validator) *http.ServeMux {
161171
return mux
162172
}
163173

164-
func serveResourceClaim(configDecoder runtime.Decoder, validate validator) func(http.ResponseWriter, *http.Request) {
174+
func serveResourceClaim(configDecoder runtime.Decoder, validate validator, driverName string) func(http.ResponseWriter, *http.Request) {
165175
return func(w http.ResponseWriter, r *http.Request) {
166-
serve(w, r, admitResourceClaimParameters(configDecoder, validate))
176+
serve(w, r, admitResourceClaimParameters(configDecoder, validate, driverName))
167177
}
168178
}
169179

@@ -238,7 +248,7 @@ func readAdmissionReview(data []byte) (*admissionv1.AdmissionReview, error) {
238248

239249
// admitResourceClaimParameters accepts both ResourceClaims and ResourceClaimTemplates and validates their
240250
// opaque device configuration parameters for this driver.
241-
func admitResourceClaimParameters(configDecoder runtime.Decoder, validate validator) func(ar admissionv1.AdmissionReview) *admissionv1.AdmissionResponse {
251+
func admitResourceClaimParameters(configDecoder runtime.Decoder, validate validator, driverName string) func(ar admissionv1.AdmissionReview) *admissionv1.AdmissionResponse {
242252
return func(ar admissionv1.AdmissionReview) *admissionv1.AdmissionResponse {
243253
klog.V(2).Info("admitting resource claim parameters")
244254

@@ -292,7 +302,7 @@ func admitResourceClaimParameters(configDecoder runtime.Decoder, validate valida
292302

293303
var errs []error
294304
for configIndex, config := range deviceConfigs {
295-
if config.Opaque == nil || config.Opaque.Driver != consts.DriverName {
305+
if config.Opaque == nil || config.Opaque.Driver != driverName {
296306
continue
297307
}
298308

cmd/dra-example-webhook/main_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ import (
3737

3838
configapi "sigs.k8s.io/dra-example-driver/api/example.com/resource/gpu/v1alpha1"
3939
"sigs.k8s.io/dra-example-driver/internal/profiles/gpu"
40-
"sigs.k8s.io/dra-example-driver/pkg/consts"
4140
)
4241

42+
const driverName = "gpu.example.com"
43+
4344
func TestReadyEndpoint(t *testing.T) {
44-
s := httptest.NewServer(newMux(nil, nil))
45+
s := httptest.NewServer(newMux(nil, nil, ""))
4546
t.Cleanup(s.Close)
4647

4748
res, err := http.Get(s.URL + "/readyz")
@@ -172,7 +173,7 @@ func TestResourceClaimValidatingWebhook(t *testing.T) {
172173
sb := gpu.ConfigSchemeBuilder
173174
assert.NoError(t, sb.AddToScheme(configScheme))
174175

175-
s := httptest.NewServer(newMux(newConfigDecoder(), gpu.ValidateConfig))
176+
s := httptest.NewServer(newMux(newConfigDecoder(), gpu.ValidateConfig, driverName))
176177
t.Cleanup(s.Close)
177178

178179
for name, test := range tests {
@@ -253,7 +254,7 @@ func resourceClaimSpecWithGpuConfigs(gpuConfigs ...*configapi.GpuConfig) resourc
253254
deviceConfig := resourceapi.DeviceClaimConfiguration{
254255
DeviceConfiguration: resourceapi.DeviceConfiguration{
255256
Opaque: &resourceapi.OpaqueDeviceConfiguration{
256-
Driver: consts.DriverName,
257+
Driver: driverName,
257258
Parameters: runtime.RawExtension{
258259
Object: gpuConfig,
259260
},

deployments/helm/dra-example-driver/templates/_helpers.tpl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,10 @@ resource.k8s.io/v1beta1
121121
{{- else -}}
122122
{{- end -}}
123123
{{- end -}}
124+
125+
{{/*
126+
The driver name.
127+
*/}}
128+
{{- define "dra-example-driver.driverName" -}}
129+
{{ default (print .Values.deviceProfile ".example.com") .Values.driverName }}
130+
{{- end -}}

deployments/helm/dra-example-driver/templates/deviceclass.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
apiVersion: {{ include "dra-example-driver.resourceApiVersion" . }}
33
kind: DeviceClass
44
metadata:
5-
name: gpu.example.com
5+
name: {{ include "dra-example-driver.driverName" . }}
66
spec:
77
selectors:
8-
- cel:
9-
expression: "device.driver == 'gpu.example.com'"
8+
- cel:
9+
expression: "device.driver == '{{ include "dra-example-driver.driverName" . }}'"

deployments/helm/dra-example-driver/templates/kubeletplugin.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ spec:
5959
periodSeconds: 10
6060
{{- end }}
6161
env:
62+
- name: DRIVER_NAME
63+
value: {{ include "dra-example-driver.driverName" . | quote }}
6264
- name: DEVICE_PROFILE
6365
value: {{ .Values.deviceProfile | quote }}
6466
- name: CDI_ROOT

0 commit comments

Comments
 (0)