-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
provider.go
451 lines (409 loc) · 13.9 KB
/
provider.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
450
451
/*
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 impliep.
See the License for the specific language governing permissions and
limitations under the License.
*/
package podman
import (
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
"sigs.k8s.io/kind/pkg/errors"
"sigs.k8s.io/kind/pkg/exec"
"sigs.k8s.io/kind/pkg/log"
internallogs "sigs.k8s.io/kind/pkg/cluster/internal/logs"
"sigs.k8s.io/kind/pkg/cluster/internal/providers"
"sigs.k8s.io/kind/pkg/cluster/internal/providers/common"
"sigs.k8s.io/kind/pkg/internal/apis/config"
"sigs.k8s.io/kind/pkg/internal/cli"
"sigs.k8s.io/kind/pkg/internal/sets"
"sigs.k8s.io/kind/pkg/internal/version"
)
// NewProvider returns a new provider based on executing `podman ...`
func NewProvider(logger log.Logger) providers.Provider {
logger.Warn("enabling experimental podman provider")
return &provider{
logger: logger,
}
}
// Provider implements provider.Provider
// see NewProvider
type provider struct {
logger log.Logger
info *providers.ProviderInfo
}
// String implements fmt.Stringer
// NOTE: the value of this should not currently be relied upon for anything!
// This is only used for setting the Node's providerID
func (p *provider) String() string {
return "podman"
}
// Provision is part of the providers.Provider interface
func (p *provider) Provision(status *cli.Status, cfg *config.Cluster) (err error) {
if err := ensureMinVersion(); err != nil {
return err
}
// TODO: validate cfg
// ensure node images are pulled before actually provisioning
if err := ensureNodeImages(p.logger, status, cfg); err != nil {
return err
}
// ensure the pre-requisite network exists
networkName := fixedNetworkName
if n := os.Getenv("KIND_EXPERIMENTAL_PODMAN_NETWORK"); n != "" {
p.logger.Warn("WARNING: Overriding podman network due to KIND_EXPERIMENTAL_PODMAN_NETWORK")
p.logger.Warn("WARNING: Here be dragons! This is not supported currently.")
networkName = n
}
if err := ensureNetwork(networkName); err != nil {
return errors.Wrap(err, "failed to ensure podman network")
}
// actually provision the cluster
icons := strings.Repeat("📦 ", len(cfg.Nodes))
status.Start(fmt.Sprintf("Preparing nodes %s", icons))
defer func() { status.End(err == nil) }()
// plan creating the containers
createContainerFuncs, err := planCreation(cfg, networkName)
if err != nil {
return err
}
// actually create nodes
return errors.UntilErrorConcurrent(createContainerFuncs)
}
// ListClusters is part of the providers.Provider interface
func (p *provider) ListClusters() ([]string, error) {
cmd := exec.Command("podman",
"ps",
"-a", // show stopped nodes
// filter for nodes with the cluster label
"--filter", "label="+clusterLabelKey,
// format to include the cluster name
"--format", fmt.Sprintf(`{{index .Labels "%s"}}`, clusterLabelKey),
)
lines, err := exec.OutputLines(cmd)
if err != nil {
return nil, errors.Wrap(err, "failed to list clusters")
}
return sets.NewString(lines...).List(), nil
}
// ListNodes is part of the providers.Provider interface
func (p *provider) ListNodes(cluster string) ([]nodes.Node, error) {
cmd := exec.Command("podman",
"ps",
"-a", // show stopped nodes
// filter for nodes with the cluster label
"--filter", fmt.Sprintf("label=%s=%s", clusterLabelKey, cluster),
// format to include the cluster name
"--format", `{{.Names}}`,
)
lines, err := exec.OutputLines(cmd)
if err != nil {
return nil, errors.Wrap(err, "failed to list nodes")
}
// convert names to node handles
ret := make([]nodes.Node, 0, len(lines))
for _, name := range lines {
ret = append(ret, p.node(name))
}
return ret, nil
}
// DeleteNodes is part of the providers.Provider interface
func (p *provider) DeleteNodes(n []nodes.Node) error {
if len(n) == 0 {
return nil
}
const command = "podman"
args := make([]string, 0, len(n)+3) // allocate once
args = append(args,
"rm",
"-f", // force the container to be delete now
"-v", // delete volumes
)
for _, node := range n {
args = append(args, node.String())
}
if err := exec.Command(command, args...).Run(); err != nil {
return errors.Wrap(err, "failed to delete nodes")
}
var nodeVolumes []string
for _, node := range n {
volumes, err := getVolumes(node.String())
if err != nil {
return err
}
nodeVolumes = append(nodeVolumes, volumes...)
}
if len(nodeVolumes) == 0 {
return nil
}
return deleteVolumes(nodeVolumes)
}
// getHostIPOrDefault defaults HostIP to localhost if is not set
// xref: https://github.com/kubernetes-sigs/kind/issues/3777
func getHostIPOrDefault(hostIP string) string {
if hostIP == "" {
return "127.0.0.1"
}
return hostIP
}
// GetAPIServerEndpoint is part of the providers.Provider interface
func (p *provider) GetAPIServerEndpoint(cluster string) (string, error) {
// locate the node that hosts this
allNodes, err := p.ListNodes(cluster)
if err != nil {
return "", errors.Wrap(err, "failed to list nodes")
}
n, err := nodeutils.APIServerEndpointNode(allNodes)
if err != nil {
return "", errors.Wrap(err, "failed to get api server endpoint")
}
// TODO: get rid of this once podman settles on how to get the port mapping using podman inspect
// This is only used to get the Kubeconfig server field
v, err := getPodmanVersion()
if err != nil {
return "", errors.Wrap(err, "failed to check podman version")
}
// podman inspect was broken between 2.2.0 and 3.0.0
// https://github.com/containers/podman/issues/8444
if v.AtLeast(version.MustParseSemantic("2.2.0")) &&
v.LessThan(version.MustParseSemantic("3.0.0")) {
p.logger.Warnf("WARNING: podman version %s not fully supported, please use versions 3.0.0+")
cmd := exec.Command(
"podman", "inspect",
"--format",
"{{range .NetworkSettings.Ports }}{{range .}}{{.HostIP}}/{{.HostPort}}{{end}}{{end}}",
n.String(),
)
lines, err := exec.OutputLines(cmd)
if err != nil {
return "", errors.Wrap(err, "failed to get api server port")
}
if len(lines) != 1 {
return "", errors.Errorf("network details should only be one line, got %d lines", len(lines))
}
// output is in the format IP/Port
parts := strings.Split(strings.TrimSpace(lines[0]), "/")
if len(parts) != 2 {
return "", errors.Errorf("network details should be in the format IP/Port, received: %s", parts)
}
host := parts[0]
port, err := strconv.Atoi(parts[1])
if err != nil {
return "", errors.Errorf("network port not an integer: %v", err)
}
return net.JoinHostPort(host, strconv.Itoa(port)), nil
}
cmd := exec.Command(
"podman", "inspect",
"--format",
"{{ json .NetworkSettings.Ports }}",
n.String(),
)
lines, err := exec.OutputLines(cmd)
if err != nil {
return "", errors.Wrap(err, "failed to get api server port")
}
if len(lines) != 1 {
return "", errors.Errorf("network details should only be one line, got %d lines", len(lines))
}
// portMapping19 maps to the standard CNI portmapping capability used in podman 1.9
// see: https://github.com/containernetworking/cni/blob/spec-v0.4.0/CONVENTIONS.md
type portMapping19 struct {
HostPort int32 `json:"hostPort"`
ContainerPort int32 `json:"containerPort"`
Protocol string `json:"protocol"`
HostIP string `json:"hostIP"`
}
// portMapping20 maps to the podman 2.0 portmap type
// see: https://github.com/containers/podman/blob/05988fc74fc25f2ad2256d6e011dfb7ad0b9a4eb/libpod/define/container_inspect.go#L134-L143
type portMapping20 struct {
HostPort string `json:"HostPort"`
HostIP string `json:"HostIp"`
}
portMappings20 := make(map[string][]portMapping20)
if err := json.Unmarshal([]byte(lines[0]), &portMappings20); err == nil {
for k, v := range portMappings20 {
protocol := "tcp"
parts := strings.Split(k, "/")
if len(parts) == 2 {
protocol = strings.ToLower(parts[1])
}
containerPort, err := strconv.Atoi(parts[0])
if err != nil {
return "", err
}
for _, pm := range v {
if containerPort == common.APIServerInternalPort && protocol == "tcp" {
return net.JoinHostPort(getHostIPOrDefault(pm.HostIP), pm.HostPort), nil
}
}
}
}
var portMappings19 []portMapping19
if err := json.Unmarshal([]byte(lines[0]), &portMappings19); err != nil {
return "", errors.Errorf("invalid network details: %v", err)
}
for _, pm := range portMappings19 {
if pm.ContainerPort == common.APIServerInternalPort && pm.Protocol == "tcp" {
return net.JoinHostPort(getHostIPOrDefault(pm.HostIP), strconv.Itoa(int(pm.HostPort))), nil
}
}
return "", errors.Errorf("failed to get api server port")
}
// GetAPIServerInternalEndpoint is part of the providers.Provider interface
func (p *provider) GetAPIServerInternalEndpoint(cluster string) (string, error) {
// locate the node that hosts this
allNodes, err := p.ListNodes(cluster)
if err != nil {
return "", errors.Wrap(err, "failed to list nodes")
}
n, err := nodeutils.APIServerEndpointNode(allNodes)
if err != nil {
return "", errors.Wrap(err, "failed to get apiserver endpoint")
}
// NOTE: we're using the nodes's hostnames which are their names
return net.JoinHostPort(n.String(), fmt.Sprintf("%d", common.APIServerInternalPort)), nil
}
// node returns a new node handle for this provider
func (p *provider) node(name string) nodes.Node {
return &node{
name: name,
}
}
// CollectLogs will populate dir with cluster logs and other debug files
func (p *provider) CollectLogs(dir string, nodes []nodes.Node) error {
execToPathFn := func(cmd exec.Cmd, path string) func() error {
return func() error {
f, err := common.FileOnHost(path)
if err != nil {
return err
}
defer f.Close()
return cmd.SetStdout(f).SetStderr(f).Run()
}
}
// construct a slice of methods to collect logs
fns := []func() error{
// record info about the host podman
execToPathFn(
exec.Command("podman", "info"),
filepath.Join(dir, "podman-info.txt"),
),
}
// collect /var/log for each node and plan collecting more logs
var errs []error
for _, n := range nodes {
node := n // https://golang.org/doc/faq#closures_and_goroutines
name := node.String()
path := filepath.Join(dir, name)
if err := internallogs.DumpDir(p.logger, node, "/var/log", path); err != nil {
errs = append(errs, err)
}
fns = append(fns,
func() error { return common.CollectLogs(node, path) },
execToPathFn(exec.Command("podman", "inspect", name), filepath.Join(path, "inspect.json")),
func() error {
f, err := common.FileOnHost(filepath.Join(path, "serial.log"))
if err != nil {
return err
}
return node.SerialLogs(f)
},
)
}
// run and collect up all errors
errs = append(errs, errors.AggregateConcurrent(fns))
return errors.NewAggregate(errs)
}
// Info returns the provider info.
// The info is cached on the first time of the execution.
func (p *provider) Info() (*providers.ProviderInfo, error) {
if p.info == nil {
var err error
p.info, err = info(p.logger)
if err != nil {
return p.info, err
}
}
return p.info, nil
}
// podmanInfo corresponds to `podman info --format 'json`.
// The structure is different from `docker info --format '{{json .}}'`,
// and lacks information about the availability of the cgroup controllers.
type podmanInfo struct {
Host struct {
CgroupVersion string `json:"cgroupVersion,omitempty"` // "v2"
CgroupControllers []string `json:"cgroupControllers,omitempty"`
Security struct {
Rootless bool `json:"rootless,omitempty"`
} `json:"security"`
} `json:"host"`
}
// info detects ProviderInfo by executing `podman info --format json`.
func info(logger log.Logger) (*providers.ProviderInfo, error) {
const podman = "podman"
args := []string{"info", "--format", "json"}
cmd := exec.Command(podman, args...)
out, err := exec.Output(cmd)
if err != nil {
return nil, errors.Wrapf(err, "failed to get podman info (%s %s): %q",
podman, strings.Join(args, " "), string(out))
}
var pInfo podmanInfo
if err := json.Unmarshal(out, &pInfo); err != nil {
return nil, err
}
stringSliceContains := func(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
// Since Podman version before v4.0.0 does not gives controller info.
// We assume all the cgroup controllers to be available.
// For rootless, this assumption is not always correct,
// so we print the warning below.
cgroupSupportsMemoryLimit := true
cgroupSupportsPidsLimit := true
cgroupSupportsCPUShares := true
v, err := getPodmanVersion()
if err != nil {
return nil, errors.Wrap(err, "failed to check podman version")
}
// Info for controllers must be available after v4.0.0
// via https://github.com/containers/podman/pull/10387
if v.AtLeast(version.MustParseSemantic("4.0.0")) {
cgroupSupportsMemoryLimit = stringSliceContains(pInfo.Host.CgroupControllers, "memory")
cgroupSupportsPidsLimit = stringSliceContains(pInfo.Host.CgroupControllers, "pids")
cgroupSupportsCPUShares = stringSliceContains(pInfo.Host.CgroupControllers, "cpu")
}
info := &providers.ProviderInfo{
Rootless: pInfo.Host.Security.Rootless,
Cgroup2: pInfo.Host.CgroupVersion == "v2",
SupportsMemoryLimit: cgroupSupportsMemoryLimit,
SupportsPidsLimit: cgroupSupportsPidsLimit,
SupportsCPUShares: cgroupSupportsCPUShares,
}
if info.Rootless && !v.AtLeast(version.MustParseSemantic("4.0.0")) {
if logger != nil {
logger.Warn("Cgroup controller detection is not implemented for Podman. " +
"If you see cgroup-related errors, you might need to set systemd property \"Delegate=yes\", see https://kind.sigs.k8s.io/docs/user/rootless/")
}
}
return info, nil
}