Skip to content

Commit

Permalink
Merge branch 'f-ignored-states'
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemarey committed Sep 6, 2022
2 parents 1bd44c7 + 89df119 commit 440daff
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## 0.3.0 (Sep 6, 2022)

NOTES:
* This version changes the default behaviour when counting the servers in the pool.
Previously servers in state `ERROR` were ignored. If you want to have the same
behaviour as previously that can be configured using the new `ignored_states`
configuration option

FEATURES:
* Allow setting a `ignored_states` to ignore server in these states when counting

## 0.2.5 (Apr 11, 2022)

BUG FIXES:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ target "os-nova" {
* `id_attribute` `(string: "")` - The nomad attribute to use that maps the nomad client to an OS Compute instance. By default `unique.platform.aws.hostname` is used and a previous search is needed
to get the instance id using the instance name
* `action_timeout` `(string: "")` - The timeout to use when performing create and delete actions over servers. This should be specified as a duration. The default vaule is 90s
* `ignored_states` `(string: "")` - A comma-separated list of server states to be ignored. The complete list can be seen [here](https://docs.openstack.org/api-guide/compute/server_concepts.html)

### Policy Configuration

Expand Down
16 changes: 10 additions & 6 deletions plugin/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,18 +436,22 @@ func (t *TargetPlugin) countServers(ctx context.Context, pool string) (int64, in
if err := servers.ExtractServersInto(page, &serverList); err != nil {
return false, err
}
// TODO: check other status of servers: https://docs.openstack.org/api-guide/compute/server_concepts.html
// add option to allow setting the status of the servers to be counted
// and another option to specify what to do with servers in ERROR state (or to allow counting them as well)

for _, v := range serverList {
if v.Status == "ERROR" {
if _, ok := t.ignoredStates[v.Status]; ok {
t.logger.Debug("Ignored server due to state", "id", v.ID, "state", v.Status)
continue
}
if v.Status == "ACTIVE" {
switch v.Status {
case "ACTIVE":
ready += 1
case "BUILD", "REBOOT", "HARD_REBOOT":
// normal state transition, don't log but we need to wait
default:
t.logger.Warn("Detected server in unexpected status", "id", v.ID, "state", v.Status)
}
azDist[v.AZ] = azDist[v.AZ] + 1

azDist[v.AZ] = azDist[v.AZ] + 1
remoteIDs = append(remoteIDs, idFn(v))
total += 1
}
Expand Down
39 changes: 32 additions & 7 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package plugin
import (
"context"
"fmt"
"strings"
"time"

"github.com/gophercloud/gophercloud"
Expand Down Expand Up @@ -49,6 +50,7 @@ const (

configKeyValueSeparator = "value_separator"
configKeyActionTimeout = "action_timeout"
configKeyIgnoredStates = "ignored_states"

configKeyStopFirst = "stop_first"
configKeyForceDelete = "force_delete"
Expand Down Expand Up @@ -77,6 +79,7 @@ type TargetPlugin struct {
avZones []string
cache map[string]string
actionTimeout int // seconds
ignoredStates map[string]struct{}

// clusterUtils provides general cluster scaling utilities for querying the
// state of nodes pools and performing scaling tasks.
Expand All @@ -98,13 +101,9 @@ func (t *TargetPlugin) SetConfig(config map[string]string) error {
if err := t.setupOSClients(config); err != nil {
return err
}
t.actionTimeout = defaultActionTimeout
if timeout, ok := config[configKeyActionTimeout]; ok {
d, err := time.ParseDuration(timeout)
if err != nil {
return fmt.Errorf("failed to parse action_timeout: %v", err)
}
t.actionTimeout = int(d.Seconds())

if err := t.configurePlugin(config); err != nil {
return err
}

clusterUtils, err := scaleutils.NewClusterScaleUtils(nomad.ConfigFromNamespacedMap(config), t.logger)
Expand All @@ -120,6 +119,32 @@ func (t *TargetPlugin) SetConfig(config map[string]string) error {
return nil
}

func (t *TargetPlugin) configurePlugin(config map[string]string) error {
t.actionTimeout = defaultActionTimeout
if timeout, ok := config[configKeyActionTimeout]; ok {
d, err := time.ParseDuration(timeout)
if err != nil {
return fmt.Errorf("failed to parse action_timeout: %v", err)
}
t.actionTimeout = int(d.Seconds())
}

t.ignoredStates = make(map[string]struct{})
if states, ok := config[configKeyIgnoredStates]; ok && strings.TrimSpace(states) != "" {
stateList := strings.Split(strings.TrimSpace(states), ",")
for _, name := range stateList {
state := strings.ToUpper(name)
switch state {
case "ACTIVE", "BUILD", "REBOOT", "HARD_REBOOT":
return fmt.Errorf("error setting ignored_states: state '%s' can't be ignored", state)
}
t.ignoredStates[state] = struct{}{}
}
}

return nil
}

// PluginInfo satisfies the PluginInfo function on the base.Base interface.
func (t *TargetPlugin) PluginInfo() (*base.PluginInfo, error) {
return pluginInfo, nil
Expand Down

0 comments on commit 440daff

Please sign in to comment.