Skip to content

Commit 0ecac9f

Browse files
author
Neil Jerram
committed
Hack hosts
1 parent 3b2de39 commit 0ecac9f

File tree

1 file changed

+27
-0
lines changed
  • pkg/cluster/internal/create/actions/config

1 file changed

+27
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ package config
2020
import (
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
4751
func (a *Action) Execute(ctx *actions.ActionContext) error {
4852
ctx.Status.Start("Writing configuration 📜")
@@ -84,6 +88,8 @@ func (a *Action) Execute(ctx *actions.ActionContext) error {
8488
RootlessProvider: providerInfo.Rootless,
8589
}
8690

91+
hosts = map[string]string{}
92+
8793
kubeadmConfigPlusPatches := func(node nodes.Node, data kubeadm.ConfigData) func() error {
8894
return func() error {
8995
data.NodeName = node.String()
@@ -115,6 +121,25 @@ func (a *Action) Execute(ctx *actions.ActionContext) error {
115121
return err
116122
}
117123

124+
hostData := ""
125+
for name, ip := range hosts {
126+
hostData = hostData + ip + " " + name + "\n"
127+
}
128+
fmt.Printf("hostData = \n%v\n", hostData)
129+
for _, nodeName := range []string{"kind-control-plane", "kind-worker", "kind-worker2", "kind-worker3"} {
130+
// docker exec NODE cat >> /etc/hosts <<< hostData
131+
err := exec.RunWithStdinWriter(
132+
exec.Command("docker", "exec", "-i", nodeName, "bash", "-c", "cat >> /etc/hosts"),
133+
func(pipe io.Writer) error {
134+
_, err := pipe.Write([]byte(hostData))
135+
return err
136+
},
137+
)
138+
if err != nil {
139+
fmt.Printf("ERROR: %v\n", err)
140+
}
141+
}
142+
118143
// if we have containerd config, patch all the nodes concurrently
119144
if len(ctx.Config.ContainerdConfigPatches) > 0 || len(ctx.Config.ContainerdConfigPatchesJSON6902) > 0 {
120145
fns := make([]func() error, len(kubeNodes))
@@ -186,6 +211,8 @@ func getKubeadmConfig(cfg *config.Cluster, data kubeadm.ConfigData, node nodes.N
186211
}
187212

188213
data.NodeAddress = nodeAddress
214+
hosts[data.NodeName] = data.NodeAddress
215+
189216
// configure the right protocol addresses
190217
if cfg.Networking.IPFamily == config.IPv6Family || cfg.Networking.IPFamily == config.DualStackFamily {
191218
if ip := net.ParseIP(nodeAddressIPv6); ip.To16() == nil {

0 commit comments

Comments
 (0)