Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add agent hash to check for detecting upgrade rollbacks #3842

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions internal/pkg/agent/application/upgrade/marker_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ func (mfw *MarkerFileWatcher) Run(ctx context.Context) error {
case e.Op&(fsnotify.Create|fsnotify.Write) != 0:
// Upgrade marker file was created or updated; read its contents
// and send them over the update channel.
mfw.processMarker(version.GetAgentPackageVersion())
mfw.processMarker(version.GetAgentPackageVersion(), version.Commit())
}
case <-doInitialRead:
mfw.processMarker(version.GetAgentPackageVersion())
mfw.processMarker(version.GetAgentPackageVersion(), version.Commit())
}
}
}()

return nil
}

func (mfw *MarkerFileWatcher) processMarker(currentVersion string) {
func (mfw *MarkerFileWatcher) processMarker(currentVersion string, commit string) {
marker, err := loadMarker(mfw.markerFilePath)
if err != nil {
mfw.logger.Error(err)
Expand All @@ -120,7 +120,7 @@ func (mfw *MarkerFileWatcher) processMarker(currentVersion string) {
// been recorded in the marker's upgrade details field but, in case it
// isn't for some reason, we fallback to explicitly setting that state as
// part of the upgrade details in the marker.
if marker.PrevVersion == currentVersion {
if marker.PrevVersion == currentVersion && marker.PrevHash == commit {
if marker.Details == nil {
marker.Details = details.NewDetails("unknown", details.StateRollback, marker.GetActionID())
} else {
Expand Down
62 changes: 61 additions & 1 deletion internal/pkg/agent/application/upgrade/marker_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func TestProcessMarker(t *testing.T) {
cases := map[string]struct {
markerFileContents string

currentAgentVersion string
currentAgentHash string

expectedErrLogMsg bool
expectedDetails *details.Details
}{
Expand Down Expand Up @@ -146,6 +149,51 @@ details:
State: details.StateWatching,
},
},
"same_version_different_hash": {
markerFileContents: `
prev_version: 8.9.2
prev_hash: aaaaaa
details:
target_version: 8.9.2
state: UPG_WATCHING
`,
currentAgentVersion: "8.9.2",
currentAgentHash: "bbbbbb",
expectedErrLogMsg: false,
expectedDetails: &details.Details{
TargetVersion: "8.9.2",
State: details.StateWatching,
},
},
"same_version_same_hash": {
markerFileContents: `
prev_version: 8.9.2
prev_hash: aaaaaa
details:
target_version: 8.9.2
state: UPG_WATCHING
`,
currentAgentVersion: "8.9.2",
currentAgentHash: "aaaaaa",
expectedErrLogMsg: false,
expectedDetails: &details.Details{
TargetVersion: "8.9.2",
State: details.StateRollback,
},
},
"same_version_same_hash_no_details": {
markerFileContents: `
prev_version: 8.9.2
prev_hash: aaaaaa
`,
currentAgentVersion: "8.9.2",
currentAgentHash: "aaaaaa",
expectedErrLogMsg: false,
expectedDetails: &details.Details{
TargetVersion: "unknown",
State: details.StateRollback,
},
},
}

for name, test := range cases {
Expand Down Expand Up @@ -182,7 +230,19 @@ details:
}
}()

mfw.processMarker("8.9.2")
// default values for version and hash
currentVersion := "8.9.2"
currentCommit := ""

// apply overrides from testcase
if test.currentAgentVersion != "" {
currentVersion = test.currentAgentVersion
}
if test.currentAgentHash != "" {
currentCommit = test.currentAgentHash
}

mfw.processMarker(currentVersion, currentCommit)

// error loading marker
if test.expectedErrLogMsg {
Expand Down