Skip to content

Commit

Permalink
move host.k3d.internal /etc/hosts injection to hook action
Browse files Browse the repository at this point in the history
  • Loading branch information
iwilltry42 committed Jan 27, 2022
1 parent d558787 commit 0d94b14
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 90 deletions.
2 changes: 1 addition & 1 deletion cmd/cluster/clusterCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ func applyCLIOverrides(cfg conf.SimpleConfig) (conf.SimpleConfig, error) {
return cfg, fmt.Errorf("invalid format of host-alias %s (exactly one ':' allowed)", ha)
}

// validate IP // TODO: move to pkg/ validate config?
// validate IP
ip, err := netaddr.ParseIP(s[0])
if err != nil {
return cfg, fmt.Errorf("invalid IP '%s' in host-alias '%s': %w", s[0], ha, err)
Expand Down
100 changes: 17 additions & 83 deletions pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ import (
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v2"

"github.com/goodhosts/hostsfile"
)

// ClusterRun orchestrates the steps of cluster creation, configuration and starting
Expand All @@ -76,6 +74,7 @@ func ClusterRun(ctx context.Context, runtime k3drt.Runtime, clusterConfig *confi
/*
* Step 2: Pre-Start Configuration
*/
// Gather Environment information, e.g. the host gateway address
envInfo, err := GatherEnvironmentInfo(ctx, runtime, &clusterConfig.Cluster)
if err != nil {
return fmt.Errorf("failed to gather environment information used for cluster creation: %w", err)
Expand Down Expand Up @@ -252,55 +251,8 @@ func ClusterPrep(ctx context.Context, runtime k3drt.Runtime, clusterConfig *conf
*/

if len(clusterConfig.ClusterCreateOpts.HostAliases) > 0 {

hostAliasesInjectEtcHostsHook := k3d.NodeHook{
Stage: k3d.LifecycleStagePostStart,
Action: actions.RewriteFileAction{
Runtime: runtime,
Path: "/etc/hosts",
Mode: 0644,
Description: "Adding HostAliases to /etc/hosts in nodes",
Opts: actions.RewriteFileActionOpts{
NoCopy: true,
},
RewriteFunc: func(input []byte) ([]byte, error) {

// write copied hosts file to temp path (for hostsfile module to read)
tmpHosts, err := os.CreateTemp("", "k3d-hostsfile-*")
if err != nil {
return nil, fmt.Errorf("error creating temp hosts file: %w", err)
}
os.WriteFile(tmpHosts.Name(), input, 0777)

// read in temp hostsfile file
hostsfile, err := hostsfile.NewCustomHosts(tmpHosts.Name())
if err != nil {
return nil, fmt.Errorf("error reading temp hosts file: %w", err)
}

// add new entries
for _, hostAlias := range clusterConfig.ClusterCreateOpts.HostAliases {
if err := hostsfile.Add(hostAlias.IP, hostAlias.Hostnames...); err != nil {
return nil, fmt.Errorf("error adding hosts file entry for %s:%s: %w", hostAlias.IP, hostAlias.Hostnames, err)
}
}

// cleanup (de-duplicate, etc.)
hostsfile.Clean()

// write changes to temp file
hostsfile.Flush()

time.Sleep(time.Second)

return os.ReadFile(tmpHosts.Name())
},
},
}

// append new action to general nodehooks
clusterConfig.ClusterCreateOpts.NodeHooks = append(clusterConfig.ClusterCreateOpts.NodeHooks, hostAliasesInjectEtcHostsHook)

clusterConfig.ClusterCreateOpts.NodeHooks = append(clusterConfig.ClusterCreateOpts.NodeHooks, NewHostAliasesInjectEtcHostsHook(runtime, clusterConfig.ClusterCreateOpts.HostAliases))
}

return nil
Expand Down Expand Up @@ -1030,9 +982,18 @@ func ClusterStart(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clust
/*** DNS ***/

// add host.k3d.internal record to /etc/hosts in all nodes
postStartErrgrp.Go(func() error {
return prepInjectHostIP(postStartErrgrpCtx, runtime, cluster, &clusterStartOpts)
})
for _, node := range append(servers, agents...) {
currNode := node
postStartErrgrp.Go(func() error {
// inject host-gateway as host.k3d.internal in /etc/hosts of node containers
return NewHostAliasesInjectEtcHostsHook(runtime, []k3d.HostAlias{
{
IP: clusterStartOpts.EnvironmentInfo.HostGateway.String(),
Hostnames: []string{"host.k3d.internal"},
},
}).Action.Run(postStartErrgrpCtx, currNode)
})
}

if len(servers) > 0 {
postStartErrgrp.Go(func() error {
Expand All @@ -1044,7 +1005,7 @@ func ClusterStart(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clust

hosts := fmt.Sprintf("%s %s\n", clusterStartOpts.EnvironmentInfo.HostGateway.String(), k3d.DefaultK3dInternalHostRecord)

net, err := runtime.GetNetwork(ctx, &cluster.Network)
net, err := runtime.GetNetwork(postStartErrgrpCtx, &cluster.Network)
if err != nil {
return fmt.Errorf("failed to get cluster network %s to inject host records into CoreDNS: %w", cluster.Network.Name, err)
}
Expand Down Expand Up @@ -1093,10 +1054,10 @@ func ClusterStart(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clust
if err != nil {
return err
}
if err := NodeWaitForLogMessage(ctx, runtime, n, "Cluster dns configmap", ts.Truncate(time.Second)); err != nil {
if err := NodeWaitForLogMessage(postStartErrgrpCtx, runtime, n, "Cluster dns configmap", ts.Truncate(time.Second)); err != nil {
return err
}
return act.Run(ctx, n)
return act.Run(postStartErrgrpCtx, n)
}
}
return nil
Expand Down Expand Up @@ -1194,33 +1155,6 @@ func corednsAddHost(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clu
return nil
}

// prepInjectHostIP adds /etc/hosts entry for host.k3d.internal, referring to the host system
func prepInjectHostIP(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Cluster, clusterStartOpts *k3d.ClusterStartOpts) error {
if cluster.Network.Name == "host" {
l.Log().Debugf("Not injecting host.k3d.internal into /etc/hosts as clusternetwork is 'host'")
return nil
}

hostIP := clusterStartOpts.EnvironmentInfo.HostGateway
hostsEntry := fmt.Sprintf("%s %s", hostIP.String(), k3d.DefaultK3dInternalHostRecord)
l.Log().Infof("Injecting '%s' into /etc/hosts of all nodes...", hostsEntry)

// entry in /etc/hosts
errgrp, errgrpctx := errgroup.WithContext(ctx)
for _, node := range cluster.Nodes {
n := node
errgrp.Go(func() error {
return runtime.ExecInNode(errgrpctx, n, []string{"sh", "-c", fmt.Sprintf("echo '%s' >> /etc/hosts", hostsEntry)})
})
}
if err := errgrp.Wait(); err != nil {
return fmt.Errorf("failed to add hosts entry %s: %w", hostsEntry, err)
}
l.Log().Debugf("Successfully added host record \"%s\" to /etc/hosts in all nodes", hostsEntry)

return nil
}

func prepCreateLocalRegistryHostingConfigMap(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Cluster) error {
success := false
for _, node := range cluster.Nodes {
Expand Down
75 changes: 75 additions & 0 deletions pkg/client/hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright © 2020-2021 The k3d Author(s)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package client

import (
"fmt"
"os"
"time"

"github.com/goodhosts/hostsfile"
"github.com/rancher/k3d/v5/pkg/actions"
"github.com/rancher/k3d/v5/pkg/runtimes"
k3d "github.com/rancher/k3d/v5/pkg/types"
)

func NewHostAliasesInjectEtcHostsHook(runtime runtimes.Runtime, hostAliases []k3d.HostAlias) k3d.NodeHook {
return k3d.NodeHook{
Stage: k3d.LifecycleStagePostStart,
Action: actions.RewriteFileAction{
Runtime: runtime,
Path: "/etc/hosts",
Mode: 0644,
Description: "Adding HostAliases to /etc/hosts in nodes",
Opts: actions.RewriteFileActionOpts{
NoCopy: true,
},
RewriteFunc: func(input []byte) ([]byte, error) {

tmpHosts, err := os.CreateTemp("", "k3d-hostsfile-*")
if err != nil {
return nil, fmt.Errorf("error creating temp hosts file: %w", err)
}
os.WriteFile(tmpHosts.Name(), input, 0777)

hostsfile, err := hostsfile.NewCustomHosts(tmpHosts.Name())
if err != nil {
return nil, fmt.Errorf("error reading temp hosts file: %w", err)
}

for _, hostAlias := range hostAliases {
if err := hostsfile.Add(hostAlias.IP, hostAlias.Hostnames...); err != nil {
return nil, fmt.Errorf("error adding hosts file entry for %s:%s: %w", hostAlias.IP, hostAlias.Hostnames, err)
}
}

hostsfile.Clean()

hostsfile.Flush()

time.Sleep(time.Second)

return os.ReadFile(tmpHosts.Name())
},
},
}
}
5 changes: 2 additions & 3 deletions pkg/client/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"fmt"
"io"
"os"
"reflect"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -413,9 +412,9 @@ func NodeStart(ctx context.Context, runtime runtimes.Runtime, node *k3d.Node, no
// execute lifecycle hook actions
for _, hook := range nodeStartOpts.NodeHooks {
if hook.Stage == k3d.LifecycleStagePreStart {
l.Log().Tracef("Node %s: Executing preStartAction '%s'", node.Name, reflect.TypeOf(hook))
l.Log().Tracef("Node %s: Executing preStartAction '%s': %s", node.Name, hook.Action.Name(), hook.Action.Info())
if err := hook.Action.Run(ctx, node); err != nil {
l.Log().Errorf("Node %s: Failed executing preStartAction '%+v': %+v", node.Name, hook, err)
l.Log().Errorf("Node %s: Failed executing preStartAction '%s': %s: %+v", node.Name, hook.Action.Name(), hook.Action.Info(), err) // TODO: should we error out on failed preStartAction?
}
}
}
Expand Down
27 changes: 24 additions & 3 deletions pkg/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/rancher/k3d/v5/pkg/runtimes"
runtimeutil "github.com/rancher/k3d/v5/pkg/runtimes/util"
k3d "github.com/rancher/k3d/v5/pkg/types"
"inet.af/netaddr"

"fmt"

Expand Down Expand Up @@ -76,9 +77,29 @@ func ValidateClusterConfig(ctx context.Context, runtime runtimes.Runtime, config
}
}

// hostAliases not allowed in hostnetwork mode
if len(config.ClusterCreateOpts.HostAliases) > 0 && config.Cluster.Network.Name == "host" {
return fmt.Errorf("hostAliases not allowed in hostnetwork mode")
// hostAliases
if len(config.ClusterCreateOpts.HostAliases) > 0 {
// not allowed in hostnetwork mode
if config.Cluster.Network.Name == "host" {
return fmt.Errorf("hostAliases not allowed in hostnetwork mode")
}

// validate IP and hostname
for _, ha := range config.ClusterCreateOpts.HostAliases {
// validate IP
_, err := netaddr.ParseIP(ha.IP)
if err != nil {
return fmt.Errorf("invalid IP '%s' in hostAlias '%s': %w", ha.IP, ha, err)
}

// validate hostnames
for _, hostname := range ha.Hostnames {
if err := k3dc.ValidateHostname(hostname); err != nil {
return fmt.Errorf("invalid hostname '%s' in hostAlias '%s': %w", hostname, ha, err)
}
}
}

}

// validate nodes one by one
Expand Down

0 comments on commit 0d94b14

Please sign in to comment.