-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathnode.go
442 lines (394 loc) · 13.2 KB
/
node.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
/*
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 nodes
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/version"
"sigs.k8s.io/kind/pkg/cluster/constants"
"sigs.k8s.io/kind/pkg/container/docker"
"sigs.k8s.io/kind/pkg/exec"
"sigs.k8s.io/kind/pkg/util"
)
// Node represents a handle to a kind node
// This struct must be created by one of: CreateControlPlane
// It should not be manually instantiated
// Node impleemnts exec.Cmder
type Node struct {
// must be one of docker container ID or name
name string
// cached node info etc.
cache *nodeCache
}
// assert Node implements Cmder
var _ exec.Cmder = &Node{}
// Cmder returns an exec.Cmder that runs on the node via docker exec
func (n *Node) Cmder() exec.Cmder {
return docker.ContainerCmder(n.name)
}
// Command returns a new exec.Cmd that will run on the node
func (n *Node) Command(command string, args ...string) exec.Cmd {
return n.Cmder().Command(command, args...)
}
// this is a separate struct so we can more easily ensure that this portion is
// thread safe
type nodeCache struct {
mu sync.RWMutex
kubernetesVersion string
ip string
ports map[int32]int32
role string
}
func (cache *nodeCache) set(setter func(*nodeCache)) {
cache.mu.Lock()
defer cache.mu.Unlock()
setter(cache)
}
func (cache *nodeCache) KubeVersion() string {
cache.mu.RLock()
defer cache.mu.RUnlock()
return cache.kubernetesVersion
}
func (cache *nodeCache) IP() string {
cache.mu.RLock()
defer cache.mu.RUnlock()
return cache.ip
}
func (cache *nodeCache) HostPort(p int32) (int32, bool) {
cache.mu.RLock()
defer cache.mu.RUnlock()
if cache.ports == nil {
return 0, false
}
v, ok := cache.ports[p]
return v, ok
}
func (cache *nodeCache) Role() string {
cache.mu.RLock()
defer cache.mu.RUnlock()
return cache.role
}
func (n *Node) String() string {
return n.name
}
// Name returns the node's name
func (n *Node) Name() string {
return n.name
}
// SignalStart sends SIGUSR1 to the node, which signals our entrypoint to boot
// see images/node/entrypoint
func (n *Node) SignalStart() error {
return docker.Kill("SIGUSR1", n.name)
}
// CopyTo copies the source file on the host to dest on the node
func (n *Node) CopyTo(source, dest string) error {
return docker.CopyTo(source, n.name, dest)
}
// CopyFrom copies the source file on the node to dest on the host
// TODO(fabrizio pandini): note that this does have limitations around symlinks
// but this should go away when kubeadm automatic copy certs lands,
// otherwise it should be refactored in something more robust in the long term
func (n *Node) CopyFrom(source, dest string) error {
return docker.CopyFrom(n.name, source, dest)
}
// WaitForDocker waits for Docker to be ready on the node
// it returns true on success, and false on a timeout
func (n *Node) WaitForDocker(until time.Time) bool {
return tryUntil(until, func() bool {
cmd := n.Command("systemctl", "is-active", "docker")
out, err := exec.CombinedOutputLines(cmd)
if err != nil {
return false
}
return len(out) == 1 && out[0] == "active"
})
}
// helper that calls `try()`` in a loop until the deadline `until`
// has passed or `try()`returns true, returns wether try ever returned true
func tryUntil(until time.Time, try func() bool) bool {
for until.After(time.Now()) {
if try() {
return true
}
}
return false
}
// LoadImages loads image tarballs stored on the node into docker on the node
func (n *Node) LoadImages() {
// load images cached on the node into docker
if err := n.Command(
"/bin/bash", "-c",
// use xargs to load images in parallel
`find /kind/images -name *.tar -print0 | xargs -0 -n 1 -P $(nproc) docker load -i`,
).Run(); err != nil {
log.Warningf("Failed to preload docker images: %v", err)
return
}
// if this fails, we don't care yet, but try to get the kubernetes version
// and see if we can skip retagging for amd64
// if this fails, we can just assume some unknown version and re-tag
// in a future release of kind, we can probably drop v1.11 support
// and remove the logic below this comment entirely
if rawVersion, err := n.KubeVersion(); err == nil {
if ver, err := version.ParseGeneric(rawVersion); err == nil {
if !ver.LessThan(version.MustParseSemantic("v1.12.0")) {
return
}
}
}
// for older releases, we need the images to have the arch in their name
// bazel built images were missing these, newer releases do not use them
// for any builds ...
// retag images that are missing -amd64 as image:tag -> image-amd64:tag
// TODO(bentheelder): this is a bit gross, move this logic out of bash
if err := n.Command(
"/bin/bash", "-c",
fmt.Sprintf(`docker images --format='{{.Repository}}:{{.Tag}}' | grep -v %s | xargs -L 1 -I '{}' /bin/bash -c 'docker tag "{}" "$(echo "{}" | sed s/:/-%s:/)"'`,
util.GetArch(), util.GetArch()),
).Run(); err != nil {
log.Warningf("Failed to re-tag docker images: %v", err)
}
}
// FixMounts will correct mounts in the node container to meet the right
// sharing and permissions for systemd and Docker / Kubernetes
func (n *Node) FixMounts() error {
// Check if userns-remap is enabled
if docker.UsernsRemap() {
// The binary /bin/mount should be owned by root:root in order to execute
// the following mount commands
if err := n.Command("chown", "root:root", "/bin/mount").Run(); err != nil {
return err
}
// The binary /bin/mount should have the setuid bit
if err := n.Command("chmod", "-s", "/bin/mount").Run(); err != nil {
return err
}
}
// systemd-in-a-container should have read only /sys
// https://www.freedesktop.org/wiki/Software/systemd/ContainerInterface/
// however, we need other things from `docker run --privileged` ...
// and this flag also happens to make /sys rw, amongst other things
if err := n.Command("mount", "-o", "remount,ro", "/sys").Run(); err != nil {
return err
}
// kubernetes needs shared mount propagation
if err := n.Command("mount", "--make-shared", "/").Run(); err != nil {
return err
}
if err := n.Command("mount", "--make-shared", "/run").Run(); err != nil {
return err
}
if err := n.Command("mount", "--make-shared", "/var/lib/docker").Run(); err != nil {
return err
}
return nil
}
// KubeVersion returns the Kubernetes version installed on the node
func (n *Node) KubeVersion() (version string, err error) {
// use the cached version first
cachedVersion := n.cache.KubeVersion()
if cachedVersion != "" {
return cachedVersion, nil
}
// grab kubernetes version from the node image
cmd := n.Command("cat", "/kind/version")
lines, err := exec.CombinedOutputLines(cmd)
if err != nil {
return "", errors.Wrap(err, "failed to get file")
}
if len(lines) != 1 {
return "", errors.Errorf("file should only be one line, got %d lines", len(lines))
}
version = lines[0]
n.cache.set(func(cache *nodeCache) {
cache.kubernetesVersion = version
})
return version, nil
}
// IP returns the IP address of the node
func (n *Node) IP() (ip string, err error) {
// use the cached version first
cachedIP := n.cache.IP()
if cachedIP != "" {
return cachedIP, nil
}
// retrive the IP address of the node using docker inspect
lines, err := docker.Inspect(n.name, "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}")
if err != nil {
return "", errors.Wrap(err, "failed to get file")
}
if len(lines) != 1 {
return "", errors.Errorf("file should only be one line, got %d lines", len(lines))
}
ip = lines[0]
n.cache.set(func(cache *nodeCache) {
cache.ip = ip
})
return ip, nil
}
// Ports returns a specific port mapping for the node
// Node by convention use well known ports internally, while random port
// are used for making the `kind` cluster accessible from the host machine
func (n *Node) Ports(containerPort int32) (hostPort int32, err error) {
// use the cached version first
hostPort, isCached := n.cache.HostPort(containerPort)
if isCached {
return hostPort, nil
}
// retrive the specific port mapping using docker inspect
lines, err := docker.Inspect(n.name, fmt.Sprintf("{{(index (index .NetworkSettings.Ports \"%d/tcp\") 0).HostPort}}", containerPort))
if err != nil {
return -1, errors.Wrap(err, "failed to get file")
}
if len(lines) != 1 {
return -1, errors.Errorf("file should only be one line, got %d lines", len(lines))
}
parsed, err := strconv.ParseInt(lines[0], 10, 32)
if err != nil {
return -1, errors.Wrap(err, "failed to get file")
}
hostPort = int32(parsed)
// cache it
n.cache.set(func(cache *nodeCache) {
if cache.ports == nil {
cache.ports = map[int32]int32{}
}
cache.ports[containerPort] = hostPort
})
return hostPort, nil
}
// Role returns the role of the node
func (n *Node) Role() (role string, err error) {
role = n.cache.Role()
// use the cached version first
if role != "" {
return role, nil
}
// retrive the role the node using docker inspect
lines, err := docker.Inspect(n.name, fmt.Sprintf("{{index .Config.Labels %q}}", constants.NodeRoleKey))
if err != nil {
return "", errors.Wrapf(err, "failed to get %q label", constants.NodeRoleKey)
}
if len(lines) != 1 {
return "", errors.Errorf("%q label should only be one line, got %d lines", constants.NodeRoleKey, len(lines))
}
role = strings.Trim(lines[0], "'")
n.cache.set(func(cache *nodeCache) {
cache.role = role
})
return role, nil
}
// matches kubeconfig server entry like:
// server: https://172.17.0.2:6443
// which we rewrite to:
// server: https://localhost:$PORT
var serverAddressRE = regexp.MustCompile(`^(\s+server:) https://.*:\d+$`)
// WriteKubeConfig writes a fixed KUBECONFIG to dest
// this should only be called on a control plane node
// While copyng to the host machine the control plane address
// is replaced with local host and the control plane port with
// a randomly generated port reserved during node creation.
func (n *Node) WriteKubeConfig(dest string, hostPort int32) error {
cmd := n.Command("cat", "/etc/kubernetes/admin.conf")
lines, err := exec.CombinedOutputLines(cmd)
if err != nil {
return errors.Wrap(err, "failed to get kubeconfig from node")
}
// fix the config file, swapping out the server for the forwarded localhost:port
var buff bytes.Buffer
for _, line := range lines {
match := serverAddressRE.FindStringSubmatch(line)
if len(match) > 1 {
line = fmt.Sprintf("%s https://localhost:%d", match[1], hostPort)
}
buff.WriteString(line)
buff.WriteString("\n")
}
// create the directory to contain the KUBECONFIG file.
// 0755 is taken from client-go's config handling logic: https://github.com/kubernetes/client-go/blob/5d107d4ebc00ee0ea606ad7e39fd6ce4b0d9bf9e/tools/clientcmd/loader.go#L412
err = os.MkdirAll(filepath.Dir(dest), 0755)
if err != nil {
return errors.Wrap(err, "failed to create kubeconfig output directory")
}
return ioutil.WriteFile(dest, buff.Bytes(), 0600)
}
// WriteFile writes content to dest on the node
func (n *Node) WriteFile(dest, content string) error {
// create destination directory
cmd := n.Command("mkdir", "-p", filepath.Dir(dest))
err := exec.RunLoggingOutputOnFail(cmd)
if err != nil {
return errors.Wrapf(err, "failed to create directory %s", dest)
}
return n.Command("cp", "/dev/stdin", dest).SetStdin(strings.NewReader(content)).Run()
}
// NeedProxy returns true if the host environment appears to have proxy settings
func NeedProxy() bool {
details := getProxyDetails()
return len(details.Envs) > 0
}
// SetProxy configures proxy settings for the node
//
// Currently it only creates systemd drop-in for Docker daemon
// as described in Docker documentation: https://docs.docker.com/config/daemon/systemd/#http-proxy
//
// See also: NeedProxy and getProxyDetails
func (n *Node) SetProxy() error {
details := getProxyDetails()
// configure Docker daemon to use proxy
proxies := ""
for key, val := range details.Envs {
proxies += fmt.Sprintf("\"%s=%s\" ", key, val)
}
err := n.WriteFile("/etc/systemd/system/docker.service.d/http-proxy.conf",
"[Service]\nEnvironment="+proxies)
if err != nil {
errors.Wrap(err, "failed to create http-proxy drop-in")
}
return nil
}
// proxyDetails contains proxy settings discovered on the host
type proxyDetails struct {
Envs map[string]string
// future proxy details here
}
// getProxyDetails returns a struct with the host environment proxy settings
// that should be passed to the nodes
func getProxyDetails() proxyDetails {
var proxyEnvs = []string{"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"}
var val string
var details proxyDetails
details.Envs = make(map[string]string)
for _, name := range proxyEnvs {
val = os.Getenv(name)
if val != "" {
details.Envs[name] = val
} else {
val = os.Getenv(strings.ToLower(name))
if val != "" {
details.Envs[name] = val
}
}
}
return details
}