Update checkpointctl v0.1.0

With a minor fix during error unwrapping.

[NO NEW TESTS NEEDED]

Signed-off-by: Adrian Reber <areber@redhat.com>
This commit is contained in:
Adrian Reber
2023-03-07 16:14:13 +00:00
parent 45e27766e6
commit c03cb57183
5 changed files with 43 additions and 317 deletions

View File

@@ -5,48 +5,14 @@ package metadata
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
type CheckpointedPod struct {
PodUID string `json:"io.kubernetes.pod.uid,omitempty"`
ID string `json:"SandboxID,omitempty"`
Name string `json:"io.kubernetes.pod.name,omitempty"`
TerminationGracePeriod int64 `json:"io.kubernetes.pod.terminationGracePeriod,omitempty"`
Namespace string `json:"io.kubernetes.pod.namespace,omitempty"`
ConfigSource string `json:"kubernetes.io/config.source,omitempty"`
ConfigSeen string `json:"kubernetes.io/config.seen,omitempty"`
Manager string `json:"io.container.manager,omitempty"`
Containers []CheckpointedContainer `json:"Containers"`
HostIP string `json:"hostIP,omitempty"`
PodIP string `json:"podIP,omitempty"`
PodIPs []string `json:"podIPs,omitempty"`
}
type CheckpointedContainer struct {
Name string `json:"io.kubernetes.container.name,omitempty"`
ID string `json:"id,omitempty"`
TerminationMessagePath string `json:"io.kubernetes.container.terminationMessagePath,omitempty"`
TerminationMessagePolicy string `json:"io.kubernetes.container.terminationMessagePolicy,omitempty"`
RestartCounter int32 `json:"io.kubernetes.container.restartCount,omitempty"`
TerminationMessagePathUID string `json:"terminationMessagePathUID,omitempty"`
Image string `json:"Image"`
}
type CheckpointMetadata struct {
Version int `json:"version"`
CheckpointedPods []CheckpointedPod
}
const (
// kubelet archive
CheckpointedPodsFile = "checkpointed.pods"
// container archive
ConfigDumpFile = "config.dump"
SpecDumpFile = "spec.dump"
@@ -61,105 +27,48 @@ const (
// pod archive
PodOptionsFile = "pod.options"
PodDumpFile = "pod.dump"
)
type CheckpointType int
const (
// The checkpoint archive contains a kubelet checkpoint
// One or multiple pods and kubelet metadata (checkpointed.pods)
Kubelet CheckpointType = iota
// The checkpoint archive contains one pod including one or multiple containers
Pod
// The checkpoint archive contains a single container
Container
Unknown
// containerd only
StatusFile = "status"
)
// This is a reduced copy of what Podman uses to store checkpoint metadata
type ContainerConfig struct {
ID string `json:"id"`
Name string `json:"name"`
RootfsImage string `json:"rootfsImage,omitempty"`
RootfsImageRef string `json:"rootfsImageRef,omitempty"`
RootfsImageName string `json:"rootfsImageName,omitempty"`
OCIRuntime string `json:"runtime,omitempty"`
CreatedTime time.Time `json:"createdTime"`
CheckpointedAt time.Time `json:"checkpointedTime"`
RestoredAt time.Time `json:"restoredTime"`
Restored bool `json:"restored"`
}
// This is metadata stored inside of a Pod checkpoint archive
type CheckpointedPodOptions struct {
Version int `json:"version"`
Containers []string `json:"containers,omitempty"`
MountLabel string `json:"mountLabel"`
ProcessLabel string `json:"processLabel"`
type ContainerdStatus struct {
CreatedAt int64
StartedAt int64
FinishedAt int64
ExitCode int32
Pid uint32
Reason string
Message string
}
// This is metadata stored inside of Pod checkpoint archive
type PodSandboxConfig struct {
Metadata SandboxMetadta `json:"metadata"`
Hostname string `json:"hostname"`
// This structure is used by the KubernetesContainerCheckpointMetadata structure
type KubernetesCheckpoint struct {
Archive string `json:"archive,omitempty"`
Size int64 `json:"size,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
}
type SandboxMetadta struct {
Name string `json:"name"`
UID string `json:"uid"`
Namespace string `json:"namespace"`
}
func checkForFile(checkpointDirectory, file string) (bool, error) {
_, err := os.Stat(filepath.Join(checkpointDirectory, file))
if err != nil && !os.IsNotExist(err) {
return false, errors.Wrapf(err, "Failed to access %q\n", file)
}
if os.IsNotExist(err) {
return false, nil
}
return true, nil
}
func DetectCheckpointArchiveType(checkpointDirectory string) (CheckpointType, error) {
kubelet, err := checkForFile(checkpointDirectory, CheckpointedPodsFile)
if os.IsNotExist(err) {
return Unknown, err
}
container, err := checkForFile(checkpointDirectory, ConfigDumpFile)
if os.IsNotExist(err) {
return Unknown, err
}
pod, err := checkForFile(checkpointDirectory, PodDumpFile)
if os.IsNotExist(err) {
return Unknown, err
}
if pod && !container && !kubelet {
return Pod, nil
}
if !pod && container && !kubelet {
return Container, nil
}
if !pod && !container && kubelet {
return Kubelet, nil
}
return Unknown, nil
}
func ReadPodCheckpointDumpFile(checkpointDirectory string) (*PodSandboxConfig, string, error) {
var podSandboxConfig PodSandboxConfig
podDumpFile, err := ReadJSONFile(&podSandboxConfig, checkpointDirectory, PodDumpFile)
return &podSandboxConfig, podDumpFile, err
}
func ReadPodCheckpointOptionsFile(checkpointDirectory string) (*CheckpointedPodOptions, string, error) {
var checkpointedPodOptions CheckpointedPodOptions
podOptionsFile, err := ReadJSONFile(&checkpointedPodOptions, checkpointDirectory, PodOptionsFile)
return &checkpointedPodOptions, podOptionsFile, err
// This structure is the basis for Kubernetes to track how many checkpoints
// for a certain container have been created.
type KubernetesContainerCheckpointMetadata struct {
PodFullName string `json:"podFullName,omitempty"`
ContainerName string `json:"containerName,omitempty"`
TotalSize int64 `json:"totalSize,omitempty"`
Checkpoints []KubernetesCheckpoint `json:"checkpoints"`
}
func ReadContainerCheckpointSpecDump(checkpointDirectory string) (*spec.Spec, string, error) {
@@ -183,22 +92,22 @@ func ReadContainerCheckpointDeletedFiles(checkpointDirectory string) ([]string,
return deletedFiles, deletedFilesFile, err
}
func ReadKubeletCheckpoints(checkpointsDirectory string) (*CheckpointMetadata, string, error) {
var checkpointMetadata CheckpointMetadata
checkpointMetadataPath, err := ReadJSONFile(&checkpointMetadata, checkpointsDirectory, CheckpointedPodsFile)
func ReadContainerCheckpointStatusFile(checkpointDirectory string) (*ContainerdStatus, string, error) {
var containerdStatus ContainerdStatus
statusFile, err := ReadJSONFile(&containerdStatus, checkpointDirectory, StatusFile)
return &checkpointMetadata, checkpointMetadataPath, err
return &containerdStatus, statusFile, err
}
// WriteJSONFile marshalls and writes the given data to a JSON file
func WriteJSONFile(v interface{}, dir, file string) (string, error) {
fileJSON, err := json.MarshalIndent(v, "", " ")
if err != nil {
return "", errors.Wrapf(err, "Error marshalling JSON")
return "", fmt.Errorf("error marshalling JSON: %w", err)
}
file = filepath.Join(dir, file)
if err := ioutil.WriteFile(file, fileJSON, 0o600); err != nil {
return "", errors.Wrapf(err, "Error writing to %q", file)
if err := os.WriteFile(file, fileJSON, 0o600); err != nil {
return "", err
}
return file, nil
@@ -206,23 +115,17 @@ func WriteJSONFile(v interface{}, dir, file string) (string, error) {
func ReadJSONFile(v interface{}, dir, file string) (string, error) {
file = filepath.Join(dir, file)
content, err := ioutil.ReadFile(file)
content, err := os.ReadFile(file)
if err != nil {
return "", errors.Wrapf(err, "failed to read %s", file)
return "", err
}
if err = json.Unmarshal(content, v); err != nil {
return "", errors.Wrapf(err, "failed to unmarshal %s", file)
return "", fmt.Errorf("failed to unmarshal %s: %w", file, err)
}
return file, nil
}
func WriteKubeletCheckpointsMetadata(checkpointMetadata *CheckpointMetadata, dir string) error {
_, err := WriteJSONFile(checkpointMetadata, dir, CheckpointedPodsFile)
return err
}
func ByteToString(b int64) string {
const unit = 1024
if b < unit {