@@ -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
3339var (
@@ -63,7 +69,6 @@ type TargetPlugin struct {
6369func 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.
226231func (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+ }
0 commit comments