Skip to content

Commit 61df511

Browse files
committed
refactor: design more akin to aws-asg, utilizing getConfigValue
1 parent 160a215 commit 61df511

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

plugins/builtin/target/gce-mig/plugin/gce.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919

2020
const (
2121
defaultRetryInterval = 10 * time.Second
22-
defaultRetryAttempts = 15
2322

2423
// nodeAttrGCEHostname is the node attribute to use when identifying the
2524
// GCE hostname of a node.

plugins/builtin/target/gce-mig/plugin/plugin.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@ const (
2222
// pluginName is the unique name of the this plugin amongst Target plugins.
2323
pluginName = "gce-mig"
2424

25+
// configKeys represents the known configuration parameters required at
26+
// varying points throughout the plugins lifecycle.
2527
configKeyCredentials = "credentials"
2628
configKeyProject = "project"
2729
configKeyRegion = "region"
2830
configKeyZone = "zone"
2931
configKeyMIGName = "mig_name"
3032
configKeyRetryAttempts = "retry_attempts"
33+
34+
// configValues are the default values used when a configuration key is not
35+
// supplied by the operator that are specific to the plugin.
36+
configValueRetryAttemptsDefault = "15"
3137
)
3238

3339
var (
@@ -63,7 +69,6 @@ type TargetPlugin struct {
6369
func NewGCEMIGPlugin(log hclog.Logger) *TargetPlugin {
6470
return &TargetPlugin{
6571
logger: log,
66-
retryAttempts: defaultRetryAttempts,
6772
}
6873
}
6974

@@ -76,14 +81,6 @@ func (t *TargetPlugin) SetConfig(config map[string]string) error {
7681
return err
7782
}
7883

79-
if val, ok := t.config[configKeyRetryAttempts]; ok {
80-
attempts, err := strconv.Atoi(val)
81-
if err != nil {
82-
return fmt.Errorf("invalid value for %s: %v", configKeyRetryAttempts, err)
83-
}
84-
t.retryAttempts = attempts
85-
}
86-
8784
clusterUtils, err := scaleutils.NewClusterScaleUtils(nomad.ConfigFromNamespacedMap(config), t.logger)
8885
if err != nil {
8986
return err
@@ -93,6 +90,12 @@ func (t *TargetPlugin) SetConfig(config map[string]string) error {
9390
t.clusterUtils = clusterUtils
9491
t.clusterUtils.ClusterNodeIDLookupFunc = gceNodeIDMap
9592

93+
retryLimit, err := strconv.Atoi(getConfigValue(config, configKeyRetryAttempts, configValueRetryAttemptsDefault))
94+
if err != nil {
95+
return err
96+
}
97+
t.retryAttempts = retryLimit
98+
9699
return nil
97100
}
98101

@@ -223,6 +226,8 @@ func (t *TargetPlugin) calculateMIG(config map[string]string) (instanceGroup, er
223226
}
224227
}
225228

229+
// getValue retrieves a configuration value from either the provided config
230+
// map or the plugin's stored config map.
226231
func (t *TargetPlugin) getValue(config map[string]string, name string) (string, bool) {
227232
v, ok := config[name]
228233
if ok {
@@ -236,3 +241,14 @@ func (t *TargetPlugin) getValue(config map[string]string, name string) (string,
236241

237242
return "", false
238243
}
244+
245+
// getConfigValue handles parameters that are optional in the operator's config
246+
// but required for the plugin's functionality.
247+
func getConfigValue(config map[string]string, key string, defaultValue string) string {
248+
value, ok := config[key]
249+
if !ok {
250+
return defaultValue
251+
}
252+
253+
return value
254+
}

plugins/builtin/target/gce-mig/plugin/plugin_test.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,36 @@ func TestTargetPlugin_calculateDirection(t *testing.T) {
5454
}
5555

5656
func TestTargetPlugin_SetConfig_RetryAttempts(t *testing.T) {
57-
p := NewGCEMIGPlugin(hclog.NewNullLogger())
57+
// Test case for the default value.
58+
t.Run("default value is used when not provided", func(t *testing.T) {
59+
p := NewGCEMIGPlugin(hclog.NewNullLogger())
60+
config := map[string]string{}
61+
err := p.SetConfig(config)
62+
assert.NoError(t, err)
63+
64+
defaultValue, _ := strconv.Atoi(configValueRetryAttemptsDefault)
65+
assert.Equal(t, defaultValue, p.retryAttempts)
66+
})
5867

59-
assert.Equal(t, defaultRetryAttempts, p.retryAttempts, "expected default retry attempts")
68+
// Test case for a valid custom retry attempts value.
69+
t.Run("valid custom value is used", func(t *testing.T) {
70+
p := NewGCEMIGPlugin(hclog.NewNullLogger())
71+
customAttempts := 20
72+
config := map[string]string{
73+
configKeyRetryAttempts: strconv.Itoa(customAttempts),
74+
}
75+
err := p.SetConfig(config)
76+
assert.NoError(t, err, "expected no error for valid config")
77+
assert.Equal(t, customAttempts, p.retryAttempts, "expected custom retry attempts")
78+
})
6079

61-
customAttempts := 20
62-
config := map[string]string{
63-
configKeyRetryAttempts: strconv.Itoa(customAttempts),
64-
}
65-
err := p.SetConfig(config)
66-
assert.NoError(t, err, "expected no error for valid config")
67-
assert.Equal(t, customAttempts, p.retryAttempts, "expected custom retry attempts")
68-
69-
invalidConfig := map[string]string{
70-
configKeyRetryAttempts: "not-a-number",
71-
}
72-
err = p.SetConfig(invalidConfig)
73-
assert.Error(t, err, "expected an error for invalid retry attempts")
80+
// Test case for an invalid retry attempts value (non-integer).
81+
t.Run("invalid value returns an error", func(t *testing.T) {
82+
p := NewGCEMIGPlugin(hclog.NewNullLogger())
83+
invalidConfig := map[string]string{
84+
configKeyRetryAttempts: "not-a-number",
85+
}
86+
err := p.SetConfig(invalidConfig)
87+
assert.Error(t, err, "expected an error for invalid retry attempts")
88+
})
7489
}

0 commit comments

Comments
 (0)