github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/kubelet/podresources/client.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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 podresources
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"time"
    23  
    24  	"google.golang.org/grpc"
    25  	"google.golang.org/grpc/credentials/insecure"
    26  	v1 "k8s.io/kubelet/pkg/apis/podresources/v1"
    27  	"k8s.io/kubernetes/pkg/kubelet/util"
    28  )
    29  
    30  // GetClientFunc is a function to get a client for the PodResourcesLister grpc service.
    31  type GetClientFunc func(socket string, connectionTimeout time.Duration, maxMsgSize int) (v1.PodResourcesListerClient, *grpc.ClientConn, error)
    32  
    33  // GetV1Client returns a client for the PodResourcesLister grpc service, and it is referring from upstream k8s's GetV1Client,
    34  // which is also an implement of GetClientFunc.
    35  func GetV1Client(socket string, connectionTimeout time.Duration, maxMsgSize int) (v1.PodResourcesListerClient, *grpc.ClientConn, error) {
    36  	addr, dialer, err := util.GetAddressAndDialer(socket)
    37  	if err != nil {
    38  		return nil, nil, err
    39  	}
    40  	ctx, cancel := context.WithTimeout(context.Background(), connectionTimeout)
    41  	defer cancel()
    42  
    43  	conn, err := grpc.DialContext(ctx, addr, grpc.WithTransportCredentials(insecure.NewCredentials()),
    44  		grpc.WithContextDialer(dialer), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)))
    45  	if err != nil {
    46  		return nil, nil, fmt.Errorf("error dialing socket %s: %v", socket, err)
    47  	}
    48  	return v1.NewPodResourcesListerClient(conn), conn, nil
    49  }