From 918ed1ceffca8db72d682519ab83f3bc2088f290 Mon Sep 17 00:00:00 2001 From: lightcode Date: Tue, 17 Apr 2018 10:08:15 +0200 Subject: [PATCH] Add an option to change consul tag --- README.md | 3 ++- consul.go | 7 ++++--- main.go | 8 +++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5445fff3..d94ea077 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,8 @@ Kube2consul runs in a kubernetes cluster by default. It is able to work out of c | Command line option | Environment option | Default value | | ------------------- | -------------------- | ------------------------- | | `-consul-api` | `K2C_CONSUL_API` | `"127.0.0.1:8500"` | +| `-consul-tag` | `K2C_CONSUL_TAG` | `"kube2consul"` | | `-consul-token` | `K2C_CONSUL_TOKEN` | `""` | +| `-kubeconfig` | `K2C_KUBECONFIG` | `""` | | `-kubernetes-api` | `K2C_KUBERNETES_API` | `""` | | `-resync-period` | `K2C_RESYNC_PERIOD` | `30` | -| `-kubeconfig` | `K2C_KUBECONFIG` | `""` | diff --git a/consul.go b/consul.go index cc7618c7..59c974fc 100644 --- a/consul.go +++ b/consul.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/golang/glog" consulapi "github.com/hashicorp/consul/api" ) @@ -27,7 +28,7 @@ func (k2c *kube2consul) registerEndpoint(e Endpoint) error { return nil } - consulServices, _, err := k2c.consulCatalog.Service(e.Name, consulTag, nil) + consulServices, _, err := k2c.consulCatalog.Service(e.Name, opts.consulTag, nil) if err != nil { return fmt.Errorf("Failed to get services: %v", err) } @@ -41,7 +42,7 @@ func (k2c *kube2consul) registerEndpoint(e Endpoint) error { service := &consulapi.AgentService{ Service: e.Name, Port: int(e.Port), - Tags: []string{consulTag}, + Tags: []string{opts.consulTag}, } reg := &consulapi.CatalogRegistration{ @@ -70,7 +71,7 @@ func endpointExists(refName, address string, port int, endpoints []Endpoint) boo func (k2c *kube2consul) removeDeletedEndpoints(serviceName string, endpoints []Endpoint) error { updatedNodes := make(map[string]struct{}) - services, _, err := k2c.consulCatalog.Service(serviceName, consulTag, nil) + services, _, err := k2c.consulCatalog.Service(serviceName, opts.consulTag, nil) if err != nil { return fmt.Errorf("Failed to get services: %v", err) } diff --git a/main.go b/main.go index a558a998..2fa664a1 100644 --- a/main.go +++ b/main.go @@ -29,10 +29,6 @@ var ( lockCh <-chan struct{} ) -const ( - consulTag = "kube2consul" -) - type kube2consul struct { consulCatalog *consulapi.Catalog endpointsStore kcache.Store @@ -48,6 +44,7 @@ type cliOpts struct { lock bool lockKey string noHealth bool + consulTag string } func init() { @@ -60,6 +57,7 @@ func init() { flag.BoolVar(&opts.lock, "lock", false, "Acquires a lock with consul to ensure that only one instance of kube2consul is running") flag.StringVar(&opts.lockKey, "lock-key", "locks/kube2consul/.lock", "Key used for locking") flag.BoolVar(&opts.noHealth, "no-health", false, "Disable endpoint /health on port 8080") + flag.StringVar(&opts.consulTag, "consul-tag", "kube2consul", "Tag setted on services to identify services managed by kube2consul in Consul") } func inSlice(value string, slice []string) bool { @@ -87,7 +85,7 @@ func (k2c *kube2consul) RemoveDNSGarbage() { } for name, tags := range services { - if !inSlice(consulTag, tags) { + if !inSlice(opts.consulTag, tags) { continue }