debugd: stop discovery loop once coordinator was downloaded successfully or coordinator binary exists

This commit is contained in:
Malte Poll 2022-03-30 13:21:05 +02:00 committed by Malte Poll
parent 6954683f18
commit aaed8ad1e3

View File

@ -2,6 +2,8 @@ package metadata
import ( import (
"context" "context"
"errors"
"io/fs"
"log" "log"
"sync" "sync"
"time" "time"
@ -49,7 +51,9 @@ func (s *Scheduler) discoveryLoop(ctx context.Context, wg *sync.WaitGroup) {
if err != nil { if err != nil {
log.Printf("error occurred while discovering debugd IPs: %v\n", err) log.Printf("error occurred while discovering debugd IPs: %v\n", err)
} else { } else {
s.downloadCoordinator(ctx, ips) if s.downloadCoordinator(ctx, ips) {
return
}
} }
ticker := time.NewTicker(debugd.DiscoverDebugdInterval) ticker := time.NewTicker(debugd.DiscoverDebugdInterval)
@ -63,8 +67,10 @@ func (s *Scheduler) discoveryLoop(ctx context.Context, wg *sync.WaitGroup) {
log.Printf("error occurred while discovering debugd IPs: %v\n", err) log.Printf("error occurred while discovering debugd IPs: %v\n", err)
continue continue
} }
s.downloadCoordinator(ctx, ips) log.Printf("discovered instances: %v\n", ips)
if s.downloadCoordinator(ctx, ips) {
return
}
case <-ctx.Done(): case <-ctx.Done():
return return
} }
@ -99,16 +105,20 @@ func (s *Scheduler) sshLoop(ctx context.Context, wg *sync.WaitGroup) {
} }
// downloadCoordinator tries to download coordinator from a list of ips and logs errors encountered. // downloadCoordinator tries to download coordinator from a list of ips and logs errors encountered.
func (s *Scheduler) downloadCoordinator(ctx context.Context, ips []string) { func (s *Scheduler) downloadCoordinator(ctx context.Context, ips []string) (success bool) {
for _, ip := range ips { for _, ip := range ips {
err := s.downloader.DownloadCoordinator(ctx, ip) err := s.downloader.DownloadCoordinator(ctx, ip)
if err != nil { if err == nil {
log.Printf("error occurred while downloading coordinator from %v: %v\n", ip, err) // early exit with success since coordinator should only be downloaded once
continue return true
} }
// early exit since coordinator should only be downloaded once if errors.Is(err, fs.ErrExist) {
return // coordinator was already uploaded
return true
}
log.Printf("error occurred while downloading coordinator from %v: %v\n", ip, err)
} }
return false
} }
// deploySSHKeys tries to deploy a list of SSH keys and logs errors encountered. // deploySSHKeys tries to deploy a list of SSH keys and logs errors encountered.