From 0ecac9f729259729f89b519e520017351a3fc3fc Mon Sep 17 00:00:00 2001 From: Neil Jerram Date: Mon, 22 Feb 2021 11:57:30 +0000 Subject: [PATCH] Hack hosts --- .../internal/create/actions/config/config.go | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pkg/cluster/internal/create/actions/config/config.go b/pkg/cluster/internal/create/actions/config/config.go index ef7e22e1cd..a1c13482e0 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 📜") @@ -84,6 +88,8 @@ func (a *Action) Execute(ctx *actions.ActionContext) error { RootlessProvider: providerInfo.Rootless, } + hosts = map[string]string{} + kubeadmConfigPlusPatches := func(node nodes.Node, data kubeadm.ConfigData) func() error { return func() error { data.NodeName = node.String() @@ -115,6 +121,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 { fns := make([]func() error, len(kubeNodes)) @@ -186,6 +211,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 == config.IPv6Family || cfg.Networking.IPFamily == config.DualStackFamily { if ip := net.ParseIP(nodeAddressIPv6); ip.To16() == nil {