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

Change nomad.Config to autoscaler.Nomad.Config when providing configuration for plugins. #945

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions agent/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}
}

Expand Down
125 changes: 52 additions & 73 deletions agent/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
42 changes: 19 additions & 23 deletions sdk/helper/nomad/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
}
}

Expand Down
35 changes: 15 additions & 20 deletions sdk/helper/nomad/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
})
}
Expand Down