Skip to content

Commit bf2a52a

Browse files
authored
Merge pull request kubernetes#129142 from googs1025/bug/dra_publishResources
fix(dra): support multiple resource in PublishResources
2 parents a4b8a3b + e3b12d8 commit bf2a52a

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

staging/src/k8s.io/dynamic-resource-allocation/kubeletplugin/draplugin.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"google.golang.org/grpc"
2727
"k8s.io/klog/v2"
2828

29-
resourceapi "k8s.io/api/resource/v1beta1"
3029
"k8s.io/apimachinery/pkg/types"
3130
"k8s.io/client-go/kubernetes"
3231
"k8s.io/dynamic-resource-allocation/resourceslice"
@@ -55,24 +54,25 @@ type DRAPlugin interface {
5554
// after it returns before all information is actually written
5655
// to the API server.
5756
//
58-
// The caller must not modify the content after the call.
57+
// It is the responsibility of the caller to ensure that the pools and
58+
// slices described in the driver resources parameters are valid
59+
// according to the restrictions defined in the resource.k8s.io API.
5960
//
60-
// Returns an error if KubeClient or NodeName options were not
61-
// set in Start() to create the DRAPlugin instance.
62-
PublishResources(ctx context.Context, resources Resources) error
61+
// Invalid ResourceSlices will be rejected by the apiserver during
62+
// publishing, which happens asynchronously and thus does not
63+
// get returned as error here. The only error returned here is
64+
// when publishing was not set up properly, for example missing
65+
// [KubeClient] or [NodeName] options.
66+
//
67+
// The caller may modify the resources after this call returns.
68+
PublishResources(ctx context.Context, resources resourceslice.DriverResources) error
6369

6470
// This unexported method ensures that we can modify the interface
6571
// without causing an API break of the package
6672
// (https://pkg.go.dev/golang.org/x/exp/apidiff#section-readme).
6773
internal()
6874
}
6975

70-
// Resources currently only supports devices. Might get extended in the
71-
// future.
72-
type Resources struct {
73-
Devices []resourceapi.Device
74-
}
75-
7676
// Option implements the functional options pattern for Start.
7777
type Option func(o *options) error
7878

@@ -407,7 +407,7 @@ func (d *draPlugin) Stop() {
407407

408408
// PublishResources implements [DRAPlugin.PublishResources]. Returns en error if
409409
// kubeClient or nodeName are unset.
410-
func (d *draPlugin) PublishResources(ctx context.Context, resources Resources) error {
410+
func (d *draPlugin) PublishResources(_ context.Context, resources resourceslice.DriverResources) error {
411411
if d.kubeClient == nil {
412412
return errors.New("no KubeClient found to publish resources")
413413
}
@@ -425,14 +425,9 @@ func (d *draPlugin) PublishResources(ctx context.Context, resources Resources) e
425425
UID: d.nodeUID, // Optional, will be determined by controller if empty.
426426
}
427427
driverResources := &resourceslice.DriverResources{
428-
Pools: map[string]resourceslice.Pool{
429-
d.nodeName: {
430-
Slices: []resourceslice.Slice{{
431-
Devices: resources.Devices,
432-
}},
433-
},
434-
},
428+
Pools: resources.Pools,
435429
}
430+
436431
if d.resourceSliceController == nil {
437432
// Start publishing the information. The controller is using
438433
// our background context, not the one passed into this

test/e2e/dra/test-driver/app/kubeletplugin.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"k8s.io/apimachinery/pkg/util/sets"
3939
"k8s.io/client-go/kubernetes"
4040
"k8s.io/dynamic-resource-allocation/kubeletplugin"
41+
"k8s.io/dynamic-resource-allocation/resourceslice"
4142
"k8s.io/klog/v2"
4243
drapbv1alpha4 "k8s.io/kubelet/pkg/apis/dra/v1alpha4"
4344
drapb "k8s.io/kubelet/pkg/apis/dra/v1beta1"
@@ -188,10 +189,16 @@ func StartPlugin(ctx context.Context, cdiDir, driverName string, kubeClient kube
188189
Basic: &resourceapi.BasicDevice{},
189190
}
190191
}
191-
resources := kubeletplugin.Resources{
192-
Devices: devices,
192+
driverResources := resourceslice.DriverResources{
193+
Pools: map[string]resourceslice.Pool{
194+
nodeName: {
195+
Slices: []resourceslice.Slice{{
196+
Devices: devices,
197+
}},
198+
},
199+
},
193200
}
194-
if err := ex.d.PublishResources(ctx, resources); err != nil {
201+
if err := ex.d.PublishResources(ctx, driverResources); err != nil {
195202
return nil, fmt.Errorf("start kubelet plugin: publish resources: %w", err)
196203
}
197204
} else if len(ex.fileOps.Devices) > 0 {
@@ -202,10 +209,16 @@ func StartPlugin(ctx context.Context, cdiDir, driverName string, kubeClient kube
202209
Basic: &resourceapi.BasicDevice{Attributes: ex.fileOps.Devices[deviceName]},
203210
}
204211
}
205-
resources := kubeletplugin.Resources{
206-
Devices: devices,
212+
driverResources := resourceslice.DriverResources{
213+
Pools: map[string]resourceslice.Pool{
214+
nodeName: {
215+
Slices: []resourceslice.Slice{{
216+
Devices: devices,
217+
}},
218+
},
219+
},
207220
}
208-
if err := ex.d.PublishResources(ctx, resources); err != nil {
221+
if err := ex.d.PublishResources(ctx, driverResources); err != nil {
209222
return nil, fmt.Errorf("start kubelet plugin: publish resources: %w", err)
210223
}
211224
}

0 commit comments

Comments
 (0)