Skip to content

Commit f600919

Browse files
committed
Allow machineid.AutoUpdateVersionReporter to shut down correctly (#60219)
1 parent 1a8335f commit f600919

File tree

4 files changed

+39
-34
lines changed

4 files changed

+39
-34
lines changed

lib/auth/auth.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ func NewServer(cfg *InitConfig, opts ...ServerOption) (*Server, error) {
726726

727727
as.RegisterLoginHook(as.ulsGenerator.LoginHook(services.UserLoginStates))
728728

729-
as.botVersionReporter, err = machineidv1.NewAutoUpdateVersionReporter(machineidv1.AutoUpdateVersionReporterConfig{
729+
as.BotInstanceVersionReporter, err = machineidv1.NewAutoUpdateVersionReporter(machineidv1.AutoUpdateVersionReporterConfig{
730730
Clock: cfg.Clock,
731731
Logger: as.logger.With(
732732
teleport.ComponentKey,
@@ -740,9 +740,7 @@ func NewServer(cfg *InitConfig, opts ...ServerOption) (*Server, error) {
740740
if err != nil {
741741
return nil, trace.Wrap(err)
742742
}
743-
if err := as.botVersionReporter.Run(as.CloseContext()); err != nil {
744-
return nil, trace.Wrap(err)
745-
}
743+
746744
if _, ok := as.getCache(); !ok {
747745
log.Warn("Auth server starting without cache (may have negative performance implications).")
748746
}
@@ -1228,9 +1226,9 @@ type Server struct {
12281226
// logger is the logger used by the auth server.
12291227
logger *slog.Logger
12301228

1231-
// botVersionReporter is called periodically to generate a report of the
1232-
// number of bot instances by version and update group.
1233-
botVersionReporter *machineidv1.AutoUpdateVersionReporter
1229+
// BotInstanceVersionReporter is called periodically to generate a report of
1230+
// the number of bot instances by version and update group.
1231+
BotInstanceVersionReporter *machineidv1.AutoUpdateVersionReporter
12341232
}
12351233

12361234
// SetSAMLService registers svc as the SAMLService that provides the SAML
@@ -1711,7 +1709,7 @@ func (a *Server) runPeriodicOperations() {
17111709
case autoUpdateAgentReportKey:
17121710
go a.reportAgentVersions(a.closeCtx)
17131711
case autoUpdateBotInstanceReportKey:
1714-
go a.botVersionReporter.Report(a.closeCtx)
1712+
go a.BotInstanceVersionReporter.Report(a.closeCtx)
17151713
case autoUpdateBotInstanceMetricsKey:
17161714
go a.updateBotInstanceMetrics()
17171715
}

lib/auth/machineid/machineidv1/auto_update_version_reporter.go

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -130,36 +130,33 @@ func (r *AutoUpdateVersionReporter) Run(ctx context.Context) error {
130130
return trace.Wrap(err)
131131
}
132132

133-
go func() {
134-
defer r.logger.DebugContext(ctx, "Shutting down")
133+
defer r.logger.DebugContext(ctx, "Shutting down")
135134

136-
for {
137-
started := r.clock.Now()
138-
r.runLeader(ctx)
139-
leaderFor := r.clock.Now().Sub(started)
135+
for {
136+
started := r.clock.Now()
137+
r.runLeader(ctx)
138+
leaderFor := r.clock.Since(started)
140139

141-
// Context is done, exit immediately.
142-
if ctx.Err() != nil {
143-
return
144-
}
140+
// Context is done, exit immediately.
141+
if ctx.Err() != nil {
142+
return nil
143+
}
145144

146-
// If we were leader for a decent amount of time, any previous
147-
// backoff likely doesn't apply anymore.
148-
if leaderFor > 5*time.Minute {
149-
retry.Reset()
150-
}
145+
// If we were leader for a decent amount of time, any previous
146+
// backoff likely doesn't apply anymore.
147+
if leaderFor > 5*time.Minute {
148+
retry.Reset()
149+
}
151150

152-
// Wait for the next retry interval.
153-
retry.Inc()
151+
// Wait for the next retry interval.
152+
retry.Inc()
154153

155-
select {
156-
case <-retry.After():
157-
case <-ctx.Done():
158-
return
159-
}
154+
select {
155+
case <-retry.After():
156+
case <-ctx.Done():
157+
return nil
160158
}
161-
}()
162-
return nil
159+
}
163160
}
164161

165162
func (r *AutoUpdateVersionReporter) runLeader(ctx context.Context) error {

lib/auth/machineid/machineidv1/auto_update_version_reporter_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ import (
4848
func TestAutoUpdateVersionReporter(t *testing.T) {
4949
t.Parallel()
5050

51-
ctx := t.Context()
51+
ctx, cancel := context.WithCancel(t.Context())
52+
t.Cleanup(cancel)
53+
5254
clock := clockwork.NewFakeClockAt(time.Now().UTC())
5355

5456
backend, err := memory.New(memory.Config{
@@ -105,7 +107,9 @@ func TestAutoUpdateVersionReporter(t *testing.T) {
105107
require.NoError(t, err)
106108

107109
// Run the leader election process. Wait for the semaphore to be acquired.
108-
require.NoError(t, reporter.Run(ctx))
110+
errCh := make(chan error, 1)
111+
go func() { errCh <- reporter.Run(ctx) }()
112+
109113
select {
110114
case <-reporter.LeaderCh():
111115
case <-time.After(1 * time.Second):
@@ -149,6 +153,9 @@ func TestAutoUpdateVersionReporter(t *testing.T) {
149153
if diff != "" {
150154
t.Fatal(diff)
151155
}
156+
157+
cancel()
158+
require.NoError(t, <-errCh)
152159
}
153160

154161
func TestEmitInstancesMetric(t *testing.T) {

lib/service/service.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,6 +2576,9 @@ func (process *TeleportProcess) initAuthService() error {
25762576
process.RegisterFunc("auth.autoupdate_agent_rollout_controller", func() error {
25772577
return trace.Wrap(agentRolloutController.Run(process.GracefulExitContext()), "running autoupdate_agent_rollout controller")
25782578
})
2579+
process.RegisterFunc("auth.autoupdate_bot_instance_version_reporter", func() error {
2580+
return trace.Wrap(authServer.BotInstanceVersionReporter.Run(process.GracefulExitContext()))
2581+
})
25792582

25802583
process.RegisterFunc("auth.server_info", func() error {
25812584
return trace.Wrap(auth.ReconcileServerInfos(process.GracefulExitContext(), authServer))

0 commit comments

Comments
 (0)