@@ -20,12 +20,14 @@ package config
2020import (
2121 "bytes"
2222 "fmt"
23+ "io"
2324 "net"
2425 "strings"
2526
2627 "sigs.k8s.io/kind/pkg/cluster/constants"
2728 "sigs.k8s.io/kind/pkg/cluster/nodes"
2829 "sigs.k8s.io/kind/pkg/errors"
30+ "sigs.k8s.io/kind/pkg/exec"
2931
3032 "sigs.k8s.io/kind/pkg/cluster/internal/create/actions"
3133 "sigs.k8s.io/kind/pkg/cluster/internal/kubeadm"
@@ -43,6 +45,8 @@ func NewAction() actions.Action {
4345 return & Action {}
4446}
4547
48+ var hosts map [string ]string
49+
4650// Execute runs the action
4751func (a * Action ) Execute (ctx * actions.ActionContext ) error {
4852 ctx .Status .Start ("Writing configuration 📜" )
@@ -78,6 +82,8 @@ func (a *Action) Execute(ctx *actions.ActionContext) error {
7882 RuntimeConfig : ctx .Config .RuntimeConfig ,
7983 }
8084
85+ hosts = map [string ]string {}
86+
8187 kubeadmConfigPlusPatches := func (node nodes.Node , data kubeadm.ConfigData ) func () error {
8288 return func () error {
8389 data .NodeName = node .String ()
@@ -124,6 +130,25 @@ func (a *Action) Execute(ctx *actions.ActionContext) error {
124130 return err
125131 }
126132
133+ hostData := ""
134+ for name , ip := range hosts {
135+ hostData = hostData + ip + " " + name + "\n "
136+ }
137+ fmt .Printf ("hostData = \n %v\n " , hostData )
138+ for _ , nodeName := range []string {"kind-control-plane" , "kind-worker" , "kind-worker2" , "kind-worker3" } {
139+ // docker exec NODE cat >> /etc/hosts <<< hostData
140+ err := exec .RunWithStdinWriter (
141+ exec .Command ("docker" , "exec" , "-i" , nodeName , "bash" , "-c" , "cat >> /etc/hosts" ),
142+ func (pipe io.Writer ) error {
143+ _ , err := pipe .Write ([]byte (hostData ))
144+ return err
145+ },
146+ )
147+ if err != nil {
148+ fmt .Printf ("ERROR: %v\n " , err )
149+ }
150+ }
151+
127152 // if we have containerd config, patch all the nodes concurrently
128153 if len (ctx .Config .ContainerdConfigPatches ) > 0 || len (ctx .Config .ContainerdConfigPatchesJSON6902 ) > 0 {
129154 // we only want to patch kubernetes nodes
@@ -200,6 +225,8 @@ func getKubeadmConfig(cfg *config.Cluster, data kubeadm.ConfigData, node nodes.N
200225 }
201226
202227 data .NodeAddress = nodeAddress
228+ hosts [data .NodeName ] = data .NodeAddress
229+
203230 // configure the right protocol addresses
204231 if cfg .Networking .IPFamily == "ipv6" {
205232 if ip := net .ParseIP (nodeAddressIPv6 ); ip .To16 () == nil {
0 commit comments