From da0b23331fb9f720eaad75976804204d3c7a6015 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Wed, 7 Oct 2020 08:23:36 +0200 Subject: [PATCH] clusterCreate: less breaking hostIP injection - only print a warning, if hostIP couldn't be fetched - even if the exec returns a non-zero exit code, still try to read the logs --- pkg/cluster/cluster.go | 26 ++++++++++++++------------ pkg/cluster/host.go | 8 ++++---- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index e858bb56..11da96ef 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -360,23 +360,25 @@ func ClusterCreate(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clus // add /etc/hosts and CoreDNS entry for host.k3d.internal, referring to the host system if !cluster.CreateClusterOpts.PrepDisableHostIPInjection { + log.Infoln("(Optional) Trying to get IP of the docker host and inject it into the cluster as 'host.k3d.internal' for easy access") hostIP, err := GetHostIP(clusterPrepCtx, runtime, cluster) if err != nil { - log.Errorln("Failed to get HostIP") - return err + log.Warnln("Failed to get HostIP: %+v", err) } - hostsEntry := fmt.Sprintf("%s %s", hostIP, k3d.DefaultK3dInternalHostRecord) - log.Debugf("Adding extra host entry '%s'...", hostsEntry) - for _, node := range cluster.Nodes { - if err := runtime.ExecInNode(clusterPrepCtx, node, []string{"sh", "-c", fmt.Sprintf("echo '%s' >> /etc/hosts", hostsEntry)}); err != nil { - log.Warnf("Failed to add extra entry '%s' to /etc/hosts in node '%s'", hostsEntry, node.Name) + if hostIP != nil { + hostsEntry := fmt.Sprintf("%s %s", hostIP, k3d.DefaultK3dInternalHostRecord) + log.Debugf("Adding extra host entry '%s'...", hostsEntry) + for _, node := range cluster.Nodes { + if err := runtime.ExecInNode(clusterPrepCtx, node, []string{"sh", "-c", fmt.Sprintf("echo '%s' >> /etc/hosts", hostsEntry)}); err != nil { + log.Warnf("Failed to add extra entry '%s' to /etc/hosts in node '%s'", hostsEntry, node.Name) + } } - } - patchCmd := `test=$(kubectl get cm coredns -n kube-system --template='{{.data.NodeHosts}}' | sed -n -E -e '/[0-9\.]{4,12}\s+host\.k3d\.internal$/!p' -e '$a` + hostsEntry + `' | tr '\n' '^' | xargs -0 printf '{"data": {"NodeHosts":"%s"}}'| sed -E 's%\^%\\n%g') && kubectl patch cm coredns -n kube-system -p="$test"` - err = runtime.ExecInNode(clusterPrepCtx, cluster.Nodes[0], []string{"sh", "-c", patchCmd}) - if err != nil { - log.Warnf("Failed to patch CoreDNS ConfigMap to include entry '%s': %+v", hostsEntry, err) + patchCmd := `test=$(kubectl get cm coredns -n kube-system --template='{{.data.NodeHosts}}' | sed -n -E -e '/[0-9\.]{4,12}\s+host\.k3d\.internal$/!p' -e '$a` + hostsEntry + `' | tr '\n' '^' | xargs -0 printf '{"data": {"NodeHosts":"%s"}}'| sed -E 's%\^%\\n%g') && kubectl patch cm coredns -n kube-system -p="$test"` + err = runtime.ExecInNode(clusterPrepCtx, cluster.Nodes[0], []string{"sh", "-c", patchCmd}) + if err != nil { + log.Warnf("Failed to patch CoreDNS ConfigMap to include entry '%s': %+v", hostsEntry, err) + } } } diff --git a/pkg/cluster/host.go b/pkg/cluster/host.go index a2ab6ef7..de19fc4d 100644 --- a/pkg/cluster/host.go +++ b/pkg/cluster/host.go @@ -76,10 +76,7 @@ func GetHostIP(ctx context.Context, rtime rt.Runtime, cluster *k3d.Cluster) (net func resolveHostnameFromInside(ctx context.Context, rtime rt.Runtime, node *k3d.Node, hostname string) (net.IP, error) { - logreader, err := rtime.ExecInNodeGetLogs(ctx, node, []string{"sh", "-c", fmt.Sprintf("nslookup %s", hostname)}) - if err != nil { - return nil, err - } + logreader, execErr := rtime.ExecInNodeGetLogs(ctx, node, []string{"sh", "-c", fmt.Sprintf("nslookup %s", hostname)}) submatches := map[string]string{} scanner := bufio.NewScanner(logreader) @@ -92,6 +89,9 @@ func resolveHostnameFromInside(ctx context.Context, rtime rt.Runtime, node *k3d. break } if _, ok := submatches["ip"]; !ok { + if execErr != nil { + log.Errorln(execErr) + } return nil, fmt.Errorf("Failed to read address for '%s' from nslookup response", hostname) }