Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to constrain fabio instance to specific consul namespace #812

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ type Consul struct {
NoRouteHTMLPath string
TagPrefix string
Register bool
Namespace string
ServiceAddr string
ServiceName string
ServiceTags []string
Expand Down
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var defaultConfig = &Config{
NoRouteHTMLPath: "/fabio/noroute.html",
TagPrefix: "urlprefix-",
Register: true,
Namespace: "",
ServiceAddr: ":9998",
ServiceName: "fabio",
ServiceStatus: []string{"passing"},
Expand Down
1 change: 1 addition & 0 deletions config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<ui.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")
Expand Down
7 changes: 7 additions & 0 deletions config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions docs/content/ref/registry.consul.namespace.md
Original file line number Diff line number Diff line change
@@ -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 =
11 changes: 11 additions & 0 deletions fabio.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion registry/consul/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
11 changes: 6 additions & 5 deletions registry/consul/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down