From 00c93beca025323587dc68e566c8d7348a72814b Mon Sep 17 00:00:00 2001 From: lordwelch Date: Tue, 10 Mar 2020 14:33:07 -0700 Subject: [PATCH] Add more logging Fix issue when trying to update with an empty address Fix govet issues Run IPv6 poller --- cmd/cloud-dyndns-client/main.go | 7 +++++-- pkg/sync/ipaddress.go | 1 + pkg/sync/sync.go | 20 ++++++++++---------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/cmd/cloud-dyndns-client/main.go b/cmd/cloud-dyndns-client/main.go index 1ef748d..a47dfe7 100644 --- a/cmd/cloud-dyndns-client/main.go +++ b/cmd/cloud-dyndns-client/main.go @@ -174,7 +174,7 @@ func main() { records := []sync.Record{} for name, d := range cfg.Domains { if !strings.HasSuffix(name, ".") { - name = name + "." + name += "." } records = append(records, constructRecord(name, d)...) } @@ -196,6 +196,7 @@ func main() { // TODO: Refactor and move this code to it's own package wg.Go(func() error { return syncer.Run(ctx.Done()) }) wg.Go(func() error { return IP4Poller.Run(ctx.Done()) }) + wg.Go(func() error { return IP6Poller.Run(ctx.Done()) }) wg.Go(func() error { // This goroutine receives IP address polling results // and updates the desired records in the Syncer. @@ -204,6 +205,7 @@ func main() { for { select { case ip := <-ip4c: + log.Printf("Updating IPv4: %v", ip) for _, r := range records { if r.Record.Type() == "A" { syncer.UpdateRecord( @@ -215,6 +217,7 @@ func main() { } } case ip := <-ip6c: + log.Printf("Updating IPv6: %v", ip) for _, r := range records { if r.Record.Type() == "AAAA" { syncer.UpdateRecord( @@ -272,7 +275,7 @@ func main() { signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) select { case s := <-signals: - log.Printf("Received signal %v, exiting...", s) + log.Printf("Received %v signal, exiting...", s) case <-ctx.Done(): } cancel() diff --git a/pkg/sync/ipaddress.go b/pkg/sync/ipaddress.go index 6d4d49e..fe3eb56 100644 --- a/pkg/sync/ipaddress.go +++ b/pkg/sync/ipaddress.go @@ -74,6 +74,7 @@ func (i *IPAddressPoller) poll() error { if err != nil { return fmt.Errorf("could not obtain IP address: %w", err) } + log.Printf("Found IP %v", ip) for _, c := range i.channels { select { case c <- ip.String(): diff --git a/pkg/sync/sync.go b/pkg/sync/sync.go index 72dffeb..64f9d98 100644 --- a/pkg/sync/sync.go +++ b/pkg/sync/sync.go @@ -89,7 +89,7 @@ func NewSyncer(records []Record, pollInterval, apiTimeout time.Duration) *Syncer } } -// UpdateRecord() updates a managed record so that it can be synced by the +// UpdateRecord updates a managed record so that it can be synced by the // sync loop. func (s *Syncer) UpdateRecord(dnsName, dnsType string, ttl int64, data []string) error { for _, d := range s.domains { @@ -109,10 +109,10 @@ func (s *Syncer) UpdateRecord(dnsName, dnsType string, ttl int64, data []string) } } - return fmt.Errorf("Domain %s not registered.", dnsName) + return fmt.Errorf("domain %s not registered.", dnsName) } -// needsUpdate() compares the left/local DNSRecord against the right/remote +// needsUpdate compares the left/local DNSRecord against the right/remote // DNSRecord and returns true if the record needs to be updated in the remote // backend. func needsUpdate(l, r backend.DNSRecord) bool { @@ -135,7 +135,7 @@ func needsUpdate(l, r backend.DNSRecord) bool { lData := l.Data() rData := r.Data() - if len(lData) <= 0 { + if len(lData) < 1 { return false } @@ -152,7 +152,7 @@ func needsUpdate(l, r backend.DNSRecord) bool { return false } -// pollSingle() performs a single refresh of the remote cache value. +// pollSingle performs a single refresh of the remote cache value. func (s *Syncer) pollSingle(d *domainObj) error { // Update record sets to reconcile ctx := context.Background() @@ -173,7 +173,7 @@ func (s *Syncer) pollSingle(d *domainObj) error { return nil } -// poll() runs a loop to poll the backend and update the remote cache. +// poll runs a loop to poll the backend and update the remote cache. func (s *Syncer) poll(d *domainObj, stopCh <-chan struct{}) { // Start by polling the domain to initialize the remote value and local cache // TODO: Implement some exponential retry backoff logic in case poll interval is long @@ -192,14 +192,14 @@ func (s *Syncer) poll(d *domainObj, stopCh <-chan struct{}) { } } -// syncSingle() syncs a single record entry, updating it with +// syncSingle syncs a single record entry, updating it with // the backend server if necessary. func (s *Syncer) syncSingle(d *domainObj) error { d.lock.Lock() defer d.lock.Unlock() // Sanity check - if !needsUpdate(d.managed, d.remote) { + if !needsUpdate(d.managed, d.remote) || len(d.managed.Data()) < 1 || len(d.managed.Data()) < 1 { return nil } @@ -223,7 +223,7 @@ func (s *Syncer) syncSingle(d *domainObj) error { return nil } -// sync() runs a loop to sync records with backend providers +// sync runs a loop to sync records with backend providers // when necessary. func (s *Syncer) sync(d *domainObj, stopCh <-chan struct{}) { for { @@ -246,7 +246,7 @@ func (s *Syncer) sync(d *domainObj, stopCh <-chan struct{}) { } } -// Run() starts the sync and poll loops +// Run starts the sync and poll loops func (s *Syncer) Run(stopCh <-chan struct{}) error { // Run a sync and poll loop for each domain. Sync and poll loops are // separated so that polling and syncing do not block on each other.