talos
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5package cluster
6
7import (
8"context"
9"errors"
10
11"google.golang.org/grpc"
12"google.golang.org/grpc/credentials/insecure"
13
14"github.com/siderolabs/talos/pkg/machinery/client"
15"github.com/siderolabs/talos/pkg/machinery/constants"
16)
17
18// LocalClientProvider builds Talos client to connect to same-node apid instance over file socket.
19type LocalClientProvider struct {
20client *client.Client
21}
22
23// Client returns Talos client instance for default (if no endpoints are given) or
24// specific endpoints.
25//
26// Client implements ClientProvider interface.
27func (c *LocalClientProvider) Client(endpoints ...string) (*client.Client, error) {
28if len(endpoints) > 0 {
29return nil, errors.New("custom endpoints not supported with LocalClientProvider")
30}
31
32var err error
33
34if c.client == nil {
35c.client, err = client.New(context.TODO(), client.WithUnixSocket(constants.APISocketPath), client.WithGRPCDialOptions(grpc.WithTransportCredentials(insecure.NewCredentials())))
36}
37
38return c.client, err
39}
40
41// Close all the client connections.
42func (c *LocalClientProvider) Close() error {
43if c.client != nil {
44if err := c.client.Close(); err != nil {
45return err
46}
47
48c.client = nil
49}
50
51return nil
52}
53