Merge pull request #127196 from byako/draplugin-arguments-sanitization

Fail draplugin PublishResources without nodeName or kubeclient
This commit is contained in:
Kubernetes Prow Robot 2024-09-11 13:35:12 +01:00 committed by GitHub
commit 91dcb61288
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 6 deletions

View File

@ -55,7 +55,10 @@ type DRAPlugin interface {
// to the API server.
//
// The caller must not modify the content after the call.
PublishResources(ctx context.Context, resources Resources)
//
// 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
// This unexported method ensures that we can modify the interface
// without causing an API break of the package
@ -254,6 +257,9 @@ type draPlugin struct {
// The context and/or DRAPlugin.Stop can be used to stop all background activity.
// Stop also blocks. A logger can be stored in the context to add values or
// a name to all log entries.
//
// If the plugin will be used to publish resources, [KubeClient] and [NodeName]
// options are mandatory.
func Start(ctx context.Context, nodeServer interface{}, opts ...Option) (result DRAPlugin, finalErr error) {
logger := klog.FromContext(ctx)
o := options{
@ -365,8 +371,16 @@ func (d *draPlugin) Stop() {
d.wg.Wait()
}
// PublishResources implements [DRAPlugin.PublishResources].
func (d *draPlugin) PublishResources(ctx context.Context, resources Resources) {
// PublishResources implements [DRAPlugin.PublishResources]. Returns en error if
// kubeClient or nodeName are unset.
func (d *draPlugin) PublishResources(ctx context.Context, resources Resources) error {
if d.kubeClient == nil {
return errors.New("no KubeClient found to publish resources")
}
if d.nodeName == "" {
return errors.New("no NodeName was set to publish resources")
}
d.mutex.Lock()
defer d.mutex.Unlock()
@ -393,11 +407,13 @@ func (d *draPlugin) PublishResources(ctx context.Context, resources Resources) {
controllerLogger = klog.LoggerWithName(controllerLogger, "ResourceSlice controller")
controllerCtx = klog.NewContext(controllerCtx, controllerLogger)
d.resourceSliceController = resourceslice.StartController(controllerCtx, d.kubeClient, d.driverName, owner, driverResources)
return
return nil
}
// Inform running controller about new information.
d.resourceSliceController.Update(driverResources)
return nil
}
// RegistrationStatus implements [DRAPlugin.RegistrationStatus].

View File

@ -182,7 +182,9 @@ func StartPlugin(ctx context.Context, cdiDir, driverName string, kubeClient kube
resources := kubeletplugin.Resources{
Devices: devices,
}
ex.d.PublishResources(ctx, resources)
if err := ex.d.PublishResources(ctx, resources); err != nil {
return nil, fmt.Errorf("start kubelet plugin: publish resources: %w", err)
}
} else if len(ex.fileOps.Devices) > 0 {
devices := make([]resourceapi.Device, len(ex.fileOps.Devices))
for i, deviceName := range sets.List(ex.deviceNames) {
@ -194,7 +196,9 @@ func StartPlugin(ctx context.Context, cdiDir, driverName string, kubeClient kube
resources := kubeletplugin.Resources{
Devices: devices,
}
ex.d.PublishResources(ctx, resources)
if err := ex.d.PublishResources(ctx, resources); err != nil {
return nil, fmt.Errorf("start kubelet plugin: publish resources: %w", err)
}
}
return ex, nil