Convert namespaces.go

This commit is contained in:
Kristoffer Dalby 2021-08-05 18:23:02 +01:00
parent 42bf566fff
commit d10b57b317
No known key found for this signature in database
GPG key ID: 09F62DC067465735

View file

@ -4,9 +4,9 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"log"
"time" "time"
"github.com/rs/zerolog/log"
"gorm.io/gorm" "gorm.io/gorm"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
) )
@ -33,7 +33,10 @@ func (h *Headscale) CreateNamespace(name string) (*Namespace, error) {
} }
n.Name = name n.Name = name
if err := h.db.Create(&n).Error; err != nil { if err := h.db.Create(&n).Error; err != nil {
log.Printf("Could not create row: %s", err) log.Error().
Str("Func", "CreateNamespace").
Err(err).
Msg("Could not create row")
return nil, err return nil, err
} }
return &n, nil return &n, nil
@ -133,7 +136,10 @@ func (h *Headscale) RequestMapUpdates(namespaceID uint) error {
names = append(names, namespace.Name) names = append(names, namespace.Name)
data, err := json.Marshal(names) data, err := json.Marshal(names)
if err != nil { if err != nil {
log.Printf("Could not marshal namespaces_pending_updates: %s", err) log.Error().
Str("Func", "RequestMapUpdates").
Err(err).
Msg("Could not marshal namespaces_pending_updates")
return err return err
} }
return h.setValue("namespaces_pending_updates", string(data)) return h.setValue("namespaces_pending_updates", string(data))
@ -154,7 +160,10 @@ func (h *Headscale) checkForNamespacesPendingUpdates() {
return return
} }
for _, name := range names { for _, name := range names {
log.Printf("Sending updates to nodes in namespace %s", name) log.Trace().
Str("Func", "RequestMapUpdates").
Str("Machine", name).
Msg("Sending updates to nodes in namespace")
machines, err := h.ListMachinesInNamespace(name) machines, err := h.ListMachinesInNamespace(name)
if err != nil { if err != nil {
continue continue
@ -165,10 +174,19 @@ func (h *Headscale) checkForNamespacesPendingUpdates() {
for _, p := range *peers { for _, p := range *peers {
pUp, ok := h.clientsPolling[uint64(p.ID)] pUp, ok := h.clientsPolling[uint64(p.ID)]
if ok { if ok {
log.Printf("[%s] Notifying peer %s (%s)", m.Name, p.Name, p.Addresses[0]) 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 <- []byte{} pUp <- []byte{}
} else { } else {
log.Printf("[%s] Peer %s does not appear to be polling", m.Name, p.Name) 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.pollMu.Unlock() h.pollMu.Unlock()
@ -181,7 +199,10 @@ func (h *Headscale) checkForNamespacesPendingUpdates() {
if v == newV { // only clear when no changes, so we notified everybody if v == newV { // only clear when no changes, so we notified everybody
err = h.setValue("namespaces_pending_updates", "") err = h.setValue("namespaces_pending_updates", "")
if err != nil { if err != nil {
log.Printf("Could not save to KV: %s", err) log.Error().
Str("Func", "checkForNamespacesPendingUpdates").
Err(err).
Msg("Could not save to KV")
return return
} }
} }