mirror of
https://github.com/siderolabs/talos.git
synced 2025-10-10 15:11:15 +02:00
This replaces codegen version of apid proxying with talos-systems/grpc-proxy based version. Proxying is transparent, it doesn't require exact information about methods and response types. It requires some common layout response to enhance it properly with node metadata or errors. There should be no signifcant changes to the API with the previous version, but it's worth mentioning a few changes: 1. grpc.ClientConn is established just once per upstream (either local service or remote apid instance). 2. When called without `-t` (`targets`), apid proxies immediately down to local service skipping proxying to itself (as before), which results in empty node metadata in response (before it had local node IP). Might revert this later to proxy to itself (?). 3. Streaming APIs are now fully supported with multiple targets, but message definition doesn't contain `ResponseMetadata`, so streaming APIs are broken now with targets (needs a fix). 4. Errors are now returned as responses with `Error` field set in `ResponseMetadata`, this requires client library update and `osctl` to handle it properly. Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
53 lines
1.4 KiB
Go
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"
|
|
"fmt"
|
|
"time"
|
|
|
|
"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), //nolint: staticcheck
|
|
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error connecting to CRI: %w", err)
|
|
}
|
|
|
|
return &Client{
|
|
conn: conn,
|
|
runtimeClient: runtimeapi.NewRuntimeServiceClient(conn),
|
|
imagesClient: runtimeapi.NewImageServiceClient(conn),
|
|
}, nil
|
|
}
|
|
|
|
// Close connection
|
|
func (c *Client) Close() error {
|
|
return c.conn.Close()
|
|
}
|