diff --git a/agent/run.go b/agent/run.go index a99faf4..f6c6585 100644 --- a/agent/run.go +++ b/agent/run.go @@ -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 diff --git a/cmd/mackerel-container-agent/main.go b/cmd/mackerel-container-agent/main.go index e847232..74d5a3a 100644 --- a/cmd/mackerel-container-agent/main.go +++ b/cmd/mackerel-container-agent/main.go @@ -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)) diff --git a/config/config.go b/config/config.go index f342ed2..58fbf1d 100644 --- a/config/config.go +++ b/config/config.go @@ -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"` @@ -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 { diff --git a/config/config_test.go b/config/config_test.go index 6ddb924..425493b 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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-.+$"