talos

Форк
0
/
config.go 
69 строк · 1.6 Кб
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

5
package cluster
6

7
import (
8
	"context"
9
	"strings"
10

11
	"github.com/siderolabs/talos/pkg/machinery/client"
12
	clientconfig "github.com/siderolabs/talos/pkg/machinery/client/config"
13
)
14

15
// ConfigClientProvider builds Talos client from client config.
16
type ConfigClientProvider struct {
17
	// DefaultClient to be used when using default endpoints.
18
	//
19
	// Not required, if missing client will be constructed from the config.
20
	DefaultClient *client.Client
21

22
	// TalosConfig is a client Talos configuration.
23
	TalosConfig *clientconfig.Config
24

25
	clients map[string]*client.Client
26
}
27

28
// Client returns Talos client instance for default (if no endpoints are given) or
29
// specific endpoints.
30
//
31
// Client implements ClientProvider interface.
32
func (c *ConfigClientProvider) Client(endpoints ...string) (*client.Client, error) {
33
	key := strings.Join(endpoints, ",")
34

35
	if c.clients == nil {
36
		c.clients = make(map[string]*client.Client)
37
	}
38

39
	if cli := c.clients[key]; cli != nil {
40
		return cli, nil
41
	}
42

43
	if len(endpoints) == 0 && c.DefaultClient != nil {
44
		return c.DefaultClient, nil
45
	}
46

47
	opts := []client.OptionFunc{
48
		client.WithConfig(c.TalosConfig),
49
	}
50

51
	if len(endpoints) > 0 {
52
		opts = append(opts, client.WithEndpoints(endpoints...))
53
	}
54

55
	return client.New(context.TODO(), opts...)
56
}
57

58
// Close all the client connections.
59
func (c *ConfigClientProvider) Close() error {
60
	for _, cli := range c.clients {
61
		if err := cli.Close(); err != nil {
62
			return err
63
		}
64
	}
65

66
	c.clients = nil
67

68
	return nil
69
}
70

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.