Skip to content

Commit 2e1e851

Browse files
committed
Merge branch 'fix/rollback-to-older-version' into feat/self-monitoring-otel-default
# Conflicts: # internal/pkg/agent/application/upgrade/rollback.go
2 parents f05539b + c7a5ac7 commit 2e1e851

File tree

19 files changed

+113
-56
lines changed

19 files changed

+113
-56
lines changed

NOTICE-fips.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,11 +1465,11 @@ Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-l
14651465

14661466
--------------------------------------------------------------------------------
14671467
Dependency : github.com/elastic/elastic-agent-system-metrics
1468-
Version: v0.13.2
1468+
Version: v0.13.3
14691469
Licence type (autodetected): Apache-2.0
14701470
--------------------------------------------------------------------------------
14711471

1472-
Contents of probable licence file $GOMODCACHE/github.com/elastic/[email protected].2/LICENSE.txt:
1472+
Contents of probable licence file $GOMODCACHE/github.com/elastic/[email protected].3/LICENSE.txt:
14731473

14741474
Apache License
14751475
Version 2.0, January 2004

NOTICE.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,11 +1465,11 @@ Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-l
14651465

14661466
--------------------------------------------------------------------------------
14671467
Dependency : github.com/elastic/elastic-agent-system-metrics
1468-
Version: v0.13.2
1468+
Version: v0.13.3
14691469
Licence type (autodetected): Apache-2.0
14701470
--------------------------------------------------------------------------------
14711471

1472-
Contents of probable licence file $GOMODCACHE/github.com/elastic/[email protected].2/LICENSE.txt:
1472+
Contents of probable licence file $GOMODCACHE/github.com/elastic/[email protected].3/LICENSE.txt:
14731473

14741474
Apache License
14751475
Version 2.0, January 2004

dev-tools/mage/gotest.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type TestBinaryArgs struct {
4646
}
4747

4848
func makeGoTestArgs(name string) GoTestArgs {
49-
fileName := fmt.Sprintf("build/TEST-go-%s", strings.Replace(strings.ToLower(name), " ", "_", -1))
49+
fileName := fmt.Sprintf("build/TEST-go-%s", strings.ReplaceAll(strings.ToLower(name), " ", "_"))
5050
params := GoTestArgs{
5151
LogName: name,
5252
Race: RaceDetector,
@@ -64,8 +64,8 @@ func makeGoTestArgs(name string) GoTestArgs {
6464

6565
func makeGoTestArgsForModule(name, module string) GoTestArgs {
6666
fileName := fmt.Sprintf("build/TEST-go-%s-%s",
67-
strings.Replace(strings.ToLower(name), " ", "_", -1),
68-
strings.Replace(strings.ToLower(module), " ", "_", -1),
67+
strings.ReplaceAll(strings.ToLower(name), " ", "_"),
68+
strings.ReplaceAll(strings.ToLower(module), " ", "_"),
6969
)
7070
params := GoTestArgs{
7171
LogName: fmt.Sprintf("%s-%s", name, module),

dev-tools/mage/kubernetes/kuberemote.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func NewKubeRemote(kubeconfig string, namespace string, name string, workDir str
7070
if err != nil {
7171
return nil, err
7272
}
73-
name = strings.Replace(name, "_", "-", -1)
73+
name = strings.ReplaceAll(name, "_", "-")
7474
svcAccName := fmt.Sprintf("%s-sa", name)
7575
secretName := fmt.Sprintf("%s-ssh-key", name)
7676
privateKey, publicKey, err := generateSSHKeyPair()
@@ -338,7 +338,7 @@ func getFreePort() (uint16, error) {
338338
return 0, err
339339
}
340340
defer l.Close()
341-
return uint16(l.Addr().(*net.TCPAddr).Port), nil
341+
return uint16(l.Addr().(*net.TCPAddr).Port), nil //nolint:gosec // G115 Conversion from int to uint16 is safe here.
342342
}
343343

344344
// createSecretManifest creates the secret object to create in the cluster.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/elastic/elastic-agent-autodiscover v0.10.0
2020
github.com/elastic/elastic-agent-client/v7 v7.17.2
2121
github.com/elastic/elastic-agent-libs v0.24.1
22-
github.com/elastic/elastic-agent-system-metrics v0.13.2
22+
github.com/elastic/elastic-agent-system-metrics v0.13.3
2323
github.com/elastic/elastic-transport-go/v8 v8.7.0
2424
github.com/elastic/go-elasticsearch/v8 v8.19.0
2525
github.com/elastic/go-licenser v0.4.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,8 @@ github.com/elastic/elastic-agent-client/v7 v7.17.2 h1:Cl2TeABqWZgW40t5fchGWT/sRk
520520
github.com/elastic/elastic-agent-client/v7 v7.17.2/go.mod h1:5irRFqp6HLqtu1S+OeY0jg8x7K6PLL+DW+PwVk1vJnk=
521521
github.com/elastic/elastic-agent-libs v0.24.1 h1:kFPpOG5mbdaGRRNXPb2KfEaR65Uoi8nsMbDOKA02/Go=
522522
github.com/elastic/elastic-agent-libs v0.24.1/go.mod h1:o/iXtbHYaojpG4BOY7Z3hFMQAgEoqq2owO17JPGpf3A=
523-
github.com/elastic/elastic-agent-system-metrics v0.13.2 h1:R4ogKHESuWhWTtopnw/aHnBxxSZbxd7KHV4GefdwT2M=
524-
github.com/elastic/elastic-agent-system-metrics v0.13.2/go.mod h1:ezM1kzDUT+vWXFh5oK8QXB/AEB0UoLWqWA8rkRicFFo=
523+
github.com/elastic/elastic-agent-system-metrics v0.13.3 h1:VlY3Ezuwi5bPZRsAM97WaJEW78XkXgqmPHcTl1WGMRw=
524+
github.com/elastic/elastic-agent-system-metrics v0.13.3/go.mod h1:lB8veYWYBlA9eF6TahmPN87G1IEgWlbep7QSqLSW90U=
525525
github.com/elastic/elastic-transport-go/v8 v8.7.0 h1:OgTneVuXP2uip4BA658Xi6Hfw+PeIOod2rY3GVMGoVE=
526526
github.com/elastic/elastic-transport-go/v8 v8.7.0/go.mod h1:YLHer5cj0csTzNFXoNQ8qhtGY1GTvSqPnKWKaqQE3Hk=
527527
github.com/elastic/fsnotify v1.6.1-0.20240920222514-49f82bdbc9e3 h1:UyNbxdkQiSfyipwsOCWAlO+ju3xXC61Z4prx/HBTtFk=

internal/pkg/agent/application/monitoring/url.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func getURL(
109109
u.Path = "/" + segs[1]
110110
}
111111

112-
p = `\\.\pipe\` + strings.Replace(p, "/", "\\", -1)
112+
p = `\\.\pipe\` + strings.ReplaceAll(p, "/", "\\")
113113
t = dialer.NewNpipeDialerBuilder(p)
114114
default:
115115
t = dialer.NewDefaultDialerBuilder()

internal/pkg/agent/application/upgrade/rollback.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ func Rollback(ctx context.Context, log *logger.Logger, c client.Client, topDirPa
4141
var FatalRollbackError = errors.New("fatal rollback error")
4242

4343
type RollbackSettings struct {
44-
skipCleanup bool
45-
skipRestart bool
46-
preRestartHook RollbackHook
47-
removeMarker bool
44+
SkipCleanup bool
45+
SkipRestart bool
46+
PreRestartHook RollbackHook
47+
RemoveMarker bool
4848
}
4949

50-
func newRollbackSettings(opts ...RollbackOpt) *RollbackSettings {
50+
func NewRollbackSettings(opts ...RollbackOpt) *RollbackSettings {
5151
rs := new(RollbackSettings)
5252
for _, opt := range opts {
5353
opt(rs)
@@ -58,24 +58,24 @@ func newRollbackSettings(opts ...RollbackOpt) *RollbackSettings {
5858
type RollbackOpt func(*RollbackSettings)
5959

6060
func (r *RollbackSettings) SetSkipCleanup(skipCleanup bool) {
61-
r.skipCleanup = skipCleanup
61+
r.SkipCleanup = skipCleanup
6262
}
6363

6464
func (r *RollbackSettings) SetSkipRestart(skipRestart bool) {
65-
r.skipRestart = skipRestart
65+
r.SkipRestart = skipRestart
6666
}
6767

6868
func (r *RollbackSettings) SetPreRestartHook(preRestartHook RollbackHook) {
69-
r.preRestartHook = preRestartHook
69+
r.PreRestartHook = preRestartHook
7070
}
7171

7272
func (r *RollbackSettings) SetRemoveMarker(removeMarker bool) {
73-
r.removeMarker = removeMarker
73+
r.RemoveMarker = removeMarker
7474
}
7575

7676
func RollbackWithOpts(ctx context.Context, log *logger.Logger, c client.Client, topDirPath string, prevVersionedHome string, prevHash string, opts ...RollbackOpt) error {
7777

78-
settings := newRollbackSettings(opts...)
78+
settings := NewRollbackSettings(opts...)
7979

8080
symlinkPath := filepath.Join(topDirPath, agentName)
8181

@@ -99,8 +99,8 @@ func RollbackWithOpts(ctx context.Context, log *logger.Logger, c client.Client,
9999
}
100100

101101
// Hook
102-
if settings.preRestartHook != nil {
103-
hookErr := settings.preRestartHook(ctx, log, topDirPath)
102+
if settings.PreRestartHook != nil {
103+
hookErr := settings.PreRestartHook(ctx, log, topDirPath)
104104
if hookErr != nil {
105105
if errors.Is(hookErr, FatalRollbackError) {
106106
return fmt.Errorf("pre-restart hook failed: %w", hookErr)
@@ -110,7 +110,7 @@ func RollbackWithOpts(ctx context.Context, log *logger.Logger, c client.Client,
110110
}
111111
}
112112

113-
if settings.skipRestart {
113+
if settings.SkipRestart {
114114
log.Info("Skipping restart")
115115
return nil
116116
}
@@ -121,13 +121,13 @@ func RollbackWithOpts(ctx context.Context, log *logger.Logger, c client.Client,
121121
return err
122122
}
123123

124-
if settings.skipCleanup {
124+
if settings.SkipCleanup {
125125
log.Info("Skipping cleanup")
126126
return nil
127127
}
128128

129129
// cleanup everything except version we're rolling back into
130-
return Cleanup(log, topDirPath, prevVersionedHome, prevHash, settings.removeMarker, true)
130+
return Cleanup(log, topDirPath, prevVersionedHome, prevHash, settings.RemoveMarker, true)
131131
}
132132

133133
// Cleanup removes all artifacts and files related to a specified version.

internal/pkg/agent/cmd/watch_test.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,48 @@ func Test_watchCmd(t *testing.T) {
153153
wantErr: assert.NoError,
154154
},
155155
{
156-
name: "unhappy path: error watching, rollback to previous install",
156+
name: "unhappy path: error watching, rollback to previous install, leaving the upgrade marker",
157+
setupUpgradeMarker: func(t *testing.T, topDir string, watcher *mockAgentWatcher, installModifier *mockInstallationModifier) {
158+
dataDirPath := paths.DataFrom(topDir)
159+
err := os.MkdirAll(dataDirPath, 0755)
160+
require.NoError(t, err)
161+
err = upgrade.SaveMarker(
162+
dataDirPath,
163+
&upgrade.UpdateMarker{
164+
Version: "9.3.0",
165+
Hash: "newver",
166+
VersionedHome: "elastic-agent-9.3.0-newver",
167+
UpdatedOn: time.Now(),
168+
PrevVersion: "9.2.0",
169+
PrevHash: "prvver",
170+
PrevVersionedHome: "elastic-agent-9.2.0-prvver",
171+
Acked: false,
172+
Action: nil,
173+
Details: nil,
174+
},
175+
true,
176+
)
177+
require.NoError(t, err)
178+
179+
watcher.EXPECT().
180+
Watch(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
181+
Return(errors.New("some watch error due to agent misbehaving"))
182+
installModifier.EXPECT().
183+
Rollback(mock.Anything, mock.Anything, mock.Anything, paths.Top(), "elastic-agent-9.2.0-prvver", "prvver", mock.MatchedBy(func(opt upgrade.RollbackOption) bool {
184+
settings := upgrade.NewRollbackSettings()
185+
opt(settings)
186+
187+
return !settings.RemoveMarker
188+
})).
189+
Return(nil)
190+
},
191+
args: args{
192+
cfg: configuration.DefaultUpgradeConfig().Watcher,
193+
},
194+
wantErr: assert.NoError,
195+
},
196+
{
197+
name: "unhappy path: error watching, rollback to previous install, removing upgrade marker if version is < 9.2.0-SNAPSHOT",
157198
setupUpgradeMarker: func(t *testing.T, topDir string, watcher *mockAgentWatcher, installModifier *mockInstallationModifier) {
158199
dataDirPath := paths.DataFrom(topDir)
159200
err := os.MkdirAll(dataDirPath, 0755)
@@ -167,7 +208,7 @@ func Test_watchCmd(t *testing.T) {
167208
UpdatedOn: time.Now(),
168209
PrevVersion: "1.2.3",
169210
PrevHash: "prvver",
170-
PrevVersionedHome: "elastic-agent-prvver",
211+
PrevVersionedHome: "elastic-agent-1.2.3-prvver",
171212
Acked: false,
172213
Action: nil,
173214
Details: nil, //details.NewDetails("4.5.6", details.StateReplacing, ""),
@@ -180,8 +221,19 @@ func Test_watchCmd(t *testing.T) {
180221
Watch(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
181222
Return(errors.New("some watch error due to agent misbehaving"))
182223
installModifier.EXPECT().
183-
Rollback(mock.Anything, mock.Anything, mock.Anything, paths.Top(), "elastic-agent-prvver", "prvver", mock.Anything).
184-
Return(nil)
224+
Rollback(
225+
mock.Anything,
226+
mock.Anything,
227+
mock.Anything,
228+
paths.Top(),
229+
"elastic-agent-1.2.3-prvver",
230+
"prvver",
231+
mock.MatchedBy(func(opt upgrade.RollbackOption) bool {
232+
settings := upgrade.NewRollbackSettings()
233+
opt(settings)
234+
235+
return settings.RemoveMarker
236+
})).Return(nil)
185237
},
186238
args: args{
187239
cfg: configuration.DefaultUpgradeConfig().Watcher,

internal/pkg/agent/install/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func Install(cfgFile, topPath string, unprivileged bool, log *logp.Logger, pt *p
135135
// We use strings.Replace instead of fmt.Sprintf here because, with the
136136
// latter, govet throws a false positive error here: "fmt.Sprintf call has
137137
// arguments but no formatting directives".
138-
shellWrapper := strings.Replace(paths.ShellWrapperFmt, "%s", topPath, -1)
138+
shellWrapper := strings.ReplaceAll(paths.ShellWrapperFmt, "%s", topPath)
139139
err = os.WriteFile(paths.ShellWrapperPath(), []byte(shellWrapper), 0755)
140140
if err != nil {
141141
return utils.FileOwner{}, errors.New(

0 commit comments

Comments
 (0)