/
kubeadm.go
299 lines (260 loc) · 8.46 KB
/
kubeadm.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package services
import (
"context"
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"time"
"github.com/autonomy/talos/internal/app/init/internal/security/cis"
"github.com/autonomy/talos/internal/app/init/pkg/system/conditions"
"github.com/autonomy/talos/internal/app/init/pkg/system/runner"
"github.com/autonomy/talos/internal/app/init/pkg/system/runner/containerd"
"github.com/autonomy/talos/internal/app/trustd/proto"
"github.com/autonomy/talos/internal/pkg/constants"
"github.com/autonomy/talos/internal/pkg/crypto/x509"
"github.com/autonomy/talos/internal/pkg/grpc/middleware/auth/basic"
"github.com/autonomy/talos/internal/pkg/userdata"
"github.com/containerd/containerd/defaults"
"github.com/containerd/containerd/oci"
criconstants "github.com/containerd/cri/pkg/constants"
specs "github.com/opencontainers/runtime-spec/specs-go"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
)
// Kubeadm implements the Service interface. It serves as the concrete type with
// the required methods.
type Kubeadm struct{}
// ID implements the Service interface.
func (k *Kubeadm) ID(data *userdata.UserData) string {
return "kubeadm"
}
// PreFunc implements the Service interface.
func (k *Kubeadm) PreFunc(data *userdata.UserData) (err error) {
if err = writeKubeadmConfig(data); err != nil {
return err
}
if data.IsBootstrap() {
if err = writeKubeadmPKIFiles(data.Security.Kubernetes.CA); err != nil {
return err
}
} else if data.IsControlPlane() {
if data.Services.Trustd == nil || data.Services.Trustd.BootstrapNode == "" {
return nil
}
creds := basic.NewCredentials(
data.Services.Trustd.Username,
data.Services.Trustd.Password,
)
files := []string{
constants.AuditPolicyPathInitramfs,
constants.EncryptionConfigInitramfsPath,
}
conn, err := basic.NewConnection(data.Services.Trustd.BootstrapNode, constants.TrustdPort, creds)
if err != nil {
return err
}
client := proto.NewTrustdClient(conn)
if err := writeFiles(client, files); err != nil {
return err
}
}
return nil
}
// PostFunc implements the Service interface.
func (k *Kubeadm) PostFunc(data *userdata.UserData) error {
return nil
}
// ConditionFunc implements the Service interface.
func (k *Kubeadm) ConditionFunc(data *userdata.UserData) conditions.ConditionFunc {
files := []string{defaults.DefaultAddress}
return conditions.WaitForFilesToExist(files...)
}
// Start implements the Service interface.
// nolint: dupl
func (k *Kubeadm) Start(data *userdata.UserData) error {
image := constants.KubernetesImage
// We only wan't to run kubeadm if it hasn't been ran already.
if _, err := os.Stat("/etc/kubernetes/kubelet.conf"); !os.IsNotExist(err) {
return nil
}
// Set the process arguments.
args := runner.Args{
ID: k.ID(data),
}
ignorePreflightErrors := []string{"cri", "kubeletversion", "numcpu", "requiredipvskernelmodulesavailable"}
ignorePreflightErrors = append(ignorePreflightErrors, data.Services.Kubeadm.IgnorePreflightErrors...)
ignore := "--ignore-preflight-errors=" + strings.Join(ignorePreflightErrors, ",")
encoded := hex.EncodeToString([]byte(data.Services.Kubeadm.CertificateKey))
certificateKey := "--certificate-key=" + encoded
switch {
case data.IsBootstrap() == true:
args.ProcessArgs = []string{
"kubeadm",
"init",
"--config=/etc/kubernetes/kubeadm-config.yaml",
certificateKey,
ignore,
"--skip-token-print",
"--skip-certificate-key-print",
"--experimental-upload-certs",
}
case data.IsControlPlane() == true:
args.ProcessArgs = []string{
"kubeadm",
"join",
"--config=/etc/kubernetes/kubeadm-config.yaml",
certificateKey,
ignore,
}
default:
args.ProcessArgs = []string{
"kubeadm",
"join",
"--config=/etc/kubernetes/kubeadm-config.yaml",
ignore,
}
}
args.ProcessArgs = append(args.ProcessArgs, data.Services.Kubeadm.ExtraArgs...)
// Set the mounts.
// nolint: dupl
mounts := []specs.Mount{
{Type: "cgroup", Destination: "/sys/fs/cgroup", Options: []string{"ro"}},
{Type: "bind", Destination: "/var/run", Source: "/run", Options: []string{"rbind", "rshared", "rw"}},
{Type: "bind", Destination: "/var/lib/kubelet", Source: "/var/lib/kubelet", Options: []string{"rbind", "rshared", "rw"}},
{Type: "bind", Destination: "/etc/kubernetes", Source: "/etc/kubernetes", Options: []string{"bind", "rw"}},
{Type: "bind", Destination: "/etc/os-release", Source: "/etc/os-release", Options: []string{"bind", "ro"}},
{Type: "bind", Destination: "/bin/crictl", Source: "/bin/crictl", Options: []string{"bind", "ro"}},
{Type: "bind", Destination: "/bin/kubeadm", Source: "/bin/kubeadm", Options: []string{"bind", "ro"}},
}
env := []string{}
for key, val := range data.Env {
env = append(env, fmt.Sprintf("%s=%s", key, val))
}
r := containerd.Containerd{}
return r.Run(
data,
args,
runner.WithNamespace(criconstants.K8sContainerdNamespace),
runner.WithContainerImage(image),
runner.WithEnv(env),
runner.WithOCISpecOpts(
containerd.WithMemoryLimit(int64(1000000*512)),
containerd.WithRootfsPropagation("slave"),
oci.WithMounts(mounts),
oci.WithHostNamespace(specs.PIDNamespace),
oci.WithParentCgroupDevices,
oci.WithPrivileged,
),
runner.WithType(runner.Once),
)
}
func enforceMasterOverrides(initConfiguration *kubeadmapi.InitConfiguration) {
initConfiguration.KubernetesVersion = constants.KubernetesVersion
initConfiguration.UseHyperKubeImage = true
}
func writeKubeadmConfig(data *userdata.UserData) (err error) {
var b []byte
if data.IsBootstrap() {
initConfiguration, ok := data.Services.Kubeadm.Configuration.(*kubeadmapi.InitConfiguration)
if !ok {
return errors.New("expected InitConfiguration")
}
initConfiguration.NodeRegistration.CRISocket = defaults.DefaultAddress
enforceMasterOverrides(initConfiguration)
if err = cis.EnforceMasterRequirements(initConfiguration); err != nil {
return err
}
b, err = configutil.MarshalKubeadmConfigObject(initConfiguration)
if err != nil {
return err
}
} else {
joinConfiguration, ok := data.Services.Kubeadm.Configuration.(*kubeadmapi.JoinConfiguration)
if !ok {
return errors.New("expected JoinConfiguration")
}
joinConfiguration.NodeRegistration.CRISocket = defaults.DefaultAddress
if err = cis.EnforceWorkerRequirements(joinConfiguration); err != nil {
return err
}
b, err = configutil.MarshalKubeadmConfigObject(joinConfiguration)
if err != nil {
return err
}
}
p := path.Dir(constants.KubeadmConfig)
if err = os.MkdirAll(p, os.ModeDir); err != nil {
return fmt.Errorf("create %s: %v", p, err)
}
if err = ioutil.WriteFile(constants.KubeadmConfig, b, 0400); err != nil {
return fmt.Errorf("write %s: %v", constants.KubeadmConfig, err)
}
return nil
}
func writeKubeadmPKIFiles(data *x509.PEMEncodedCertificateAndKey) (err error) {
if data == nil {
return nil
}
if data.Crt != nil {
if err = os.MkdirAll(path.Dir(constants.KubeadmCACert), 0600); err != nil {
return err
}
if err = ioutil.WriteFile(constants.KubeadmCACert, data.Crt, 0400); err != nil {
return fmt.Errorf("write %s: %v", constants.KubeadmCACert, err)
}
}
if data.Key != nil {
if err = os.MkdirAll(path.Dir(constants.KubeadmCAKey), 0600); err != nil {
return err
}
if err = ioutil.WriteFile(constants.KubeadmCAKey, data.Key, 0400); err != nil {
return fmt.Errorf("write %s: %v", constants.KubeadmCAKey, err)
}
}
return nil
}
func writeFiles(client proto.TrustdClient, files []string) (err error) {
errChan := make(chan error)
doneChan := make(chan bool)
ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancelFunc()
go func() {
<-ctx.Done()
errChan <- ctx.Err()
}()
go func() {
var err error
for _, f := range files {
L:
req := &proto.ReadFileRequest{
Path: f,
}
var resp *proto.ReadFileResponse
if resp, err = client.ReadFile(context.Background(), req); err != nil {
log.Printf("failed to read file %s: %v", f, err)
time.Sleep(1 * time.Second)
goto L
}
if err = ioutil.WriteFile(f, resp.Data, 0400); err != nil {
log.Printf("failed to read file %s: %v", f, err)
time.Sleep(1 * time.Second)
goto L
}
}
doneChan <- true
}()
select {
case err := <-errChan:
return err
case <-doneChan:
return nil
}
}