Skip to content

Commit 91dcb61

Browse files
authored
Merge pull request kubernetes#127196 from byako/draplugin-arguments-sanitization
Fail draplugin PublishResources without nodeName or kubeclient
2 parents 5639b4b + 87871bd commit 91dcb61

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ type DRAPlugin interface {
5555
// to the API server.
5656
//
5757
// The caller must not modify the content after the call.
58-
PublishResources(ctx context.Context, resources Resources)
58+
//
59+
// Returns an error if KubeClient or NodeName options were not
60+
// set in Start() to create the DRAPlugin instance.
61+
PublishResources(ctx context.Context, resources Resources) error
5962

6063
// This unexported method ensures that we can modify the interface
6164
// without causing an API break of the package
@@ -254,6 +257,9 @@ type draPlugin struct {
254257
// The context and/or DRAPlugin.Stop can be used to stop all background activity.
255258
// Stop also blocks. A logger can be stored in the context to add values or
256259
// a name to all log entries.
260+
//
261+
// If the plugin will be used to publish resources, [KubeClient] and [NodeName]
262+
// options are mandatory.
257263
func Start(ctx context.Context, nodeServer interface{}, opts ...Option) (result DRAPlugin, finalErr error) {
258264
logger := klog.FromContext(ctx)
259265
o := options{
@@ -365,8 +371,16 @@ func (d *draPlugin) Stop() {
365371
d.wg.Wait()
366372
}
367373

368-
// PublishResources implements [DRAPlugin.PublishResources].
369-
func (d *draPlugin) PublishResources(ctx context.Context, resources Resources) {
374+
// PublishResources implements [DRAPlugin.PublishResources]. Returns en error if
375+
// kubeClient or nodeName are unset.
376+
func (d *draPlugin) PublishResources(ctx context.Context, resources Resources) error {
377+
if d.kubeClient == nil {
378+
return errors.New("no KubeClient found to publish resources")
379+
}
380+
if d.nodeName == "" {
381+
return errors.New("no NodeName was set to publish resources")
382+
}
383+
370384
d.mutex.Lock()
371385
defer d.mutex.Unlock()
372386

@@ -393,11 +407,13 @@ func (d *draPlugin) PublishResources(ctx context.Context, resources Resources) {
393407
controllerLogger = klog.LoggerWithName(controllerLogger, "ResourceSlice controller")
394408
controllerCtx = klog.NewContext(controllerCtx, controllerLogger)
395409
d.resourceSliceController = resourceslice.StartController(controllerCtx, d.kubeClient, d.driverName, owner, driverResources)
396-
return
410+
return nil
397411
}
398412

399413
// Inform running controller about new information.
400414
d.resourceSliceController.Update(driverResources)
415+
416+
return nil
401417
}
402418

403419
// RegistrationStatus implements [DRAPlugin.RegistrationStatus].

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ func StartPlugin(ctx context.Context, cdiDir, driverName string, kubeClient kube
182182
resources := kubeletplugin.Resources{
183183
Devices: devices,
184184
}
185-
ex.d.PublishResources(ctx, resources)
185+
if err := ex.d.PublishResources(ctx, resources); err != nil {
186+
return nil, fmt.Errorf("start kubelet plugin: publish resources: %w", err)
187+
}
186188
} else if len(ex.fileOps.Devices) > 0 {
187189
devices := make([]resourceapi.Device, len(ex.fileOps.Devices))
188190
for i, deviceName := range sets.List(ex.deviceNames) {
@@ -194,7 +196,9 @@ func StartPlugin(ctx context.Context, cdiDir, driverName string, kubeClient kube
194196
resources := kubeletplugin.Resources{
195197
Devices: devices,
196198
}
197-
ex.d.PublishResources(ctx, resources)
199+
if err := ex.d.PublishResources(ctx, resources); err != nil {
200+
return nil, fmt.Errorf("start kubelet plugin: publish resources: %w", err)
201+
}
198202
}
199203

200204
return ex, nil

0 commit comments

Comments
 (0)