Skip to content

Commit ef328bf

Browse files
author
Neil Jerram
committed
Support bootstrap routes
1 parent 47c55de commit ef328bf

File tree

8 files changed

+52
-4
lines changed

8 files changed

+52
-4
lines changed

pkg/apis/config/v1alpha4/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cluster/constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package constants
2121
const DefaultClusterName = "kind"
2222

2323
const NodeLoopbackKey = "io.k8s.sigs.kind.loopback"
24+
const NodeRoutesKey = "io.k8s.sigs.kind.routes"
2425

2526
/* node role value constants */
2627
const (

pkg/cluster/internal/create/actions/loopback/loopback.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"sigs.k8s.io/kind/pkg/errors"
2323
"sigs.k8s.io/kind/pkg/internal/cluster/create/actions"
24+
"strings"
2425
)
2526

2627
// kubeadmInitAction implements action for executing the kubadm init
@@ -52,7 +53,7 @@ func (a *action) Execute(ctx *actions.ActionContext) error {
5253
"ip",
5354
"a",
5455
"a",
55-
loopAddress + "/32",
56+
loopAddress+"/32",
5657
"dev",
5758
"lo",
5859
)
@@ -61,6 +62,23 @@ func (a *action) Execute(ctx *actions.ActionContext) error {
6162
return errors.Wrap(err, "failed to set loopback address")
6263
}
6364
}
65+
66+
routes, err := node.Routes()
67+
if err != nil {
68+
fmt.Printf("Loopback action error: %v\n", err)
69+
continue
70+
}
71+
for _, route := range strings.Split(routes, ",") {
72+
fmt.Printf("Install route: %v\n", route)
73+
args := []string{"r", "a"}
74+
args = append(args, strings.Split(route, " ")...)
75+
cmd := node.Command("ip", args...)
76+
err := cmd.Run()
77+
if err != nil {
78+
return errors.Wrap(err, "failed to install route")
79+
}
80+
}
81+
6482
}
6583
return nil
6684
}

pkg/cluster/internal/providers/docker/node.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,25 @@ func (n *node) Loopback() (string, error) {
5757
)
5858
lines, err := exec.OutputLines(cmd)
5959
if err != nil {
60-
return "", errors.Wrap(err, "failed to get role for node")
60+
return "", errors.Wrap(err, "failed to get loopback for node")
6161
}
6262
if len(lines) != 1 {
63-
return "", errors.Errorf("failed to get role for node: output lines %d != 1", len(lines))
63+
return "", errors.Errorf("failed to get loopback for node: output lines %d != 1", len(lines))
64+
}
65+
return lines[0], nil
66+
}
67+
68+
func (n *node) Routes() (string, error) {
69+
cmd := exec.Command("docker", "inspect",
70+
"--format", fmt.Sprintf(`{{ index .Config.Labels "%s"}}`, constants.NodeRoutesKey),
71+
n.name,
72+
)
73+
lines, err := exec.OutputLines(cmd)
74+
if err != nil {
75+
return "", errors.Wrap(err, "failed to get routes for node")
76+
}
77+
if len(lines) != 1 {
78+
return "", errors.Errorf("failed to get routes for node: output lines %d != 1", len(lines))
6479
}
6580
return lines[0], nil
6681
}

pkg/cluster/internal/providers/docker/provision.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ func runArgsForNode(node *config.Node, clusterIPFamily config.ClusterIPFamily, n
245245
// label the node with the role ID
246246
"--label", fmt.Sprintf("%s=%s", nodeRoleLabelKey, node.Role),
247247
"--label", fmt.Sprintf("%s=%s", constants.NodeLoopbackKey, node.Loopback),
248+
"--label", fmt.Sprintf("%s=%s", constants.NodeRoutesKey, strings.Join(node.Routes, ",")),
248249
// running containers in a container requires privileged
249250
// NOTE: we could try to replicate this with --cap-add, and use less
250251
// privileges, but this flag also changes some mounts that are necessary

pkg/cluster/nodes/types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ type Node interface {
3030
// String should return the node name
3131
String() string // see also: fmt.Stringer
3232
// Role should return the node's role
33-
Role() (string, error) // see also: pkg/cluster/constants
33+
Role() (string, error) // see also: pkg/cluster/constants
3434
Loopback() (string, error) // see also: pkg/cluster/constants
35+
Routes() (string, error) // see also: pkg/cluster/constants
3536
// TODO(bentheelder): should return node addresses more generally
3637
// Possibly remove this method in favor of obtaining this detail with
3738
// exec or from the provider

pkg/internal/apis/config/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ type Node struct {
110110

111111
// Loopback IP.
112112
Loopback string
113+
114+
Routes []string
113115
}
114116

115117
// NodeRole defines possible role for nodes in a Kubernetes cluster managed by `kind`

pkg/internal/apis/config/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)