This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
/
kubeadm_init.go
449 lines (410 loc) · 16.4 KB
/
kubeadm_init.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package resource
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/weaveworks/cluster-api-provider-existinginfra/pkg/apis/wksprovider/machine/config"
"github.com/weaveworks/cluster-api-provider-existinginfra/pkg/apis/wksprovider/machine/config/kubeadm"
"github.com/weaveworks/cluster-api-provider-existinginfra/pkg/apis/wksprovider/machine/config/kubeproxy"
"github.com/weaveworks/cluster-api-provider-existinginfra/pkg/apis/wksprovider/machine/scripts"
"github.com/weaveworks/cluster-api-provider-existinginfra/pkg/apis/wksprovider/manifests"
"github.com/weaveworks/cluster-api-provider-existinginfra/pkg/plan"
kubeadmutil "github.com/weaveworks/cluster-api-provider-existinginfra/pkg/utilities/kubeadm"
"github.com/weaveworks/cluster-api-provider-existinginfra/pkg/utilities/manifest"
capeimanifest "github.com/weaveworks/cluster-api-provider-existinginfra/pkg/utilities/manifest"
"github.com/weaveworks/cluster-api-provider-existinginfra/pkg/utilities/object"
"github.com/weaveworks/cluster-api-provider-existinginfra/pkg/utilities/ssh"
"github.com/weaveworks/cluster-api-provider-existinginfra/pkg/utilities/version"
"github.com/weaveworks/libgitops/pkg/serializer"
corev1 "k8s.io/api/core/v1"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta1"
"sigs.k8s.io/yaml"
)
// KubeadmInit represents an attempt to init a Kubernetes node via kubeadm.
type KubeadmInit struct {
Base
// PublicIP is public IP of the master node we are trying to setup here.
PublicIP string `structs:"publicIP"`
// PrivateIP is private IP of the master node we are trying to setup here.
PrivateIP string `structs:"privateIP"`
// NodeName, if non-empty, will override the default node name guessed by kubeadm.
NodeName string
// KubeletConfig groups all options & flags which need to be passed to kubelet.
KubeletConfig *config.KubeletConfig `structs:"kubeletConfig"`
// ConntrackMax is the maximum number of NAT connections for kubeproxy to track (0 to leave as-is).
ConntrackMax int32 `structs:"conntrackMax"`
// UseIPTables controls whether the following command is called or not:
// sysctl net.bridge.bridge-nf-call-iptables=1
// prior to running kubeadm init.
UseIPTables bool `structs:"useIPTables"`
// kubeadmInitScriptPath is the path to the "kubeadm init" script to use.
KubeadmInitScriptPath string `structs:"kubeadmInitScriptPath"`
// IgnorePreflightErrors is optionally used to skip kubeadm's preflight checks.
IgnorePreflightErrors []string `structs:"ignorePreflightErrors"`
// SSHKeyPath is the path to the private SSH key used by WKS to SSH into
// nodes to add/remove them to/from the Kubernetes cluster. Only one of SSHKeyPath
// and SSHKey may be specified
SSHKeyPath string `structs:"sshKeyPath"`
// SSHKey is the private SSH key used by WKS to SSH into
// nodes to add/remove them to/from the Kubernetes cluster. Only one of SSHKeyPath
// and SSHKey may be specified
SSHKey string `structs:"-" plan:"hide"`
// BootstrapToken is the token used by kubeadm init and kubeadm join to
// safely form new clusters.
BootstrapToken *kubeadmapi.BootstrapTokenString `structs:"bootstrapToken"`
// The version of Kubernetes to install
KubernetesVersion string `structs:"kubernetesVersion"`
// ControlPlaneEndpoint is the IP:port of the control plane load balancer.
// Default: localhost:6443
// See also: https://kubernetes.io/docs/setup/independent/high-availability/#stacked-control-plane-and-etcd-nodes
ControlPlaneEndpoint string `structs:"controlPlaneEndpoint"`
// Cloud provider setting which is needed for kubeadm and kubelet
CloudProvider string `structs:"cloudProvider"`
// ImageRepository sets the container registry to pull images from. If empty,
// `k8s.gcr.io` will be used by default.
ImageRepository string `structs:"imageRepository"`
// AssetDescriptions specifies the image repository and image tag for each potentially overridden
// asset (currently, DNS, Etcd, and Kubernetes)
AssetDescriptions map[string]kubeadmutil.AssetDescription `structs:"assetDescriptions"`
// AdditionalSANs can hold additional SANs to add to the API server certificate.
AdditionalSANs []string
// The namespace in which to init kubeadm
Namespace fmt.Stringer
// Extra arguments to pass to the APIServer
ExtraAPIServerArgs map[string]string
// The IP range for service VIPs
ServiceCIDRBlock string
// PodCIDRBlock is the subnet used by pods.
PodCIDRBlock string
}
var _ plan.Resource = plan.RegisterResource(&KubeadmInit{})
// State implements plan.Resource.
func (ki *KubeadmInit) State() plan.State {
return ToState(ki)
}
func isEKSD(repo string) bool {
return repo == "public.ecr.aws/eks-distro/kubernetes"
}
// Apply implements plan.Resource.
// TODO: find a way to make this idempotent.
// TODO: should such a resource be split into smaller resources?
func (ki *KubeadmInit) Apply(ctx context.Context, runner plan.Runner, diff plan.Diff) (bool, error) {
// Can't override two ways
if ki.ImageRepository != "" && len(ki.AssetDescriptions) != 0 {
return false, fmt.Errorf("Kubernetes image repository defined twice; you must either define " +
"a global image repository in the ExistingInfraCluster manifest, or define specific image " +
"repositories via a flavor specification.")
}
log.Debug("Initializing Kubernetes cluster")
sshKey := []byte(ki.SSHKey)
var err error
if ki.SSHKeyPath != "" {
sshKey, err = ssh.ReadPrivateKey(ki.SSHKeyPath)
if err != nil {
return false, err
}
}
namespace := ki.Namespace.String()
if namespace == "" {
namespace = manifest.DefaultNamespace
}
var dns *kubeadmapi.DNS = nil
dnsAD, found := ki.AssetDescriptions["DNS"]
if found {
dns = &kubeadmapi.DNS{
Type: "",
ImageMeta: kubeadmapi.ImageMeta{
ImageRepository: dnsAD.ImageRepository, //"public.ecr.aws/eks-distro/coredns",
ImageTag: dnsAD.ImageTag, //"v1.7.0-eks-1-18-1",
},
}
}
var etcd *kubeadmapi.Etcd = nil
etcdAD, found := ki.AssetDescriptions["Etcd"]
if found {
etcd = &kubeadmapi.Etcd{
Local: &kubeadmapi.LocalEtcd{
ImageMeta: kubeadmapi.ImageMeta{
ImageRepository: etcdAD.ImageRepository, //"public.ecr.aws/eks-distro/etcd-io",
ImageTag: etcdAD.ImageTag, //"v3.4.14-eks-1-18-1",
},
DataDir: "",
ExtraArgs: nil,
ServerCertSANs: nil,
PeerCertSANs: nil,
},
}
}
k8sImageTag := ""
kubeAD, found := ki.AssetDescriptions["Kubernetes"]
if found {
k8sImageTag = kubeAD.ImageTag
ki.ImageRepository = kubeAD.ImageRepository //"public.ecr.aws/eks-distro/kubernetes" // XXXXXXXXXXXXXXXXXX
}
clusterConfig, err := yaml.Marshal(kubeadm.NewClusterConfiguration(kubeadm.ClusterConfigurationParams{
KubernetesVersion: ki.KubernetesVersion + k8sImageTag,
NodeIPs: []string{ki.PublicIP, ki.PrivateIP},
ControlPlaneEndpoint: ki.ControlPlaneEndpoint,
CloudProvider: ki.CloudProvider,
ImageRepository: ki.ImageRepository,
AdditionalSANs: ki.AdditionalSANs,
ExtraArgs: ki.ExtraAPIServerArgs,
ServiceCIDRBlock: ki.ServiceCIDRBlock,
PodCIDRBlock: ki.PodCIDRBlock,
DNS: dns,
Etcd: etcd,
}))
if err != nil {
return false, errors.Wrap(err, "failed to serialize kubeadm's ClusterConfiguration object")
}
kubeadmConfig, err := yaml.Marshal(kubeadm.NewInitConfiguration(kubeadm.InitConfigurationParams{
NodeName: ki.NodeName,
BootstrapToken: ki.BootstrapToken,
KubeletConfig: *ki.KubeletConfig,
}))
if err != nil {
return false, errors.Wrap(err, "failed to serialize kubeadm's InitConfiguration object")
}
kubeproxyConfig, err := yaml.Marshal(kubeproxy.NewConfig(ki.ConntrackMax))
if err != nil {
return false, errors.Wrap(err, "failed to serialize kube-proxy's KubeProxyConfiguration object")
}
// Create a temporary buffer for YAML frames, and the corresponding YAML FrameWriter
buf := new(bytes.Buffer)
fw := serializer.NewYAMLFrameWriter(buf)
// Write all three frames into the FrameWriter, and use the output in configBytes
if err := serializer.WriteFrameList(fw,
[][]byte{clusterConfig, kubeadmConfig, kubeproxyConfig},
); err != nil {
return false, err
}
configBytes := buf.Bytes()
remotePath := "/tmp/wks_kubeadm_init_config.yaml"
if err = scripts.WriteFile(ctx, configBytes, remotePath, 0660, runner); err != nil {
return false, errors.Wrap(err, "failed to upload kubeadm's configuration")
}
log.WithField("yaml", string(configBytes)).Debug("uploaded kubeadm's configuration")
//nolint:errcheck
defer removeFile(ctx, remotePath, runner) // TODO: Deferred error checking
var stdOutErr string
p := buildKubeadmInitPlan(
remotePath,
strings.Join(ki.IgnorePreflightErrors, ","),
ki.UseIPTables,
ki.KubernetesVersion,
&stdOutErr)
_, err = p.Apply(ctx, runner, plan.EmptyDiff())
if err != nil {
return false, errors.Wrap(err, "failed to initialize Kubernetes cluster with kubeadm")
}
// TODO: switch to cluster-info.yaml approach.
kubeadmJoinCmd, err := kubeadmutil.ExtractJoinCmd(stdOutErr)
if err != nil {
return false, err
}
log.Debug(kubeadmJoinCmd)
caCertHash, err := kubeadmutil.ExtractDiscoveryTokenCaCertHash(kubeadmJoinCmd)
if err != nil {
return false, err
}
certKey, err := kubeadmutil.ExtractCertificateKey(kubeadmJoinCmd)
if err != nil {
return false, err
}
if err := ki.kubectlApply(ctx, "01_namespace.yaml", namespace, runner); err != nil {
return false, err
}
if err := ki.kubectlApply(ctx, "02_rbac.yaml", namespace, runner); err != nil {
return false, err
}
return true, ki.applySecretWith(ctx, sshKey, caCertHash, certKey, namespace, runner)
}
func (ki *KubeadmInit) updateManifestNamespace(fileName, namespace string) ([]byte, error) {
content, err := ki.manifestContent(fileName)
if err != nil {
return nil, errors.Wrap(err, "Failed to open manifest")
}
c, err := capeimanifest.WithNamespace(serializer.FromBytes(content), namespace)
if err != nil {
return nil, err
}
return c, nil
}
func (ki *KubeadmInit) kubectlApply(ctx context.Context, fileName, namespace string, runner plan.Runner) error {
content, err := ki.updateManifestNamespace(fileName, namespace)
if err != nil {
return errors.Wrap(err, "Failed to upate manifest namespace")
}
return RunKubectlApply(ctx, runner, KubectlApplyArgs{Content: content}, fileName)
}
func (ki *KubeadmInit) manifestContent(fileName string) ([]byte, error) {
file, err := manifests.Manifests.Open(fileName)
if err != nil {
return nil, err
}
content, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
return content, nil
}
func (ki *KubeadmInit) applySecretWith(ctx context.Context, sshKey []byte, discoveryTokenCaCertHash, certKey, namespace string, runner plan.Runner) error {
log.Info("adding SSH key to WKS secret and applying its manifest")
fileName := "03_secrets.yaml"
secret, err := ki.deserializeSecret(fileName, namespace)
if err != nil {
return err
}
secret.Data["sshKey"] = sshKey
secret.Data["discoveryTokenCaCertHash"] = []byte(discoveryTokenCaCertHash)
secret.Data["certificateKey"] = []byte(certKey)
// We only store the ID as a Secret object containing the bootstrap token's
// secret is already created by kubeadm init under:
// kube-system/bootstrap-token-$ID
secret.Data["bootstrapTokenID"] = []byte(ki.BootstrapToken.ID)
bytes, err := yaml.Marshal(secret)
if err != nil {
return errors.Wrap(err, "failed to serialize manifest")
}
return RunKubectlApply(ctx, runner, KubectlApplyArgs{Content: bytes}, fileName)
}
func (ki *KubeadmInit) deserializeSecret(fileName, namespace string) (*corev1.Secret, error) {
content, err := ki.updateManifestNamespace(fileName, namespace)
if err != nil {
return nil, errors.Wrap(err, "Failed to upate manifest namespace")
}
secret := &corev1.Secret{}
if err = yaml.Unmarshal(content, secret); err != nil {
return nil, errors.Wrap(err, "failed to deserialize manifest")
}
return secret, nil
}
// Undo implements plan.Resource.
func (ki *KubeadmInit) Undo(ctx context.Context, runner plan.Runner, current plan.State) error {
remotePath := "/tmp/wks_kubeadm_init_config.yaml"
var ignored string
return buildKubeadmInitPlan(
remotePath,
strings.Join(ki.IgnorePreflightErrors, ","),
ki.UseIPTables, ki.KubernetesVersion, &ignored).Undo(
ctx, runner, plan.EmptyState)
}
// buildKubeadmInitPlan builds a plan for kubeadm init command.
// Parameter k8sversion specified here represents the version of both Kubernetes and Kubeadm.
func buildKubeadmInitPlan(path string, ignorePreflightErrors string, useIPTables bool, k8sVersion string, output *string) plan.Resource {
// Detect version for --upload-cert-flags
uploadCertsFlag := "--upload-certs"
if lt, err := version.LessThan(k8sVersion, "v1.15.0"); err == nil && lt {
uploadCertsFlag = "--experimental-upload-certs"
}
// If we're at 1.17.0 or greater, we need to upgrade the kubeadm config before running "kubeadm init"
upgradeKubeadmConfig := false
if lt, err := version.LessThan(k8sVersion, "1.17.0"); err == nil && !lt {
upgradeKubeadmConfig = true
}
//
// We add resources to the plan graph for both "if" and "else" paths to make all resources deterministically connected.
// The graph resources will be easier to reason about when we execute them in parallel in the future.
//
b := plan.NewBuilder()
if useIPTables {
b.AddResource(
"configure:iptables",
&Run{Script: object.String("sysctl net.bridge.bridge-nf-call-iptables=1")}) // TODO: undo?
} else {
b.AddResource(
"configure:iptables",
&Run{Script: object.String("echo no operation")})
}
if upgradeKubeadmConfig {
b.AddResource(
"kubeadm:config:upgrade",
&Run{Script: plan.ParamString(
WithoutProxy("kubeadm config migrate --old-config %s --new-config %s_upgraded && mv %s_upgraded %s"), &path, &path, &path, &path),
},
plan.DependOn("configure:iptables"),
)
} else {
b.AddResource(
"kubeadm:config:upgrade",
&Run{Script: object.String("echo no upgrade is required")},
plan.DependOn("configure:iptables"),
)
}
b.AddResource(
"kubeadm:reset",
&Run{Script: object.String("kubeadm reset --force")},
plan.DependOn("kubeadm:config:upgrade"),
).AddResource(
"kubeadm:config:images",
&Run{Script: plan.ParamString("kubeadm config images pull --config=%s", &path)},
plan.DependOn("kubeadm:reset"),
).AddResource(
"kubeadm:run-init",
// N.B.: --experimental-upload-certs encrypts & uploads
// certificates of the primary control plane in the kubeadm-certs
// Secret, and prints the value for --certificate-key to STDOUT.
&Run{Script: plan.ParamString("kubeadm init --config=%s --ignore-preflight-errors=%s %s", &path, &ignorePreflightErrors, &uploadCertsFlag),
UndoResource: buildKubeadmRunInitUndoPlan(),
Output: output,
},
plan.DependOn("kubeadm:config:images"),
)
var homedir string
b.AddResource(
"kubeadm:get-homedir",
&Run{Script: object.String("echo -n $HOME"), Output: &homedir},
).AddResource(
"kubeadm:config:kubectl-dir",
&Dir{Path: plan.ParamString("%s/.kube", &homedir)},
plan.DependOn("kubeadm:get-homedir"),
).AddResource(
"kubeadm:config:copy",
&Run{Script: plan.ParamString("cp /etc/kubernetes/admin.conf %s/.kube/config", &homedir)},
plan.DependOn("kubeadm:run-init", "kubeadm:config:kubectl-dir"),
).AddResource(
"kubeadm:config:set-ownership",
&Run{Script: plan.ParamString("chown -R $(id -u):$(id -g) %s/.kube", &homedir)},
plan.DependOn("kubeadm:config:copy"),
)
p, err := b.Plan()
if err != nil {
log.Fatalf("%v", err)
}
return &p
}
func buildKubeadmRunInitUndoPlan() plan.Resource {
b := plan.NewBuilder()
b.AddResource(
"file:kube-apiserver.yaml",
&File{Destination: "/etc/kubernetes/manifests/kube-apiserver.yaml"},
).AddResource(
"file:kube-controller-manager.yaml",
&File{Destination: "/etc/kubernetes/manifests/kube-controller-manager.yaml"},
).AddResource(
"file:kube-scheduler.yaml",
&File{Destination: "/etc/kubernetes/manifests/kube-scheduler.yaml"},
).AddResource(
"file:etcd.yaml",
&File{Destination: "/etc/kubernetes/manifests/etcd.yaml"},
).AddResource(
"dir:etcd",
&Dir{Path: object.String("/var/lib/etcd"), RecursiveDelete: true},
)
p, err := b.Plan()
if err != nil {
log.Fatalf("%v", err)
}
return &p
}
func removeFile(ctx context.Context, remotePath string, runner plan.Runner) error {
if stdouterr, err := runner.RunCommand(ctx, fmt.Sprintf("rm -f %q", remotePath), nil); err != nil {
log.WithField("stdouterr", stdouterr).WithField("path", remotePath).Debugf("failed to delete file")
return errors.Wrapf(err, "failed to delete %q", remotePath)
}
return nil
}