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 support for raw tags #454

Open
wants to merge 2 commits 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 @@ -132,6 +132,7 @@ type Consul struct {
KVPath string
NoRouteHTMLPath string
TagPrefix string
RawTagPrefix string
Register bool
ServiceAddr string
ServiceName string
Expand Down
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var defaultConfig = &Config{
KVPath: "/fabio/config",
NoRouteHTMLPath: "/fabio/noroute.html",
TagPrefix: "urlprefix-",
RawTagPrefix: "fabio",
Register: true,
ServiceAddr: ":9998",
ServiceName: "fabio",
Expand Down
1 change: 1 addition & 0 deletions config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c
f.StringVar(&cfg.Registry.Consul.KVPath, "registry.consul.kvpath", defaultConfig.Registry.Consul.KVPath, "consul KV path for manual overrides")
f.StringVar(&cfg.Registry.Consul.NoRouteHTMLPath, "registry.consul.noroutehtmlpath", defaultConfig.Registry.Consul.NoRouteHTMLPath, "consul KV path for HTML returned when no route is found")
f.StringVar(&cfg.Registry.Consul.TagPrefix, "registry.consul.tagprefix", defaultConfig.Registry.Consul.TagPrefix, "prefix for consul tags")
f.StringVar(&cfg.Registry.Consul.RawTagPrefix, "registry.consul.rawtagprefix", defaultConfig.Registry.Consul.RawTagPrefix, "prefix for raw consul tags")
f.BoolVar(&cfg.Registry.Consul.Register, "registry.consul.register.enabled", defaultConfig.Registry.Consul.Register, "register fabio in consul")
f.StringVar(&cfg.Registry.Consul.ServiceAddr, "registry.consul.register.addr", defaultConfig.Registry.Consul.ServiceAddr, "service registration address")
f.StringVar(&cfg.Registry.Consul.ServiceName, "registry.consul.register.name", defaultConfig.Registry.Consul.ServiceName, "service registration name")
Expand Down
7 changes: 7 additions & 0 deletions config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,13 @@ func TestLoad(t *testing.T) {
return cfg
},
},
{
args: []string{"-registry.consul.rawtagprefix", "p-"},
cfg: func(cfg *Config) *Config {
cfg.Registry.Consul.RawTagPrefix = "p-"
return cfg
},
},
{
args: []string{"-registry.consul.register.enabled=false"},
cfg: func(cfg *Config) *Config {
Expand Down
11 changes: 11 additions & 0 deletions fabio.properties
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,17 @@
# registry.consul.tagprefix = urlprefix-


# registry.consul.rawtagprefix configures the prefix for tags which contain route commands.
#
# Consul tags with this prefix will be added to the routing table as is
# but without the tag prefix and any leading whitespace. This allows to
# publish any valid route command to the routing table.
#
# The default is
#
# registry.consul.rawtagprefix = fabio


# registry.consul.register.enabled configures whether fabio registers itself in consul.
#
# Fabio will register itself in consul only if this value is set to "true" which
Expand Down
23 changes: 17 additions & 6 deletions registry/consul/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ func watchServices(client *api.Client, config *config.Consul, svcConfig chan str
}

log.Printf("[DEBUG] consul: Health changed to #%d", meta.LastIndex)
svcConfig <- servicesConfig(client, passingServices(checks, config.ServiceStatus, strict), config.TagPrefix)
svcConfig <- servicesConfig(client, passingServices(checks, config.ServiceStatus, strict), config.TagPrefix, config.RawTagPrefix)
lastIndex = meta.LastIndex
}
}

// servicesConfig determines which service instances have passing health checks
// and then finds the ones which have tags with the right prefix to build the config from.
func servicesConfig(client *api.Client, checks []*api.HealthCheck, tagPrefix string) string {
func servicesConfig(client *api.Client, checks []*api.HealthCheck, tagPrefix, rawTagPrefix string) string {
// map service name to list of service passing for which the health check is ok
m := map[string]map[string]bool{}
for _, check := range checks {
Expand All @@ -54,7 +54,7 @@ func servicesConfig(client *api.Client, checks []*api.HealthCheck, tagPrefix str

var config []string
for name, passing := range m {
cfg := serviceConfig(client, name, passing, tagPrefix)
cfg := serviceConfig(client, name, passing, tagPrefix, rawTagPrefix)
config = append(config, cfg...)
}

Expand All @@ -65,7 +65,7 @@ func servicesConfig(client *api.Client, checks []*api.HealthCheck, tagPrefix str
}

// serviceConfig constructs the config for all good instances of a single service.
func serviceConfig(client *api.Client, name string, passing map[string]bool, tagPrefix string) (config []string) {
func serviceConfig(client *api.Client, name string, passing map[string]bool, tagPrefix, rawTagPrefix string) (config []string) {
if name == "" || len(passing) == 0 {
return nil
}
Expand All @@ -87,23 +87,34 @@ func serviceConfig(client *api.Client, name string, passing map[string]bool, tag
"DC": dc,
}

if !strings.HasSuffix(rawTagPrefix, " ") {
rawTagPrefix += " "
}

for _, svc := range svcs {
// check if the instance is in the list of instances
// which passed the health check
if _, ok := passing[fmt.Sprintf("%s.%s", svc.Node, svc.ServiceID)]; !ok {
continue
}

// get all tags which do not have the tag prefix
// get all tags which do not have the tag prefix or the raw tag prefix
var svctags []string
for _, tag := range svc.ServiceTags {
if !strings.HasPrefix(tag, tagPrefix) {
if !strings.HasPrefix(tag, tagPrefix) && !strings.HasPrefix(tag, rawTagPrefix) {
svctags = append(svctags, tag)
}
}

// generate route commands
for _, tag := range svc.ServiceTags {
// add raw tags as is
if strings.HasPrefix(tag, rawTagPrefix) {
tag = strings.TrimSpace(tag[len(rawTagPrefix):])
config = append(config, tag)
continue
}

if route, opts, ok := parseURLPrefixTag(tag, tagPrefix, env); ok {
name, addr, port := svc.ServiceName, svc.ServiceAddress, svc.ServicePort

Expand Down