Skip to content

Commit 0bb8bf2

Browse files
committed
Refactor names and add bulk handling of available rollbacks
1 parent 93ff97d commit 0bb8bf2

File tree

8 files changed

+63
-78
lines changed

8 files changed

+63
-78
lines changed

internal/pkg/api/handleCheckin.go

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import (
2222
"sync"
2323
"time"
2424

25-
"github.com/dustin/go-humanize"
26-
2725
"github.com/elastic/fleet-server/v7/internal/pkg/action"
2826
"github.com/elastic/fleet-server/v7/internal/pkg/bulk"
2927
"github.com/elastic/fleet-server/v7/internal/pkg/cache"
@@ -169,13 +167,13 @@ func invalidateAPIKeysOfInactiveAgent(ctx context.Context, zlog zerolog.Logger,
169167

170168
// validatedCheckin is a struct to wrap all the things that validateRequest returns.
171169
type validatedCheckin struct {
172-
req *CheckinRequest
173-
dur time.Duration
174-
rawMeta []byte
175-
rawComp []byte
176-
seqno sqn.SeqNo
177-
unhealthyReason *[]string
178-
rawRollbacks []byte
170+
req *CheckinRequest
171+
dur time.Duration
172+
rawMeta []byte
173+
rawComp []byte
174+
seqno sqn.SeqNo
175+
unhealthyReason *[]string
176+
rawAvailableRollbacks []byte
179177
}
180178

181179
func (ct *CheckinT) validateRequest(zlog zerolog.Logger, w http.ResponseWriter, r *http.Request, start time.Time, agent *model.Agent) (validatedCheckin, error) {
@@ -254,19 +252,19 @@ func (ct *CheckinT) validateRequest(zlog zerolog.Logger, w http.ResponseWriter,
254252
return val, err
255253
}
256254

257-
rawRollbacks, err := parseRollbacks(zlog, agent, &req)
255+
rawRollbacks, err := parseAvailableRollbacks(zlog, agent, &req)
258256
if err != nil {
259257
return val, err
260258
}
261259

262260
return validatedCheckin{
263-
req: &req,
264-
dur: pollDuration,
265-
rawMeta: rawMeta,
266-
rawComp: rawComponents,
267-
seqno: seqno,
268-
unhealthyReason: unhealthyReason,
269-
rawRollbacks: rawRollbacks,
261+
req: &req,
262+
dur: pollDuration,
263+
rawMeta: rawMeta,
264+
rawComp: rawComponents,
265+
seqno: seqno,
266+
unhealthyReason: unhealthyReason,
267+
rawAvailableRollbacks: rawRollbacks,
270268
}, nil
271269
}
272270

@@ -299,7 +297,7 @@ func (ct *CheckinT) ProcessRequest(zlog zerolog.Logger, w http.ResponseWriter, r
299297
checkin.WithVer(ver),
300298
checkin.WithUnhealthyReason(unhealthyReason),
301299
checkin.WithDeleteAudit(agent.AuditUnenrolledReason != "" || agent.UnenrolledAt != ""),
302-
checkin.WithRollbacks(req.Rollbacks),
300+
checkin.WithAvailableRollbacks(validated.rawAvailableRollbacks),
303301
}
304302

305303
revID, opts, err := ct.processPolicyDetails(r.Context(), zlog, agent, req)
@@ -1142,34 +1140,33 @@ func parseComponents(zlog zerolog.Logger, agent *model.Agent, req *CheckinReques
11421140
return outComponents, &unhealthyReason, nil
11431141
}
11441142

1145-
func parseRollbacks(zlog zerolog.Logger, agent *model.Agent, req *CheckinRequest) ([]byte, error) {
1146-
if req.Rollbacks == nil && agent.Rollbacks == nil {
1147-
return nil, nil
1148-
}
1149-
1150-
var reqRollbacks []model.Rollback
1151-
if req.Rollbacks != nil {
1152-
reqRollbacks = make([]model.Rollback, len(*req.Rollbacks))
1153-
for i, rr := range *req.Rollbacks {
1154-
reqRollbacks[i] = model.Rollback{
1143+
func parseAvailableRollbacks(zlog zerolog.Logger, agent *model.Agent, req *CheckinRequest) ([]byte, error) {
1144+
var reqRollbacks []model.AvailableRollback
1145+
if req.AvailableRollbacks != nil {
1146+
reqRollbacks = make([]model.AvailableRollback, len(*req.AvailableRollbacks))
1147+
for i, rr := range *req.AvailableRollbacks {
1148+
reqRollbacks[i] = model.AvailableRollback{
11551149
ValidUntil: rr.ValidUntil.UTC().Format(time.RFC3339),
11561150
Version: rr.Version,
11571151
}
11581152
}
1153+
} else {
1154+
// still set an empty slice in order to clear obsolete information, if any
1155+
reqRollbacks = []model.AvailableRollback{}
11591156
}
11601157

11611158
var outRollbacks []byte
11621159
// Compare the deserialized meta structures and return the bytes to update if different
1163-
if !reflect.DeepEqual(reqRollbacks, agent.Rollbacks) {
1160+
if !reflect.DeepEqual(reqRollbacks, agent.AvailableRollbacks) {
11641161
zlog.Trace().
1165-
Any("oldRollbacks", agent.Rollbacks).
1166-
Any("req.Rollbacks", req.Rollbacks).
1162+
Any("oldAvailableRollbacks", agent.AvailableRollbacks).
1163+
Any("req.AvailableRollbacks", req.AvailableRollbacks).
11671164
Msg("available rollback data is not equal")
11681165

11691166
zlog.Info().Msg("applying new rollback data")
11701167
marshalled, err := json.Marshal(reqRollbacks)
11711168
if err != nil {
1172-
return nil, fmt.Errorf("marshalling rollbacks: %w", err)
1169+
return nil, fmt.Errorf("marshalling available rollbacks: %w", err)
11731170
}
11741171
outRollbacks = marshalled
11751172
}

internal/pkg/api/openapi.gen.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/pkg/checkin/bulk.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package checkin
77

88
import (
9-
"bytes"
109
"context"
1110
_ "embed"
1211
"encoding/json"
@@ -15,7 +14,6 @@ import (
1514
"sync"
1615
"time"
1716

18-
"github.com/elastic/fleet-server/v7/internal/pkg/api"
1917
"github.com/elastic/fleet-server/v7/internal/pkg/bulk"
2018
"github.com/elastic/fleet-server/v7/internal/pkg/dl"
2119
"github.com/elastic/fleet-server/v7/internal/pkg/sqn"
@@ -127,29 +125,22 @@ func WithPolicyRevisionIDX(idx int64) Option {
127125
}
128126
}
129127

130-
func WithRollbacks(rollbacks *api.AvailableRollbacks) Option {
128+
func WithAvailableRollbacks(availableRollbacks []byte) Option {
131129
return func(pending *pendingT) {
132130
if pending.extra == nil {
133131
pending.extra = &extraT{}
134132
}
135-
if rollbacks == nil {
136-
pending.extra.rollbacks = nil
137-
return
138-
}
139-
// FIXME this should be done in the validated checkin
140-
buf := bytes.NewBuffer(nil)
141-
json.
142-
pending.extra.rollbacks = rollbacks
133+
pending.extra.availableRollbacks = availableRollbacks
143134
}
144135
}
145136

146137
type extraT struct {
147-
meta []byte
148-
seqNo sqn.SeqNo
149-
ver string
150-
components []byte
151-
deleteAudit bool
152-
rollbacks []byte
138+
meta []byte
139+
seqNo sqn.SeqNo
140+
ver string
141+
components []byte
142+
deleteAudit bool
143+
availableRollbacks []byte
153144
}
154145

155146
// Minimize the size of this structure.
@@ -377,6 +368,10 @@ func toUpdateBody(now string, pending pendingT) ([]byte, error) {
377368
if pending.extra.seqNo.IsSet() {
378369
fields[dl.FieldActionSeqNo] = pending.extra.seqNo
379370
}
371+
372+
if pending.extra.availableRollbacks != nil {
373+
fields[dl.FieldAvailableRollbacks] = json.RawMessage(pending.extra.availableRollbacks)
374+
}
380375
}
381376
return fields.Marshal()
382377
}

internal/pkg/dl/constants.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ const (
4545
FieldUnenrolledReason = "unenrolled_reason"
4646
FiledType = "type"
4747
FieldUnhealthyReason = "unhealthy_reason"
48-
49-
FieldActive = "active"
50-
FieldNamespaces = "namespaces"
51-
FieldTags = "tags"
52-
FieldUpdatedAt = "updated_at"
53-
FieldUnenrolledAt = "unenrolled_at"
54-
FieldUpgradedAt = "upgraded_at"
55-
FieldUpgradeStartedAt = "upgrade_started_at"
56-
FieldUpgradeStatus = "upgrade_status"
57-
FieldUpgradeDetails = "upgrade_details"
58-
FieldUpgradeAttempts = "upgrade_attempts"
48+
FieldAvailableRollbacks = "available_rollbacks"
49+
FieldActive = "active"
50+
FieldNamespaces = "namespaces"
51+
FieldTags = "tags"
52+
FieldUpdatedAt = "updated_at"
53+
FieldUnenrolledAt = "unenrolled_at"
54+
FieldUpgradedAt = "upgraded_at"
55+
FieldUpgradeStartedAt = "upgrade_started_at"
56+
FieldUpgradeStatus = "upgrade_status"
57+
FieldUpgradeDetails = "upgrade_details"
58+
FieldUpgradeAttempts = "upgrade_attempts"
5959

6060
FieldAuditUnenrolledTime = "audit_unenrolled_time"
6161
FieldAuditUnenrolledReason = "audit_unenrolled_reason"

internal/pkg/model/schema.go

Lines changed: 3 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

model/openapi.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ components:
448448
The revision of the policy that the agent is currently running.
449449
type: integer
450450
format: int64
451-
rollbacks:
451+
available_rollbacks:
452452
$ref: "#/components/schemas/available_rollbacks"
453453
actionSignature:
454454
description: Optional action signing data.

model/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@
732732
"description": "hash of token provided during enrollment that allows replacement by another enrollment with same ID",
733733
"type": "string"
734734
},
735-
"rollbacks": {
735+
"available_rollbacks": {
736736
"description": "list of available rollbacks for the agent",
737737
"type": "array",
738738
"items": {

pkg/api/types.gen.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)