talos/internal/pkg/cri/client.go
Andrey Smirnov c10ef0f15a chore: extract CRI client as separate package
This is preparation for implementing CRI runner.

CRI client moved into its own package, I split it into multiple files
and added rudimentary tests for it.

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2019-07-11 01:52:19 +03:00

53 lines
1.4 KiB
Go

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package cri
import (
"context"
"time"
"github.com/pkg/errors"
"google.golang.org/grpc"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)
// Client is a lightweight implementation of CRI client.
type Client struct {
conn *grpc.ClientConn
runtimeClient runtimeapi.RuntimeServiceClient
imagesClient runtimeapi.ImageServiceClient
}
// maxMsgSize use 16MB as the default message size limit.
// grpc library default is 4MB
const maxMsgSize = 1024 * 1024 * 16
// NewClient builds CRI client
func NewClient(endpoint string, connectionTimeout time.Duration) (*Client, error) {
ctx, cancel := context.WithTimeout(context.Background(), connectionTimeout)
defer cancel()
conn, err := grpc.DialContext(ctx, endpoint,
grpc.WithInsecure(),
grpc.WithBlock(),
grpc.FailOnNonTempDialError(false),
grpc.WithBackoffMaxDelay(3*time.Second),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)))
if err != nil {
return nil, errors.Wrapf(err, "error connecting to CRI")
}
return &Client{
conn: conn,
runtimeClient: runtimeapi.NewRuntimeServiceClient(conn),
imagesClient: runtimeapi.NewImageServiceClient(conn),
}, nil
}
// Close connection
func (c *Client) Close() error {
return c.conn.Close()
}