Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log the list of config files loaded when starting the nomad agent #536

Merged
merged 3 commits into from
Dec 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions command/agent/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ func (c *Command) Run(args []string) int {
return 1
}

// Log config files
c.Ui.Info(fmt.Sprintf("Loaded configuration from %s", strings.Join(config.Files, ", ")))

// Initialize the telemetry
if err := c.setupTelementry(config); err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing telemetry: %s", err))
Expand Down
14 changes: 13 additions & 1 deletion command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ type Config struct {
Revision string
Version string
VersionPrerelease string

// List of config files that have been loaded (in order)
Files []string
}

// AtlasConfig is used to enable an parameterize the Atlas integration
Expand Down Expand Up @@ -366,6 +369,9 @@ func (a *Config) Merge(b *Config) *Config {
result.Atlas = result.Atlas.Merge(b.Atlas)
}

// Merge config files lists
result.Files = append(result.Files, b.Files...)

return &result
}

Expand Down Expand Up @@ -565,7 +571,13 @@ func LoadConfigFile(path string) (*Config, error) {
if err != nil {
return nil, err
}
return LoadConfigString(string(d))

config, err := LoadConfigString(string(d))
if err == nil {
config.Files = append(config.Files, path)
}

return config, err
}

// LoadConfigDir loads all the configurations in the given directory
Expand Down
37 changes: 37 additions & 0 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ func TestConfig_LoadConfig(t *testing.T) {
t.Fatalf("bad: %#v", config)
}

expectedConfigFiles := []string{fh.Name()}
if !reflect.DeepEqual(config.Files, expectedConfigFiles) {
t.Errorf("Loaded configs don't match\nExpected\n%+vGot\n%+v\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda weird style: Could it instead be t.Errorf("Loaded configs don't match: want %#v; got %#v"
You don't need the last \n anyways.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use this pattern when dumping a complex type (struct, list, etc.) because each will appear on its own line. This makes it easier to spot the differences and/or copy-paste into a diff program if it's very large.

For example, this is easy to read:

Loaded configs don't match: want 1; got 2

But this is not:

Loaded configs don't match: want []string{"charlie", "bracvo", "delta"}; got []string{"charlie", "bravco", "delta"}

The pattern I used shows up like this when the comparison fails:

Loaded configs don't match
Expected
[charlie bracvo delta]
Got
[charlie bravco delta]

expectedConfigFiles, config.Files)
}

dir, err := ioutil.TempDir("", "nomad")
if err != nil {
t.Fatalf("err: %s", err)
Expand All @@ -286,6 +292,37 @@ func TestConfig_LoadConfig(t *testing.T) {
if config.Datacenter != "sfo" {
t.Fatalf("bad: %#v", config)
}

expectedConfigFiles = []string{file1}
if !reflect.DeepEqual(config.Files, expectedConfigFiles) {
t.Errorf("Loaded configs don't match\nExpected\n%+vGot\n%+v\n",
expectedConfigFiles, config.Files)
}
}

func TestConfig_LoadConfigsFileOrder(t *testing.T) {
config1, err := LoadConfigDir("test-resources/etcnomad")
if err != nil {
t.Fatalf("Failed to load config: %s", err)
}

config2, err := LoadConfig("test-resources/myconf")
if err != nil {
t.Fatalf("Failed to load config: %s", err)
}

expected := []string{
"test-resources/etcnomad/common.hcl",
"test-resources/etcnomad/server.json",
"test-resources/myconf",
}

config := config1.Merge(config2)

if !reflect.DeepEqual(config.Files, expected) {
t.Errorf("Loaded configs don't match\nExpected\n%+vGot\n%+v\n",
expected, config.Files)
}
}

func TestConfig_Listener(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions command/agent/test-resources/etcnomad/common.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bind_addr = "0.0.0.0"
Empty file.
5 changes: 5 additions & 0 deletions command/agent/test-resources/etcnomad/server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"advertise": {
"rpc": "127.0.0.1:4647"
}
}
1 change: 1 addition & 0 deletions command/agent/test-resources/myconf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data_dir = "/var/lib/nomad"