Remove the unneeded writeToEtcHosts function

This commit is contained in:
kaiyou 2020-05-07 14:51:03 +02:00 committed by Leo Antunes
parent 61104a9272
commit f715cfa787
1 changed files with 11 additions and 15 deletions

26
main.go
View File

@ -44,6 +44,11 @@ func main() {
logrus.WithError(err).Fatal("could not create cluster") 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 // Assign a local node address and propagate it to the cluster
wgstate.AssignOverlayAddr((*net.IPNet)(config.OverlayNet), cluster.LocalName) wgstate.AssignOverlayAddr((*net.IPNet)(config.OverlayNet), cluster.LocalName)
localNode := common.Node{} localNode := common.Node{}
@ -70,22 +75,24 @@ func main() {
for { for {
select { select {
case rawNodes := <-nodec: case rawNodes := <-nodec:
logrus.Info("cluster members:\n")
nodes := make([]common.Node, 0, len(rawNodes)) nodes := make([]common.Node, 0, len(rawNodes))
hosts := make(map[string][]string, len(rawNodes))
logrus.Info("cluster members:\n")
for _, node := range rawNodes { for _, node := range rawNodes {
if err := node.Decode(); err != nil { if err := node.Decode(); err != nil {
logrus.Warnf("\t addr: %s, could not decode metadata", node.Addr) logrus.Warnf("\t addr: %s, could not decode metadata", node.Addr)
continue continue
} }
nodes = append(nodes, node)
logrus.Infof("\taddr: %s, overlay: %s, pubkey: %s", node.Addr, node.OverlayAddr, node.PubKey) 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 { if err := wgstate.SetUpInterface(nodes); err != nil {
logrus.WithError(err).Error("could not up interface") logrus.WithError(err).Error("could not up interface")
wgstate.DownInterface() wgstate.DownInterface()
} }
if !config.NoEtcHosts { 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") logrus.WithError(err).Error("could not write hosts entries")
} }
} }
@ -93,7 +100,7 @@ func main() {
logrus.Info("terminating...") logrus.Info("terminating...")
cluster.Leave() cluster.Leave()
if !config.NoEtcHosts { 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") 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)
}