-
Notifications
You must be signed in to change notification settings - Fork 273
/
kind.go
268 lines (228 loc) Β· 7.78 KB
/
kind.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
268
package executables
import (
"bufio"
"context"
_ "embed"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/aws/eks-anywhere/pkg/bootstrapper"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/config"
"github.com/aws/eks-anywhere/pkg/filewriter"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/registrymirror"
"github.com/aws/eks-anywhere/pkg/registrymirror/containerd"
"github.com/aws/eks-anywhere/pkg/templater"
"github.com/aws/eks-anywhere/pkg/types"
)
const kindPath = "kind"
//go:embed config/kind.yaml
var kindConfigTemplate string
const configFileName = "kind_tmp.yaml"
type Kind struct {
writer filewriter.FileWriter
Executable
execConfig *kindExecConfig
}
// kindExecConfig contains transient information for the execution of kind commands
// It's used by BootstrapClusterClientOption's to store/change information prior to a command execution
// It must be cleaned after each execution to prevent side effects from past executions options.
type kindExecConfig struct {
env map[string]string
ConfigFile string
KindImage string
KubernetesRepository string
EtcdRepository string
EtcdVersion string
CorednsRepository string
CorednsVersion string
KubernetesVersion string
RegistryMirrorMap map[string]string
MirrorBase string
RegistryCACertPath string
RegistryAuth bool
RegistryUsername string
RegistryPassword string
ExtraPortMappings []int
DockerExtraMounts bool
DisableDefaultCNI bool
}
func NewKind(executable Executable, writer filewriter.FileWriter) *Kind {
return &Kind{
writer: writer,
Executable: executable,
}
}
func (k *Kind) CreateBootstrapCluster(ctx context.Context, clusterSpec *cluster.Spec, opts ...bootstrapper.BootstrapClusterClientOption) (kubeconfig string, err error) {
err = k.setupExecConfig(clusterSpec)
if err != nil {
return "", err
}
defer k.cleanExecConfig()
err = processOpts(opts)
if err != nil {
return "", err
}
err = k.buildConfigFile()
if err != nil {
return "", err
}
kubeconfigName, err := k.createKubeConfig(clusterSpec.Cluster.Name, []byte(""))
if err != nil {
return "", err
}
executionArgs := k.execArguments(clusterSpec.Cluster.Name, kubeconfigName)
logger.V(4).Info("Creating kind cluster", "name", getInternalName(clusterSpec.Cluster.Name), "kubeconfig", kubeconfigName)
_, err = k.ExecuteWithEnv(ctx, k.execConfig.env, executionArgs...)
if err != nil {
return "", fmt.Errorf("executing create cluster: %v", err)
}
return kubeconfigName, nil
}
func (k *Kind) ClusterExists(ctx context.Context, clusterName string) (bool, error) {
internalName := getInternalName(clusterName)
stdOut, err := k.Execute(ctx, "get", "clusters")
if err != nil {
return false, fmt.Errorf("executing get clusters: %v", err)
}
logger.V(5).Info("Executed kind get clusters", "response", stdOut.String())
scanner := bufio.NewScanner(&stdOut)
for scanner.Scan() {
if kindClusterName := scanner.Text(); kindClusterName == internalName {
return true, nil
}
}
if err := scanner.Err(); err != nil {
return false, fmt.Errorf("failed checking if cluster exists when reading kind get cluster response: %v", err)
}
return false, nil
}
func (k *Kind) GetKubeconfig(ctx context.Context, clusterName string) (string, error) {
internalName := getInternalName(clusterName)
stdOut, err := k.Execute(ctx, "get", "kubeconfig", "--name", internalName)
if err != nil {
return "", fmt.Errorf("executing get kubeconfig: %v", err)
}
return k.createKubeConfig(clusterName, stdOut.Bytes())
}
func (k *Kind) WithExtraDockerMounts() bootstrapper.BootstrapClusterClientOption {
return func() error {
if k.execConfig == nil {
return errors.New("kind exec config is not ready")
}
k.execConfig.DockerExtraMounts = true
return nil
}
}
func (k *Kind) WithExtraPortMappings(ports []int) bootstrapper.BootstrapClusterClientOption {
return func() error {
if k.execConfig == nil {
return errors.New("kind exec config is not ready")
}
if len(ports) == 0 {
return errors.New("no ports found in the list")
}
k.execConfig.ExtraPortMappings = ports
return nil
}
}
func (k *Kind) WithEnv(env map[string]string) bootstrapper.BootstrapClusterClientOption {
return func() error {
if k.execConfig == nil {
return errors.New("kind exec config is not ready")
}
for name, value := range env {
k.execConfig.env[name] = value
}
return nil
}
}
func (k *Kind) DeleteBootstrapCluster(ctx context.Context, cluster *types.Cluster) error {
internalName := getInternalName(cluster.Name)
logger.V(4).Info("Deleting kind cluster", "name", internalName)
_, err := k.Execute(ctx, "delete", "cluster", "--name", internalName)
if err != nil {
return fmt.Errorf("executing delete cluster: %v", err)
}
return err
}
func (k *Kind) setupExecConfig(clusterSpec *cluster.Spec) error {
bundle := clusterSpec.VersionsBundle
registryMirror := registrymirror.FromCluster(clusterSpec.Cluster)
k.execConfig = &kindExecConfig{
KindImage: registryMirror.ReplaceRegistry(bundle.EksD.KindNode.VersionedImage()),
KubernetesRepository: registryMirror.ReplaceRegistry(bundle.KubeDistro.Kubernetes.Repository),
KubernetesVersion: bundle.KubeDistro.Kubernetes.Tag,
EtcdRepository: registryMirror.ReplaceRegistry(bundle.KubeDistro.Etcd.Repository),
EtcdVersion: bundle.KubeDistro.Etcd.Tag,
CorednsRepository: registryMirror.ReplaceRegistry(bundle.KubeDistro.CoreDNS.Repository),
CorednsVersion: bundle.KubeDistro.CoreDNS.Tag,
env: make(map[string]string),
}
if clusterSpec.Cluster.Spec.RegistryMirrorConfiguration != nil {
k.execConfig.MirrorBase = registryMirror.BaseRegistry
k.execConfig.RegistryMirrorMap = containerd.ToAPIEndpoints(registryMirror.NamespacedRegistryMap)
if registryMirror.CACertContent != "" {
path := filepath.Join(clusterSpec.Cluster.Name, "generated", "certs.d", registryMirror.BaseRegistry)
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(path, "ca.crt"), []byte(registryMirror.CACertContent), 0o644); err != nil {
return errors.New("error writing the registry certification file")
}
k.execConfig.RegistryCACertPath = filepath.Join(clusterSpec.Cluster.Name, "generated", "certs.d")
}
if registryMirror.Auth {
k.execConfig.RegistryAuth = registryMirror.Auth
username, password, err := config.ReadCredentials()
if err != nil {
return err
}
k.execConfig.RegistryUsername = username
k.execConfig.RegistryPassword = password
}
}
return nil
}
func (k *Kind) cleanExecConfig() {
k.execConfig = nil
}
func (k *Kind) buildConfigFile() error {
t := templater.New(k.writer)
writtenFileName, err := t.WriteToFile(kindConfigTemplate, k.execConfig, configFileName)
if err != nil {
return fmt.Errorf("creating file for kind config: %v", err)
}
k.execConfig.ConfigFile = writtenFileName
return nil
}
func (k *Kind) execArguments(clusterName string, kubeconfigName string) []string {
return []string{
"create", "cluster",
"--name", getInternalName(clusterName),
"--kubeconfig", kubeconfigName,
"--image", k.execConfig.KindImage,
"--config", k.execConfig.ConfigFile,
}
}
func (k *Kind) createKubeConfig(clusterName string, content []byte) (string, error) {
fileName, err := k.writer.Write(fmt.Sprintf("%s.kind.kubeconfig", clusterName), content)
if err != nil {
return "", fmt.Errorf("generating temp file for storing kind kubeconfig: %v", err)
}
return fileName, nil
}
func processOpts(opts []bootstrapper.BootstrapClusterClientOption) error {
for _, opt := range opts {
err := opt()
if err != nil {
return err
}
}
return nil
}
func getInternalName(clusterName string) string {
return fmt.Sprintf("%s-eks-a-cluster", clusterName)
}