Skip to content

Commit

Permalink
Change nomad Config to autoscaler config to use on plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemarey committed Aug 7, 2024
1 parent 1fc382b commit ea04ece
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 45 deletions.
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
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

0 comments on commit ea04ece

Please sign in to comment.