diff --git a/cmd/cluster/clusterCreate.go b/cmd/cluster/clusterCreate.go index d8a1e97b0..63a15e49e 100644 --- a/cmd/cluster/clusterCreate.go +++ b/cmd/cluster/clusterCreate.go @@ -134,6 +134,7 @@ func NewCmdClusterCreate() *cobra.Command { cmd.Flags().BoolVar(&updateCurrentContext, "switch-context", true, "Directly switch the default kubeconfig's current-context to the new cluster's context (requires --update-default-kubeconfig)") cmd.Flags().BoolVar(&createClusterOpts.DisableLoadBalancer, "no-lb", false, "Disable the creation of a LoadBalancer in front of the server nodes") cmd.Flags().BoolVar(&noRollback, "no-rollback", false, "Disable the automatic rollback actions, if anything goes wrong") + cmd.Flags().BoolVar(&createClusterOpts.PrepDisableHostIPInjection, "no-hostip", false, "Disable the automatic injection of the Host IP as 'host.k3d.internal' into the containers and CoreDNS") /* Image Importing */ cmd.Flags().BoolVar(&createClusterOpts.DisableImageVolume, "no-image-volume", false, "Disable the creation of a volume for importing images") diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 8fffc932e..e858bb56b 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -358,24 +358,26 @@ func ClusterCreate(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clus * Networking Magic */ - // add extra host - hostIP, err := GetHostIP(clusterPrepCtx, runtime, cluster) - if err != nil { - log.Errorln("Failed to get HostIP") - return 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) + // add /etc/hosts and CoreDNS entry for host.k3d.internal, referring to the host system + if !cluster.CreateClusterOpts.PrepDisableHostIPInjection { + hostIP, err := GetHostIP(clusterPrepCtx, runtime, cluster) + if err != nil { + log.Errorln("Failed to get HostIP") + return 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) + } } - } - 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) + } } return nil diff --git a/pkg/types/types.go b/pkg/types/types.go index f8441a76c..f64f57a09 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -133,12 +133,13 @@ var DoNotCopyServerFlags = []string{ // ClusterCreateOpts describe a set of options one can set when creating a cluster type ClusterCreateOpts struct { - DisableImageVolume bool - WaitForServer bool - Timeout time.Duration - DisableLoadBalancer bool - K3sServerArgs []string - K3sAgentArgs []string + PrepDisableHostIPInjection bool + DisableImageVolume bool + WaitForServer bool + Timeout time.Duration + DisableLoadBalancer bool + K3sServerArgs []string + K3sAgentArgs []string } // ClusterStartOpts describe a set of options one can set when (re-)starting a cluster