1
0
mirror of https://github.com/juanfont/headscale.git synced 2024-12-20 19:09:07 +01:00

Account for updates in shared namespaces

This commit is contained in:
Kristoffer Dalby 2021-10-06 19:32:15 +00:00
parent c582c8d206
commit ba391bc2ed
2 changed files with 20 additions and 5 deletions

2
app.go
View File

@ -250,8 +250,8 @@ func (h *Headscale) setLastStateChangeToNow(namespace string) {
func (h *Headscale) getLastStateChange(namespace string) time.Time { func (h *Headscale) getLastStateChange(namespace string) time.Time {
if wrapped, ok := h.lastStateChange.Load(namespace); ok { if wrapped, ok := h.lastStateChange.Load(namespace); ok {
lastChange, _ := wrapped.(time.Time) lastChange, _ := wrapped.(time.Time)
return lastChange
return lastChange
} }
now := time.Now().UTC() now := time.Now().UTC()

View File

@ -65,7 +65,7 @@ func (h *Headscale) getDirectPeers(m *Machine) (Machines, error) {
if err := h.db.Where("namespace_id = ? AND machine_key <> ? AND registered", if err := h.db.Where("namespace_id = ? AND machine_key <> ? AND registered",
m.NamespaceID, m.MachineKey).Find(&machines).Error; err != nil { m.NamespaceID, m.MachineKey).Find(&machines).Error; err != nil {
log.Error().Err(err).Msg("Error accessing db") log.Error().Err(err).Msg("Error accessing db")
return nil, err return Machines{}, err
} }
sort.Slice(machines, func(i, j int) bool { return machines[i].ID < machines[j].ID }) sort.Slice(machines, func(i, j int) bool { return machines[i].ID < machines[j].ID })
@ -87,7 +87,7 @@ func (h *Headscale) getShared(m *Machine) (Machines, error) {
sharedMachines := []SharedMachine{} sharedMachines := []SharedMachine{}
if err := h.db.Preload("Namespace").Preload("Machine").Where("namespace_id = ?", if err := h.db.Preload("Namespace").Preload("Machine").Where("namespace_id = ?",
m.NamespaceID).Find(&sharedMachines).Error; err != nil { m.NamespaceID).Find(&sharedMachines).Error; err != nil {
return nil, err return Machines{}, err
} }
peers := make(Machines, 0) peers := make(Machines, 0)
@ -111,7 +111,7 @@ func (h *Headscale) getPeers(m *Machine) (Machines, error) {
Str("func", "getPeers"). Str("func", "getPeers").
Err(err). Err(err).
Msg("Cannot fetch peers") Msg("Cannot fetch peers")
return nil, err return Machines{}, err
} }
shared, err := h.getShared(m) shared, err := h.getShared(m)
@ -120,7 +120,7 @@ func (h *Headscale) getPeers(m *Machine) (Machines, error) {
Str("func", "getDirectPeers"). Str("func", "getDirectPeers").
Err(err). Err(err).
Msg("Cannot fetch peers") Msg("Cannot fetch peers")
return nil, err return Machines{}, err
} }
peers := append(direct, shared...) peers := append(direct, shared...)
@ -219,6 +219,21 @@ func (h *Headscale) isOutdated(m *Machine) bool {
return true return true
} }
sharedMachines, _ := h.getShared(m)
// Check if any of our shared namespaces has updates that we have
// not propagated.
for _, sharedMachine := range sharedMachines {
lastChange := h.getLastStateChange(sharedMachine.Namespace.Name)
log.Trace().
Str("func", "keepAlive").
Str("machine", m.Name).
Time("last_successful_update", *m.LastSuccessfulUpdate).
Time("last_state_change", lastChange).
Msgf("Checking if %s is missing updates", m.Name)
return m.LastSuccessfulUpdate.Before(lastChange)
}
lastChange := h.getLastStateChange(m.Namespace.Name) lastChange := h.getLastStateChange(m.Namespace.Name)
log.Trace(). log.Trace().
Str("func", "keepAlive"). Str("func", "keepAlive").