-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
client.go
119 lines (93 loc) · 3.73 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"context"
"github.com/pkg/errors"
)
// Client is used to interact with the clusterctl configurations.
// Clusterctl v2 handles the following configurations:
// 1. The cert manager configuration (URL of the repository)
// 2. The configuration of the providers (name, type and URL of the provider repository)
// 3. Variables used when installing providers/creating clusters. Variables can be read from the environment or from the config file
// 4. The configuration about image overrides.
type Client interface {
// CertManager provide access to the cert-manager configurations.
CertManager() CertManagerClient
// Providers provide access to provider configurations.
Providers() ProvidersClient
// Variables provide access to environment variables and/or variables defined in the clusterctl configuration file.
Variables() VariablesClient
// ImageMeta provide access to image meta configurations.
ImageMeta() ImageMetaClient
}
// configClient implements Client.
type configClient struct {
reader Reader
}
// ensure configClient implements Client.
var _ Client = &configClient{}
func (c *configClient) CertManager() CertManagerClient {
return newCertManagerClient(c.reader)
}
func (c *configClient) Providers() ProvidersClient {
return newProvidersClient(c.reader)
}
func (c *configClient) Variables() VariablesClient {
return newVariablesClient(c.reader)
}
func (c *configClient) ImageMeta() ImageMetaClient {
return newImageMetaClient(c.reader)
}
// Option is a configuration option supplied to New.
type Option func(*configClient)
// InjectReader allows to override the default configuration reader used by clusterctl.
func InjectReader(reader Reader) Option {
return func(c *configClient) {
c.reader = reader
}
}
// New returns a Client for interacting with the clusterctl configuration.
func New(ctx context.Context, path string, options ...Option) (Client, error) {
return newConfigClient(ctx, path, options...)
}
func newConfigClient(ctx context.Context, path string, options ...Option) (*configClient, error) {
client := &configClient{}
for _, o := range options {
o(client)
}
// if there is an injected reader, use it, otherwise use a default one
var err error
if client.reader == nil {
if client.reader, err = newViperReader(); err != nil {
return nil, errors.Wrap(err, "failed to create the configuration reader")
}
if err = client.reader.Init(ctx, path); err != nil {
return nil, errors.Wrap(err, "failed to initialize the configuration reader")
}
}
return client, nil
}
// Reader define the behaviours of a configuration reader.
type Reader interface {
// Init allows to initialize the configuration reader.
Init(ctx context.Context, path string) error
// Get returns a configuration value of type string.
// In case the configuration value does not exists, it returns an error.
Get(key string) (string, error)
// Set allows to set an explicit override for a config value.
// e.g. It is used to set an override from a flag value over environment/config file variables.
Set(key, value string)
// UnmarshalKey reads a configuration value and unmarshals it into the provided value object.
UnmarshalKey(key string, value interface{}) error
}