diff --git a/agent/plugins.go b/agent/plugins.go index 8e2e58ab..fc4dc572 100644 --- a/agent/plugins.go +++ b/agent/plugins.go @@ -65,7 +65,7 @@ func (a *Agent) setupPluginConfig(cfg map[string]string) { // Nomad config from the agent. If we do not find it, opt-in by default. val, ok := cfg[plugins.ConfigKeyNomadConfigInherit] if !ok { - nomadHelper.MergeMapWithAgentConfig(cfg, a.nomadCfg) + nomadHelper.MergeMapWithAgentConfig(cfg, a.config.Nomad) return } @@ -79,7 +79,7 @@ func (a *Agent) setupPluginConfig(cfg map[string]string) { return } if boolVal { - nomadHelper.MergeMapWithAgentConfig(cfg, a.nomadCfg) + nomadHelper.MergeMapWithAgentConfig(cfg, a.config.Nomad) } } diff --git a/agent/plugins_test.go b/agent/plugins_test.go index 69e87155..82234c20 100644 --- a/agent/plugins_test.go +++ b/agent/plugins_test.go @@ -8,7 +8,6 @@ import ( hclog "github.com/hashicorp/go-hclog" "github.com/hashicorp/nomad-autoscaler/agent/config" - "github.com/hashicorp/nomad/api" "github.com/stretchr/testify/assert" ) @@ -25,24 +24,19 @@ func TestAgent_setupPluginConfig(t *testing.T) { }, inputAgent: &Agent{ logger: hclog.NewNullLogger(), - nomadCfg: &api.Config{ - Address: "test", - Region: "test", - SecretID: "test", - Namespace: "test", - HttpAuth: &api.HttpBasicAuth{ - Username: "test", - Password: "test", - }, - TLSConfig: &api.TLSConfig{ - CACert: "test", - CAPath: "test", - ClientCert: "test", - ClientKey: "test", - TLSServerName: "test", - Insecure: true, - }, - }, + config: &config.Agent{Nomad: &config.Nomad{ + Address: "test", + Region: "test", + Namespace: "test", + Token: "test", + HTTPAuth: "test:test", + CACert: "test", + CAPath: "test", + ClientCert: "test", + ClientKey: "test", + TLSServerName: "test", + SkipVerify: true, + }}, }, expectedOutputCfg: map[string]string{ "nomad_config_inherit": "false", @@ -55,24 +49,19 @@ func TestAgent_setupPluginConfig(t *testing.T) { }, inputAgent: &Agent{ logger: hclog.NewNullLogger(), - nomadCfg: &api.Config{ - Address: "test", - Region: "test", - SecretID: "test", - Namespace: "test", - HttpAuth: &api.HttpBasicAuth{ - Username: "test", - Password: "test", - }, - TLSConfig: &api.TLSConfig{ - CACert: "test", - CAPath: "test", - ClientCert: "test", - ClientKey: "test", - TLSServerName: "test", - Insecure: true, - }, - }, + config: &config.Agent{Nomad: &config.Nomad{ + Address: "test", + Region: "test", + Namespace: "test", + Token: "test", + HTTPAuth: "test:test", + CACert: "test", + CAPath: "test", + ClientCert: "test", + ClientKey: "test", + TLSServerName: "test", + SkipVerify: true, + }}, }, expectedOutputCfg: map[string]string{ "nomad_config_inherit": "falso", @@ -85,24 +74,19 @@ func TestAgent_setupPluginConfig(t *testing.T) { }, inputAgent: &Agent{ logger: hclog.NewNullLogger(), - nomadCfg: &api.Config{ - Address: "test", - Region: "test", - SecretID: "test", - Namespace: "test", - HttpAuth: &api.HttpBasicAuth{ - Username: "test", - Password: "test", - }, - TLSConfig: &api.TLSConfig{ - CACert: "test", - CAPath: "test", - ClientCert: "test", - ClientKey: "test", - TLSServerName: "test", - Insecure: true, - }, - }, + config: &config.Agent{Nomad: &config.Nomad{ + Address: "test", + Region: "test", + Namespace: "test", + Token: "test", + HTTPAuth: "test:test", + CACert: "test", + CAPath: "test", + ClientCert: "test", + ClientKey: "test", + TLSServerName: "test", + SkipVerify: true, + }}, }, expectedOutputCfg: map[string]string{ "nomad_config_inherit": "true", @@ -124,24 +108,19 @@ func TestAgent_setupPluginConfig(t *testing.T) { inputCfg: map[string]string{}, inputAgent: &Agent{ logger: hclog.NewNullLogger(), - nomadCfg: &api.Config{ - Address: "test", - Region: "test", - SecretID: "test", - Namespace: "test", - HttpAuth: &api.HttpBasicAuth{ - Username: "test", - Password: "test", - }, - TLSConfig: &api.TLSConfig{ - CACert: "test", - CAPath: "test", - ClientCert: "test", - ClientKey: "test", - TLSServerName: "test", - Insecure: true, - }, - }, + config: &config.Agent{Nomad: &config.Nomad{ + Address: "test", + Region: "test", + Namespace: "test", + Token: "test", + HTTPAuth: "test:test", + CACert: "test", + CAPath: "test", + ClientCert: "test", + ClientKey: "test", + TLSServerName: "test", + SkipVerify: true, + }}, }, expectedOutputCfg: map[string]string{ "nomad_address": "test", diff --git a/sdk/helper/nomad/config.go b/sdk/helper/nomad/config.go index 75578cc1..c0f14a2b 100644 --- a/sdk/helper/nomad/config.go +++ b/sdk/helper/nomad/config.go @@ -106,7 +106,7 @@ func HTTPAuthFromString(auth string) *api.HttpBasicAuth { // with the map config taking precedence. This allows users to override only a // subset of params, while inheriting the agent configured items which are also // derived from Nomad API default and env vars. -func MergeMapWithAgentConfig(m map[string]string, cfg *api.Config) { +func MergeMapWithAgentConfig(m map[string]string, cfg *config.Nomad) { if cfg == nil { return } @@ -120,36 +120,32 @@ func MergeMapWithAgentConfig(m map[string]string, cfg *api.Config) { if cfg.Namespace != "" && m[configKeyNomadNamespace] == "" { m[configKeyNomadNamespace] = cfg.Namespace } - if cfg.SecretID != "" && m[configKeyNomadToken] == "" { - m[configKeyNomadToken] = cfg.SecretID + if cfg.Token != "" && m[configKeyNomadToken] == "" { + m[configKeyNomadToken] = cfg.Token } - if cfg.TLSConfig.CACert != "" && m[configKeyNomadCACert] == "" { - m[configKeyNomadCACert] = cfg.TLSConfig.CACert + if cfg.CACert != "" && m[configKeyNomadCACert] == "" { + m[configKeyNomadCACert] = cfg.CACert } - if cfg.TLSConfig.CAPath != "" && m[configKeyNomadCAPath] == "" { - m[configKeyNomadCAPath] = cfg.TLSConfig.CAPath + if cfg.CAPath != "" && m[configKeyNomadCAPath] == "" { + m[configKeyNomadCAPath] = cfg.CAPath } - if cfg.TLSConfig.ClientCert != "" && m[configKeyNomadClientCert] == "" { - m[configKeyNomadClientCert] = cfg.TLSConfig.ClientCert + if cfg.ClientCert != "" && m[configKeyNomadClientCert] == "" { + m[configKeyNomadClientCert] = cfg.ClientCert } - if cfg.TLSConfig.ClientKey != "" && m[configKeyNomadClientKey] == "" { - m[configKeyNomadClientKey] = cfg.TLSConfig.ClientKey + if cfg.ClientKey != "" && m[configKeyNomadClientKey] == "" { + m[configKeyNomadClientKey] = cfg.ClientKey } - if cfg.TLSConfig.TLSServerName != "" && m[configKeyNomadTLSServerName] == "" { - m[configKeyNomadTLSServerName] = cfg.TLSConfig.TLSServerName + if cfg.TLSServerName != "" && m[configKeyNomadTLSServerName] == "" { + m[configKeyNomadTLSServerName] = cfg.TLSServerName } - if cfg.TLSConfig.Insecure && m[configKeyNomadSkipVerify] == "" { - m[configKeyNomadSkipVerify] = strconv.FormatBool(cfg.TLSConfig.Insecure) + if cfg.SkipVerify && m[configKeyNomadSkipVerify] == "" { + m[configKeyNomadSkipVerify] = strconv.FormatBool(cfg.SkipVerify) } - if cfg.HttpAuth != nil && m[configKeyNomadHTTPAuth] == "" { - auth := cfg.HttpAuth.Username - if cfg.HttpAuth.Password != "" { - auth += ":" + cfg.HttpAuth.Password - } - m[configKeyNomadHTTPAuth] = auth + if cfg.HTTPAuth != "" && m[configKeyNomadHTTPAuth] == "" { + m[configKeyNomadHTTPAuth] = cfg.HTTPAuth } - if cfg.WaitTime != 0 && m[configKeyNomadBlockQueryWaitTime] == "" { - m[configKeyNomadBlockQueryWaitTime] = cfg.WaitTime.String() + if cfg.BlockQueryWaitTime != 0 && m[configKeyNomadBlockQueryWaitTime] == "" { + m[configKeyNomadBlockQueryWaitTime] = cfg.BlockQueryWaitTime.String() } } diff --git a/sdk/helper/nomad/config_test.go b/sdk/helper/nomad/config_test.go index c34aa936..e72bd955 100644 --- a/sdk/helper/nomad/config_test.go +++ b/sdk/helper/nomad/config_test.go @@ -88,30 +88,25 @@ func Test_HTTPAuthFromString(t *testing.T) { func Test_MergeMapWithAgentConfig(t *testing.T) { testCases := []struct { inputMap map[string]string - inputAPIConfig *api.Config + inputConfig *config.Nomad expectedOutputMap map[string]string name string }{ { inputMap: map[string]string{}, - inputAPIConfig: &api.Config{ - Address: "test", - Region: "test", - Namespace: "test", - SecretID: "test", - HttpAuth: &api.HttpBasicAuth{ - Username: "test", - Password: "test", - }, - TLSConfig: &api.TLSConfig{ - CACert: "test", - CAPath: "test", - ClientCert: "test", - ClientKey: "test", - TLSServerName: "test", - Insecure: true, - }, - WaitTime: 2 * time.Minute, + inputConfig: &config.Nomad{ + Address: "test", + Region: "test", + Namespace: "test", + Token: "test", + HTTPAuth: "test:test", + CACert: "test", + CAPath: "test", + ClientCert: "test", + ClientKey: "test", + TLSServerName: "test", + SkipVerify: true, + BlockQueryWaitTime: 2 * time.Minute, }, expectedOutputMap: map[string]string{ "nomad_address": "test", @@ -133,7 +128,7 @@ func Test_MergeMapWithAgentConfig(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - MergeMapWithAgentConfig(tc.inputMap, tc.inputAPIConfig) + MergeMapWithAgentConfig(tc.inputMap, tc.inputConfig) assert.Equal(t, tc.expectedOutputMap, tc.inputMap, tc.name) }) }