Skip to content

Commit 41e4f1e

Browse files
[8.17] (backport #9562) fix: scheduled upgrade details state (#9666)
* fix: scheduled upgrade details state (#9562) * fix: persisting and reporting of upgrade details * ci: align and extend dispatcher unit-tests * ci: update coordinator and application new signatures in unit-tests * ci: add integration tests for scheduled upgrade details * doc: add changelog fragment * doc: reword existing and add more comments in code * feat: change queuedUpgradeActions inside dispatchCancelActions to have values of struct{} * fix: remove redundant continue * fix: dedupe upgrade actions from fleetgateway actions, handle correctly the expiration of retried stored actions, and update upgrade details on retries (cherry picked from commit ff80471) # Conflicts: # internal/pkg/agent/application/actions/handlers/handler_action_upgrade_test.go # internal/pkg/agent/application/application.go # internal/pkg/agent/application/application_test.go # internal/pkg/agent/application/coordinator/coordinator.go # internal/pkg/agent/application/coordinator/coordinator_test.go # internal/pkg/agent/cmd/run.go * fix: resolve conflicts * fix: define missing test helper func --------- Co-authored-by: Panos Koutsovasilis <[email protected]>
1 parent b281d6a commit 41e4f1e

File tree

15 files changed

+1145
-304
lines changed

15 files changed

+1145
-304
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: bug-fix
2+
summary: fix reporting of scheduled upgrade details across restarts and cancels
3+
component: elastic-agent
4+
pr: https://github.com/elastic/elastic-agent/pull/9562
5+
issue: https://github.com/elastic/elastic-agent/issues/8778

internal/pkg/agent/application/actions/handlers/handler_action_upgrade_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func TestUpgradeHandler(t *testing.T) {
115115
return nil, nil
116116
},
117117
},
118-
nil, nil, nil, nil, nil, false, nil)
118+
nil, nil, nil, nil, nil, false, nil, nil)
119119
//nolint:errcheck // We don't need the termination state of the Coordinator
120120
go c.Run(ctx)
121121

@@ -174,7 +174,7 @@ func TestUpgradeHandlerSameVersion(t *testing.T) {
174174
return nil, err
175175
},
176176
},
177-
nil, nil, nil, nil, nil, false, nil)
177+
nil, nil, nil, nil, nil, false, nil, nil)
178178
//nolint:errcheck // We don't need the termination state of the Coordinator
179179
go c.Run(ctx)
180180

@@ -233,7 +233,7 @@ func TestDuplicateActionsHandled(t *testing.T) {
233233
return nil, nil
234234
},
235235
},
236-
nil, nil, nil, nil, nil, false, acker)
236+
nil, nil, nil, nil, nil, false, acker, nil)
237237
//nolint:errcheck // We don't need the termination state of the Coordinator
238238
go c.Run(ctx)
239239

@@ -327,7 +327,7 @@ func TestUpgradeHandlerNewVersion(t *testing.T) {
327327
return nil, nil
328328
},
329329
},
330-
nil, nil, nil, nil, nil, false, nil)
330+
nil, nil, nil, nil, nil, false, nil, nil)
331331
//nolint:errcheck // We don't need the termination state of the Coordinator
332332
go c.Run(ctx)
333333

internal/pkg/agent/application/application.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ import (
1818
"github.com/elastic/elastic-agent-libs/logp"
1919

2020
"github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator"
21+
"github.com/elastic/elastic-agent/internal/pkg/agent/application/dispatcher"
2122
"github.com/elastic/elastic-agent/internal/pkg/agent/application/info"
2223
"github.com/elastic/elastic-agent/internal/pkg/agent/application/monitoring"
2324
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
2425
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade"
26+
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/details"
2527
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
2628
"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
2729
"github.com/elastic/elastic-agent/internal/pkg/agent/storage"
@@ -34,6 +36,7 @@ import (
3436
"github.com/elastic/elastic-agent/internal/pkg/fleetapi/acker/lazy"
3537
"github.com/elastic/elastic-agent/internal/pkg/fleetapi/acker/retrier"
3638
fleetclient "github.com/elastic/elastic-agent/internal/pkg/fleetapi/client"
39+
"github.com/elastic/elastic-agent/internal/pkg/queue"
3740
"github.com/elastic/elastic-agent/internal/pkg/release"
3841
"github.com/elastic/elastic-agent/pkg/component"
3942
"github.com/elastic/elastic-agent/pkg/component/runtime"
@@ -52,6 +55,7 @@ func New(
5255
testingMode bool,
5356
fleetInitTimeout time.Duration,
5457
disableMonitoring bool,
58+
initialUpgradeDetails *details.Details,
5559
modifiers ...component.PlatformModifier,
5660
) (*coordinator.Coordinator, coordinator.ConfigManager, composable.Controller, error) {
5761

@@ -131,7 +135,6 @@ func New(
131135
var compModifiers = []coordinator.ComponentsModifier{InjectAPMConfig}
132136
var composableManaged bool
133137
var isManaged bool
134-
135138
var actionAcker acker.Acker
136139
if testingMode {
137140
log.Info("Elastic Agent has been started in testing mode and is managed through the control protocol")
@@ -195,8 +198,19 @@ func New(
195198
batchedAcker := lazy.NewAcker(fleetAcker, log, lazy.WithRetrier(retrier))
196199
actionAcker = stateStore.NewStateStoreActionAcker(batchedAcker, stateStorage)
197200

201+
actionQueue, err := queue.NewActionQueue(stateStorage.Queue(), stateStorage)
202+
if err != nil {
203+
return nil, nil, nil, fmt.Errorf("unable to initialize action queue: %w", err)
204+
}
205+
206+
if initialUpgradeDetails == nil {
207+
// initial upgrade details are nil (normally the caller supplies the ones from the marker file at this point),
208+
// hence, extract any scheduled upgrade details from the action queue.
209+
initialUpgradeDetails = dispatcher.GetScheduledUpgradeDetails(log, actionQueue.Actions(), time.Now())
210+
}
211+
198212
// TODO: stop using global state
199-
managed, err = newManagedConfigManager(ctx, log, agentInfo, cfg, store, runtime, fleetInitTimeout, paths.Top(), client, fleetAcker, actionAcker, retrier, stateStorage, upgrader)
213+
managed, err = newManagedConfigManager(ctx, log, agentInfo, cfg, store, runtime, fleetInitTimeout, paths.Top(), client, fleetAcker, actionAcker, retrier, stateStorage, actionQueue, upgrader)
200214
if err != nil {
201215
return nil, nil, nil, err
202216
}
@@ -210,7 +224,7 @@ func New(
210224
return nil, nil, nil, errors.New(err, "failed to initialize composable controller")
211225
}
212226

213-
coord := coordinator.New(log, cfg, logLevel, agentInfo, specs, reexec, upgrader, runtime, configMgr, varsManager, caps, monitor, isManaged, actionAcker, compModifiers...)
227+
coord := coordinator.New(log, cfg, logLevel, agentInfo, specs, reexec, upgrader, runtime, configMgr, varsManager, caps, monitor, isManaged, actionAcker, initialUpgradeDetails, compModifiers...)
214228
if managed != nil {
215229
// the coordinator requires the config manager as well as in managed-mode the config manager requires the
216230
// coordinator, so it must be set here once the coordinator is created

internal/pkg/agent/application/application_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func TestLimitsLog(t *testing.T) {
6363
true, // testingMode
6464
time.Millisecond, // fleetInitTimeout
6565
true, // disable monitoring
66+
nil,
6667
)
6768
require.NoError(t, err)
6869

internal/pkg/agent/application/coordinator/coordinator.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,24 @@ type UpdateComponentChange struct {
343343
}
344344

345345
// New creates a new coordinator.
346-
func New(logger *logger.Logger, cfg *configuration.Configuration, logLevel logp.Level, agentInfo info.Agent, specs component.RuntimeSpecs, reexecMgr ReExecManager, upgradeMgr UpgradeManager, runtimeMgr RuntimeManager, configMgr ConfigManager, varsMgr VarsManager, caps capabilities.Capabilities, monitorMgr MonitorManager, isManaged bool, fleetAcker acker.Acker, modifiers ...ComponentsModifier) *Coordinator {
346+
func New(
347+
logger *logger.Logger,
348+
cfg *configuration.Configuration,
349+
logLevel logp.Level,
350+
agentInfo info.Agent,
351+
specs component.RuntimeSpecs,
352+
reexecMgr ReExecManager,
353+
upgradeMgr UpgradeManager,
354+
runtimeMgr RuntimeManager,
355+
configMgr ConfigManager,
356+
varsMgr VarsManager,
357+
caps capabilities.Capabilities,
358+
monitorMgr MonitorManager,
359+
isManaged bool,
360+
fleetAcker acker.Acker,
361+
initialUpgradeDetails *details.Details,
362+
modifiers ...ComponentsModifier,
363+
) *Coordinator {
347364
var fleetState cproto.State
348365
var fleetMessage string
349366
if !isManaged {
@@ -352,11 +369,12 @@ func New(logger *logger.Logger, cfg *configuration.Configuration, logLevel logp.
352369
fleetMessage = "Not enrolled into Fleet"
353370
}
354371
state := State{
355-
State: agentclient.Starting,
356-
Message: "Starting",
357-
FleetState: fleetState,
358-
FleetMessage: fleetMessage,
359-
LogLevel: logLevel,
372+
State: agentclient.Starting,
373+
Message: "Starting",
374+
FleetState: fleetState,
375+
FleetMessage: fleetMessage,
376+
LogLevel: logLevel,
377+
UpgradeDetails: initialUpgradeDetails,
360378
}
361379
c := &Coordinator{
362380
logger: logger,

internal/pkg/agent/application/coordinator/coordinator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ func createCoordinator(t testing.TB, ctx context.Context, opts ...CoordinatorOpt
10161016
acker = &fakeActionAcker{}
10171017
}
10181018

1019-
coord := New(l, nil, logp.DebugLevel, ai, specs, &fakeReExecManager{}, upgradeManager, rm, cfgMgr, varsMgr, caps, monitoringMgr, o.managed, acker)
1019+
coord := New(l, nil, logp.DebugLevel, ai, specs, &fakeReExecManager{}, upgradeManager, rm, cfgMgr, varsMgr, caps, monitoringMgr, o.managed, acker, nil)
10201020
return coord, cfgMgr, varsMgr
10211021
}
10221022

0 commit comments

Comments
 (0)