-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathevaluation.go
More file actions
306 lines (265 loc) · 9.26 KB
/
evaluation.go
File metadata and controls
306 lines (265 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright (c) 2026 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package types
import (
"fmt"
"strings"
"time"
"github.com/lf-edge/eve/pkg/pillar/base"
)
// EvalPhase represents the current phase of evaluation
type EvalPhase string
const (
// EvalPhaseInit - Initial phase, evaluation manager starting up
EvalPhaseInit EvalPhase = "init"
// EvalPhaseTesting - Currently testing slots one by one
EvalPhaseTesting EvalPhase = "testing"
// EvalPhaseFinal - Evaluation complete, best slot selected and running
EvalPhaseFinal EvalPhase = "final"
)
// SlotName represents a partition slot name
type SlotName string
const (
// SlotIMGA - Image A partition
SlotIMGA SlotName = "IMGA"
// SlotIMGB - Image B partition
SlotIMGB SlotName = "IMGB"
// SlotIMGC - Image C partition
SlotIMGC SlotName = "IMGC"
// SlotFinal - Special value indicating no more slots to test (evaluation complete)
SlotFinal SlotName = "FINAL"
)
// AllSlots returns all valid slot names
func AllSlots() []SlotName {
return []SlotName{SlotIMGA, SlotIMGB, SlotIMGC}
}
// SlotEvalState tracks the evaluation state of a single slot
type SlotEvalState struct {
// Tried indicates if this slot has been attempted to boot
Tried bool `json:"tried"`
// Success indicates if the slot booted successfully
Success bool `json:"success"`
// Note contains additional information about this slot's evaluation
Note string `json:"note,omitempty"`
// AttemptTime when this slot was last attempted
AttemptTime time.Time `json:"attempt_time,omitempty"`
}
// EvalPersist represents the persistent state stored in /persist/eval/state.json
type EvalPersist struct {
// Slots maps slot names to their evaluation state
Slots map[SlotName]SlotEvalState `json:"slots"`
// BestSlot is the slot determined to be the best after evaluation
BestSlot SlotName `json:"best_slot,omitempty"`
// Phase tracks the current evaluation phase
Phase EvalPhase `json:"phase"`
// LastUpdated timestamp of last state update
LastUpdated time.Time `json:"last_updated"`
}
// EvalStatus is published by evalmgr to communicate evaluation state
type EvalStatus struct {
// IsEvaluationPlatform indicates if this is an evaluation device
IsEvaluationPlatform bool
// CurrentSlot is the currently booted slot
CurrentSlot SlotName
// Phase is the current evaluation phase
Phase EvalPhase
// AllowOnboard gates whether onboarding should proceed
AllowOnboard bool
// Note contains human-readable status information
Note string
// LastUpdated timestamp when this status was last updated
LastUpdated time.Time
// TestStartTime when current test phase started
TestStartTime time.Time
// TestDuration total duration for current test phase
TestDuration time.Duration
// RebootCountdown seconds until reboot (0 if no reboot pending)
RebootCountdown int
// InventoryCollected indicates if hardware inventory was collected for current slot
InventoryCollected bool
// InventoryDir is the directory path where inventory was stored (empty if not collected)
InventoryDir string
}
// Key returns the key for pubsub (single instance)
func (status EvalStatus) Key() string {
return "evalmgr"
}
// LogCreate logs the creation of EvalStatus
func (status EvalStatus) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.EvalStatusLogType, "",
nilUUID, status.LogKey())
if logObject == nil {
return
}
logObject.CloneAndAddField("is-evaluation", status.IsEvaluationPlatform).
AddField("current-slot", string(status.CurrentSlot)).
AddField("phase", string(status.Phase)).
AddField("allow-onboard", status.AllowOnboard).
AddField("note", status.Note).
AddField("test-duration", status.TestDuration).
AddField("reboot-countdown", status.RebootCountdown).
Noticef("EvalStatus create")
}
// LogModify logs modifications to EvalStatus
func (status EvalStatus) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.EnsureLogObject(logBase, base.EvalStatusLogType, "",
nilUUID, status.LogKey())
oldStatus, ok := old.(EvalStatus)
if !ok {
logObject.Clone().Fatalf("LogModify: Old object interface passed is not of EvalStatus type")
}
// Log changes to key fields
if oldStatus.Phase != status.Phase ||
oldStatus.AllowOnboard != status.AllowOnboard ||
oldStatus.CurrentSlot != status.CurrentSlot {
logObject.CloneAndAddField("phase", string(status.Phase)).
AddField("old-phase", string(oldStatus.Phase)).
AddField("allow-onboard", status.AllowOnboard).
AddField("old-allow-onboard", oldStatus.AllowOnboard).
AddField("current-slot", string(status.CurrentSlot)).
AddField("old-current-slot", string(oldStatus.CurrentSlot)).
AddField("note", status.Note).
Noticef("EvalStatus modify")
}
}
// LogDelete logs the deletion of EvalStatus
func (status EvalStatus) LogDelete(logBase *base.LogObject) {
logObject := base.EnsureLogObject(logBase, base.EvalStatusLogType, "",
nilUUID, status.LogKey())
logObject.CloneAndAddField("phase", string(status.Phase)).
AddField("allow-onboard", status.AllowOnboard).
Noticef("EvalStatus delete")
base.DeleteLogObject(logBase, status.LogKey())
}
// LogKey returns the log key for EvalStatus
func (status EvalStatus) LogKey() string {
return string(base.EvalStatusLogType) + "-evalmgr"
}
// IsOnboardingAllowed returns whether onboarding should be allowed based on the current evaluation status.
// This method encapsulates the logic for determining onboarding permission based on multiple internal states.
func (status EvalStatus) IsOnboardingAllowed() bool {
// If not an evaluation platform, always allow onboarding
if !status.IsEvaluationPlatform {
return true
}
// For evaluation platforms, check multiple conditions:
// 1. Explicit AllowOnboard flag (primary control)
if !status.AllowOnboard {
return false
}
// 2. Phase-based logic - don't allow onboarding during active testing
switch status.Phase {
case EvalPhaseInit:
// During init phase, allow if explicitly permitted
return status.AllowOnboard
case EvalPhaseTesting:
// During testing phase, be more restrictive
// Only allow if explicitly set (for manual overrides)
return status.AllowOnboard
case EvalPhaseFinal:
// Final phase - evaluation complete, allow onboarding
return true
default:
// Unknown phase - be conservative
return false
}
}
// OnboardingBlockReason returns a human-readable reason why onboarding is blocked.
// Returns empty string if onboarding is allowed.
func (status EvalStatus) OnboardingBlockReason() string {
// If not an evaluation platform, always allow onboarding
if !status.IsEvaluationPlatform {
return ""
}
// Check if onboarding is allowed first
if status.IsOnboardingAllowed() {
return ""
}
// Provide specific reasons based on state
if !status.AllowOnboard {
switch status.Phase {
case EvalPhaseInit:
return "evaluation platform initializing"
case EvalPhaseTesting:
return "evaluation platform testing in progress"
case EvalPhaseFinal:
return "evaluation complete but onboarding explicitly disabled"
default:
return "evaluation platform not ready"
}
}
// Phase-based restrictions
switch status.Phase {
case EvalPhaseTesting:
return "evaluation testing in progress"
default:
return "evaluation platform in unknown state"
}
}
// RemainingTime returns the remaining time for current test phase
func (status EvalStatus) RemainingTime() time.Duration {
if status.TestStartTime.IsZero() || status.TestDuration == 0 {
return 0
}
elapsed := time.Since(status.TestStartTime)
if elapsed >= status.TestDuration {
return 0
}
return status.TestDuration - elapsed
}
// ElapsedTime returns elapsed time since test started
func (status EvalStatus) ElapsedTime() time.Duration {
if status.TestStartTime.IsZero() {
return 0
}
return time.Since(status.TestStartTime)
}
// ProgressPercent returns test progress as percentage (0-100)
func (status EvalStatus) ProgressPercent() int {
if status.TestDuration == 0 {
return 0
}
elapsed := status.ElapsedTime()
if elapsed >= status.TestDuration {
return 100
}
return int((elapsed * 100) / status.TestDuration)
}
// TimeStatusString returns human-readable time status
func (status EvalStatus) TimeStatusString() string {
if status.Phase != EvalPhaseTesting {
return ""
}
remaining := status.RemainingTime()
elapsed := status.ElapsedTime()
progress := status.ProgressPercent()
if remaining == 0 {
return fmt.Sprintf("Test complete (%v elapsed)", elapsed.Round(time.Second))
}
return fmt.Sprintf("Progress %d%% (%v/%v remaining: %v)",
progress,
elapsed.Round(time.Second),
status.TestDuration.Round(time.Second),
remaining.Round(time.Second))
}
// RebootStatusString returns reboot countdown status
func (status EvalStatus) RebootStatusString() string {
if status.RebootCountdown <= 0 {
return ""
}
return fmt.Sprintf("Requesting reboot in %d sec", status.RebootCountdown)
}
// DetailedNote returns comprehensive status with timing info
func (status EvalStatus) DetailedNote() string {
parts := []string{status.Note}
if timeStatus := status.TimeStatusString(); timeStatus != "" {
parts = append(parts, timeStatus)
}
if rebootStatus := status.RebootStatusString(); rebootStatus != "" {
parts = append(parts, rebootStatus)
}
if status.InventoryCollected && status.InventoryDir != "" {
parts = append(parts, fmt.Sprintf("inventory=%s", status.InventoryDir))
}
return strings.Join(parts, "; ")
}