Change:
See comment for details:
/sig node
/area kubelet
/assign
The Kubernetes project currently lacks enough contributors to adequately respond to all issues. This bot triages un-triaged issues according to the following rules: After 90d of inactivity, is applied After 30d of inactivity since was applied, is applied After 30d of inactivity since was applied, the issue is closed You can: Mark this issue as fresh with Close this issue with Offer to help out with [Issue Triage][1] Please send feedback to sig-contributor-experience at . /lifecycle stale [1]:
/remove-lifecycle stale
/triage accepted

Discussion:
import ( "context" <del> "errors" </del> "fmt" <del> "io" "net" </del> "time" "google.golang.org/grpc" grpccodes "google.golang.org/grpc/codes" <del> "google.golang.org/grpc/credentials/insecure" </del> grpcstatus "google.golang.org/grpc/status" "k8s.io/klog/v2"

const PluginClientTimeout = 45 * time.Second <del> // Strongly typed address. type draAddr string </del> // draPluginClient encapsulates all dra plugin methods. type draPluginClient struct { <del> pluginName        string addr              draAddr nodeClientCreator nodeClientCreator } var _ drapb.NodeClient = &draPluginClient{} type nodeClientCreator func(addr draAddr) ( nodeClient drapb.NodeClient, nodeClientOld drapbv1alpha2.NodeClient, closer io.Closer, err error, ) // newNodeClient creates a new NodeClient with the internally used gRPC // connection set up. It also returns a closer which must be called to close // the gRPC connection when the NodeClient is not used anymore. // This is the default implementation for the nodeClientCreator, used in // newDRAPluginClient. func newNodeClient(addr draAddr) (nodeClient drapb.NodeClient, nodeClientOld drapbv1alpha2.NodeClient, closer io.Closer, err error) { var conn *grpc.ClientConn conn, err = newGrpcConn(addr) if err != nil { return nil, nil, nil, err } return drapb.NewNodeClient(conn), drapbv1alpha2.NewNodeClient(conn), conn, nil </del> <ins> pluginName string plugin     *Plugin </ins> } func NewDRAPluginClient(pluginName string) (drapb.NodeClient, error) {

} return &draPluginClient{ <del> pluginName:        pluginName, addr:              draAddr(existingPlugin.endpoint), nodeClientCreator: newNodeClient, </del> <ins> pluginName: pluginName, plugin:     existingPlugin, </ins> }, nil }

logger.V(4).Info(log("calling NodePrepareResources rpc"), "request", req) defer logger.V(4).Info(log("done calling NodePrepareResources rpc"), "response", resp, "err", err) <del> if r.nodeClientCreator == nil { return nil, errors.New("failed to call NodePrepareResources. nodeClientCreator is nil") } nodeClient, nodeClientOld, closer, err := r.nodeClientCreator(r.addr) </del> <ins> conn, err := r.plugin.getOrCreateGRPCConn() </ins> if err != nil { return nil, err } <del> defer closer.Close() </del> <ins> nodeClient := drapb.NewNodeClient(conn) nodeClientOld := drapbv1alpha2.NewNodeClient(conn) </ins> ctx, cancel := context.WithTimeout(ctx, PluginClientTimeout) defer cancel()

logger.V(4).Info(log("calling NodeUnprepareResource rpc"), "request", req) defer logger.V(4).Info(log("done calling NodeUnprepareResources rpc"), "response", resp, "err", err) <del> if r.nodeClientCreator == nil { return nil, errors.New("failed to call NodeUnprepareResources. nodeClientCreator is nil") } nodeClient, nodeClientOld, closer, err := r.nodeClientCreator(r.addr) </del> <ins> conn, err := r.plugin.getOrCreateGRPCConn() </ins> if err != nil { return nil, err } <del> defer closer.Close() </del> <ins> nodeClient := drapb.NewNodeClient(conn) nodeClientOld := drapbv1alpha2.NewNodeClient(conn) </ins> ctx, cancel := context.WithTimeout(ctx, PluginClientTimeout) defer cancel()

return } <del> func newGrpcConn(addr draAddr) (*grpc.ClientConn, error) { network := "unix" klog.V(4).InfoS(log("creating new gRPC connection"), "protocol", network, "endpoint", addr) return grpc.Dial( string(addr), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(func(ctx context.Context, target string) (net.Conn, error) { return (&net.Dialer{}).DialContext(ctx, network, target) }), ) } </del>

<ins> /* 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" "net" "os" "path/filepath" "sync" "testing" "google.golang.org/grpc" drapbv1 "k8s.io/kubelet/pkg/apis/dra/v1alpha3" ) type fakeGRPCServer struct { drapbv1.UnimplementedNodeServer } func (f *fakeGRPCServer) NodePrepareResource(ctx context.Context, in *drapbv1.NodePrepareResourcesRequest) (*drapbv1.NodePrepareResourcesResponse, error) { return &drapbv1.NodePrepareResourcesResponse{Claims: map[string]*drapbv1.NodePrepareResourceResponse{"dummy": {CDIDevices: []string{"dummy"}}}}, nil } func (f *fakeGRPCServer) NodeUnprepareResource(ctx context.Context, in *drapbv1.NodeUnprepareResourcesRequest) (*drapbv1.NodeUnprepareResourcesResponse, error) { return &drapbv1.NodeUnprepareResourcesResponse{}, nil } type tearDown func() func setupFakeGRPCServer() (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() fakeGRPCServer := &fakeGRPCServer{} drapbv1.RegisterNodeServer(s, fakeGRPCServer) go func() { go s.Serve(listener) <-closeCh s.GracefulStop() }() return addr, teardown, nil } func TestGRPCConnIsReused(t *testing.T) { addr, teardown, err := setupFakeGRPCServer() if err != nil { t.Fatal(err) } defer teardown() reusedConns := make(map[*grpc.ClientConn]int) wg := sync.WaitGroup{} m := sync.Mutex{} plugin := &Plugin{ endpoint: addr, } conn, err := plugin.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.Set("dummy-plugin", 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 := &drapbv1.NodePrepareResourcesRequest{ Claims: []*drapbv1.Claim{ { Namespace:      "dummy-namespace", Uid:            "dummy-uid", Name:           "dummy-claim", ResourceHandle: "dummy-resource", }, }, } client.NodePrepareResources(context.TODO(), req) client.(*draPluginClient).plugin.Lock() conn := client.(*draPluginClient).plugin.conn client.(*draPluginClient).plugin.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) } draPlugins.Delete("dummy-plugin") } </ins>

// Storing endpoint of newly registered DRA Plugin into the map, where plugin name will be the key // all other DRA components will be able to get the actual socket of DRA plugins by its name. draPlugins.Set(pluginName, &Plugin{ <ins> conn:                    nil, </ins> endpoint:                endpoint, highestSupportedVersion: highestSupportedVersion, })

package plugin import ( <ins> "context" "errors" "net" </ins> "sync" <ins> "time" "google.golang.org/grpc" "google.golang.org/grpc/connectivity" "google.golang.org/grpc/credentials/insecure" </ins> utilversion "k8s.io/apimachinery/pkg/util/version" <ins> "k8s.io/klog/v2" </ins> ) // Plugin is a description of a DRA Plugin, defined by an endpoint // and the highest DRA version supported. type Plugin struct { <ins> sync.RWMutex conn                    *grpc.ClientConn </ins> endpoint                string highestSupportedVersion *utilversion.Version } <ins> func (p *Plugin) getOrCreateGRPCConn() (*grpc.ClientConn, error) { p.Lock() defer p.Unlock() if p.conn != nil { return p.conn, nil } network := "unix" klog.V(4).InfoS(log("creating new gRPC connection"), "protocol", network, "endpoint", p.endpoint) conn, err := grpc.Dial( p.endpoint, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(func(ctx context.Context, target string) (net.Conn, error) { return (&net.Dialer{}).DialContext(ctx, network, target) }), ) if err != nil { return nil, err } ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() if ok := conn.WaitForStateChange(ctx, connectivity.Connecting); !ok { return nil, errors.New("timed out waiting for gRPC connection to be ready") } p.conn = conn return p.conn, nil } </ins> // PluginsStore holds a list of DRA Plugins. type PluginsStore struct { sync.RWMutex