Skip to content

Commit

Permalink
Hack hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Jerram committed Feb 22, 2021
1 parent 856951b commit eac22e6
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/cluster/internal/create/actions/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ package config
import (
"bytes"
"fmt"
"io"
"net"
"strings"

"sigs.k8s.io/kind/pkg/cluster/constants"
"sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/errors"
"sigs.k8s.io/kind/pkg/exec"

"sigs.k8s.io/kind/pkg/cluster/internal/create/actions"
"sigs.k8s.io/kind/pkg/cluster/internal/kubeadm"
Expand All @@ -43,6 +45,8 @@ func NewAction() actions.Action {
return &Action{}
}

var hosts map[string]string

// Execute runs the action
func (a *Action) Execute(ctx *actions.ActionContext) error {
ctx.Status.Start("Writing configuration 📜")
Expand Down Expand Up @@ -78,6 +82,8 @@ func (a *Action) Execute(ctx *actions.ActionContext) error {
RuntimeConfig: ctx.Config.RuntimeConfig,
}

hosts = map[string]string{}

kubeadmConfigPlusPatches := func(node nodes.Node, data kubeadm.ConfigData) func() error {
return func() error {
data.NodeName = node.String()
Expand Down Expand Up @@ -124,6 +130,25 @@ func (a *Action) Execute(ctx *actions.ActionContext) error {
return err
}

hostData := ""
for name, ip := range hosts {
hostData = hostData + ip + " " + name + "\n"
}
fmt.Printf("hostData = \n%v\n", hostData)
for _, nodeName := range []string{"kind-control-plane", "kind-worker", "kind-worker2", "kind-worker3"} {
// docker exec NODE cat >> /etc/hosts <<< hostData
err := exec.RunWithStdinWriter(
exec.Command("docker", "exec", "-i", nodeName, "bash", "-c", "cat >> /etc/hosts"),
func(pipe io.Writer) error {
_, err := pipe.Write([]byte(hostData))
return err
},
)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
}
}

// if we have containerd config, patch all the nodes concurrently
if len(ctx.Config.ContainerdConfigPatches) > 0 || len(ctx.Config.ContainerdConfigPatchesJSON6902) > 0 {
// we only want to patch kubernetes nodes
Expand Down Expand Up @@ -200,6 +225,8 @@ func getKubeadmConfig(cfg *config.Cluster, data kubeadm.ConfigData, node nodes.N
}

data.NodeAddress = nodeAddress
hosts[data.NodeName] = data.NodeAddress

// configure the right protocol addresses
if cfg.Networking.IPFamily == "ipv6" {
if ip := net.ParseIP(nodeAddressIPv6); ip.To16() == nil {
Expand Down

0 comments on commit eac22e6

Please sign in to comment.