k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/kubelet/cm/dra/plugin/client.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package plugin
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"time"
    23  
    24  	"google.golang.org/grpc"
    25  
    26  	"k8s.io/klog/v2"
    27  	drapb "k8s.io/kubelet/pkg/apis/dra/v1alpha3"
    28  )
    29  
    30  const PluginClientTimeout = 45 * time.Second
    31  
    32  func NewDRAPluginClient(pluginName string) (drapb.NodeClient, error) {
    33  	if pluginName == "" {
    34  		return nil, fmt.Errorf("plugin name is empty")
    35  	}
    36  
    37  	existingPlugin := draPlugins.get(pluginName)
    38  	if existingPlugin == nil {
    39  		return nil, fmt.Errorf("plugin name %s not found in the list of registered DRA plugins", pluginName)
    40  	}
    41  
    42  	return existingPlugin, nil
    43  }
    44  
    45  func (p *plugin) NodePrepareResources(
    46  	ctx context.Context,
    47  	req *drapb.NodePrepareResourcesRequest,
    48  	opts ...grpc.CallOption,
    49  ) (*drapb.NodePrepareResourcesResponse, error) {
    50  	logger := klog.FromContext(ctx)
    51  	logger.V(4).Info(log("calling NodePrepareResources rpc"), "request", req)
    52  
    53  	conn, err := p.getOrCreateGRPCConn()
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	ctx, cancel := context.WithTimeout(ctx, p.clientTimeout)
    59  	defer cancel()
    60  
    61  	nodeClient := drapb.NewNodeClient(conn)
    62  	response, err := nodeClient.NodePrepareResources(ctx, req)
    63  	logger.V(4).Info(log("done calling NodePrepareResources rpc"), "response", response, "err", err)
    64  	return response, err
    65  }
    66  
    67  func (p *plugin) NodeUnprepareResources(
    68  	ctx context.Context,
    69  	req *drapb.NodeUnprepareResourcesRequest,
    70  	opts ...grpc.CallOption,
    71  ) (*drapb.NodeUnprepareResourcesResponse, error) {
    72  	logger := klog.FromContext(ctx)
    73  	logger.V(4).Info(log("calling NodeUnprepareResource rpc"), "request", req)
    74  
    75  	conn, err := p.getOrCreateGRPCConn()
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	ctx, cancel := context.WithTimeout(ctx, p.clientTimeout)
    81  	defer cancel()
    82  
    83  	nodeClient := drapb.NewNodeClient(conn)
    84  	response, err := nodeClient.NodeUnprepareResources(ctx, req)
    85  	logger.V(4).Info(log("done calling NodeUnprepareResources rpc"), "response", response, "err", err)
    86  	return response, err
    87  }
    88  
    89  func (p *plugin) NodeListAndWatchResources(
    90  	ctx context.Context,
    91  	req *drapb.NodeListAndWatchResourcesRequest,
    92  	opts ...grpc.CallOption,
    93  ) (drapb.Node_NodeListAndWatchResourcesClient, error) {
    94  	logger := klog.FromContext(ctx)
    95  	logger.V(4).Info(log("calling NodeListAndWatchResources rpc"), "request", req)
    96  
    97  	conn, err := p.getOrCreateGRPCConn()
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	nodeClient := drapb.NewNodeClient(conn)
   103  	return nodeClient.NodeListAndWatchResources(ctx, req, opts...)
   104  }