diff --git a/config/config.go b/config/config.go index 85edd5fdb..6a1cafb0b 100644 --- a/config/config.go +++ b/config/config.go @@ -145,6 +145,7 @@ type Consul struct { NoRouteHTMLPath string TagPrefix string Register bool + Namespace string ServiceAddr string ServiceName string ServiceTags []string diff --git a/config/default.go b/config/default.go index 5738b0392..544c0ca55 100644 --- a/config/default.go +++ b/config/default.go @@ -57,6 +57,7 @@ var defaultConfig = &Config{ NoRouteHTMLPath: "/fabio/noroute.html", TagPrefix: "urlprefix-", Register: true, + Namespace: "", ServiceAddr: ":9998", ServiceName: "fabio", ServiceStatus: []string{"passing"}, diff --git a/config/load.go b/config/load.go index 60ce836b1..eea8551e5 100644 --- a/config/load.go +++ b/config/load.go @@ -181,6 +181,7 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c f.StringVar(&cfg.Registry.Consul.TLS.CAPath, "registry.consul.tls.capath", defaultConfig.Registry.Consul.TLS.CAPath, "path to consul CA directory") f.BoolVar(&cfg.Registry.Consul.TLS.InsecureSkipVerify, "registry.consul.tls.insecureskipverify", defaultConfig.Registry.Consul.TLS.InsecureSkipVerify, "is tls check enabled") f.BoolVar(&cfg.Registry.Consul.Register, "registry.consul.register.enabled", defaultConfig.Registry.Consul.Register, "register fabio in consul") + f.StringVar(&cfg.Registry.Consul.Namespace, "registry.consul.namespace", defaultConfig.Registry.Consul.Namespace, "consul namespace in which fabio is active") f.StringVar(&cfg.Registry.Consul.ServiceAddr, "registry.consul.register.addr", "", "service registration address") f.StringVar(&cfg.Registry.Consul.ServiceName, "registry.consul.register.name", defaultConfig.Registry.Consul.ServiceName, "service registration name") f.StringSliceVar(&cfg.Registry.Consul.ServiceTags, "registry.consul.register.tags", defaultConfig.Registry.Consul.ServiceTags, "service registration tags") diff --git a/config/load_test.go b/config/load_test.go index 371b73f85..cdca8ed8f 100644 --- a/config/load_test.go +++ b/config/load_test.go @@ -619,6 +619,13 @@ func TestLoad(t *testing.T) { return cfg }, }, + { + args: []string{"-registry.consul.namespace", "ns1"}, + cfg: func(cfg *Config) *Config { + cfg.Registry.Consul.Namespace = "ns1" + return cfg + }, + }, { args: []string{"-registry.consul.register.addr", "1.2.3.4:5555"}, cfg: func(cfg *Config) *Config { diff --git a/docs/content/ref/registry.consul.namespace.md b/docs/content/ref/registry.consul.namespace.md new file mode 100644 index 000000000..e11c47d6d --- /dev/null +++ b/docs/content/ref/registry.consul.namespace.md @@ -0,0 +1,13 @@ +--- +title: "registry.consul.namespace" +--- + +`registry.consul.namespace` configures the consul namespace in which fabio will register itself. + + Namespaces are a feature only available in the enterprise version of consul. In the open-source + version or with an empty namespace option fabio will be registered in the default namespace. Only + services running in the same consul namespace will be picked up by fabio. + +The default is + + registry.consul.namespace = diff --git a/fabio.properties b/fabio.properties index ae432fb47..381274641 100644 --- a/fabio.properties +++ b/fabio.properties @@ -803,6 +803,17 @@ # registry.consul.register.enabled = true +# registry.consul.namespace configures the consul namespace in which fabio will register itself. +# +# Namespaces are a feature only available in the enterprise version of consul. In the open-source +# version or with an empty namespace option fabio will be registered in the default namespace. Only +# services running in the same consul namespace will be picked up by fabio. +# +# The default is +# +# registry.consul.namespace = + + # registry.consul.register.addr configures the address for the service registration. # # Fabio registers itself in consul with this host:port address. diff --git a/registry/consul/backend.go b/registry/consul/backend.go index cfff930ef..8965b9061 100644 --- a/registry/consul/backend.go +++ b/registry/consul/backend.go @@ -20,7 +20,7 @@ type be struct { func NewBackend(cfg *config.Consul) (registry.Backend, error) { - consulCfg := &api.Config{Address: cfg.Addr, Scheme: cfg.Scheme, Token: cfg.Token} + consulCfg := &api.Config{Address: cfg.Addr, Scheme: cfg.Scheme, Token: cfg.Token, Namespace: cfg.Namespace} if cfg.Scheme == "https" { consulCfg.TLSConfig.KeyFile = cfg.TLS.KeyFile consulCfg.TLSConfig.CertFile = cfg.TLS.CertFile @@ -43,6 +43,11 @@ func NewBackend(cfg *config.Consul) (registry.Backend, error) { // we're good log.Printf("[INFO] consul: Connecting to %q in datacenter %q", cfg.Addr, dc) + if cfg.Namespace != "" { + log.Printf("[INFO] consul: Connecting to namespace %q", cfg.Namespace) + } else { + log.Printf("[INFO] consul: Connecting to default namespace") + } return &be{c: c, dc: dc, cfg: cfg}, nil } diff --git a/registry/consul/register.go b/registry/consul/register.go index 89fd65778..fcbbbd23e 100644 --- a/registry/consul/register.go +++ b/registry/consul/register.go @@ -128,11 +128,12 @@ func serviceRegistration(cfg *config.Consul, serviceName string) (*api.AgentServ } service := &api.AgentServiceRegistration{ - ID: serviceID, - Name: serviceName, - Address: ip.String(), - Port: port, - Tags: cfg.ServiceTags, + ID: serviceID, + Name: serviceName, + Address: ip.String(), + Port: port, + Tags: cfg.ServiceTags, + Namespace: cfg.Namespace, // Set the checks for the service. // // Both checks must pass for Consul to consider the service healthy and therefore serve the fabio instance to clients.