Skip to content
This repository was archived by the owner on Jun 27, 2025. It is now read-only.

Commit 8011161

Browse files
authored
Merge pull request #253 from jrasell/gh_251
Fix a panic when running scale* deployment watcher.
2 parents 66ea059 + 9f5a9f7 commit 8011161

File tree

6 files changed

+113
-90
lines changed

6 files changed

+113
-90
lines changed

command/scale_in.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ General Options:
2626
-address=<http_address>
2727
The Nomad HTTP API address including port which Levant will use to make
2828
calls.
29+
30+
-allow-stale
31+
Allow stale consistency mode for requests into nomad.
2932
3033
-log-level=<level>
3134
Specify the verbosity level of Levant's logs. Valid values include DEBUG,
@@ -64,18 +67,23 @@ func (c *ScaleInCommand) Run(args []string) int {
6467
var err error
6568
var logL, logF string
6669

67-
config := &structs.ScalingConfig{}
68-
config.Direction = structs.ScalingDirectionIn
70+
config := &scale.Config{
71+
Client: &structs.ClientConfig{},
72+
Scale: &structs.ScaleConfig{
73+
Direction: structs.ScalingDirectionIn,
74+
},
75+
}
6976

7077
flags := c.Meta.FlagSet("scale-in", FlagSetVars)
7178
flags.Usage = func() { c.UI.Output(c.Help()) }
7279

73-
flags.StringVar(&config.Addr, "address", "", "")
80+
flags.StringVar(&config.Client.Addr, "address", "", "")
81+
flags.BoolVar(&config.Client.AllowStale, "allow-stale", false, "")
7482
flags.StringVar(&logL, "log-level", "INFO", "")
7583
flags.StringVar(&logF, "log-format", "HUMAN", "")
76-
flags.IntVar(&config.Count, "count", 0, "")
77-
flags.IntVar(&config.Percent, "percent", 0, "")
78-
flags.StringVar(&config.TaskGroup, "task-group", "", "")
84+
flags.IntVar(&config.Scale.Count, "count", 0, "")
85+
flags.IntVar(&config.Scale.Percent, "percent", 0, "")
86+
flags.StringVar(&config.Scale.TaskGroup, "task-group", "", "")
7987

8088
if err = flags.Parse(args); err != nil {
8189
return 1
@@ -88,20 +96,19 @@ func (c *ScaleInCommand) Run(args []string) int {
8896
return 1
8997
}
9098

91-
config.JobID = args[0]
99+
config.Scale.JobID = args[0]
92100

93-
if config.Count == 0 && config.Percent == 0 || config.Count > 0 && config.Percent > 0 ||
94-
config.Count < 0 || config.Percent < 0 {
95-
c.UI.Error("You must set either -count or -percent flag to scale-in")
101+
if config.Scale.Count == 0 && config.Scale.Percent == 0 || config.Scale.Count > 0 && config.Scale.Percent > 0 {
102+
c.UI.Error("You must set either -count or -percent flag to scale-out")
96103
return 1
97104
}
98105

99-
if config.Count > 0 {
100-
config.DirectionType = structs.ScalingDirectionTypeCount
106+
if config.Scale.Count > 0 {
107+
config.Scale.DirectionType = structs.ScalingDirectionTypeCount
101108
}
102109

103-
if config.Percent > 0 {
104-
config.DirectionType = structs.ScalingDirectionTypePercent
110+
if config.Scale.Percent > 0 {
111+
config.Scale.DirectionType = structs.ScalingDirectionTypePercent
105112
}
106113

107114
if err = logging.SetupLogger(logL, logF); err != nil {

command/scale_out.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ General Options:
2626
-address=<http_address>
2727
The Nomad HTTP API address including port which Levant will use to make
2828
calls.
29+
30+
-allow-stale
31+
Allow stale consistency mode for requests into nomad.
2932
3033
-log-level=<level>
3134
Specify the verbosity level of Levant's logs. Valid values include DEBUG,
@@ -48,7 +51,7 @@ Scale Out Options:
4851
4952
-task-group=<name>
5053
The name of the task group you wish to target for scaling. Is this is not
51-
speicified all task groups within the job will be scaled.
54+
specified all task groups within the job will be scaled.
5255
`
5356
return strings.TrimSpace(helpText)
5457
}
@@ -64,18 +67,23 @@ func (c *ScaleOutCommand) Run(args []string) int {
6467
var err error
6568
var logL, logF string
6669

67-
config := &structs.ScalingConfig{}
68-
config.Direction = structs.ScalingDirectionOut
70+
config := &scale.Config{
71+
Client: &structs.ClientConfig{},
72+
Scale: &structs.ScaleConfig{
73+
Direction: structs.ScalingDirectionOut,
74+
},
75+
}
6976

7077
flags := c.Meta.FlagSet("scale-out", FlagSetVars)
7178
flags.Usage = func() { c.UI.Output(c.Help()) }
7279

73-
flags.StringVar(&config.Addr, "address", "", "")
80+
flags.StringVar(&config.Client.Addr, "address", "", "")
81+
flags.BoolVar(&config.Client.AllowStale, "allow-stale", false, "")
7482
flags.StringVar(&logL, "log-level", "INFO", "")
7583
flags.StringVar(&logF, "log-format", "HUMAN", "")
76-
flags.IntVar(&config.Count, "count", 0, "")
77-
flags.IntVar(&config.Percent, "percent", 0, "")
78-
flags.StringVar(&config.TaskGroup, "task-group", "", "")
84+
flags.IntVar(&config.Scale.Count, "count", 0, "")
85+
flags.IntVar(&config.Scale.Percent, "percent", 0, "")
86+
flags.StringVar(&config.Scale.TaskGroup, "task-group", "", "")
7987

8088
if err = flags.Parse(args); err != nil {
8189
return 1
@@ -88,19 +96,19 @@ func (c *ScaleOutCommand) Run(args []string) int {
8896
return 1
8997
}
9098

91-
config.JobID = args[0]
99+
config.Scale.JobID = args[0]
92100

93-
if config.Count == 0 && config.Percent == 0 || config.Count > 0 && config.Percent > 0 {
101+
if config.Scale.Count == 0 && config.Scale.Percent == 0 || config.Scale.Count > 0 && config.Scale.Percent > 0 {
94102
c.UI.Error("You must set either -count or -percent flag to scale-out")
95103
return 1
96104
}
97105

98-
if config.Count > 0 {
99-
config.DirectionType = structs.ScalingDirectionTypeCount
106+
if config.Scale.Count > 0 {
107+
config.Scale.DirectionType = structs.ScalingDirectionTypeCount
100108
}
101109

102-
if config.Percent > 0 {
103-
config.DirectionType = structs.ScalingDirectionTypePercent
110+
if config.Scale.Percent > 0 {
111+
config.Scale.DirectionType = structs.ScalingDirectionTypePercent
104112
}
105113

106114
if err = logging.SetupLogger(logL, logF); err != nil {

levant/structs/config.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ const (
66
// JobIDContextField is the logging context feild added when interacting
77
// with jobs.
88
JobIDContextField = "job_id"
9+
10+
// ScalingDirectionOut represents a scaling out event; adding to the total number.
11+
ScalingDirectionOut = "Out"
12+
13+
// ScalingDirectionIn represents a scaling in event; removing from the total number.
14+
ScalingDirectionIn = "In"
15+
16+
// ScalingDirectionTypeCount means the scale event will use a change by count.
17+
ScalingDirectionTypeCount = "Count"
18+
19+
// ScalingDirectionTypePercent means the scale event will use a percentage of current change.
20+
ScalingDirectionTypePercent = "Percent"
921
)
1022

1123
// DeployConfig is the main struct used to configure and run a Levant deployment on
@@ -61,3 +73,28 @@ type TemplateConfig struct {
6173
// templateFile before deployment.
6274
VariableFiles []string
6375
}
76+
77+
// ScaleConfig contains all the scaling specific configuration options.
78+
type ScaleConfig struct {
79+
// Count is the count by which the operator has asked to scale the Nomad job
80+
// and optional taskgroup by.
81+
Count int
82+
83+
// Direction is the direction in which the scaling will take place and is
84+
// populated by consts.
85+
Direction string
86+
87+
// DirectionType is an identifier on whether the operator has specified to
88+
// scale using a count increase or percentage.
89+
DirectionType string
90+
91+
// JobID is the Nomad job which will be interacted with for scaling.
92+
JobID string
93+
94+
// Percent is the percentage by which the operator has asked to scale the
95+
// Nomad job and optional taskgroup by.
96+
Percent int
97+
98+
// TaskGroup is the Nomad job taskgroup which has been selected for scaling.
99+
TaskGroup string
100+
}

levant/structs/scaling.go

Lines changed: 0 additions & 40 deletions
This file was deleted.

scale/scale.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@ import (
1010
"github.com/rs/zerolog/log"
1111
)
1212

13+
// Config is the set of config structs required to run a Levant scale.
14+
type Config struct {
15+
Client *structs.ClientConfig
16+
Scale *structs.ScaleConfig
17+
}
18+
1319
// TriggerScalingEvent provides the exported entry point into performing a job
1420
// scale based on user inputs.
15-
func TriggerScalingEvent(config *structs.ScalingConfig) bool {
21+
func TriggerScalingEvent(config *Config) bool {
1622

1723
// Add the JobID as a log context field.
18-
log.Logger = log.With().Str(structs.JobIDContextField, config.JobID).Logger()
24+
log.Logger = log.With().Str(structs.JobIDContextField, config.Scale.JobID).Logger()
1925

20-
nomadClient, err := client.NewNomadClient(config.Addr)
26+
nomadClient, err := client.NewNomadClient(config.Client.Addr)
2127
if err != nil {
2228
log.Error().Msg("levant/scale: unable to setup Levant scaling event")
2329
return false
@@ -33,6 +39,7 @@ func TriggerScalingEvent(config *structs.ScalingConfig) bool {
3339
// go through the same process and code upgrades.
3440
deploymentConfig := &levant.DeployConfig{}
3541
deploymentConfig.Template = &structs.TemplateConfig{Job: job}
42+
deploymentConfig.Client = config.Client
3643
deploymentConfig.Deploy = &structs.DeployConfig{ForceCount: true}
3744

3845
log.Info().Msg("levant/scale: job will now be deployed with updated counts")
@@ -51,9 +58,9 @@ func TriggerScalingEvent(config *structs.ScalingConfig) bool {
5158
// updateJob gathers information on the current state of the running job and
5259
// along with the user defined input updates the in-memory job specification
5360
// to reflect the desired scaled state.
54-
func updateJob(client *nomad.Client, config *structs.ScalingConfig) *nomad.Job {
61+
func updateJob(client *nomad.Client, config *Config) *nomad.Job {
5562

56-
job, _, err := client.Jobs().Info(config.JobID, nil)
63+
job, _, err := client.Jobs().Info(config.Scale.JobID, nil)
5764
if err != nil {
5865
log.Error().Err(err).Msg("levant/scale: unable to obtain job information from Nomad")
5966
return nil
@@ -70,10 +77,10 @@ func updateJob(client *nomad.Client, config *structs.ScalingConfig) *nomad.Job {
7077

7178
// If the user has specified a taskgroup to scale, ensure we only change
7279
// the specific of this.
73-
if config.TaskGroup != "" {
74-
if *group.Name == config.TaskGroup {
80+
if config.Scale.TaskGroup != "" {
81+
if *group.Name == config.Scale.TaskGroup {
7582
log.Debug().Msgf("levant/scale: scaling action to be requested on taskgroup %s only",
76-
config.TaskGroup)
83+
config.Scale.TaskGroup)
7784
updateTaskGroup(config, group)
7885
}
7986

@@ -90,24 +97,24 @@ func updateJob(client *nomad.Client, config *structs.ScalingConfig) *nomad.Job {
9097

9198
// updateTaskGroup is tasked with performing the count update based on the user
9299
// configuration when a group is identified as being marked for scaling.
93-
func updateTaskGroup(config *structs.ScalingConfig, group *nomad.TaskGroup) {
100+
func updateTaskGroup(config *Config, group *nomad.TaskGroup) {
94101

95102
var c int
96103

97104
// If a percentage scale value has been passed, we must convert this to an
98105
// int which represents the count to scale by as Nomad job submissions must
99106
// be done with group counts as desired ints.
100-
switch config.DirectionType {
107+
switch config.Scale.DirectionType {
101108
case structs.ScalingDirectionTypeCount:
102-
c = config.Count
109+
c = config.Scale.Count
103110
case structs.ScalingDirectionTypePercent:
104-
c = calculateCountBasedOnPercent(*group.Count, config.Percent)
111+
c = calculateCountBasedOnPercent(*group.Count, config.Scale.Percent)
105112
}
106113

107114
// Depending on whether we are scaling-out or scaling-in we need to perform
108115
// the correct maths. There is a little duplication here, but that is to
109116
// provide better logging.
110-
switch config.Direction {
117+
switch config.Scale.Direction {
111118
case structs.ScalingDirectionOut:
112119
nc := *group.Count + c
113120
log.Info().Msgf("levant/scale: task group %s will scale-out from %v to %v",

scale/scale_test.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestScale_updateTaskGroup(t *testing.T) {
1515
sPercent := structs.ScalingDirectionTypePercent
1616

1717
cases := []struct {
18-
Config *structs.ScalingConfig
18+
Config *Config
1919
Group *nomad.TaskGroup
2020
EndCount int
2121
}{
@@ -83,17 +83,20 @@ func TestScale_calculateCountBasedOnPercent(t *testing.T) {
8383
}
8484
}
8585

86-
func buildScalingConfig(direction, dType string, number int) *structs.ScalingConfig {
86+
func buildScalingConfig(direction, dType string, number int) *Config {
8787

88-
c := &structs.ScalingConfig{}
89-
c.Direction = direction
90-
c.DirectionType = dType
88+
c := &Config{
89+
Scale: &structs.ScaleConfig{
90+
Direction: direction,
91+
DirectionType: dType,
92+
},
93+
}
9194

9295
switch dType {
9396
case structs.ScalingDirectionTypeCount:
94-
c.Count = number
97+
c.Scale.Count = number
9598
case structs.ScalingDirectionTypePercent:
96-
c.Percent = number
99+
c.Scale.Percent = number
97100
}
98101

99102
return c
@@ -104,9 +107,10 @@ func buildTaskGroup(count int) *nomad.TaskGroup {
104107
n := "LevantTest"
105108
c := count
106109

107-
t := &nomad.TaskGroup{}
108-
t.Name = &n
109-
t.Count = &c
110+
t := &nomad.TaskGroup{
111+
Name: &n,
112+
Count: &c,
113+
}
110114

111115
return t
112116
}

0 commit comments

Comments
 (0)