Skip to content

Commit fb724ec

Browse files
Change allocator vacate default polling settings (#151)
Changes the allocator vacate command default polling settings and also adds `--max-poll-retries` and `--poll-frequency` flags which allow the user to control the track settings providing the newly changed defaults. Signed-off-by: Marc Lopez <marc5.12@outlook.com> Co-authored-by: Papapetrou Patroklos <1743100+ppapapetrou76@users.noreply.github.com>
1 parent 58e8c7d commit fb724ec

File tree

6 files changed

+133
-11
lines changed

6 files changed

+133
-11
lines changed

cmd/platform/allocator/vacate.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ var vacateAllocatorCmd = &cobra.Command{
155155
fmt.Fprint(ecctl.Get().Config.OutputDevice, merr)
156156
}
157157

158+
maxRetries, pollFrequency := cmdutil.GetTrackSettings(cmd)
158159
var params = &allocator.VacateParams{
159160
API: ecctl.Get().API,
160161
Allocators: args,
@@ -170,6 +171,8 @@ var vacateAllocatorCmd = &cobra.Command{
170171
SkipDataMigration: skipDataMigration,
171172
OverrideFailsafe: ec.Bool(overrideFailsafe),
172173
},
174+
MaxPollRetries: uint8(maxRetries),
175+
TrackFrequency: pollFrequency,
173176
}
174177
if len(args) == 1 && allocatorDownRaw != "" {
175178
params.AllocatorDown = &allocatorDown
@@ -200,4 +203,5 @@ func init() {
200203
vacateAllocatorCmd.Flags().Bool("override-failsafe", false, "If false (the default) then the plan will fail out if it believes the requested sequence of operations can result in data loss - this flag will override some of these restraints. [true|false]")
201204
vacateAllocatorCmd.Flags().String("skip-snapshot", "", "Skips the snapshot operation on the specified cluster IDs. ONLY available when the cluster IDs are specified. [true|false]")
202205
vacateAllocatorCmd.Flags().String("skip-data-migration", "", "Skips the data-migration operation on the specified cluster IDs. ONLY available when the cluster IDs are specified and --move-only is true. [true|false]")
206+
cmdutil.AddTrackFlags(vacateAllocatorCmd)
203207
}

cmd/util/flag.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package cmdutil
2020
import (
2121
"fmt"
2222
"strings"
23+
"time"
2324

2425
"github.com/spf13/cobra"
2526

@@ -35,6 +36,11 @@ const (
3536
`
3637
)
3738

39+
const (
40+
maxPollRetriesFlag = "max-poll-retries"
41+
pollFrequencyFlag = "poll-frequency"
42+
)
43+
3844
var (
3945
// StatelessTypes declares the stateless deployment resource types
4046
StatelessTypes = []string{"apm", "appsearch", "kibana"}
@@ -91,3 +97,18 @@ func AddTypeFlag(cmd *cobra.Command, prefix string, all bool) *string {
9197

9298
return s
9399
}
100+
101+
// AddTrackFlags adds flags which control the tracking frequency to the passed
102+
// command reference.
103+
func AddTrackFlags(cmd *cobra.Command) {
104+
cmd.Flags().Int(maxPollRetriesFlag, util.DefaultRetries, "Optional maximum plan tracking retries")
105+
cmd.Flags().Duration(pollFrequencyFlag, util.DefaultPollFrequency, "Optional polling frequency to check for plan change updates")
106+
}
107+
108+
// GetTrackSettings obtains the currently set tracking settings, the first
109+
// return value being the MaxPollRetries and the second one the poll frequency.
110+
func GetTrackSettings(cmd *cobra.Command) (int, time.Duration) {
111+
maxPollRetries, _ := cmd.Flags().GetInt(maxPollRetriesFlag)
112+
pollFrequency, _ := cmd.Flags().GetDuration(pollFrequencyFlag)
113+
return maxPollRetries, pollFrequency
114+
}

cmd/util/flag_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121
"errors"
2222
"net/http"
2323
"reflect"
24+
"strconv"
2425
"testing"
26+
"time"
2527

2628
"github.com/elastic/cloud-sdk-go/pkg/api"
2729
"github.com/elastic/cloud-sdk-go/pkg/api/mock"
@@ -216,3 +218,101 @@ func TestAddTypeFlag(t *testing.T) {
216218
})
217219
}
218220
}
221+
222+
func TestAddTrackFlags(t *testing.T) {
223+
var wantRetriesPflag = &flag.Flag{
224+
Name: maxPollRetriesFlag,
225+
DefValue: strconv.Itoa(util.DefaultRetries),
226+
Usage: "Optional maximum plan tracking retries",
227+
}
228+
var wantPollFrequencyPflag = &flag.Flag{
229+
Name: pollFrequencyFlag,
230+
DefValue: util.DefaultPollFrequency.String(),
231+
Usage: "Optional polling frequency to check for plan change updates",
232+
}
233+
type args struct {
234+
cmd *cobra.Command
235+
}
236+
tests := []struct {
237+
name string
238+
args args
239+
wantRetries *flag.Flag
240+
wantFrequency *flag.Flag
241+
}{
242+
{
243+
name: "Annotates the type flag with all types",
244+
args: args{
245+
cmd: &cobra.Command{
246+
Use: "somethingrequired",
247+
Run: func(cmd *cobra.Command, args []string) {},
248+
},
249+
},
250+
wantRetries: wantRetriesPflag,
251+
wantFrequency: wantPollFrequencyPflag,
252+
},
253+
}
254+
for _, tt := range tests {
255+
t.Run(tt.name, func(t *testing.T) {
256+
AddTrackFlags(tt.args.cmd)
257+
})
258+
gotRetries := tt.args.cmd.Flag(maxPollRetriesFlag)
259+
gotRetries.Value = nil
260+
if !reflect.DeepEqual(gotRetries, tt.wantRetries) {
261+
t.Errorf("AddTypeFlag() gotRetries = \n%+v, wantRetries \n%+v", gotRetries, tt.wantRetries)
262+
}
263+
gotFrequency := tt.args.cmd.Flag(pollFrequencyFlag)
264+
gotFrequency.Value = nil
265+
if !reflect.DeepEqual(gotFrequency, tt.wantFrequency) {
266+
t.Errorf("AddTypeFlag() gotFrequency = \n%+v, wantFrequency \n%+v", gotFrequency, tt.wantFrequency)
267+
}
268+
}
269+
}
270+
271+
func TestGetTrackSettings(t *testing.T) {
272+
c := &cobra.Command{
273+
Use: "somethingrequired",
274+
Run: func(cmd *cobra.Command, args []string) {},
275+
}
276+
AddTrackFlags(c)
277+
278+
c2 := &cobra.Command{
279+
Use: "somethingrequired",
280+
Run: func(cmd *cobra.Command, args []string) {},
281+
}
282+
AddTrackFlags(c2)
283+
_ = c2.Flag(maxPollRetriesFlag).Value.Set("5")
284+
_ = c2.Flag(pollFrequencyFlag).Value.Set("50s")
285+
type args struct {
286+
cmd *cobra.Command
287+
}
288+
tests := []struct {
289+
name string
290+
args args
291+
want int
292+
want1 time.Duration
293+
}{
294+
{
295+
name: "Gets default values",
296+
args: args{cmd: c},
297+
want: util.DefaultRetries,
298+
want1: util.DefaultPollFrequency,
299+
},
300+
{
301+
name: "Gets the changed values",
302+
args: args{cmd: c2},
303+
want: 5,
304+
want1: time.Second * 50,
305+
},
306+
}
307+
for _, tt := range tests {
308+
t.Run(tt.name, func(t *testing.T) {
309+
got, got1 := GetTrackSettings(tt.args.cmd)
310+
if got != tt.want {
311+
t.Errorf("GetTrackSettings() got = %v, want %v", got, tt.want)
312+
}
313+
if got1 != tt.want1 {
314+
t.Errorf("GetTrackSettings() got1 = %v, want %v", got1, tt.want1)
315+
}
316+
})
317+
}
318+
}

docs/ecctl_platform_allocator_vacate.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ ecctl platform allocator vacate <source> [flags]
5757
-h, --help help for vacate
5858
-k, --kind string Kind of workload to vacate (elasticsearch|kibana)
5959
-m, --maintenance Whether to set the allocator(s) in maintenance before performing the vacate
60+
--max-poll-retries int Optional maximum plan tracking retries (default 2)
6061
--move-only Keeps the cluster in its current -possibly broken- state and just does the bare minimum to move the requested instances across to another allocator. [true|false] (default true)
6162
--override-failsafe If false (the default) then the plan will fail out if it believes the requested sequence of operations can result in data loss - this flag will override some of these restraints. [true|false]
63+
--poll-frequency duration Optional polling frequency to check for plan change updates (default 10s)
6264
--skip-data-migration string Skips the data-migration operation on the specified cluster IDs. ONLY available when the cluster IDs are specified and --move-only is true. [true|false]
6365
--skip-snapshot string Skips the snapshot operation on the specified cluster IDs. ONLY available when the cluster IDs are specified. [true|false]
6466
--skip-tracking Skips tracking the vacate progress causing the command to return after the move operation has been executed. Not recommended.

pkg/platform/allocator/vacate.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package allocator
2020
import (
2121
"fmt"
2222
"strings"
23-
"time"
2423

2524
"github.com/elastic/cloud-sdk-go/pkg/api"
2625
"github.com/elastic/cloud-sdk-go/pkg/client/platform_infrastructure"
@@ -31,15 +30,11 @@ import (
3130
"github.com/elastic/cloud-sdk-go/pkg/util/slice"
3231
multierror "github.com/hashicorp/go-multierror"
3332
"github.com/pkg/errors"
33+
34+
"github.com/elastic/ecctl/pkg/util"
3435
)
3536

3637
const (
37-
// DefaultTrackRetries is used when TrackRetries is empty
38-
DefaultTrackRetries uint8 = 4
39-
40-
// DefaultTrackFrequency is used when TrackFrequency is 0
41-
DefaultTrackFrequency = time.Second
42-
4338
// PlanPendingMessage is used to discard
4439
PlanPendingMessage = "There is a plan still pending, cancel that or wait for it to complete before restarting"
4540
)
@@ -301,11 +296,11 @@ func fillVacateClusterParams(params *VacateClusterParams) (*VacateClusterParams,
301296
}
302297

303298
if params.MaxPollRetries == 0 {
304-
params.MaxPollRetries = DefaultTrackRetries
299+
params.MaxPollRetries = util.DefaultRetries
305300
}
306301

307302
if params.TrackFrequency.Nanoseconds() == 0 {
308-
params.TrackFrequency = DefaultTrackFrequency
303+
params.TrackFrequency = util.DefaultPollFrequency
309304
}
310305

311306
return params, nil

pkg/platform/allocator/vacate_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -909,8 +909,8 @@ func Test_fillVacateClusterParams(t *testing.T) {
909909
ClusterID: "3ee11eb40eda22cac0cce259625c6734",
910910
Kind: "elasticsearch",
911911
Output: output.NewDevice(new(bytes.Buffer)),
912-
MaxPollRetries: DefaultTrackRetries,
913-
TrackFrequency: DefaultTrackFrequency,
912+
MaxPollRetries: util.DefaultRetries,
913+
TrackFrequency: util.DefaultPollFrequency,
914914
AllocatorDown: ec.Bool(false),
915915
},
916916
},

0 commit comments

Comments
 (0)