-
Notifications
You must be signed in to change notification settings - Fork 36
lib: Add CheckpointOptions struct and parsing for CRI options #199
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
Open
adrianreber
wants to merge
1
commit into
checkpoint-restore:main
Choose a base branch
from
adrianreber:2026-02-13-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package metadata | ||
|
|
||
| import ( | ||
| "fmt" | ||
| ) | ||
|
|
||
| // OptionType represents the type of a checkpoint option value | ||
| type OptionType int | ||
|
|
||
| const ( | ||
| OptionTypeBool OptionType = iota | ||
| OptionTypeInt | ||
| OptionTypeString | ||
| ) | ||
|
|
||
| // CheckpointOption defines a known checkpoint option with its expected type | ||
| type CheckpointOption struct { | ||
| Type OptionType | ||
| } | ||
|
|
||
| // SupportedCheckpointOption defines a supported checkpoint option with help text | ||
| // for use by tools that include this package. | ||
| type SupportedCheckpointOption struct { | ||
| Type OptionType | ||
| Help string | ||
| } | ||
|
|
||
| // SupportedCheckpointOptions lists all checkpoint options that are currently | ||
| // supported. Tools can use this to display help text or list available options. | ||
| var SupportedCheckpointOptions = map[string]SupportedCheckpointOption{ | ||
| "leave-running": { | ||
| Type: OptionTypeBool, | ||
| Help: "leave container(s) in running state after checkpointing", | ||
| }, | ||
| } | ||
|
|
||
| // CheckpointOptions represents the options from a CheckpointPodRequest | ||
| type CheckpointOptions struct { | ||
| // LeaveRunning leaves the processes running in the container after checkpointing | ||
| LeaveRunning bool `json:"leaveRunning,omitempty"` | ||
| // TCPEstablished enables support for established TCP connections in the checkpoint | ||
| TCPEstablished bool `json:"tcpEstablished,omitempty"` | ||
| // GhostLimit limits max size of deleted file contents inside image | ||
| GhostLimit int64 `json:"ghostLimit,omitempty"` | ||
| // NetworkLock specifies the network locking/unlocking method | ||
| NetworkLock string `json:"networkLock,omitempty"` | ||
| } | ||
|
|
||
| // knownCheckpointOptions defines all known checkpoint options and their expected types. | ||
| // It is built from SupportedCheckpointOptions plus additional options that are not yet | ||
| // supported but are defined here to ensure the parsing logic for all option types | ||
| // (bool, int, string) is tested and works. | ||
| var knownCheckpointOptions map[string]CheckpointOption | ||
|
|
||
| func init() { | ||
| knownCheckpointOptions = make(map[string]CheckpointOption) | ||
|
|
||
| // Include all supported options | ||
| for name, opt := range SupportedCheckpointOptions { | ||
| knownCheckpointOptions[name] = CheckpointOption{Type: opt.Type} | ||
| } | ||
|
|
||
| // Additional options for testing different option types (not yet supported) | ||
| knownCheckpointOptions["tcp-established"] = CheckpointOption{Type: OptionTypeBool} | ||
| knownCheckpointOptions["ghost-limit"] = CheckpointOption{Type: OptionTypeInt} | ||
| knownCheckpointOptions["network-lock"] = CheckpointOption{Type: OptionTypeString} | ||
| } | ||
|
|
||
| // parseBool parses a string value as a boolean, accepting: | ||
| // yes, no, true, false, on, off, 0, 1 (case-insensitive) | ||
| func parseBool(value string) (bool, error) { | ||
| switch value { | ||
| case "yes", "Yes", "YES", "true", "True", "TRUE", "on", "On", "ON", "1": | ||
| return true, nil | ||
| case "no", "No", "NO", "false", "False", "FALSE", "off", "Off", "OFF", "0": | ||
| return false, nil | ||
| default: | ||
| return false, fmt.Errorf("invalid boolean value: %q (accepted: yes, no, true, false, on, off, 0, 1)", value) | ||
| } | ||
| } | ||
|
|
||
| // ParseCheckpointOptions validates and parses checkpoint options from a map[string]string. | ||
| // It checks if the options are known and if their values match the expected types. | ||
| // Returns a CheckpointOptions struct and any validation errors encountered. | ||
| func ParseCheckpointOptions(options map[string]string) (*CheckpointOptions, error) { | ||
| result := &CheckpointOptions{} | ||
| var errs []string | ||
|
|
||
| for key, value := range options { | ||
| opt, known := knownCheckpointOptions[key] | ||
| if !known { | ||
| errs = append(errs, fmt.Sprintf("unknown option: %q", key)) | ||
| continue | ||
| } | ||
|
|
||
| switch opt.Type { | ||
| case OptionTypeBool: | ||
| boolVal, err := parseBool(value) | ||
| if err != nil { | ||
| errs = append(errs, fmt.Sprintf("option %q: %v", key, err)) | ||
| continue | ||
| } | ||
| switch key { | ||
| case "leave-running": | ||
| result.LeaveRunning = boolVal | ||
| case "tcp-established": | ||
| result.TCPEstablished = boolVal | ||
| } | ||
|
|
||
| case OptionTypeInt: | ||
| var intVal int64 | ||
| if _, err := fmt.Sscanf(value, "%d", &intVal); err != nil { | ||
| errs = append(errs, fmt.Sprintf("option %q: invalid integer value: %q", key, value)) | ||
| continue | ||
| } | ||
| switch key { | ||
| case "ghost-limit": | ||
| result.GhostLimit = intVal | ||
| } | ||
|
|
||
| case OptionTypeString: | ||
| switch key { | ||
| case "network-lock": | ||
| result.NetworkLock = value | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if len(errs) > 0 { | ||
| return result, fmt.Errorf("validation errors: %v", errs) | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also add tcp-close.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just as an example for now. Just for testing. I think this can be added any time later.