forked from kubernetes-sigs/kind
-
Notifications
You must be signed in to change notification settings - Fork 15
/
provider.go
267 lines (233 loc) · 9.05 KB
/
provider.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
Copyright 2018 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 cluster
import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"sigs.k8s.io/kind/pkg/cmd/kind/version"
"sigs.k8s.io/kind/pkg/commons"
"sigs.k8s.io/kind/pkg/cluster/constants"
"sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
"sigs.k8s.io/kind/pkg/errors"
"sigs.k8s.io/kind/pkg/log"
internalcreate "sigs.k8s.io/kind/pkg/cluster/internal/create"
internaldelete "sigs.k8s.io/kind/pkg/cluster/internal/delete"
"sigs.k8s.io/kind/pkg/cluster/internal/kubeconfig"
internalproviders "sigs.k8s.io/kind/pkg/cluster/internal/providers"
"sigs.k8s.io/kind/pkg/cluster/internal/providers/docker"
"sigs.k8s.io/kind/pkg/cluster/internal/providers/podman"
internalvalidate "sigs.k8s.io/kind/pkg/cluster/internal/validate"
)
// DefaultName is the default cluster name
const DefaultName = constants.DefaultClusterName
// defaultName is a helper that given a name defaults it if unset
func defaultName(name string) string {
if name == "" {
name = DefaultName
}
return name
}
// Provider is used to perform cluster operations
type Provider struct {
provider internalproviders.Provider
logger log.Logger
}
// NewProvider returns a new provider based on the supplied options
func NewProvider(options ...ProviderOption) *Provider {
p := &Provider{
logger: log.NoopLogger{},
}
// Ensure we apply the logger options first, while maintaining the order
// otherwise. This way we can trivially init the internal provider with
// the logger.
sort.SliceStable(options, func(i, j int) bool {
_, iIsLogger := options[i].(providerLoggerOption)
_, jIsLogger := options[j].(providerLoggerOption)
return iIsLogger && !jIsLogger
})
for _, o := range options {
if o != nil {
o.apply(p)
}
}
// ensure a provider if none was set
// NOTE: depends on logger being set (see sorting above)
if p.provider == nil {
// DetectNodeProvider does not fallback to allow callers to determine
// this behavior
// However for compatibility if the caller of NewProvider supplied no
// option and we autodetect internally, we default to the docker provider
// for fallback, to avoid a breaking change for now.
// This may change in the future.
// TODO: consider breaking this API for earlier errors.
providerOpt, _ := DetectNodeProvider()
if providerOpt == nil {
providerOpt = ProviderWithDocker()
}
providerOpt.apply(p)
}
return p
}
// NoNodeProviderDetectedError indicates that we could not autolocate an available
// NodeProvider backend on the host
var NoNodeProviderDetectedError = errors.NewWithoutStack("failed to detect any supported node provider")
// DetectNodeProvider allows callers to autodetect the node provider
// *without* fallback to the default.
//
// Pass the returned ProviderOption to NewProvider to pass the auto-detect Docker
// or Podman option explicitly (in the future there will be more options)
//
// NOTE: The kind *cli* also checks `KIND_EXPERIMENTAL_PROVIDER` for "podman" or
// "docker" currently and does not auto-detect / respects this if set.
//
// This will be replaced with some other mechanism in the future (likely when
// podman support is GA), in the meantime though your tool may wish to match this.
//
// In the future when this is not considered experimental,
// that logic will be in a public API as well.
func DetectNodeProvider() (ProviderOption, error) {
// auto-detect based on each node provider's IsAvailable() function
if docker.IsAvailable() {
return ProviderWithDocker(), nil
}
if podman.IsAvailable() {
return ProviderWithPodman(), nil
}
return nil, errors.WithStack(NoNodeProviderDetectedError)
}
// ProviderOption is an option for configuring a provider
type ProviderOption interface {
apply(p *Provider)
}
// providerLoggerOption is a trivial ProviderOption adapter
// we use a type specific to logging options so we can handle them first
type providerLoggerOption func(p *Provider)
func (a providerLoggerOption) apply(p *Provider) {
a(p)
}
var _ ProviderOption = providerLoggerOption(nil)
// ProviderWithLogger configures the provider to use Logger logger
func ProviderWithLogger(logger log.Logger) ProviderOption {
return providerLoggerOption(func(p *Provider) {
p.logger = logger
})
}
// providerLoggerOption is a trivial ProviderOption adapter
// we use a type specific to logging options so we can handle them first
type providerRuntimeOption func(p *Provider)
func (a providerRuntimeOption) apply(p *Provider) {
a(p)
}
var _ ProviderOption = providerRuntimeOption(nil)
// ProviderWithDocker configures the provider to use docker runtime
func ProviderWithDocker() ProviderOption {
return providerRuntimeOption(func(p *Provider) {
p.provider = docker.NewProvider(p.logger)
})
}
// ProviderWithPodman configures the provider to use podman runtime
func ProviderWithPodman() ProviderOption {
return providerRuntimeOption(func(p *Provider) {
p.provider = podman.NewProvider(p.logger)
})
}
// Create provisions and starts a kubernetes-in-docker cluster
func (p *Provider) Create(name string, vaultPassword string, descriptorPath string, moveManagement bool, avoidCreation bool, useLocalStratioImage bool, dockerRegUrl string, clusterConfig *commons.ClusterConfig, keosCluster commons.KeosCluster, clusterCredentials commons.ClusterCredentials, options ...CreateOption) error { // apply options
opts := &internalcreate.ClusterOptions{
NameOverride: name,
VaultPassword: vaultPassword,
DescriptorPath: descriptorPath,
MoveManagement: moveManagement,
AvoidCreation: avoidCreation,
UseLocalStratioImage: useLocalStratioImage,
KeosCluster: keosCluster,
ClusterCredentials: clusterCredentials,
ClusterConfig: clusterConfig,
DockerRegUrl: dockerRegUrl,
}
for _, o := range options {
if err := o.apply(opts); err != nil {
return err
}
}
return internalcreate.Cluster(p.logger, p.provider, opts)
}
// Delete tears down a kubernetes-in-docker cluster
func (p *Provider) Delete(name, explicitKubeconfigPath string) error {
return internaldelete.Cluster(p.logger, p.provider, defaultName(name), explicitKubeconfigPath)
}
// List returns a list of clusters for which nodes exist
func (p *Provider) List() ([]string, error) {
return p.provider.ListClusters()
}
// KubeConfig returns the KUBECONFIG for the cluster
// If internal is true, this will contain the internal IP etc.
// If internal is false, this will contain the host IP etc.
func (p *Provider) KubeConfig(name string, internal bool) (string, error) {
return kubeconfig.Get(p.provider, defaultName(name), !internal)
}
// ExportKubeConfig exports the KUBECONFIG for the cluster, merging
// it into the selected file, following the rules from
// https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#config
// where explicitPath is the --kubeconfig value.
func (p *Provider) ExportKubeConfig(name string, explicitPath string, internal bool) error {
return kubeconfig.Export(p.provider, defaultName(name), explicitPath, !internal)
}
// ListNodes returns the list of container IDs for the "nodes" in the cluster
func (p *Provider) ListNodes(name string) ([]nodes.Node, error) {
return p.provider.ListNodes(defaultName(name))
}
// ListInternalNodes returns the list of container IDs for the "nodes" in the cluster
// that are not external
func (p *Provider) ListInternalNodes(name string) ([]nodes.Node, error) {
n, err := p.provider.ListNodes(name)
if err != nil {
return nil, err
}
return nodeutils.InternalNodes(n)
}
// CollectLogs will populate dir with cluster logs and other debug files
func (p *Provider) CollectLogs(name, dir string) error {
// TODO: should use ListNodes and Collect should handle nodes differently
// based on role ...
n, err := p.ListInternalNodes(name)
if err != nil {
return err
}
// ensure directory
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return errors.Wrap(err, "failed to create logs directory")
}
// write kind version
if err := ioutil.WriteFile(
filepath.Join(dir, "kind-version.txt"),
[]byte(version.DisplayVersion()),
0666, // match os.Create
); err != nil {
return errors.Wrap(err, "failed to write kind-version.txt")
}
// collect and write cluster logs
return p.provider.CollectLogs(dir, n)
}
func (p *Provider) Validate(keosCluster commons.KeosCluster, clusterConfig *commons.ClusterConfig, secretsPath string, vaultPassword string) (commons.ClusterCredentials, error) {
params := &internalvalidate.ValidateParams{
KeosCluster: keosCluster,
ClusterConfig: clusterConfig,
SecretsPath: secretsPath,
VaultPassword: vaultPassword,
}
return internalvalidate.Cluster(params)
}