Skip to content

Commit b3cf35d

Browse files
mergify[bot]Kaan Yalti
andauthored
[9.0] (backport #9392) Enhancement/5235 use disk space error to set upgrade detail in coordinator (#10056)
* Enhancement/5235 use disk space error to set upgrade detail in coordinator (#9392) * enhancement(5235): added insufficinet disk space error handling in the coordinator * enhancement(5235): added coordinator tests for insufficient disk space error enhancement(5235): updated error in test enhancement(5235): fix coordinator test * enhancement(5235): added changelog fragment (cherry picked from commit a7a76f6) # Conflicts: # internal/pkg/agent/application/coordinator/coordinator_unit_test.go * resolved merge conflicts --------- Co-authored-by: Kaan Yalti <kaan.yalti@elastic.co>
1 parent 456d1a8 commit b3cf35d

File tree

3 files changed

+147
-1
lines changed

3 files changed

+147
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: enhancement
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: when there is a disk space error during an upgrade, agent responds with clean insufficient disk space error message
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
#description:
20+
21+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
22+
component: "elastic-agent"
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
pr: https://github.com/elastic/elastic-agent/pull/9392
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
issue: https://github.com/elastic/elastic-agent/issues/5235

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/elastic/elastic-agent/internal/pkg/agent/application/info"
3030
"github.com/elastic/elastic-agent/internal/pkg/agent/application/reexec"
3131
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade"
32+
upgradeErrors "github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/artifact/download/errors"
3233
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/details"
3334
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
3435
"github.com/elastic/elastic-agent/internal/pkg/agent/transpiler"
@@ -669,6 +670,15 @@ func (c *Coordinator) Upgrade(ctx context.Context, version string, sourceURI str
669670
det.SetState(details.StateCompleted)
670671
return c.upgradeMgr.AckAction(ctx, c.fleetAcker, action)
671672
}
673+
674+
c.logger.Errorw("upgrade failed", "error", logp.Error(err))
675+
// If ErrInsufficientDiskSpace is in the error chain, we want to set the
676+
// the error to ErrInsufficientDiskSpace so that the error message is
677+
// more concise and clear.
678+
if errors.Is(err, upgradeErrors.ErrInsufficientDiskSpace) {
679+
err = upgradeErrors.ErrInsufficientDiskSpace
680+
}
681+
672682
det.Fail(err)
673683
return err
674684
}

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

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ package coordinator
1313
import (
1414
"context"
1515
"errors"
16+
"fmt"
1617
"net"
18+
"sync"
1719
"testing"
1820
"time"
1921

22+
"github.com/elastic/elastic-agent/internal/pkg/config"
23+
"github.com/elastic/elastic-agent/internal/pkg/fleetapi"
24+
"github.com/elastic/elastic-agent/internal/pkg/fleetapi/acker"
25+
2026
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/status"
2127
"go.opentelemetry.io/collector/component/componentstatus"
2228
"go.opentelemetry.io/collector/confmap"
@@ -28,11 +34,12 @@ import (
2834
"github.com/elastic/elastic-agent-libs/logp"
2935
"github.com/elastic/elastic-agent/internal/pkg/agent/application/info"
3036
"github.com/elastic/elastic-agent/internal/pkg/agent/application/monitoring/reload"
37+
"github.com/elastic/elastic-agent/internal/pkg/agent/application/reexec"
3138
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade"
3239
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/artifact"
40+
upgradeErrors "github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/artifact/download/errors"
3341
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/details"
3442
"github.com/elastic/elastic-agent/internal/pkg/agent/transpiler"
35-
"github.com/elastic/elastic-agent/internal/pkg/config"
3643
monitoringCfg "github.com/elastic/elastic-agent/internal/pkg/core/monitoring/config"
3744
"github.com/elastic/elastic-agent/pkg/component"
3845
"github.com/elastic/elastic-agent/pkg/component/runtime"
@@ -1437,3 +1444,100 @@ func (fs *fakeMonitoringServer) Reset() {
14371444
func (fs *fakeMonitoringServer) Addr() net.Addr {
14381445
return nil
14391446
}
1447+
1448+
type mockUpgradeManager struct {
1449+
upgradeErr error
1450+
}
1451+
1452+
func (m *mockUpgradeManager) Upgradeable() bool {
1453+
return true
1454+
}
1455+
1456+
func (m *mockUpgradeManager) Reload(cfg *config.Config) error {
1457+
return nil
1458+
}
1459+
1460+
func (m *mockUpgradeManager) Upgrade(ctx context.Context, version string, sourceURI string, action *fleetapi.ActionUpgrade, details *details.Details, skipVerifyOverride bool, skipDefaultPgp bool, pgpBytes ...string) (_ reexec.ShutdownCallbackFn, err error) {
1461+
return nil, m.upgradeErr
1462+
}
1463+
1464+
func (m *mockUpgradeManager) Ack(ctx context.Context, acker acker.Acker) error {
1465+
return nil
1466+
}
1467+
1468+
func (m *mockUpgradeManager) AckAction(ctx context.Context, acker acker.Acker, action fleetapi.Action) error {
1469+
return nil
1470+
}
1471+
1472+
func (m *mockUpgradeManager) MarkerWatcher() upgrade.MarkerWatcher {
1473+
return nil
1474+
}
1475+
1476+
func TestCoordinator_Upgrade_InsufficientDiskSpaceError(t *testing.T) {
1477+
log, _ := loggertest.New("coordinator-insufficient-disk-space-test")
1478+
1479+
mockUpgradeManager := &mockUpgradeManager{
1480+
upgradeErr: fmt.Errorf("wrapped: %w", upgradeErrors.ErrInsufficientDiskSpace),
1481+
}
1482+
1483+
initialState := State{
1484+
CoordinatorState: agentclient.Healthy,
1485+
CoordinatorMessage: "Running",
1486+
}
1487+
1488+
coord := &Coordinator{
1489+
state: initialState,
1490+
logger: log,
1491+
upgradeMgr: mockUpgradeManager,
1492+
stateBroadcaster: broadcaster.New(initialState, 64, 32),
1493+
overrideStateChan: make(chan *coordinatorOverrideState),
1494+
upgradeDetailsChan: make(chan *details.Details),
1495+
}
1496+
1497+
wg := sync.WaitGroup{}
1498+
wg.Add(2)
1499+
1500+
overrideStates := []agentclient.State{}
1501+
go func() {
1502+
state1 := <-coord.overrideStateChan
1503+
overrideStates = append(overrideStates, state1.state)
1504+
1505+
state2 := <-coord.overrideStateChan
1506+
if state2 != nil {
1507+
overrideStates = append(overrideStates, state2.state)
1508+
}
1509+
1510+
wg.Done()
1511+
}()
1512+
1513+
upgradeDetails := []*details.Details{}
1514+
go func() {
1515+
upgradeDetails = append(upgradeDetails, <-coord.upgradeDetailsChan)
1516+
upgradeDetails = append(upgradeDetails, <-coord.upgradeDetailsChan)
1517+
wg.Done()
1518+
}()
1519+
1520+
err := coord.Upgrade(t.Context(), "", "", nil)
1521+
require.Error(t, err)
1522+
require.Equal(t, err, upgradeErrors.ErrInsufficientDiskSpace)
1523+
1524+
wg.Wait()
1525+
1526+
require.Equal(t, []agentclient.State{agentclient.Upgrading}, overrideStates)
1527+
1528+
require.Equal(t, []*details.Details{
1529+
{
1530+
TargetVersion: "",
1531+
State: details.StateRequested,
1532+
ActionID: "",
1533+
},
1534+
{
1535+
TargetVersion: "",
1536+
State: details.StateFailed,
1537+
Metadata: details.Metadata{
1538+
FailedState: details.StateRequested,
1539+
ErrorMsg: upgradeErrors.ErrInsufficientDiskSpace.Error(),
1540+
},
1541+
},
1542+
}, upgradeDetails)
1543+
}

0 commit comments

Comments
 (0)