Skip to content

Commit

Permalink
speed up /etc/hosts injection
Browse files Browse the repository at this point in the history
  • Loading branch information
iwilltry42 committed Oct 5, 2021
1 parent 53bdbec commit 5f2ea9a
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,14 +1016,21 @@ func prepInjectHostIP(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.C
l.Log().Tracef("Not injecting hostIP as clusternetwork is 'host'")
return nil
}
l.Log().Infoln("Trying to get IP of the docker host and inject it into the cluster as 'host.k3d.internal' for easy access")

hostIP := clusterStartOpts.EnvironmentInfo.HostGateway
hostsEntry := fmt.Sprintf("%s %s", hostIP.String(), k3d.DefaultK3dInternalHostRecord)
l.Log().Debugf("Adding extra host entry '%s'...", hostsEntry)
l.Log().Infof("Injecting record '%s'...", hostsEntry)

// entry in /etc/hosts
errgrp, errgrpctx := errgroup.WithContext(ctx)
for _, node := range cluster.Nodes {
if err := runtime.ExecInNode(ctx, node, []string{"sh", "-c", fmt.Sprintf("echo '%s' >> /etc/hosts", hostsEntry)}); err != nil {
return fmt.Errorf("failed to add extra entry '%s' to /etc/hosts in node '%s': %w", hostsEntry, node.Name, err)
}
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)

Expand Down

0 comments on commit 5f2ea9a

Please sign in to comment.