mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-02 03:38:42 -07:00
[FIXED] Route/Cluster override
If the server was started with a cluster section in a configuration file and one would want to override the routes (using `-routes`) the server would complain that you need to use `-cluster`. Adding an override of cluster would not work, server would still complain. Trying to override simply the cluster listen info (without override of routes) would also not work.
This commit is contained in:
50
main.go
50
main.go
@@ -260,33 +260,45 @@ func configureTLS(opts *server.Options) {
|
||||
}
|
||||
|
||||
func configureClusterOpts(opts *server.Options) error {
|
||||
if opts.ClusterListenStr == "" {
|
||||
// If we don't have cluster defined in the configuration
|
||||
// file and no cluster listen string override, but we do
|
||||
// have a routes override, we need to report misconfiguration.
|
||||
if opts.ClusterListenStr == "" && opts.ClusterHost == "" &&
|
||||
opts.ClusterPort == 0 {
|
||||
if opts.RoutesStr != "" {
|
||||
server.PrintAndDie("Solicited routes require cluster capabilities, e.g. --cluster.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
clusterURL, err := url.Parse(opts.ClusterListenStr)
|
||||
h, p, err := net.SplitHostPort(clusterURL.Host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts.ClusterHost = h
|
||||
_, err = fmt.Sscan(p, &opts.ClusterPort)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if clusterURL.User != nil {
|
||||
pass, hasPassword := clusterURL.User.Password()
|
||||
if !hasPassword {
|
||||
return fmt.Errorf("Expected cluster password to be set.")
|
||||
// If cluster flag override, process it
|
||||
if opts.ClusterListenStr != "" {
|
||||
clusterURL, err := url.Parse(opts.ClusterListenStr)
|
||||
h, p, err := net.SplitHostPort(clusterURL.Host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts.ClusterHost = h
|
||||
_, err = fmt.Sscan(p, &opts.ClusterPort)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts.ClusterPassword = pass
|
||||
|
||||
user := clusterURL.User.Username()
|
||||
opts.ClusterUsername = user
|
||||
if clusterURL.User != nil {
|
||||
pass, hasPassword := clusterURL.User.Password()
|
||||
if !hasPassword {
|
||||
return fmt.Errorf("Expected cluster password to be set.")
|
||||
}
|
||||
opts.ClusterPassword = pass
|
||||
|
||||
user := clusterURL.User.Username()
|
||||
opts.ClusterUsername = user
|
||||
} else {
|
||||
// Since we override from flag and there is no user/pwd, make
|
||||
// sure we clear what we may have gotten from config file.
|
||||
opts.ClusterUsername = ""
|
||||
opts.ClusterPassword = ""
|
||||
}
|
||||
}
|
||||
|
||||
// If we have routes but no config file, fill in here.
|
||||
|
||||
@@ -637,6 +637,9 @@ func MergeOptions(fileOpts, flagOpts *Options) *Options {
|
||||
if flagOpts.ProfPort != 0 {
|
||||
opts.ProfPort = flagOpts.ProfPort
|
||||
}
|
||||
if flagOpts.ClusterListenStr != "" {
|
||||
opts.ClusterListenStr = flagOpts.ClusterListenStr
|
||||
}
|
||||
if flagOpts.RoutesStr != "" {
|
||||
mergeRoutes(&opts, flagOpts)
|
||||
}
|
||||
|
||||
@@ -276,6 +276,45 @@ func TestRouteFlagOverride(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestClusterFlagsOverride(t *testing.T) {
|
||||
routeFlag := "nats-route://ruser:top_secret@127.0.0.1:7246"
|
||||
rurl, _ := url.Parse(routeFlag)
|
||||
|
||||
// In this test, we override the cluster listen string. Note that in
|
||||
// the golden options, the cluster other infos correspond to what
|
||||
// is recovered from the configuration file, this explains the
|
||||
// discrepency between ClusterListenStr and the rest.
|
||||
// The server would then process the ClusterListenStr override and
|
||||
// correctly override ClusterHost/ClustherPort/etc..
|
||||
golden := &Options{
|
||||
Host: "127.0.0.1",
|
||||
Port: 7222,
|
||||
ClusterHost: "127.0.0.1",
|
||||
ClusterPort: 7244,
|
||||
ClusterListenStr: "nats://127.0.0.1:8224",
|
||||
ClusterUsername: "ruser",
|
||||
ClusterPassword: "top_secret",
|
||||
ClusterAuthTimeout: 0.5,
|
||||
Routes: []*url.URL{rurl},
|
||||
}
|
||||
|
||||
fopts, err := ProcessConfigFile("./configs/srv_a.conf")
|
||||
if err != nil {
|
||||
t.Fatalf("Received an error reading config file: %v\n", err)
|
||||
}
|
||||
|
||||
// Overrides via flags
|
||||
opts := &Options{
|
||||
ClusterListenStr: "nats://127.0.0.1:8224",
|
||||
}
|
||||
merged := MergeOptions(fopts, opts)
|
||||
|
||||
if !reflect.DeepEqual(golden, merged) {
|
||||
t.Fatalf("Options are incorrect.\nexpected: %+v\ngot: %+v",
|
||||
golden, merged)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteFlagOverrideWithMultiple(t *testing.T) {
|
||||
routeFlag := "nats-route://ruser:top_secret@127.0.0.1:8246, nats-route://ruser:top_secret@127.0.0.1:8266"
|
||||
rurls := RoutesFromStr(routeFlag)
|
||||
|
||||
Reference in New Issue
Block a user