mirror of
https://github.com/intel/intel-device-plugins-for-kubernetes.git
synced 2025-06-03 03:59:37 +00:00
linter: enable staticcheck
This commit is contained in:
parent
e3f29e0214
commit
7ff08ee874
@ -29,6 +29,7 @@ linters:
|
|||||||
- nakedret
|
- nakedret
|
||||||
- nolintlint
|
- nolintlint
|
||||||
- rowserrcheck
|
- rowserrcheck
|
||||||
|
- staticcheck
|
||||||
- structcheck
|
- structcheck
|
||||||
- stylecheck
|
- stylecheck
|
||||||
- typecheck
|
- typecheck
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
// +build kerneldrv
|
// +build kerneldrv
|
||||||
|
|
||||||
|
// Package kerneldrv populates a device tree with QAT devices using the kernel QAT driver.
|
||||||
package kerneldrv
|
package kerneldrv
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -199,7 +200,7 @@ func (dp *DevicePlugin) parseConfigs(devices []device) (map[string]section, erro
|
|||||||
devNum++
|
devNum++
|
||||||
|
|
||||||
for _, section := range config.Sections() {
|
for _, section := range config.Sections() {
|
||||||
if section.Name() == "GENERAL" || section.Name() == "KERNEL" || section.Name() == "KERNEL_QAT" || section.Name() == ini.DEFAULT_SECTION {
|
if section.Name() == "GENERAL" || section.Name() == "KERNEL" || section.Name() == "KERNEL_QAT" || section.Name() == ini.DefaultSection {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
klog.V(4).Info(section.Name())
|
klog.V(4).Info(section.Name())
|
||||||
|
@ -25,7 +25,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.Set("v", "4")
|
_ = flag.Set("v", "4")
|
||||||
}
|
}
|
||||||
|
|
||||||
type testCase struct {
|
type testCase struct {
|
||||||
@ -78,7 +78,7 @@ func TestScan(t *testing.T) {
|
|||||||
testPlugin := newDevicePlugin(tc, vendorID, productIDs, 10)
|
testPlugin := newDevicePlugin(tc, vendorID, productIDs, 10)
|
||||||
|
|
||||||
if testPlugin == nil {
|
if testPlugin == nil {
|
||||||
t.Error("vpu plugin test failed with newDevicePlugin().")
|
t.Fatal("vpu plugin test failed with newDevicePlugin().")
|
||||||
}
|
}
|
||||||
|
|
||||||
fN.scanDone = testPlugin.scanDone
|
fN.scanDone = testPlugin.scanDone
|
||||||
@ -92,12 +92,12 @@ func TestScan(t *testing.T) {
|
|||||||
klog.V(4).Infof("tree len is %d", len(fN.tree[deviceType]))
|
klog.V(4).Infof("tree len is %d", len(fN.tree[deviceType]))
|
||||||
|
|
||||||
//remove the hddl_service.sock and test with no hddl socket case
|
//remove the hddl_service.sock and test with no hddl socket case
|
||||||
f.Close()
|
_ = f.Close()
|
||||||
os.Remove("/var/tmp/hddl_service.sock")
|
_ = os.Remove("/var/tmp/hddl_service.sock")
|
||||||
testPlugin = newDevicePlugin(tc, vendorID, productIDs, 10)
|
testPlugin = newDevicePlugin(tc, vendorID, productIDs, 10)
|
||||||
|
|
||||||
if testPlugin == nil {
|
if testPlugin == nil {
|
||||||
t.Error("vpu plugin test failed with newDevicePlugin() in no hddl_service.sock case.")
|
t.Fatal("vpu plugin test failed with newDevicePlugin() in no hddl_service.sock case.")
|
||||||
}
|
}
|
||||||
|
|
||||||
fN.scanDone = testPlugin.scanDone
|
fN.scanDone = testPlugin.scanDone
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package deviceplugin provides API for reporting available devices to kubelet.
|
||||||
package deviceplugin
|
package deviceplugin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -90,7 +90,7 @@ func (srv *server) sendDevices(stream pluginapi.DevicePlugin_ListAndWatchServer)
|
|||||||
}
|
}
|
||||||
klog.V(4).Info("Sending to kubelet", resp.Devices)
|
klog.V(4).Info("Sending to kubelet", resp.Devices)
|
||||||
if err := stream.Send(resp); err != nil {
|
if err := stream.Send(resp); err != nil {
|
||||||
srv.Stop()
|
_ = srv.Stop()
|
||||||
return errors.Wrapf(err, "Cannot update device list")
|
return errors.Wrapf(err, "Cannot update device list")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,7 +204,8 @@ func (srv *server) setupAndServe(namespace string, devicePluginPath string, kube
|
|||||||
if err := waitForServer(pluginSocket, time.Second); err == nil {
|
if err := waitForServer(pluginSocket, time.Second); err == nil {
|
||||||
return errors.Errorf("Socket %s is already in use", pluginSocket)
|
return errors.Errorf("Socket %s is already in use", pluginSocket)
|
||||||
}
|
}
|
||||||
os.Remove(pluginSocket)
|
// We don't care if the plugin's socket file doesn't exist.
|
||||||
|
_ = os.Remove(pluginSocket)
|
||||||
|
|
||||||
lis, err := net.Listen("unix", pluginSocket)
|
lis, err := net.Listen("unix", pluginSocket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -217,7 +218,9 @@ func (srv *server) setupAndServe(namespace string, devicePluginPath string, kube
|
|||||||
// Starts device plugin service.
|
// Starts device plugin service.
|
||||||
go func() {
|
go func() {
|
||||||
klog.V(1).Infof("Start server for %s at: %s", srv.devType, pluginSocket)
|
klog.V(1).Infof("Start server for %s at: %s", srv.devType, pluginSocket)
|
||||||
srv.grpcServer.Serve(lis)
|
if serveErr := srv.grpcServer.Serve(lis); serveErr != nil {
|
||||||
|
klog.Errorf("unable to start gRPC server: %+v", serveErr)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Wait for the server to start
|
// Wait for the server to start
|
||||||
@ -278,9 +281,10 @@ func watchFile(file string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func registerWithKubelet(kubeletSocket, pluginEndPoint, resourceName string, options *pluginapi.DevicePluginOptions) error {
|
func registerWithKubelet(kubeletSocket, pluginEndPoint, resourceName string, options *pluginapi.DevicePluginOptions) error {
|
||||||
conn, err := grpc.Dial(kubeletSocket, grpc.WithInsecure(),
|
ctx := context.Background()
|
||||||
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
|
conn, err := grpc.DialContext(ctx, kubeletSocket, grpc.WithInsecure(),
|
||||||
return net.DialTimeout("unix", addr, timeout)
|
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
|
||||||
|
return (&net.Dialer{}).DialContext(ctx, "unix", addr)
|
||||||
}))
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Cannot connect to kubelet service")
|
return errors.Wrap(err, "Cannot connect to kubelet service")
|
||||||
@ -294,7 +298,7 @@ func registerWithKubelet(kubeletSocket, pluginEndPoint, resourceName string, opt
|
|||||||
Options: options,
|
Options: options,
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = client.Register(context.Background(), reqt)
|
_, err = client.Register(ctx, reqt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Cannot register to kubelet service")
|
return errors.Wrap(err, "Cannot register to kubelet service")
|
||||||
}
|
}
|
||||||
@ -308,12 +312,12 @@ func waitForServer(socket string, timeout time.Duration) error {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
conn, err := grpc.DialContext(ctx, socket, grpc.WithInsecure(), grpc.WithBlock(),
|
conn, err := grpc.DialContext(ctx, socket, grpc.WithInsecure(), grpc.WithBlock(),
|
||||||
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
|
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
|
||||||
return net.DialTimeout("unix", addr, timeout)
|
return (&net.Dialer{}).DialContext(ctx, "unix", addr)
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
if conn != nil {
|
if conn != nil {
|
||||||
conn.Close()
|
_ = conn.Close()
|
||||||
}
|
}
|
||||||
return errors.Wrapf(err, "Failed dial context at %s", socket)
|
return errors.Wrapf(err, "Failed dial context at %s", socket)
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ type kubeletStub struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.Set("v", "4") //Enable debug output
|
_ = flag.Set("v", "4") //Enable debug output
|
||||||
}
|
}
|
||||||
|
|
||||||
// newKubeletStub returns an initialized kubeletStub for testing purpose.
|
// newKubeletStub returns an initialized kubeletStub for testing purpose.
|
||||||
@ -71,7 +71,7 @@ func (k *kubeletStub) Register(ctx context.Context, r *pluginapi.RegisterRequest
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (k *kubeletStub) start() error {
|
func (k *kubeletStub) start() error {
|
||||||
os.Remove(k.socket)
|
_ = os.Remove(k.socket)
|
||||||
s, err := net.Listen("unix", k.socket)
|
s, err := net.Listen("unix", k.socket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Can't listen at the socket")
|
return errors.Wrap(err, "Can't listen at the socket")
|
||||||
@ -83,9 +83,7 @@ func (k *kubeletStub) start() error {
|
|||||||
go k.server.Serve(s)
|
go k.server.Serve(s)
|
||||||
|
|
||||||
// Wait till the grpcServer is ready to serve services.
|
// Wait till the grpcServer is ready to serve services.
|
||||||
waitForServer(k.socket, 10*time.Second)
|
return waitForServer(k.socket, 10*time.Second)
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRegisterWithKublet(t *testing.T) {
|
func TestRegisterWithKublet(t *testing.T) {
|
||||||
@ -114,7 +112,9 @@ func TestSetupAndServe(t *testing.T) {
|
|||||||
var pEndpoint string
|
var pEndpoint string
|
||||||
|
|
||||||
kubelet := newKubeletStub(kubeletSocket)
|
kubelet := newKubeletStub(kubeletSocket)
|
||||||
kubelet.start()
|
if err := kubelet.start(); err != nil {
|
||||||
|
t.Fatalf("unable to start kubelet stub: %+v", err)
|
||||||
|
}
|
||||||
defer kubelet.server.Stop()
|
defer kubelet.server.Stop()
|
||||||
|
|
||||||
srv := &server{
|
srv := &server{
|
||||||
@ -152,16 +152,17 @@ func TestSetupAndServe(t *testing.T) {
|
|||||||
t.Fatalf("Server was able to start on occupied socket %s: %+v", pluginSocket, err)
|
t.Fatalf("Server was able to start on occupied socket %s: %+v", pluginSocket, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := grpc.Dial(pluginSocket, grpc.WithInsecure(),
|
ctx := context.Background()
|
||||||
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
|
conn, err := grpc.DialContext(ctx, pluginSocket, grpc.WithInsecure(),
|
||||||
return net.DialTimeout("unix", addr, timeout)
|
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
|
||||||
|
return (&net.Dialer{}).DialContext(ctx, "unix", addr)
|
||||||
}))
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to get connection: %+v", err)
|
t.Fatalf("Failed to get connection: %+v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
client := pluginapi.NewDevicePluginClient(conn)
|
client := pluginapi.NewDevicePluginClient(conn)
|
||||||
_, err = client.Allocate(context.Background(), &pluginapi.AllocateRequest{
|
_, err = client.Allocate(ctx, &pluginapi.AllocateRequest{
|
||||||
ContainerRequests: []*pluginapi.ContainerAllocateRequest{
|
ContainerRequests: []*pluginapi.ContainerAllocateRequest{
|
||||||
{
|
{
|
||||||
DevicesIDs: []string{"dev1", "dev2"},
|
DevicesIDs: []string{"dev1", "dev2"},
|
||||||
@ -171,7 +172,7 @@ func TestSetupAndServe(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to allocate device dev1: %+v", err)
|
t.Errorf("Failed to allocate device dev1: %+v", err)
|
||||||
}
|
}
|
||||||
conn.Close()
|
_ = conn.Close()
|
||||||
|
|
||||||
// Check if plugins re-registers after its socket has been removed
|
// Check if plugins re-registers after its socket has been removed
|
||||||
kubelet.Lock()
|
kubelet.Lock()
|
||||||
@ -180,7 +181,7 @@ func TestSetupAndServe(t *testing.T) {
|
|||||||
if pEndpoint == "" {
|
if pEndpoint == "" {
|
||||||
t.Fatal("After successful Allocate() pluginEndpoint is empty")
|
t.Fatal("After successful Allocate() pluginEndpoint is empty")
|
||||||
}
|
}
|
||||||
os.Remove(path.Join(devicePluginPath, pEndpoint))
|
_ = os.Remove(path.Join(devicePluginPath, pEndpoint))
|
||||||
for {
|
for {
|
||||||
kubelet.Lock()
|
kubelet.Lock()
|
||||||
pEndpoint = kubelet.pluginEndpoint
|
pEndpoint = kubelet.pluginEndpoint
|
||||||
@ -194,16 +195,16 @@ func TestSetupAndServe(t *testing.T) {
|
|||||||
klog.V(1).Info("No plugin socket. Waiting...")
|
klog.V(1).Info("No plugin socket. Waiting...")
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
conn, err = grpc.Dial(pluginSocket, grpc.WithInsecure(),
|
conn, err = grpc.DialContext(ctx, pluginSocket, grpc.WithInsecure(),
|
||||||
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
|
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
|
||||||
return net.DialTimeout("unix", addr, timeout)
|
return (&net.Dialer{}).DialContext(ctx, "unix", addr)
|
||||||
}))
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to get connection: %+v", err)
|
t.Fatalf("Failed to get connection: %+v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
client = pluginapi.NewDevicePluginClient(conn)
|
client = pluginapi.NewDevicePluginClient(conn)
|
||||||
_, err = client.Allocate(context.Background(), &pluginapi.AllocateRequest{
|
_, err = client.Allocate(ctx, &pluginapi.AllocateRequest{
|
||||||
ContainerRequests: []*pluginapi.ContainerAllocateRequest{
|
ContainerRequests: []*pluginapi.ContainerAllocateRequest{
|
||||||
{
|
{
|
||||||
DevicesIDs: []string{"dev1", "dev2"},
|
DevicesIDs: []string{"dev1", "dev2"},
|
||||||
@ -213,7 +214,7 @@ func TestSetupAndServe(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to allocate device dev1: %+v", err)
|
t.Errorf("Failed to allocate device dev1: %+v", err)
|
||||||
}
|
}
|
||||||
conn.Close()
|
_ = conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStop(t *testing.T) {
|
func TestStop(t *testing.T) {
|
||||||
@ -337,7 +338,7 @@ func TestAllocate(t *testing.T) {
|
|||||||
for _, tt := range tcases {
|
for _, tt := range tcases {
|
||||||
srv.devices = tt.devices
|
srv.devices = tt.devices
|
||||||
srv.postAllocate = tt.postAllocate
|
srv.postAllocate = tt.postAllocate
|
||||||
resp, err := srv.Allocate(nil, rqt)
|
resp, err := srv.Allocate(context.Background(), rqt)
|
||||||
|
|
||||||
if tt.expectedErr && err == nil {
|
if tt.expectedErr && err == nil {
|
||||||
t.Errorf("Test case '%s': no error returned", tt.name)
|
t.Errorf("Test case '%s': no error returned", tt.name)
|
||||||
@ -481,7 +482,9 @@ func TestListAndWatch(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetDevicePluginOptions(t *testing.T) {
|
func TestGetDevicePluginOptions(t *testing.T) {
|
||||||
srv := &server{}
|
srv := &server{}
|
||||||
srv.GetDevicePluginOptions(nil, nil)
|
if _, err := srv.GetDevicePluginOptions(context.Background(), nil); err != nil {
|
||||||
|
t.Errorf("unexpected error: %+v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPreStartContainer(t *testing.T) {
|
func TestPreStartContainer(t *testing.T) {
|
||||||
@ -506,7 +509,7 @@ func TestPreStartContainer(t *testing.T) {
|
|||||||
srv := &server{
|
srv := &server{
|
||||||
preStartContainer: tc.preStartContainer,
|
preStartContainer: tc.preStartContainer,
|
||||||
}
|
}
|
||||||
_, err := srv.PreStartContainer(nil, nil)
|
_, err := srv.PreStartContainer(context.Background(), nil)
|
||||||
if !tc.expectedError && err != nil {
|
if !tc.expectedError && err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
} else if tc.expectedError && err == nil {
|
} else if tc.expectedError && err == nil {
|
||||||
|
@ -52,11 +52,14 @@ type Hint struct {
|
|||||||
type Hints map[string]Hint
|
type Hints map[string]Hint
|
||||||
|
|
||||||
func getDevicesFromVirtual(realDevPath string) (devs []string, err error) {
|
func getDevicesFromVirtual(realDevPath string) (devs []string, err error) {
|
||||||
if !filepath.HasPrefix(realDevPath, "/sys/devices/virtual") {
|
relPath, err := filepath.Rel("/sys/devices/virtual", realDevPath)
|
||||||
return nil, fmt.Errorf("%s is not a virtual device", realDevPath)
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "unable to find relative path")
|
||||||
}
|
}
|
||||||
|
|
||||||
relPath, _ := filepath.Rel("/sys/devices/virtual", realDevPath)
|
if strings.HasPrefix(relPath, "..") {
|
||||||
|
return nil, errors.Errorf("%s is not a virtual device", realDevPath)
|
||||||
|
}
|
||||||
|
|
||||||
dir, file := filepath.Split(relPath)
|
dir, file := filepath.Split(relPath)
|
||||||
switch dir {
|
switch dir {
|
||||||
|
@ -188,6 +188,12 @@ func TestGetDevicesFromVirtual(t *testing.T) {
|
|||||||
output: nil,
|
output: nil,
|
||||||
expectedErr: true,
|
expectedErr: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "garbage",
|
||||||
|
input: "./sys/devices/virtual/vfio/42",
|
||||||
|
output: nil,
|
||||||
|
expectedErr: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
|
Loading…
Reference in New Issue
Block a user