Skip to content

Commit

Permalink
clusterCreate: add --no-hostip flag to disable the automatic injectio…
Browse files Browse the repository at this point in the history
…n of the host.k3d.internal entry into /etc/hosts and CoreDNS
  • Loading branch information
iwilltry42 committed Oct 5, 2020
1 parent 60069f6 commit d063482
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
1 change: 1 addition & 0 deletions cmd/cluster/clusterCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
34 changes: 18 additions & 16 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d063482

Please sign in to comment.