mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-12-07 16:45:35 +00:00
This is a first step towards making kubelet independent of the resource.k8s.io API versioning because it now doesn't need to copy structs defined by that API from the driver to the API server. The next step is removing the other direction (reading ResourceClaim status and passing the resource handle to drivers). The drivers must get deployed so that they have their own connection to the API server. Securing at least the writes via a validating admission policy should be possible. As before, the kubelet removes all ResourceSlices for its node at startup, then DRA drivers recreate them if (and only if) they start up again. This ensures that there are no orphaned ResourceSlices when a driver gets removed while the kubelet was down. While at it, logging gets cleaned up and updated to use structured, contextual logging as much as possible. gRPC requests and streams now use a shared, per-process request ID and streams also get logged.
271 lines
6.2 KiB
Go
271 lines
6.2 KiB
Go
/*
|
|
Copyright 2023 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 plugin
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"google.golang.org/grpc"
|
|
drapbv1alpha3 "k8s.io/kubelet/pkg/apis/dra/v1alpha3"
|
|
"k8s.io/kubernetes/test/utils/ktesting"
|
|
)
|
|
|
|
const (
|
|
v1alpha3Version = "v1alpha3"
|
|
)
|
|
|
|
type fakeV1alpha3GRPCServer struct {
|
|
drapbv1alpha3.UnimplementedNodeServer
|
|
}
|
|
|
|
var _ drapbv1alpha3.NodeServer = &fakeV1alpha3GRPCServer{}
|
|
|
|
func (f *fakeV1alpha3GRPCServer) NodePrepareResources(ctx context.Context, in *drapbv1alpha3.NodePrepareResourcesRequest) (*drapbv1alpha3.NodePrepareResourcesResponse, error) {
|
|
return &drapbv1alpha3.NodePrepareResourcesResponse{Claims: map[string]*drapbv1alpha3.NodePrepareResourceResponse{"dummy": {CDIDevices: []string{"dummy"}}}}, nil
|
|
}
|
|
|
|
func (f *fakeV1alpha3GRPCServer) NodeUnprepareResources(ctx context.Context, in *drapbv1alpha3.NodeUnprepareResourcesRequest) (*drapbv1alpha3.NodeUnprepareResourcesResponse, error) {
|
|
|
|
return &drapbv1alpha3.NodeUnprepareResourcesResponse{}, nil
|
|
}
|
|
|
|
type tearDown func()
|
|
|
|
func setupFakeGRPCServer(version string) (string, tearDown, error) {
|
|
p, err := os.MkdirTemp("", "dra_plugin")
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
|
|
closeCh := make(chan struct{})
|
|
addr := filepath.Join(p, "server.sock")
|
|
teardown := func() {
|
|
close(closeCh)
|
|
os.RemoveAll(addr)
|
|
}
|
|
|
|
listener, err := net.Listen("unix", addr)
|
|
if err != nil {
|
|
teardown()
|
|
return "", nil, err
|
|
}
|
|
|
|
s := grpc.NewServer()
|
|
switch version {
|
|
case v1alpha3Version:
|
|
fakeGRPCServer := &fakeV1alpha3GRPCServer{}
|
|
drapbv1alpha3.RegisterNodeServer(s, fakeGRPCServer)
|
|
default:
|
|
return "", nil, fmt.Errorf("unsupported version: %s", version)
|
|
}
|
|
|
|
go func() {
|
|
go s.Serve(listener)
|
|
<-closeCh
|
|
s.GracefulStop()
|
|
}()
|
|
|
|
return addr, teardown, nil
|
|
}
|
|
|
|
func TestGRPCConnIsReused(t *testing.T) {
|
|
ctx := ktesting.Init(t)
|
|
addr, teardown, err := setupFakeGRPCServer(v1alpha3Version)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer teardown()
|
|
|
|
reusedConns := make(map[*grpc.ClientConn]int)
|
|
wg := sync.WaitGroup{}
|
|
m := sync.Mutex{}
|
|
|
|
p := &Plugin{
|
|
backgroundCtx: ctx,
|
|
endpoint: addr,
|
|
}
|
|
|
|
conn, err := p.getOrCreateGRPCConn()
|
|
defer func() {
|
|
err := conn.Close()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// ensure the plugin we are using is registered
|
|
draPlugins.add("dummy-plugin", p)
|
|
defer draPlugins.delete("dummy-plugin")
|
|
|
|
// we call `NodePrepareResource` 2 times and check whether a new connection is created or the same is reused
|
|
for i := 0; i < 2; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
client, err := NewDRAPluginClient("dummy-plugin")
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
req := &drapbv1alpha3.NodePrepareResourcesRequest{
|
|
Claims: []*drapbv1alpha3.Claim{
|
|
{
|
|
Namespace: "dummy-namespace",
|
|
Uid: "dummy-uid",
|
|
Name: "dummy-claim",
|
|
ResourceHandle: "dummy-resource",
|
|
},
|
|
},
|
|
}
|
|
client.NodePrepareResources(context.TODO(), req)
|
|
|
|
client.mutex.Lock()
|
|
conn := client.conn
|
|
client.mutex.Unlock()
|
|
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
reusedConns[conn]++
|
|
}()
|
|
}
|
|
|
|
wg.Wait()
|
|
// We should have only one entry otherwise it means another gRPC connection has been created
|
|
if len(reusedConns) != 1 {
|
|
t.Errorf("expected length to be 1 but got %d", len(reusedConns))
|
|
}
|
|
if counter, ok := reusedConns[conn]; ok && counter != 2 {
|
|
t.Errorf("expected counter to be 2 but got %d", counter)
|
|
}
|
|
}
|
|
|
|
func TestNewDRAPluginClient(t *testing.T) {
|
|
for _, test := range []struct {
|
|
description string
|
|
setup func(string) tearDown
|
|
pluginName string
|
|
shouldError bool
|
|
}{
|
|
{
|
|
description: "plugin name is empty",
|
|
setup: func(_ string) tearDown {
|
|
return func() {}
|
|
},
|
|
pluginName: "",
|
|
shouldError: true,
|
|
},
|
|
{
|
|
description: "plugin name not found in the list",
|
|
setup: func(_ string) tearDown {
|
|
return func() {}
|
|
},
|
|
pluginName: "plugin-name-not-found-in-the-list",
|
|
shouldError: true,
|
|
},
|
|
{
|
|
description: "plugin exists",
|
|
setup: func(name string) tearDown {
|
|
draPlugins.add(name, &Plugin{})
|
|
return func() {
|
|
draPlugins.delete(name)
|
|
}
|
|
},
|
|
pluginName: "dummy-plugin",
|
|
},
|
|
} {
|
|
t.Run(test.description, func(t *testing.T) {
|
|
teardown := test.setup(test.pluginName)
|
|
defer teardown()
|
|
|
|
client, err := NewDRAPluginClient(test.pluginName)
|
|
if test.shouldError {
|
|
assert.Nil(t, client)
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NotNil(t, client)
|
|
assert.Nil(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNodeUnprepareResources(t *testing.T) {
|
|
for _, test := range []struct {
|
|
description string
|
|
serverSetup func(string) (string, tearDown, error)
|
|
serverVersion string
|
|
request *drapbv1alpha3.NodeUnprepareResourcesRequest
|
|
}{
|
|
{
|
|
description: "server supports v1alpha3",
|
|
serverSetup: setupFakeGRPCServer,
|
|
serverVersion: v1alpha3Version,
|
|
request: &drapbv1alpha3.NodeUnprepareResourcesRequest{},
|
|
},
|
|
} {
|
|
t.Run(test.description, func(t *testing.T) {
|
|
ctx := ktesting.Init(t)
|
|
addr, teardown, err := setupFakeGRPCServer(test.serverVersion)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer teardown()
|
|
|
|
p := &Plugin{
|
|
backgroundCtx: ctx,
|
|
endpoint: addr,
|
|
clientTimeout: PluginClientTimeout,
|
|
}
|
|
|
|
conn, err := p.getOrCreateGRPCConn()
|
|
defer func() {
|
|
err := conn.Close()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
draPlugins.add("dummy-plugin", p)
|
|
defer draPlugins.delete("dummy-plugin")
|
|
|
|
client, err := NewDRAPluginClient("dummy-plugin")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, err = client.NodeUnprepareResources(context.TODO(), test.request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
}
|
|
}
|