Skip to content

Commit

Permalink
fix: Fix the regression caused by local sidekick logic (#62)
Browse files Browse the repository at this point in the history
Signed-off-by: Ashwin Vasani <ashwin.vasani@granica.ai>
  • Loading branch information
avasani committed Aug 21, 2023
1 parent f7894eb commit 9d67bbb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
17 changes: 14 additions & 3 deletions boltrouter/bolt_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ func (br *BoltRouter) SelectBoltEndpoint(reqMethod string) (*url.URL, error) {
liveEndpoints = append(liveEndpoints, endpoint)
}
}

if len(liveEndpoints) > 0 {
if ok && len(liveEndpoints) > 0 {
randomIndex := rand.Intn(len(liveEndpoints))
selectedEndpoint := liveEndpoints[randomIndex]
if br.config.Local {
return url.Parse(fmt.Sprintf("http://%s", selectedEndpoint))
}
return url.Parse(fmt.Sprintf("https://%s", selectedEndpoint))
}
}
Expand Down Expand Up @@ -106,7 +108,16 @@ func (br *BoltRouter) RefreshBoltInfo(ctx context.Context) error {
func (br *BoltRouter) getBoltInfo(ctx context.Context) (BoltInfo, error) {
// If running locally, we can't connect to quicksilver.
if br.config.Local {
return BoltInfo{}, nil
if br.config.BoltEndpointOverride == "" {
return BoltInfo{}, nil
}
endpoints := make(BoltInfo)
endpoints["main_write_endpoints"] = []interface{}{br.config.BoltEndpointOverride}
endpoints["failover_write_endpoints"] = []interface{}{br.config.BoltEndpointOverride}
endpoints["main_read_endpoints"] = []interface{}{br.config.BoltEndpointOverride}
endpoints["failover_read_endpoints"] = []interface{}{br.config.BoltEndpointOverride}
endpoints["cluster_healthy"] = true
return endpoints, nil
}

if br.boltVars.QuicksilverURL.Get() == "" || br.boltVars.Region.Get() == "" {
Expand Down
7 changes: 5 additions & 2 deletions boltrouter/bolt_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ func (br *BoltRouter) NewBoltRequest(ctx context.Context, logger *zap.Logger, re
BoltURL.Path = "/" + BoltURL.Path
BoltURL.RawQuery = req.URL.RawQuery
req.URL = BoltURL
req.URL.Scheme = "https"

if br.config.Local {
req.URL.Scheme = "http"
} else {
req.URL.Scheme = "https"
}
if v := headReq.Header.Get("X-Amz-Security-Token"); v != "" {
req.Header.Set("X-Amz-Security-Token", v)
}
Expand Down
10 changes: 7 additions & 3 deletions boltrouter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ type Config struct {
// For example, it will not query quicksilver to get endpoints.
Local bool `yaml:"Local"`

// Set the BoltEndpointOverride while running from local mode.
BoltEndpointOverride string `yaml:"BoltEndpointOverride"`

// Enable pass through in Bolt.
Passthrough bool `yaml:"Passthrough"`

Expand All @@ -13,7 +16,8 @@ type Config struct {
}

var DefaultConfig = Config{
Local: false,
Passthrough: false,
Failover: true,
Local: false,
Passthrough: false,
Failover: true,
BoltEndpointOverride: "",
}
4 changes: 4 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func init() {
func initServerFlags(cmd *cobra.Command) {
cmd.Flags().IntP("port", "p", DEFAULT_PORT, "The port for sidekick to listen on.")
cmd.Flags().BoolP("local", "l", false, "Run sidekick in local (non cloud) mode. This is mostly use for testing locally.")
cmd.Flags().String("bolt-endpoint-override", "", "Specify the local bolt endpoint with port to override in local mode. e.g: <local-bolt-ip>:9000")
cmd.Flags().Bool("passthrough", false, "Set passthrough flag to bolt requests.")
cmd.Flags().BoolP("failover", "f", true, "Enables aws request failover if bolt request fails.")
}
Expand Down Expand Up @@ -76,6 +77,9 @@ func getBoltRouterConfig(cmd *cobra.Command) boltrouter.Config {
if cmd.Flags().Lookup("local").Changed {
boltRouterConfig.Local, _ = cmd.Flags().GetBool("local")
}
if cmd.Flags().Lookup("bolt-endpoint-override").Changed {
boltRouterConfig.BoltEndpointOverride, _ = cmd.Flags().GetString("bolt-endpoint-override")
}
if cmd.Flags().Lookup("passthrough").Changed {
boltRouterConfig.Passthrough, _ = cmd.Flags().GetBool("passthrough")
}
Expand Down

0 comments on commit 9d67bbb

Please sign in to comment.