@@ -72,6 +72,9 @@ type enrollCmd struct {
72
72
remoteConfig remote.Config
73
73
agentProc * process.Info
74
74
configPath string
75
+
76
+ // For testability
77
+ daemonReloadFunc func (context.Context ) error
75
78
}
76
79
77
80
// enrollCmdFleetServerOption define all the supported enrollment options for bootstrapping with Fleet Server.
@@ -192,10 +195,11 @@ func newEnrollCmdWithStore(
192
195
store saver ,
193
196
) (* enrollCmd , error ) {
194
197
return & enrollCmd {
195
- log : log ,
196
- options : options ,
197
- configStore : store ,
198
- configPath : configPath ,
198
+ log : log ,
199
+ options : options ,
200
+ configStore : store ,
201
+ configPath : configPath ,
202
+ daemonReloadFunc : daemonReload ,
199
203
}, nil
200
204
}
201
205
@@ -462,17 +466,21 @@ func (c *enrollCmd) prepareFleetTLS() error {
462
466
return nil
463
467
}
464
468
469
+ const (
470
+ daemonReloadInitBackoff = time .Second
471
+ daemonReloadMaxBackoff = time .Minute
472
+ daemonReloadRetries = 5
473
+ )
474
+
465
475
func (c * enrollCmd ) daemonReloadWithBackoff (ctx context.Context ) error {
466
- signal := make (chan struct {})
467
- defer close (signal )
468
- backExp := backoff .NewExpBackoff (signal , 1 * time .Second , 1 * time .Minute )
476
+ backExp := backoff .NewExpBackoff (ctx .Done (), daemonReloadInitBackoff , daemonReloadMaxBackoff )
469
477
470
478
var lastErr error
471
- for i := 0 ; i < 5 ; i ++ {
479
+ for i := 0 ; i < daemonReloadRetries ; i ++ {
472
480
attempt := i
473
481
474
482
c .log .Infof ("Restarting agent daemon, attempt %d" , attempt )
475
- err := c .daemonReload (ctx )
483
+ err := c .daemonReloadFunc (ctx )
476
484
if err == nil {
477
485
return nil
478
486
}
@@ -486,14 +494,16 @@ func (c *enrollCmd) daemonReloadWithBackoff(ctx context.Context) error {
486
494
lastErr = err
487
495
488
496
c .log .Errorf ("Restart attempt %d failed: '%s'. Waiting for %s" , attempt , err , backExp .NextWait ().String ())
489
- backExp .Wait ()
490
-
497
+ // backoff Wait returns false if context.Done()
498
+ if ! backExp .Wait () {
499
+ return ctx .Err ()
500
+ }
491
501
}
492
502
493
503
return fmt .Errorf ("could not reload agent's daemon, all retries failed. Last error: %w" , lastErr )
494
504
}
495
505
496
- func ( c * enrollCmd ) daemonReload (ctx context.Context ) error {
506
+ func daemonReload (ctx context.Context ) error {
497
507
daemon := client .New ()
498
508
err := daemon .Connect (ctx )
499
509
if err != nil {
0 commit comments