Merge pull request #129142 from googs1025/bug/dra_publishResources

fix(dra): support multiple resource in PublishResources
This commit is contained in:
Kubernetes Prow Robot 2024-12-20 08:58:08 +01:00 committed by GitHub
commit bf2a52a57a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 25 deletions

View File

@ -26,7 +26,6 @@ import (
"google.golang.org/grpc"
"k8s.io/klog/v2"
resourceapi "k8s.io/api/resource/v1beta1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/dynamic-resource-allocation/resourceslice"
@ -55,11 +54,18 @@ type DRAPlugin interface {
// after it returns before all information is actually written
// to the API server.
//
// The caller must not modify the content after the call.
// It is the responsibility of the caller to ensure that the pools and
// slices described in the driver resources parameters are valid
// according to the restrictions defined in the resource.k8s.io API.
//
// Returns an error if KubeClient or NodeName options were not
// set in Start() to create the DRAPlugin instance.
PublishResources(ctx context.Context, resources Resources) error
// Invalid ResourceSlices will be rejected by the apiserver during
// publishing, which happens asynchronously and thus does not
// get returned as error here. The only error returned here is
// when publishing was not set up properly, for example missing
// [KubeClient] or [NodeName] options.
//
// The caller may modify the resources after this call returns.
PublishResources(ctx context.Context, resources resourceslice.DriverResources) error
// This unexported method ensures that we can modify the interface
// without causing an API break of the package
@ -67,12 +73,6 @@ type DRAPlugin interface {
internal()
}
// Resources currently only supports devices. Might get extended in the
// future.
type Resources struct {
Devices []resourceapi.Device
}
// Option implements the functional options pattern for Start.
type Option func(o *options) error
@ -407,7 +407,7 @@ func (d *draPlugin) Stop() {
// PublishResources implements [DRAPlugin.PublishResources]. Returns en error if
// kubeClient or nodeName are unset.
func (d *draPlugin) PublishResources(ctx context.Context, resources Resources) error {
func (d *draPlugin) PublishResources(_ context.Context, resources resourceslice.DriverResources) error {
if d.kubeClient == nil {
return errors.New("no KubeClient found to publish resources")
}
@ -425,14 +425,9 @@ func (d *draPlugin) PublishResources(ctx context.Context, resources Resources) e
UID: d.nodeUID, // Optional, will be determined by controller if empty.
}
driverResources := &resourceslice.DriverResources{
Pools: map[string]resourceslice.Pool{
d.nodeName: {
Slices: []resourceslice.Slice{{
Devices: resources.Devices,
}},
},
},
Pools: resources.Pools,
}
if d.resourceSliceController == nil {
// Start publishing the information. The controller is using
// our background context, not the one passed into this

View File

@ -38,6 +38,7 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
"k8s.io/dynamic-resource-allocation/kubeletplugin"
"k8s.io/dynamic-resource-allocation/resourceslice"
"k8s.io/klog/v2"
drapbv1alpha4 "k8s.io/kubelet/pkg/apis/dra/v1alpha4"
drapb "k8s.io/kubelet/pkg/apis/dra/v1beta1"
@ -188,10 +189,16 @@ func StartPlugin(ctx context.Context, cdiDir, driverName string, kubeClient kube
Basic: &resourceapi.BasicDevice{},
}
}
resources := kubeletplugin.Resources{
Devices: devices,
driverResources := resourceslice.DriverResources{
Pools: map[string]resourceslice.Pool{
nodeName: {
Slices: []resourceslice.Slice{{
Devices: devices,
}},
},
},
}
if err := ex.d.PublishResources(ctx, resources); err != nil {
if err := ex.d.PublishResources(ctx, driverResources); err != nil {
return nil, fmt.Errorf("start kubelet plugin: publish resources: %w", err)
}
} else if len(ex.fileOps.Devices) > 0 {
@ -202,10 +209,16 @@ func StartPlugin(ctx context.Context, cdiDir, driverName string, kubeClient kube
Basic: &resourceapi.BasicDevice{Attributes: ex.fileOps.Devices[deviceName]},
}
}
resources := kubeletplugin.Resources{
Devices: devices,
driverResources := resourceslice.DriverResources{
Pools: map[string]resourceslice.Pool{
nodeName: {
Slices: []resourceslice.Slice{{
Devices: devices,
}},
},
},
}
if err := ex.d.PublishResources(ctx, resources); err != nil {
if err := ex.d.PublishResources(ctx, driverResources); err != nil {
return nil, fmt.Errorf("start kubelet plugin: publish resources: %w", err)
}
}