Skip to content

Commit

Permalink
Merge pull request #354 from mkadokawa-idcf/feature/host-displayName-…
Browse files Browse the repository at this point in the history
…and-memo

Set `displayName`, `memo` via agent
  • Loading branch information
azukiazusa1 authored Apr 9, 2024
2 parents c90bcb7 + 7de613a commit 96e2133
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions agent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func run(
return err
}
hostParam.RoleFullnames = conf.Roles
hostParam.DisplayName = conf.DisplayName
hostParam.Memo = conf.Memo
hostParam.Checks = checkManager.Configs()

duration = hostIDInitialRetryInterval
Expand Down
2 changes: 2 additions & 0 deletions cmd/mackerel-container-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func main() {
"MACKEREL_KUBERNETES_POD_NAME",
"MACKEREL_LOG_LEVEL",
"MACKEREL_ROLES",
"MACKEREL_DISPLAY_NAME",
"MACKEREL_MEMO",
}
for _, v := range env {
logger.Debugf("%s=%s", v, os.Getenv(v))
Expand Down
10 changes: 10 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Config struct {
Apikey string `yaml:"apikey"`
Root string `yaml:"root"`
Roles []string `yaml:"roles"`
DisplayName string `yaml:"displayName"`
Memo string `yaml:"memo"`
IgnoreContainer Regexpwrapper `yaml:"ignoreContainer"`
ReadinessProbe *Probe `yaml:"readinessProbe"`
HostStatusOnStart HostStatus `yaml:"hostStatusOnStart"`
Expand Down Expand Up @@ -150,6 +152,14 @@ func load(ctx context.Context, location string) (*Config, error) {
conf.Roles = parseRoles(v)
}

if conf.DisplayName == "" {
conf.DisplayName = os.Getenv("MACKEREL_DISPLAY_NAME")
}

if conf.Memo == "" {
conf.Memo = os.Getenv("MACKEREL_MEMO")
}

if conf.IgnoreContainer.Regexp == nil {
if r := os.Getenv("MACKEREL_IGNORE_CONTAINER"); r != "" {
if err := conf.IgnoreContainer.UnmarshalText([]byte(r)); err != nil {
Expand Down
86 changes: 86 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,92 @@ roles:
}
}

func TestDisplayName(t *testing.T) {
conf := newConfigFile(t, `
displayName: foo
`)

t.Setenv("MACKEREL_DISPLAY_NAME", "bar")

testCases := []struct {
name string
location string
expect *Config
}{
{
name: "config",
location: conf,
expect: &Config{
Root: defaultRoot,
DisplayName: "foo",
},
},
{
name: "env",
location: "",
expect: &Config{
Root: defaultRoot,
DisplayName: "bar",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
conf, err := load(context.Background(), tc.location)
if err != nil {
t.Errorf("should not raise error: %v", err)
}
if !reflect.DeepEqual(conf, tc.expect) {
t.Errorf("expect %#v, got %#v", tc.expect, conf)
}
})
}
}

func TestMemo(t *testing.T) {
conf := newConfigFile(t, `
memo: foo
`)

t.Setenv("MACKEREL_MEMO", "bar")

testCases := []struct {
name string
location string
expect *Config
}{
{
name: "config",
location: conf,
expect: &Config{
Root: defaultRoot,
Memo: "foo",
},
},
{
name: "env",
location: "",
expect: &Config{
Root: defaultRoot,
Memo: "bar",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
conf, err := load(context.Background(), tc.location)
if err != nil {
t.Errorf("should not raise error: %v", err)
}
if !reflect.DeepEqual(conf, tc.expect) {
t.Errorf("expect %#v, got %#v", tc.expect, conf)
}
})
}
}

func TestIgnoreContainer(t *testing.T) {
conf := newConfigFile(t, `
ignoreContainer: "^mackerel-.+$"
Expand Down

0 comments on commit 96e2133

Please sign in to comment.