Skip to content

Commit

Permalink
Hack hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Jerram authored and coutinhop committed Aug 19, 2022
1 parent f84fd12 commit 527eca6
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 @@ -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()
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 527eca6

Please sign in to comment.