diff --git a/pkg/cluster/internal/create/actions/config/config.go b/pkg/cluster/internal/create/actions/config/config.go index cfe970f388..85a3fea9a1 100644 --- a/pkg/cluster/internal/create/actions/config/config.go +++ b/pkg/cluster/internal/create/actions/config/config.go @@ -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" @@ -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 📜") @@ -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() @@ -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 @@ -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 {