Skip to content

Commit

Permalink
Merge pull request #185 from mailgun/thrawn/develop
Browse files Browse the repository at this point in the history
Use the nomad alloc id if it exists in the environment
  • Loading branch information
thrawn01 authored Aug 4, 2020
2 parents aae1449 + fea45de commit 9746b7d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,13 @@ func defaultProxyWithClientID(clientID string) *Proxy {
// newClientID creates a unique id that identifies this particular Kafka-Pixy
// in both Kafka and ZooKeeper.
func newClientID() string {
if nomad := os.Getenv("NOMAD_ALLOC_ID"); nomad != "" {
parts := strings.Split(nomad, "-")
if idx := os.Getenv("NOMAD_ALLOC_INDEX"); idx != "" {
return "kp_nomad_" + parts[0] + "_" + idx
}
return "kp_nomad_" + parts[0]
}
hostname, err := os.Hostname()
if err != nil {
token := make([]byte, 4)
Expand Down
16 changes: 16 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"os"
"testing"
"time"

Expand Down Expand Up @@ -154,3 +155,18 @@ func (s *ConfigSuite) TestFromYAMLKafkaTLS(c *C) {
c.Assert(kafkaCfg.ClientCertKeyFile, Equals, "../testdata/client.key")
c.Assert(kafkaCfg.InsecureSkipVerify, Equals, false)
}

func (s *ConfigSuite) TestClientIDFromNomad(c *C) {
// When
os.Setenv("NOMAD_ALLOC_ID", "b313e983-c906-49df-b5f7-f6fbc59b3297")
os.Setenv("NOMAD_ALLOC_INDEX", "0")
defer func() {
os.Setenv("NOMAD_ALLOC_ID", "")
os.Setenv("NOMAD_ALLOC_INDEX", "")
}()
appCfg, err := FromYAMLFile("../default.yaml")

// Then
c.Assert(err, IsNil)
c.Assert("kp_nomad_b313e983_0", Equals, appCfg.Proxies["default"].ClientID)
}

0 comments on commit 9746b7d

Please sign in to comment.