From bed7fa8add77cb44cc4884b746d4ed90213e0eb6 Mon Sep 17 00:00:00 2001 From: "bastian.b" Date: Thu, 16 Aug 2018 18:59:13 +0200 Subject: [PATCH] Add availibity to add default attrs --- README.md | 3 ++- bridge/bridge.go | 2 +- bridge/types.go | 1 + bridge/util.go | 12 +++++++++++- docs/user/run.md | 1 + registrator.go | 2 ++ 6 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c3151649e..95034da10 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ Guide. Typically, running Registrator looks like this: Usage of /bin/registrator: /bin/registrator [options] + -attrs="": Append attrs (ServiceMeta) for all registered services (only for consul) -cleanup=false: Remove dangling services -deregister="always": Deregister exited services "always" or "on-success" -internal=false: Use internal ports instead of published ones @@ -52,8 +53,8 @@ Usage of /bin/registrator: -retry-attempts=0: Max retry attempts to establish a connection with the backend. Use -1 for infinite retries -retry-interval=2000: Interval (in millisecond) between retry-attempts. -tags="": Append tags for all registered services - -ttl=0: TTL for services (default is no expiry) -ttl-refresh=0: Frequency with which service TTLs are refreshed + -ttl=0: TTL for services (default is no expiry) ``` ## Contributing diff --git a/bridge/bridge.go b/bridge/bridge.go index f02ca99c9..28eda5fea 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -266,7 +266,7 @@ func (b *Bridge) newService(port ServicePort, isgroup bool) *Service { port.HostIP = b.config.HostIp } - metadata, metadataFromPort := serviceMetaData(container.Config, port.ExposedPort) + metadata, metadataFromPort := serviceMetaData(container.Config, port.ExposedPort, b.config.ForceAttrs) ignore := mapDefault(metadata, "ignore", "") if ignore != "" { diff --git a/bridge/types.go b/bridge/types.go index e643ed3f5..0600435c2 100644 --- a/bridge/types.go +++ b/bridge/types.go @@ -25,6 +25,7 @@ type Config struct { Explicit bool UseIpFromLabel string ForceTags string + ForceAttrs string RefreshTtl int RefreshInterval int DeregisterCheck string diff --git a/bridge/util.go b/bridge/util.go index e3c295cfb..fe3275135 100644 --- a/bridge/util.go +++ b/bridge/util.go @@ -54,13 +54,23 @@ func combineTags(tagParts ...string) []string { return tags } -func serviceMetaData(config *dockerapi.Config, port string) (map[string]string, map[string]bool) { +func serviceMetaData(config *dockerapi.Config, port string, default_attrs string) (map[string]string, map[string]bool) { meta := config.Env for k, v := range config.Labels { meta = append(meta, k+"="+v) } metadata := make(map[string]string) metadataFromPort := make(map[string]bool) + + meta_attrs := strings.Split(default_attrs, ",") + if len(meta_attrs) > 1 { + for _, kv := range meta_attrs { + kvp := strings.SplitN(kv, "=", 2) + key := strings.ToLower(kvp[0]) + metadata[key] = kvp[1] + } + } + for _, kv := range meta { kvp := strings.SplitN(kv, "=", 2) if strings.HasPrefix(kvp[0], "SERVICE_") && len(kvp) > 1 { diff --git a/docs/user/run.md b/docs/user/run.md index 5c084474b..be186788f 100644 --- a/docs/user/run.md +++ b/docs/user/run.md @@ -33,6 +33,7 @@ hostname (`-h $HOSTNAME`) and using the `-ip` Registrator option below. Option | Since | Description ------ | ----- | ----------- +`-attrs ` | v? | Force comma-separated attrs on all registered services (Consul Only) `-cleanup` | v7 | Cleanup dangling services `-deregister ` | v6 | Deregister exited services "always" or "on-success". Default: always `-internal` | | Use exposed ports instead of published ports diff --git a/registrator.go b/registrator.go index e26d3397a..9d47b0a97 100644 --- a/registrator.go +++ b/registrator.go @@ -26,6 +26,7 @@ var useIpFromLabel = flag.String("useIpFromLabel", "", "Use IP which is stored i var refreshInterval = flag.Int("ttl-refresh", 0, "Frequency with which service TTLs are refreshed") var refreshTtl = flag.Int("ttl", 0, "TTL for services (default is no expiry)") var forceTags = flag.String("tags", "", "Append tags for all registered services") +var forceAttrs = flag.String("attrs", "", "Append attrs (ServiceMeta) for all registered services (only for consul)") var resyncInterval = flag.Int("resync", 0, "Frequency with which services are resynchronized") var deregister = flag.String("deregister", "always", "Deregister exited services \"always\" or \"on-success\"") var retryAttempts = flag.Int("retry-attempts", 0, "Max retry attempts to establish a connection with the backend. Use -1 for infinite retries") @@ -108,6 +109,7 @@ func main() { Explicit: *explicit, UseIpFromLabel: *useIpFromLabel, ForceTags: *forceTags, + ForceAttrs: *forceAttrs, RefreshTtl: *refreshTtl, RefreshInterval: *refreshInterval, DeregisterCheck: *deregister,