Skip to content

Commit

Permalink
configurable dataplane connection retries (#69)
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Siwiec <[email protected]>
  • Loading branch information
rizzza authored Sep 11, 2023
1 parent 95624c2 commit 9ca236a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
25 changes: 19 additions & 6 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (
"github.com/spf13/viper"
)

const (
defaultDataplaneConnRetries = 30
defaultDataplaneConnRetryInterval = 1 * time.Second
)

// runCmd starts loadbalancer-manager-haproxy service
var runCmd = &cobra.Command{
Use: "run",
Expand All @@ -51,6 +56,12 @@ func init() {
runCmd.PersistentFlags().String("dataplane-url", "http://127.0.0.1:5555/v2/", "DataplaneAPI base url")
viperx.MustBindFlag(viper.GetViper(), "dataplane.url", runCmd.PersistentFlags().Lookup("dataplane-url"))

runCmd.PersistentFlags().Int("dataplane-connect-retries", defaultDataplaneConnRetries, "DataplaneAPI connection retry attempts")
viperx.MustBindFlag(viper.GetViper(), "dataplane-connect-retries", runCmd.PersistentFlags().Lookup("dataplane-connect-retries"))

runCmd.PersistentFlags().Duration("dataplane-connect-retry-interval", defaultDataplaneConnRetryInterval, "DataplaneAPI connection retry interval")
viperx.MustBindFlag(viper.GetViper(), "dataplane-connect-retry-interval", runCmd.PersistentFlags().Lookup("dataplane-connect-retry-interval"))

runCmd.PersistentFlags().String("base-haproxy-config", "", "Base config for haproxy")
viperx.MustBindFlag(viper.GetViper(), "haproxy.config.base", runCmd.PersistentFlags().Lookup("base-haproxy-config"))

Expand Down Expand Up @@ -88,12 +99,14 @@ func run(cmdCtx context.Context, v *viper.Viper) error {
}

mgr := &manager.Manager{
Context: ctx,
Logger: logger,
DataPlaneClient: dataplaneapi.NewClient(viper.GetString("dataplane.url"), dataplaneapi.WithLogger(logger)),
LBClient: lbapi.NewClient(viper.GetString("loadbalancerapi.url")),
ManagedLBID: managedLBID,
BaseCfgPath: viper.GetString("haproxy.config.base"),
Context: ctx,
Logger: logger,
DataPlaneClient: dataplaneapi.NewClient(viper.GetString("dataplane.url"), dataplaneapi.WithLogger(logger)),
DataPlaneConnectRetries: viper.GetInt("dataplane-connect-retries"),
DataPlaneConnectRetryInterval: viper.GetDuration("dataplane-connect-retry-interval"),
LBClient: lbapi.NewClient(viper.GetString("loadbalancerapi.url")),
ManagedLBID: managedLBID,
BaseCfgPath: viper.GetString("haproxy.config.base"),
}

logger.Infow("Initializing...", zap.String("loadbalancerID", viper.GetString("loadbalancer.id")))
Expand Down
23 changes: 10 additions & 13 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ import (
"go.uber.org/zap"
)

var (
dataPlaneAPIRetryLimit = 10
dataPlaneAPIRetrySleep = 1 * time.Second
)

type lbAPI interface {
GetLoadBalancer(ctx context.Context, id string) (*lbapi.LoadBalancer, error)
}
Expand All @@ -39,13 +34,15 @@ type eventSubscriber interface {

// Manager contains configuration and client connections
type Manager struct {
Context context.Context
Logger *zap.SugaredLogger
Subscriber eventSubscriber
DataPlaneClient dataPlaneAPI
LBClient lbAPI
ManagedLBID gidx.PrefixedID
BaseCfgPath string
Context context.Context
Logger *zap.SugaredLogger
Subscriber eventSubscriber
DataPlaneClient dataPlaneAPI
DataPlaneConnectRetries int
DataPlaneConnectRetryInterval time.Duration
LBClient lbAPI
ManagedLBID gidx.PrefixedID
BaseCfgPath string

// currentConfig for unit testing
currentConfig string
Expand All @@ -68,7 +65,7 @@ func (m *Manager) Run() error {
}

// wait until the Data Plane API is running
if err := m.DataPlaneClient.WaitForDataPlaneReady(m.Context, dataPlaneAPIRetryLimit, dataPlaneAPIRetrySleep); err != nil {
if err := m.DataPlaneClient.WaitForDataPlaneReady(m.Context, m.DataPlaneConnectRetries, m.DataPlaneConnectRetryInterval); err != nil {
m.Logger.Fatal("unable to reach dataplaneapi. is it running?")
}

Expand Down

0 comments on commit 9ca236a

Please sign in to comment.