Skip to content

Commit 6980022

Browse files
authored
fix process upgrade details when null (#3264)
* fix process upgrade details when null * fix test * added changelog * updated schema to let null be converted to nil automatically * refactor to reduce complexity
1 parent 2eebe82 commit 6980022

File tree

5 files changed

+63
-19
lines changed

5 files changed

+63
-19
lines changed
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: bug-fix
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Fixed a bug where agents were stuck in non-upgradeable state after upgrade.
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:
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: 3264
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: 3263

internal/pkg/api/handleCheckin.go

+24-16
Original file line numberDiff line numberDiff line change
@@ -390,25 +390,11 @@ func (ct *CheckinT) ProcessRequest(zlog zerolog.Logger, w http.ResponseWriter, r
390390
// otherwise the details are validated; action_id is checked and upgrade_details.metadata is validated based on upgrade_details.state and the agent doc is updated.
391391
func (ct *CheckinT) processUpgradeDetails(ctx context.Context, agent *model.Agent, details *UpgradeDetails) error {
392392
if details == nil {
393-
// nop if there are no checkin details, and the agent has no details
394-
if len(agent.UpgradeDetails) == 0 {
395-
return nil
396-
}
397-
span, ctx := apm.StartSpan(ctx, "Mark update complete", "update")
398-
span.Context.SetLabel("agent_id", agent.Agent.ID)
399-
defer span.End()
400-
// if the checkin had no details, but agent has details treat like a successful upgrade
401-
doc := bulk.UpdateFields{
402-
dl.FieldUpgradeDetails: nil,
403-
dl.FieldUpgradeStartedAt: nil,
404-
dl.FieldUpgradeStatus: nil,
405-
dl.FieldUpgradedAt: time.Now().UTC().Format(time.RFC3339),
406-
}
407-
body, err := doc.Marshal()
393+
err := ct.markUpgradeComplete(ctx, agent)
408394
if err != nil {
409395
return err
410396
}
411-
return ct.bulker.Update(ctx, dl.FleetAgents, agent.Id, body, bulk.WithRefresh(), bulk.WithRetryOnConflict(3))
397+
return nil
412398
}
413399
// update docs with in progress details
414400

@@ -506,6 +492,28 @@ func (ct *CheckinT) processUpgradeDetails(ctx context.Context, agent *model.Agen
506492
return ct.bulker.Update(ctx, dl.FleetAgents, agent.Id, body, bulk.WithRefresh(), bulk.WithRetryOnConflict(3))
507493
}
508494

495+
func (ct *CheckinT) markUpgradeComplete(ctx context.Context, agent *model.Agent) error {
496+
// nop if there are no checkin details, and the agent has no details
497+
if agent.UpgradeDetails == nil {
498+
return nil
499+
}
500+
span, ctx := apm.StartSpan(ctx, "Mark update complete", "update")
501+
span.Context.SetLabel("agent_id", agent.Agent.ID)
502+
defer span.End()
503+
// if the checkin had no details, but agent has details treat like a successful upgrade
504+
doc := bulk.UpdateFields{
505+
dl.FieldUpgradeDetails: nil,
506+
dl.FieldUpgradeStartedAt: nil,
507+
dl.FieldUpgradeStatus: nil,
508+
dl.FieldUpgradedAt: time.Now().UTC().Format(time.RFC3339),
509+
}
510+
body, err := doc.Marshal()
511+
if err != nil {
512+
return err
513+
}
514+
return ct.bulker.Update(ctx, dl.FleetAgents, agent.Id, body, bulk.WithRefresh(), bulk.WithRetryOnConflict(3))
515+
}
516+
509517
func (ct *CheckinT) writeResponse(zlog zerolog.Logger, w http.ResponseWriter, r *http.Request, agent *model.Agent, resp CheckinResponse) error {
510518
ctx := r.Context()
511519
var links []apm.SpanLink

internal/pkg/api/handleCheckin_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func TestProcessUpgradeDetails(t *testing.T) {
304304
err: nil,
305305
}, {
306306
name: "agent has details checkin details are nil",
307-
agent: &model.Agent{ESDocument: esd, Agent: &model.AgentMetadata{ID: "test-agent"}, UpgradeDetails: json.RawMessage(`{"action_id":"test"}`)},
307+
agent: &model.Agent{ESDocument: esd, Agent: &model.AgentMetadata{ID: "test-agent"}, UpgradeDetails: &model.UpgradeDetails{}},
308308
details: nil,
309309
bulk: func() *ftesting.MockBulk {
310310
mBulk := ftesting.NewMockBulk()

internal/pkg/model/schema.go

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

model/schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@
628628
},
629629
"upgrade_details": {
630630
"description": "Additional upgrade status details.",
631-
"format": "raw"
631+
"type": "object"
632632
}
633633
},
634634
"required": [

0 commit comments

Comments
 (0)