Commit 9f4a8af
fix: ensure numeric sorting of manifest group directories
# Background / Context
The lifecycle-agent processes manifests from subdirectories in a base path
using the LoadGroupedManifestsFromPath function. This function reads
subdirectories using os.ReadDir, which returns directory entries in an
alphabetically sorted order by default in Go. The manifest groups are typically
named with numeric suffixes (e.g., restore1, restore2, restore10) to indicate
their processing order.
The current implementation processes these directories in the order returned by
os.ReadDir without any additional sorting logic to handle numeric suffixes.
# Issue / Requirement / Reason for change
When manifest group directories have numeric suffixes, alphabetical sorting
causes incorrect ordering. Specifically:
- restore1, restore10, restore11, restore12, restore2, restore3, ...
This is because alphabetically "restore10" comes before "restore2". This
violates the expected numeric ordering and can cause manifests to be applied in
the wrong sequence, potentially leading to restore failures or unexpected
behavior.
Related to: https://issues.redhat.com/browse/OCPBUGS-77043
# Solution / Feature Overview
Implemented numeric sorting for manifest group directories by extracting
trailing numbers from directory names and sorting based on those numeric
values. This ensures that directories are processed in the correct numeric
order regardless of whether they have single-digit or multi-digit suffixes.
# Implementation Details
1. Added a new extractTrailingNumber helper function that:
- Uses a regex pattern (\d+$) to extract trailing numbers from strings
- Returns the numeric value, or 0 if no trailing number exists
- Handles conversion errors gracefully
2. Modified LoadGroupedManifestsFromPath to sort directories after reading:
- Added sort.Slice call that compares directories using extractTrailingNumber
- This happens before the loop that processes each directory
- Maintains backward compatibility for directories without numeric suffixes
3. Added comprehensive test coverage:
- TestLoadGroupedManifestsFromPathWithNumericSorting: Creates 12 directories
(restore1 through restore12) and verifies they're processed in numeric order
- TestExtractTrailingNumber: Unit tests for the helper function covering
single/double/triple digits, no numbers, and edge cases
# Other Information
This is a bug fix that ensures correct ordering without changing the external
API or behavior for properly named directories. The fix is defensive - if a
directory has no trailing number, it defaults to 0, maintaining deterministic
behavior.
No breaking changes are introduced. Existing manifest groups will continue to
work, and this fix only affects the order in which directories are processed.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>1 parent 0c10d92 commit 9f4a8af
2 files changed
+121
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
| |||
465 | 467 | | |
466 | 468 | | |
467 | 469 | | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
468 | 476 | | |
469 | 477 | | |
470 | 478 | | |
| |||
502 | 510 | | |
503 | 511 | | |
504 | 512 | | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
505 | 529 | | |
506 | 530 | | |
507 | 531 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
186 | 187 | | |
187 | 188 | | |
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 | + | |
189 | 286 | | |
190 | 287 | | |
191 | 288 | | |
| |||
0 commit comments