forked from kubernetes-sigs/kind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
180 lines (150 loc) · 5.75 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
/*
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 (
"os/exec"
"sort"
"sigs.k8s.io/kind/pkg/cluster/constants"
"sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/log"
internalcontext "sigs.k8s.io/kind/pkg/cluster/internal/context"
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"
"sigs.k8s.io/kind/pkg/cluster/internal/providers/docker"
"sigs.k8s.io/kind/pkg/cluster/internal/providers/podman"
internalprovider "sigs.k8s.io/kind/pkg/cluster/internal/providers/provider"
)
// DefaultName is the default cluster name
const DefaultName = constants.DefaultClusterName
// Provider is used to perform cluster operations
type Provider struct {
provider internalprovider.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)
}
}
if p.provider == nil {
// auto-detect based on what is available in path
// default to docker for backwards compatibility
if path, err := exec.LookPath("docker"); err == nil && path != "" {
p.provider = docker.NewProvider(p.logger)
} else if path, err := exec.LookPath("podman"); err == nil && path != "" {
p.provider = podman.NewProvider(p.logger)
} else {
p.provider = docker.NewProvider(p.logger)
}
}
return p
}
// 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)
})
}
// TODO: remove this, rename internal context to something else
func (p *Provider) ic(name string) *internalcontext.Context {
return internalcontext.NewProviderContext(p.provider, name)
}
// Create provisions and starts a kubernetes-in-docker cluster
func (p *Provider) Create(name string, options ...CreateOption) error {
// apply options
opts := &internalcreate.ClusterOptions{}
for _, o := range options {
if err := o.apply(opts); err != nil {
return err
}
}
return internalcreate.Cluster(p.logger, p.ic(name), opts)
}
// Delete tears down a kubernetes-in-docker cluster
func (p *Provider) Delete(name, explicitKubeconfigPath string) error {
return internaldelete.Cluster(p.logger, p.ic(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.ic(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) error {
return kubeconfig.Export(p.ic(name), explicitPath)
}
// ListNodes returns the list of container IDs for the "nodes" in the cluster
func (p *Provider) ListNodes(name string) ([]nodes.Node, error) {
return p.ic(name).ListNodes()
}
// 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) {
return p.ic(name).ListInternalNodes()
}
// CollectLogs will populate dir with cluster logs and other debug files
func (p *Provider) CollectLogs(name, dir string) error {
return p.ic(name).CollectLogs(dir)
}