From 4c849539fc672b72e9e992db017f0bab2106020e Mon Sep 17 00:00:00 2001 From: Juan Font Date: Thu, 12 Aug 2021 21:44:12 +0200 Subject: [PATCH 1/4] Expire the ephemeral nodes in the Serve method --- app.go | 1 + cmd/headscale/cli/server.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app.go b/app.go index 91605c03..c5577917 100644 --- a/app.go +++ b/app.go @@ -165,6 +165,7 @@ func (h *Headscale) Serve() error { var err error go h.watchForKVUpdates(5000) + go h.ExpireEphemeralNodes(5000) if h.cfg.TLSLetsEncryptHostname != "" { if !strings.HasPrefix(h.cfg.ServerURL, "https://") { diff --git a/cmd/headscale/cli/server.go b/cmd/headscale/cli/server.go index 1f8db6a2..6d9ad194 100644 --- a/cmd/headscale/cli/server.go +++ b/cmd/headscale/cli/server.go @@ -21,7 +21,7 @@ var serveCmd = &cobra.Command{ if err != nil { log.Fatalf("Error initializing: %s", err) } - go h.ExpireEphemeralNodes(5000) + err = h.Serve() if err != nil { log.Fatalf("Error initializing: %s", err) From c1e61578475f392e1fbe30236c2837e10e63fedf Mon Sep 17 00:00:00 2001 From: Juan Font Date: Thu, 12 Aug 2021 21:45:40 +0200 Subject: [PATCH 2/4] Expire ephemeral is internal --- app.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app.go b/app.go index c5577917..f58cccc4 100644 --- a/app.go +++ b/app.go @@ -107,9 +107,9 @@ func (h *Headscale) redirect(w http.ResponseWriter, req *http.Request) { http.Redirect(w, req, target, http.StatusFound) } -// ExpireEphemeralNodes deletes ephemeral machine records that have not been +// expireEphemeralNodes deletes ephemeral machine records that have not been // seen for longer than h.cfg.EphemeralNodeInactivityTimeout -func (h *Headscale) ExpireEphemeralNodes(milliSeconds int64) { +func (h *Headscale) expireEphemeralNodes(milliSeconds int64) { ticker := time.NewTicker(time.Duration(milliSeconds) * time.Millisecond) for range ticker.C { h.expireEphemeralNodesWorker() @@ -165,7 +165,7 @@ func (h *Headscale) Serve() error { var err error go h.watchForKVUpdates(5000) - go h.ExpireEphemeralNodes(5000) + go h.expireEphemeralNodes(5000) if h.cfg.TLSLetsEncryptHostname != "" { if !strings.HasPrefix(h.cfg.ServerURL, "https://") { From ab61c877019475d59fe0c8437cea539fca63af94 Mon Sep 17 00:00:00 2001 From: Juan Font Date: Thu, 12 Aug 2021 21:53:37 +0200 Subject: [PATCH 3/4] Also notify peers when deleting ephemerals --- app.go | 1 + machine.go | 23 +++++++++++++++++++++++ namespaces.go | 20 +------------------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/app.go b/app.go index f58cccc4..fcf287f7 100644 --- a/app.go +++ b/app.go @@ -135,6 +135,7 @@ func (h *Headscale) expireEphemeralNodesWorker() { if err != nil { log.Error().Err(err).Str("machine", m.Name).Msg("🤮 Cannot delete ephemeral machine from the database") } + h.notifyChangesToPeers(&m) } } } diff --git a/machine.go b/machine.go index 4c7e5945..81208a83 100644 --- a/machine.go +++ b/machine.go @@ -238,3 +238,26 @@ func (m *Machine) GetHostInfo() (*tailcfg.Hostinfo, error) { } return &hostinfo, nil } + +func (h *Headscale) notifyChangesToPeers(m *Machine) error { + peers, _ := h.getPeers(*m) + for _, p := range *peers { + pUp, ok := h.clientsPolling.Load(uint64(p.ID)) + if ok { + log.Info(). + Str("func", "notifyChangesToPeers"). + Str("machine", m.Name). + Str("peer", m.Name). + Str("address", p.Addresses[0].String()). + Msgf("Notifying peer %s (%s)", p.Name, p.Addresses[0]) + pUp.(chan []byte) <- []byte{} + } else { + log.Info(). + Str("func", "notifyChangesToPeers"). + Str("machine", m.Name). + Str("peer", m.Name). + Msgf("Peer %s does not appear to be polling", p.Name) + } + } + return nil +} diff --git a/namespaces.go b/namespaces.go index 9b8d1904..ff9eeacc 100644 --- a/namespaces.go +++ b/namespaces.go @@ -169,25 +169,7 @@ func (h *Headscale) checkForNamespacesPendingUpdates() { continue } for _, m := range *machines { - peers, _ := h.getPeers(m) - for _, p := range *peers { - pUp, ok := h.clientsPolling.Load(uint64(p.ID)) - if ok { - log.Info(). - Str("func", "checkForNamespacesPendingUpdates"). - Str("machine", m.Name). - Str("peer", m.Name). - Str("address", p.Addresses[0].String()). - Msgf("Notifying peer %s (%s)", p.Name, p.Addresses[0]) - pUp.(chan []byte) <- []byte{} - } else { - log.Info(). - Str("func", "checkForNamespacesPendingUpdates"). - Str("machine", m.Name). - Str("peer", m.Name). - Msgf("Peer %s does not appear to be polling", p.Name) - } - } + h.notifyChangesToPeers(&m) } } newV, err := h.getValue("namespaces_pending_updates") From 8eb7d47072cb2c39ba9595668688f24b1f561d86 Mon Sep 17 00:00:00 2001 From: Juan Font Date: Thu, 12 Aug 2021 21:57:20 +0200 Subject: [PATCH 4/4] Fixed linting --- app.go | 5 ++++- namespaces.go | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app.go b/app.go index fcf287f7..34fb77b1 100644 --- a/app.go +++ b/app.go @@ -135,7 +135,10 @@ func (h *Headscale) expireEphemeralNodesWorker() { if err != nil { log.Error().Err(err).Str("machine", m.Name).Msg("🤮 Cannot delete ephemeral machine from the database") } - h.notifyChangesToPeers(&m) + err = h.notifyChangesToPeers(&m) + if err != nil { + continue + } } } } diff --git a/namespaces.go b/namespaces.go index ff9eeacc..1bf8c2df 100644 --- a/namespaces.go +++ b/namespaces.go @@ -169,7 +169,10 @@ func (h *Headscale) checkForNamespacesPendingUpdates() { continue } for _, m := range *machines { - h.notifyChangesToPeers(&m) + err = h.notifyChangesToPeers(&m) + if err != nil { + continue + } } } newV, err := h.getValue("namespaces_pending_updates")