Skip to content

Commit

Permalink
Add availibity to add default attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
gokuatkai committed Aug 17, 2018
1 parent 4b918ac commit bed7fa8
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Guide. Typically, running Registrator looks like this:
Usage of /bin/registrator:
/bin/registrator [options] <registry URI>
-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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
1 change: 1 addition & 0 deletions bridge/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Config struct {
Explicit bool
UseIpFromLabel string
ForceTags string
ForceAttrs string
RefreshTtl int
RefreshInterval int
DeregisterCheck string
Expand Down
12 changes: 11 additions & 1 deletion bridge/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions docs/user/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ hostname (`-h $HOSTNAME`) and using the `-ip` Registrator option below.

Option | Since | Description
------ | ----- | -----------
`-attrs <attrs>` | v? | Force comma-separated attrs on all registered services (Consul Only)
`-cleanup` | v7 | Cleanup dangling services
`-deregister <mode>` | v6 | Deregister exited services "always" or "on-success". Default: always
`-internal` | | Use exposed ports instead of published ports
Expand Down
2 changes: 2 additions & 0 deletions registrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -108,6 +109,7 @@ func main() {
Explicit: *explicit,
UseIpFromLabel: *useIpFromLabel,
ForceTags: *forceTags,
ForceAttrs: *forceAttrs,
RefreshTtl: *refreshTtl,
RefreshInterval: *refreshInterval,
DeregisterCheck: *deregister,
Expand Down

0 comments on commit bed7fa8

Please sign in to comment.