2017-08-11 00:14:34 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package deviceplugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2017-10-16 19:48:28 +00:00
|
|
|
pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1alpha"
|
2017-08-11 00:14:34 +00:00
|
|
|
)
|
|
|
|
|
2017-08-22 22:03:47 +00:00
|
|
|
// endpoint maps to a single registered device plugin. It is responsible
|
|
|
|
// for managing gRPC communications with the device plugin and caching
|
|
|
|
// device states reported by the device plugin.
|
2017-11-14 21:06:07 +00:00
|
|
|
type endpoint interface {
|
|
|
|
run()
|
|
|
|
stop()
|
|
|
|
allocate(devs []string) (*pluginapi.AllocateResponse, error)
|
|
|
|
getDevices() []pluginapi.Device
|
|
|
|
callback(resourceName string, added, updated, deleted []pluginapi.Device)
|
|
|
|
}
|
|
|
|
|
|
|
|
type endpointImpl struct {
|
2017-09-18 20:29:47 +00:00
|
|
|
client pluginapi.DevicePluginClient
|
|
|
|
clientConn *grpc.ClientConn
|
2017-08-11 00:14:34 +00:00
|
|
|
|
|
|
|
socketPath string
|
|
|
|
resourceName string
|
|
|
|
|
2017-09-08 04:14:15 +00:00
|
|
|
devices map[string]pluginapi.Device
|
2017-08-11 00:14:34 +00:00
|
|
|
mutex sync.Mutex
|
|
|
|
|
2017-11-14 21:06:07 +00:00
|
|
|
cb monitorCallback
|
2017-08-11 00:14:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 22:03:47 +00:00
|
|
|
// newEndpoint creates a new endpoint for the given resourceName.
|
2017-11-14 21:06:07 +00:00
|
|
|
func newEndpointImpl(socketPath, resourceName string, devices map[string]pluginapi.Device, callback monitorCallback) (*endpointImpl, error) {
|
2017-09-18 20:29:47 +00:00
|
|
|
client, c, err := dial(socketPath)
|
2017-08-11 00:14:34 +00:00
|
|
|
if err != nil {
|
2017-08-22 22:03:47 +00:00
|
|
|
glog.Errorf("Can't create new endpoint with path %s err %v", socketPath, err)
|
2017-08-11 00:14:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:06:07 +00:00
|
|
|
return &endpointImpl{
|
2017-09-18 20:29:47 +00:00
|
|
|
client: client,
|
|
|
|
clientConn: c,
|
2017-08-11 00:14:34 +00:00
|
|
|
|
|
|
|
socketPath: socketPath,
|
|
|
|
resourceName: resourceName,
|
|
|
|
|
2017-11-14 21:06:07 +00:00
|
|
|
devices: devices,
|
|
|
|
cb: callback,
|
2017-08-11 00:14:34 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:06:07 +00:00
|
|
|
func (e *endpointImpl) callback(resourceName string, added, updated, deleted []pluginapi.Device) {
|
|
|
|
e.cb(resourceName, added, updated, deleted)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *endpointImpl) getDevices() []pluginapi.Device {
|
2017-08-22 22:03:47 +00:00
|
|
|
e.mutex.Lock()
|
|
|
|
defer e.mutex.Unlock()
|
2017-09-08 04:14:15 +00:00
|
|
|
var devs []pluginapi.Device
|
|
|
|
|
|
|
|
for _, d := range e.devices {
|
|
|
|
devs = append(devs, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
return devs
|
2017-08-22 22:03:47 +00:00
|
|
|
}
|
2017-08-11 00:14:34 +00:00
|
|
|
|
2017-10-26 08:36:16 +00:00
|
|
|
// run initializes ListAndWatch gRPC call for the device plugin and
|
|
|
|
// blocks on receiving ListAndWatch gRPC stream updates. Each ListAndWatch
|
2017-08-22 22:03:47 +00:00
|
|
|
// stream update contains a new list of device states. listAndWatch compares the new
|
|
|
|
// device states with its cached states to get list of new, updated, and deleted devices.
|
2017-11-14 21:06:07 +00:00
|
|
|
// It then issues a callback to pass this information to the device manager which
|
2017-08-22 22:03:47 +00:00
|
|
|
// will adjust the resource available information accordingly.
|
2017-11-14 21:06:07 +00:00
|
|
|
func (e *endpointImpl) run() {
|
2017-10-26 08:36:16 +00:00
|
|
|
stream, err := e.client.ListAndWatch(context.Background(), &pluginapi.Empty{})
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf(errListAndWatch, e.resourceName, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-08 04:14:15 +00:00
|
|
|
devices := make(map[string]pluginapi.Device)
|
2017-08-11 00:14:34 +00:00
|
|
|
|
|
|
|
e.mutex.Lock()
|
|
|
|
for _, d := range e.devices {
|
2017-09-08 04:14:15 +00:00
|
|
|
devices[d.ID] = d
|
2017-08-11 00:14:34 +00:00
|
|
|
}
|
|
|
|
e.mutex.Unlock()
|
|
|
|
|
|
|
|
for {
|
|
|
|
response, err := stream.Recv()
|
|
|
|
if err != nil {
|
2017-08-22 22:03:47 +00:00
|
|
|
glog.Errorf(errListAndWatch, e.resourceName, err)
|
2017-08-11 00:14:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
devs := response.Devices
|
|
|
|
glog.V(2).Infof("State pushed for device plugin %s", e.resourceName)
|
|
|
|
|
|
|
|
newDevs := make(map[string]*pluginapi.Device)
|
2017-09-08 04:14:15 +00:00
|
|
|
var added, updated []pluginapi.Device
|
2017-08-11 00:14:34 +00:00
|
|
|
|
|
|
|
for _, d := range devs {
|
|
|
|
dOld, ok := devices[d.ID]
|
|
|
|
newDevs[d.ID] = d
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
glog.V(2).Infof("New device for Endpoint %s: %v", e.resourceName, d)
|
|
|
|
|
2017-09-08 04:14:15 +00:00
|
|
|
devices[d.ID] = *d
|
|
|
|
added = append(added, *d)
|
2017-08-11 00:14:34 +00:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Health == dOld.Health {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Health == pluginapi.Unhealthy {
|
|
|
|
glog.Errorf("Device %s is now Unhealthy", d.ID)
|
|
|
|
} else if d.Health == pluginapi.Healthy {
|
|
|
|
glog.V(2).Infof("Device %s is now Healthy", d.ID)
|
|
|
|
}
|
|
|
|
|
2017-09-08 04:14:15 +00:00
|
|
|
devices[d.ID] = *d
|
|
|
|
updated = append(updated, *d)
|
2017-08-11 00:14:34 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 04:14:15 +00:00
|
|
|
var deleted []pluginapi.Device
|
2017-08-11 00:14:34 +00:00
|
|
|
for id, d := range devices {
|
|
|
|
if _, ok := newDevs[id]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.Errorf("Device %s was deleted", d.ID)
|
|
|
|
|
2017-09-08 04:14:15 +00:00
|
|
|
deleted = append(deleted, d)
|
2017-08-11 00:14:34 +00:00
|
|
|
delete(devices, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
e.mutex.Lock()
|
2017-12-23 08:02:33 +00:00
|
|
|
// NOTE: Return a copy of 'devices' instead of returning a direct reference to local 'devices'
|
|
|
|
e.devices = make(map[string]pluginapi.Device)
|
|
|
|
for _, d := range devices {
|
|
|
|
e.devices[d.ID] = d
|
|
|
|
}
|
2017-08-11 00:14:34 +00:00
|
|
|
e.mutex.Unlock()
|
|
|
|
|
|
|
|
e.callback(e.resourceName, added, updated, deleted)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 22:03:47 +00:00
|
|
|
// allocate issues Allocate gRPC call to the device plugin.
|
2017-11-14 21:06:07 +00:00
|
|
|
func (e *endpointImpl) allocate(devs []string) (*pluginapi.AllocateResponse, error) {
|
2017-08-11 00:14:34 +00:00
|
|
|
return e.client.Allocate(context.Background(), &pluginapi.AllocateRequest{
|
2017-08-22 22:03:47 +00:00
|
|
|
DevicesIDs: devs,
|
2017-08-11 00:14:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:06:07 +00:00
|
|
|
func (e *endpointImpl) stop() {
|
2017-09-18 20:29:47 +00:00
|
|
|
e.clientConn.Close()
|
2017-08-11 00:14:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 22:03:47 +00:00
|
|
|
// dial establishes the gRPC communication with the registered device plugin.
|
2017-09-18 20:29:47 +00:00
|
|
|
func dial(unixSocketPath string) (pluginapi.DevicePluginClient, *grpc.ClientConn, error) {
|
2017-08-11 00:14:34 +00:00
|
|
|
c, err := grpc.Dial(unixSocketPath, grpc.WithInsecure(),
|
|
|
|
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
|
|
|
|
return net.DialTimeout("unix", addr, timeout)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2017-09-18 20:29:47 +00:00
|
|
|
return nil, nil, fmt.Errorf(errFailedToDialDevicePlugin+" %v", err)
|
2017-08-11 00:14:34 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 20:29:47 +00:00
|
|
|
return pluginapi.NewDevicePluginClient(c), c, nil
|
2017-08-11 00:14:34 +00:00
|
|
|
}
|