/
init.go
123 lines (105 loc) · 3.69 KB
/
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
/*
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 kubeadminit implements the kubeadm init action
package kubeadminit
import (
"strings"
"sigs.k8s.io/kind/pkg/errors"
"sigs.k8s.io/kind/pkg/exec"
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
"sigs.k8s.io/kind/pkg/cluster/internal/create/actions"
"sigs.k8s.io/kind/pkg/internal/apis/config"
)
// kubeadmInitAction implements action for executing the kubeadm init
// and a set of default post init operations like e.g. install the
// CNI network plugin.
type action struct {
skipKubeProxy bool
}
// NewAction returns a new action for kubeadm init
func NewAction(cfg *config.Cluster) actions.Action {
return &action{skipKubeProxy: cfg.Networking.KubeProxyMode == config.NoneProxyMode}
}
// Execute runs the action
func (a *action) Execute(ctx *actions.ActionContext) error {
ctx.Status.Start("Starting control-plane 🕹️")
defer ctx.Status.End(false)
allNodes, err := ctx.Nodes()
if err != nil {
return err
}
// get the target node for this task
// TODO: eliminate the concept of bootstrapcontrolplane node entirely
// outside this method
node, err := nodeutils.BootstrapControlPlaneNode(allNodes)
if err != nil {
return err
}
// skip preflight checks, as these have undesirable side effects
// and don't tell us much. requires kubeadm 1.13+
skipPhases := "preflight"
if a.skipKubeProxy {
skipPhases += ",addon/kube-proxy"
}
// run kubeadm
cmd := node.Command(
// init because this is the control plane node
"kubeadm", "init",
"--skip-phases="+skipPhases,
// specify our generated config file
"--config=/kind/kubeadm.conf",
"--skip-token-print",
// increase verbosity for debugging
"--v=6",
)
lines, err := exec.CombinedOutputLines(cmd)
ctx.Logger.V(3).Info(strings.Join(lines, "\n"))
if err != nil {
return errors.Wrap(err, "failed to init node with kubeadm")
}
// copy some files to the other control plane nodes
otherControlPlanes, err := nodeutils.SecondaryControlPlaneNodes(allNodes)
if err != nil {
return err
}
for _, otherNode := range otherControlPlanes {
for _, file := range []string{
// copy over admin config so we can use any control plane to get it later
"/etc/kubernetes/admin.conf",
// copy over certs
"/etc/kubernetes/pki/ca.crt", "/etc/kubernetes/pki/ca.key",
"/etc/kubernetes/pki/front-proxy-ca.crt", "/etc/kubernetes/pki/front-proxy-ca.key",
"/etc/kubernetes/pki/sa.pub", "/etc/kubernetes/pki/sa.key",
// TODO: if we gain external etcd support these will be
// handled differently
"/etc/kubernetes/pki/etcd/ca.crt", "/etc/kubernetes/pki/etcd/ca.key",
} {
if err := nodeutils.CopyNodeToNode(node, otherNode, file); err != nil {
return errors.Wrap(err, "failed to copy admin kubeconfig")
}
}
}
// if we are only provisioning one node, remove the master taint
// https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/#master-isolation
if len(allNodes) == 1 {
if err := node.Command(
"kubectl", "--kubeconfig=/etc/kubernetes/admin.conf",
"taint", "nodes", "--all", "node-role.kubernetes.io/master-",
).Run(); err != nil {
return errors.Wrap(err, "failed to remove master taint")
}
}
// mark success
ctx.Status.End(true)
return nil
}