From f715cfa78774b6630c61b0b86002573fd5cbb208 Mon Sep 17 00:00:00 2001 From: kaiyou Date: Thu, 7 May 2020 14:51:03 +0200 Subject: [PATCH] Remove the unneeded writeToEtcHosts function --- main.go | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index a0e1558..7f3b2c8 100644 --- a/main.go +++ b/main.go @@ -44,6 +44,11 @@ func main() { logrus.WithError(err).Fatal("could not create cluster") } + // Prepare the /etc/hosts writer + hostsFile := &etchosts.EtcHosts{ + Logger: logrus.StandardLogger(), + } + // Assign a local node address and propagate it to the cluster wgstate.AssignOverlayAddr((*net.IPNet)(config.OverlayNet), cluster.LocalName) localNode := common.Node{} @@ -70,22 +75,24 @@ func main() { for { select { case rawNodes := <-nodec: - logrus.Info("cluster members:\n") nodes := make([]common.Node, 0, len(rawNodes)) + hosts := make(map[string][]string, len(rawNodes)) + logrus.Info("cluster members:\n") for _, node := range rawNodes { if err := node.Decode(); err != nil { logrus.Warnf("\t addr: %s, could not decode metadata", node.Addr) continue } - nodes = append(nodes, node) logrus.Infof("\taddr: %s, overlay: %s, pubkey: %s", node.Addr, node.OverlayAddr, node.PubKey) + nodes = append(nodes, node) + hosts[node.OverlayAddr.IP.String()] = []string{node.Name} } if err := wgstate.SetUpInterface(nodes); err != nil { logrus.WithError(err).Error("could not up interface") wgstate.DownInterface() } if !config.NoEtcHosts { - if err := writeToEtcHosts(nodes); err != nil { + if err := hostsFile.WriteEntries(hosts); err != nil { logrus.WithError(err).Error("could not write hosts entries") } } @@ -93,7 +100,7 @@ func main() { logrus.Info("terminating...") cluster.Leave() if !config.NoEtcHosts { - if err := writeToEtcHosts(nil); err != nil { + if err := hostsFile.WriteEntries(map[string][]string{}); err != nil { logrus.WithError(err).Error("could not remove stale hosts entries") } } @@ -104,14 +111,3 @@ func main() { } } } - -func writeToEtcHosts(nodes []common.Node) error { - hosts := make(map[string][]string, len(nodes)) - for _, n := range nodes { - hosts[n.OverlayAddr.IP.String()] = []string{n.Name} - } - hostsFile := &etchosts.EtcHosts{ - Logger: logrus.StandardLogger(), - } - return hostsFile.WriteEntries(hosts) -}