@@ -41,6 +41,7 @@ import (
4141
4242 "github.com/fsnotify/fsnotify"
4343 "google.golang.org/grpc"
44+ "google.golang.org/grpc/credentials/insecure"
4445 pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
4546)
4647
@@ -54,7 +55,7 @@ const (
5455
5556var returnIommuMap = getIommuMap
5657
57- //Implements the kubernetes device plugin API
58+ // Implements the kubernetes device plugin API
5859type GenericDevicePlugin struct {
5960 devs []* pluginapi.Device
6061 server * grpc.Server
@@ -68,7 +69,7 @@ type GenericDevicePlugin struct {
6869 devsHealth []* pluginapi.Device
6970}
7071
71- //Returns an initialized instance of GenericDevicePlugin
72+ // Returns an initialized instance of GenericDevicePlugin
7273func NewGenericDevicePlugin (deviceName string , devicePath string , devices []* pluginapi.Device ) * GenericDevicePlugin {
7374 log .Println ("Devicename " + deviceName )
7475 serverSock := fmt .Sprintf (pluginapi .DevicePluginPath + "kubevirt-%s.sock" , deviceName )
@@ -103,15 +104,17 @@ func waitForGrpcServer(socketPath string, timeout time.Duration) error {
103104
104105// dial establishes the gRPC communication with the registered device plugin.
105106func connect (socketPath string , timeout time.Duration ) (* grpc.ClientConn , error ) {
106- c , err := grpc .Dial (socketPath ,
107- grpc .WithInsecure (),
107+ ctx , _ := context .WithTimeout (context .Background (), timeout )
108+ c , err := grpc .DialContext (ctx , socketPath ,
109+ grpc .WithTransportCredentials (insecure .NewCredentials ()),
108110 grpc .WithBlock (),
109- grpc .WithTimeout (timeout ),
110- grpc .WithDialer (func (addr string , timeout time.Duration ) (net.Conn , error ) {
111- return net .DialTimeout ("unix" , addr , timeout )
111+ grpc .WithContextDialer (func (ctx context.Context , addr string ) (net.Conn , error ) {
112+ if deadline , ok := ctx .Deadline (); ok {
113+ return net .DialTimeout ("unix" , addr , time .Until (deadline ))
114+ }
115+ return net .DialTimeout ("unix" , addr , connectionTimeout )
112116 }),
113117 )
114-
115118 if err != nil {
116119 return nil , err
117120 }
@@ -213,7 +216,7 @@ func (dpi *GenericDevicePlugin) Register() error {
213216 return nil
214217}
215218
216- //ListAndWatch lists devices and update that list according to the health status
219+ // ListAndWatch lists devices and update that list according to the health status
217220func (dpi * GenericDevicePlugin ) ListAndWatch (e * pluginapi.Empty , s pluginapi.DevicePlugin_ListAndWatchServer ) error {
218221
219222 s .Send (& pluginapi.ListAndWatchResponse {Devices : dpi .devs })
@@ -244,7 +247,7 @@ func (dpi *GenericDevicePlugin) ListAndWatch(e *pluginapi.Empty, s pluginapi.Dev
244247 }
245248}
246249
247- //Performs pre allocation checks and allocates a devices based on the request
250+ // Performs pre allocation checks and allocates a devices based on the request
248251func (dpi * GenericDevicePlugin ) Allocate (ctx context.Context , reqs * pluginapi.AllocateRequest ) (* pluginapi.AllocateResponse , error ) {
249252 log .Println ("In allocate" )
250253 responses := pluginapi.AllocateResponse {}
@@ -334,7 +337,7 @@ func (dpi *GenericDevicePlugin) GetPreferredAllocation(ctx context.Context, in *
334337 return nil , nil
335338}
336339
337- //Health check of GPU devices
340+ // Health check of GPU devices
338341func (dpi * GenericDevicePlugin ) healthCheck () error {
339342 method := fmt .Sprintf ("healthCheck(%s)" , dpi .deviceName )
340343 log .Printf ("%s: invoked" , method )
0 commit comments