Skip to content

Commit

Permalink
Wait until DNS returns the expected number of IPs
Browse files Browse the repository at this point in the history
  • Loading branch information
mkuratczyk committed Nov 22, 2024
1 parent e048b3b commit 490d976
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"fmt"
"math"
"net"
"os"
"os/signal"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -356,14 +358,36 @@ func join_cluster(expectedInstance int, serviceName string) {
os.Exit(1)
}

// wait until DNS name serviceName returns the expected number of instances
var nodes []net.IP
var err error
for {
nodes, err = net.LookupIP(serviceName)
if err != nil {
log.Error("failed to lookup DNS name; retrying...", "name", serviceName, "error", err)
time.Sleep(time.Second)
continue
}
if len(nodes) >= expectedInstance {
log.Info("reached the expected number of instances", "expected instances", expectedInstance, "current instances", len(nodes))
break
}
log.Info("waiting for DNS to return the expected number of IPs", "exepcted", expectedInstance, "current", len(nodes))
time.Sleep(time.Second)
}

list, err := memberlist.Create(memberlist.DefaultLANConfig())
if err != nil {
panic("Failed to create memberlist: " + err.Error())
}

sort.Slice(nodes, func(i, j int) bool {
return nodes[i].String() < nodes[j].String()
})

// join the cluster
for {
_, err = list.Join([]string{serviceName})
_, err = list.Join([]string{nodes[0].String()})
if err == nil {
break
}
Expand Down

0 comments on commit 490d976

Please sign in to comment.