clusterCreate: add --no-hostip flag to disable the automatic injection of the host.k3d.internal entry into /etc/hosts and CoreDNS

This commit is contained in:
iwilltry42 2020-10-05 15:19:00 +02:00
parent 60069f6f19
commit d063482a32
No known key found for this signature in database
GPG Key ID: 7BA57AD1CFF16110
3 changed files with 26 additions and 22 deletions

View File

@ -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(&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(&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(&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 */ /* Image Importing */
cmd.Flags().BoolVar(&createClusterOpts.DisableImageVolume, "no-image-volume", false, "Disable the creation of a volume for importing images") cmd.Flags().BoolVar(&createClusterOpts.DisableImageVolume, "no-image-volume", false, "Disable the creation of a volume for importing images")

View File

@ -358,24 +358,26 @@ func ClusterCreate(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clus
* Networking Magic * Networking Magic
*/ */
// add extra host // add /etc/hosts and CoreDNS entry for host.k3d.internal, referring to the host system
hostIP, err := GetHostIP(clusterPrepCtx, runtime, cluster) if !cluster.CreateClusterOpts.PrepDisableHostIPInjection {
if err != nil { hostIP, err := GetHostIP(clusterPrepCtx, runtime, cluster)
log.Errorln("Failed to get HostIP") if err != nil {
return err log.Errorln("Failed to get HostIP")
} return err
hostsEntry := fmt.Sprintf("%s %s", hostIP, k3d.DefaultK3dInternalHostRecord) }
log.Debugf("Adding extra host entry '%s'...", hostsEntry) hostsEntry := fmt.Sprintf("%s %s", hostIP, k3d.DefaultK3dInternalHostRecord)
for _, node := range cluster.Nodes { log.Debugf("Adding extra host entry '%s'...", hostsEntry)
if err := runtime.ExecInNode(clusterPrepCtx, node, []string{"sh", "-c", fmt.Sprintf("echo '%s' >> /etc/hosts", hostsEntry)}); err != nil { for _, node := range cluster.Nodes {
log.Warnf("Failed to add extra entry '%s' to /etc/hosts in node '%s'", hostsEntry, node.Name) 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"` 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}) err = runtime.ExecInNode(clusterPrepCtx, cluster.Nodes[0], []string{"sh", "-c", patchCmd})
if err != nil { if err != nil {
log.Warnf("Failed to patch CoreDNS ConfigMap to include entry '%s': %+v", hostsEntry, err) log.Warnf("Failed to patch CoreDNS ConfigMap to include entry '%s': %+v", hostsEntry, err)
}
} }
return nil return nil

View File

@ -133,12 +133,13 @@ var DoNotCopyServerFlags = []string{
// ClusterCreateOpts describe a set of options one can set when creating a cluster // ClusterCreateOpts describe a set of options one can set when creating a cluster
type ClusterCreateOpts struct { type ClusterCreateOpts struct {
DisableImageVolume bool PrepDisableHostIPInjection bool
WaitForServer bool DisableImageVolume bool
Timeout time.Duration WaitForServer bool
DisableLoadBalancer bool Timeout time.Duration
K3sServerArgs []string DisableLoadBalancer bool
K3sAgentArgs []string K3sServerArgs []string
K3sAgentArgs []string
} }
// ClusterStartOpts describe a set of options one can set when (re-)starting a cluster // ClusterStartOpts describe a set of options one can set when (re-)starting a cluster