Skip to content

Commit

Permalink
Issue #396: treat registry.consul.kvpath as prefix
Browse files Browse the repository at this point in the history
This patch extends the behavior of the registry.consul.kvpath
and treats it as a prefix instead of a single key. fabio will
now list the key and all available subkeys in alphabetical order
and combine them for the routing table. Each subsection will get
header which contains key name.

Todo: the ui needs to handle that as well
  • Loading branch information
magiconair committed Feb 2, 2018
1 parent fcc935c commit 17363fc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
20 changes: 20 additions & 0 deletions docs/content/ref/registry.consul.kvpath.md
Expand Up @@ -8,6 +8,26 @@ The Consul KV path is watched for changes which get appended to
the routing table. This allows for manual overrides and weighted
round-robin routes.

As of version 1.5.7 fabio will treat the kv path as a prefix and
combine the values of the key itself and all its subkeys in
alphabetical order.

To see all updates you may want to set [`-log.routes.format`](/ref/log.routes.format/)
to `all`.

You can modify the content of the routes with the `consul` tool or via
the [Consul API](https://www.consul.io/api/index.html):

```
consul put fabio/config "route add svc /maint http://5.6.7.8:5000\nroute add svc / http://1.2.3.4:5000\n"
# fabio >= 1.5.7 supports prefix match
consul put fabio/config/maint "route add svc /maint http://5.6.7.8:5000"
consul put fabio/config/catchall "route add svc / http://1.2.3.4:5000"
consul delete fabio/config/maint
```

The default is

registry.consul.kvpath = /fabio/config
4 changes: 3 additions & 1 deletion fabio.properties
Expand Up @@ -609,7 +609,9 @@
#
# The consul KV path is watched for changes which get appended to
# the routing table. This allows for manual overrides and weighted
# round-robin routes.
# round-robin routes. The key itself (e.g. fabio/config) and all
# subkeys (e.g. fabio/config/foo and fabio/config/bar) are combined
# in alphabetical order.
#
# The default is
#
Expand Down
19 changes: 18 additions & 1 deletion registry/consul/kv.go
Expand Up @@ -15,7 +15,7 @@ func watchKV(client *api.Client, path string, config chan string) {
var lastValue string

for {
value, index, err := getKV(client, path, lastIndex)
value, index, err := listKV(client, path, lastIndex)
if err != nil {
log.Printf("[WARN] consul: Error fetching config from %s. %v", path, err)
time.Sleep(time.Second)
Expand All @@ -30,6 +30,23 @@ func watchKV(client *api.Client, path string, config chan string) {
}
}

func listKV(client *api.Client, path string, waitIndex uint64) (string, uint64, error) {
q := &api.QueryOptions{RequireConsistent: true, WaitIndex: waitIndex}
kvpairs, meta, err := client.KV().List(path, q)
if err != nil {
return "", 0, err
}
if len(kvpairs) == 0 {
return "", meta.LastIndex, nil
}
var s []string
for _, kvpair := range kvpairs {
val := "# --- " + kvpair.Key + "\n" + strings.TrimSpace(string(kvpair.Value))
s = append(s, val)
}
return strings.Join(s, "\n\n"), meta.LastIndex, nil
}

func getKV(client *api.Client, key string, waitIndex uint64) (string, uint64, error) {
q := &api.QueryOptions{RequireConsistent: true, WaitIndex: waitIndex}
kvpair, meta, err := client.KV().Get(key, q)
Expand Down

0 comments on commit 17363fc

Please sign in to comment.