Skip to content
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

Simplified 'list' command code #122

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,26 @@ import (
"github.com/spf13/cobra"
)

var (
defaultCheckpointPath = "/var/lib/kubelet/checkpoints/"
additionalCheckpointPaths []string
)
var defaultCheckpointPath = "/var/lib/kubelet/checkpoints/"

func List() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List checkpoints stored in the default and additional paths",
RunE: list,
Use: "list [directories]",
Short: "List checkpoints stored in the default and additional directories",
RunE: list,
DisableFlagsInUseLine: true,
}

flags := cmd.Flags()
flags.StringSliceVarP(
&additionalCheckpointPaths,
"paths",
"p",
[]string{},
"Specify additional paths to include in checkpoint listing",
)

return cmd
}

func list(cmd *cobra.Command, args []string) error {
allPaths := append([]string{defaultCheckpointPath}, additionalCheckpointPaths...)
allPaths := func() []string {
if len(args) == 0 {
return []string{defaultCheckpointPath}
}
return args
}()
showTable := false

table := tablewriter.NewWriter(os.Stdout)
Expand Down Expand Up @@ -92,7 +86,7 @@ func list(cmd *cobra.Command, args []string) error {
}

if !showTable {
fmt.Println("No checkpoints found")
fmt.Printf("No checkpoints found in %v\n", allPaths)
return nil
}

Expand Down
9 changes: 3 additions & 6 deletions docs/checkpointctl-list.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@ include::footer.adoc[]

== Name

*checkpointctl-list* - List checkpoints stored in the default and additional paths
*checkpointctl-list* - List checkpoints stored in the default and additional directories

== Synopsis

*checkpointctl list* [_OPTION_]... _FOLDER_...
*checkpointctl list* [_directories_]

== Options

*-h*, *--help*::
Show help for checkpointctl list

*-p*, *--path*::
Specify additional paths to include in checkpoint listing

== Default Path
== Default Directory

The default path for checking checkpoints is `/var/lib/kubelet/checkpoints/`.

Expand Down
43 changes: 10 additions & 33 deletions internal/config_extractor.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package internal

import (
"encoding/json"
"log"
"os"
"path/filepath"
"time"

metadata "github.com/checkpoint-restore/checkpointctl/lib"
Expand All @@ -31,46 +29,25 @@ func ExtractConfigDump(checkpointPath string) (*ChkptConfig, error) {
return nil, err
}

specDumpPath := filepath.Join(tempDir, "spec.dump")
specContent, err := os.ReadFile(specDumpPath)
info := &checkpointInfo{}
info.configDump, _, err = metadata.ReadContainerCheckpointConfigDump(tempDir)
if err != nil {
log.Printf("Error reading spec.dump file: %v\n", err)
return nil, err
}

configDumpPath := filepath.Join(tempDir, "config.dump")
configContent, err := os.ReadFile(configDumpPath)
info.specDump, _, err = metadata.ReadContainerCheckpointSpecDump(tempDir)
if err != nil {
log.Printf("Error reading config.dump file: %v\n", err)
return nil, err
}

return extractConfigDumpContent(configContent, specContent)
}

func extractConfigDumpContent(configContent []byte, specContent []byte) (*ChkptConfig, error) {
var spec metadata.Spec
var config metadata.ContainerConfig

if err := json.Unmarshal(configContent, &config); err != nil {
return nil, err
}

if err := json.Unmarshal(specContent, &spec); err != nil {
info.containerInfo, err = getContainerInfo(info.specDump, info.configDump)
if err != nil {
return nil, err
}

namespace := spec.Annotations["io.kubernetes.pod.namespace"]
timestamp := config.CheckpointedAt
pod := spec.Annotations["io.kubernetes.pod.name"]
container := spec.Annotations["io.kubernetes.container.name"]
containerManager := spec.Annotations["io.container.manager"]

return &ChkptConfig{
Namespace: namespace,
Pod: pod,
Container: container,
ContainerManager: containerManager,
Timestamp: timestamp,
Namespace: info.containerInfo.Namespace,
Pod: info.containerInfo.Pod,
Container: info.containerInfo.Name,
ContainerManager: info.containerInfo.Engine,
Timestamp: info.configDump.CheckpointedAt,
}, nil
}
33 changes: 0 additions & 33 deletions internal/config_extractor_test.go

This file was deleted.

30 changes: 18 additions & 12 deletions internal/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ type containerMetadata struct {
}

type containerInfo struct {
Name string
IP string
MAC string
Created string
Engine string
Name string
IP string
MAC string
Created string
Engine string
Namespace string
Pod string
}

type checkpointInfo struct {
Expand All @@ -54,9 +56,11 @@ func getPodmanInfo(containerConfig *metadata.ContainerConfig, _ *spec.Spec) *con

func getContainerdInfo(containerConfig *metadata.ContainerConfig, specDump *spec.Spec) *containerInfo {
return &containerInfo{
Name: specDump.Annotations["io.kubernetes.cri.container-name"],
Created: containerConfig.CreatedTime.Format(time.RFC3339),
Engine: "containerd",
Name: specDump.Annotations["io.kubernetes.cri.container-name"],
Created: containerConfig.CreatedTime.Format(time.RFC3339),
Engine: "containerd",
Namespace: specDump.Annotations["io.kubernetes.cri.sandbox-namespace"],
Pod: specDump.Annotations["io.kubernetes.cri.sandbox-name"],
}
}

Expand All @@ -67,10 +71,12 @@ func getCRIOInfo(_ *metadata.ContainerConfig, specDump *spec.Spec) (*containerIn
}

return &containerInfo{
IP: specDump.Annotations["io.kubernetes.cri-o.IP.0"],
Name: cm.Name,
Created: specDump.Annotations["io.kubernetes.cri-o.Created"],
Engine: "CRI-O",
IP: specDump.Annotations["io.kubernetes.cri-o.IP.0"],
Name: cm.Name,
Created: specDump.Annotations["io.kubernetes.cri-o.Created"],
Engine: "CRI-O",
Namespace: specDump.Annotations["io.kubernetes.pod.namespace"],
Pod: specDump.Annotations["io.kubernetes.pod.name"],
}, nil
}

Expand Down
28 changes: 14 additions & 14 deletions test/checkpointctl.bats
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ function setup() {
}

function teardown() {
[ "$TEST_TMP_DIR1" != "" ] && rm -rf "$TEST_TMP_DIR1"
[ "$TEST_TMP_DIR2" != "" ] && rm -rf "$TEST_TMP_DIR2"
#[ "$TEST_TMP_DIR1" != "" ] && rm -rf "$TEST_TMP_DIR1"
#[ "$TEST_TMP_DIR2" != "" ] && rm -rf "$TEST_TMP_DIR2"
echo hu
}

@test "Run checkpointctl" {
Expand Down Expand Up @@ -644,43 +645,42 @@ function teardown() {

@test "Run checkpointctl list with empty directory" {
mkdir "$TEST_TMP_DIR1"/empty
checkpointctl list -p "$TEST_TMP_DIR1"/empty/
checkpointctl list "$TEST_TMP_DIR1"/empty/
[ "$status" -eq 0 ]
[[ ${lines[0]} == *"No checkpoints found"* ]]
}

@test "Run checkpointctl list with non existing directory" {
checkpointctl list -p /does-not-exist
checkpointctl list /does-not-exist
[ "$status" -eq 0 ]
[[ ${lines[0]} == *"No checkpoints found"* ]]
}

@test "Run checkpointctl list with empty tar file" {
touch "$TEST_TMP_DIR1"/checkpoint-nginx-empty.tar
checkpointctl list -p "$TEST_TMP_DIR1"
checkpointctl list "$TEST_TMP_DIR1"
[ "$status" -eq 0 ]
[[ "${lines[1]}" == *"Error reading spec.dump file"* ]]
[[ "${lines[2]}" == *"Error extracting information"* ]]
[[ "${lines[1]}" == *"Error extracting information"* ]]
}

@test "Run checkpointctl list with tar file with valid spec.dump and empty config.dump" {
touch "$TEST_TMP_DIR1"/config.dump
cp data/list_config_spec.dump/spec.dump "$TEST_TMP_DIR1"
mkdir "$TEST_TMP_DIR1"/checkpoint
( cd "$TEST_TMP_DIR1" && tar cf "$TEST_TMP_DIR2"/checkpoint-config.tar . )
checkpointctl list -p "$TEST_TMP_DIR2"
checkpointctl list "$TEST_TMP_DIR2"
[ "$status" -eq 0 ]
[[ "${lines[1]}" == *"Error extracting information from $TEST_TMP_DIR2/checkpoint-config.tar: unexpected end of JSON input"* ]]
[[ "${lines[1]}" == *"Error extracting information from $TEST_TMP_DIR2/checkpoint-config.tar: failed to unmarshal"* ]]
}

@test "Run checkpointctl list with tar file with valid config.dump and empty spec.dump" {
touch "$TEST_TMP_DIR1"/spec.dump
cp data/list_config_spec.dump/config.dump "$TEST_TMP_DIR1"
mkdir "$TEST_TMP_DIR1"/checkpoint
( cd "$TEST_TMP_DIR1" && tar cf "$TEST_TMP_DIR2"/checkpoint-config.tar . )
checkpointctl list -p "$TEST_TMP_DIR2"
checkpointctl list "$TEST_TMP_DIR2"
[ "$status" -eq 0 ]
[[ ${lines[1]} == *"Error extracting information from $TEST_TMP_DIR2/checkpoint-config.tar: unexpected end of JSON input" ]]
[[ ${lines[1]} == *"Error extracting information from $TEST_TMP_DIR2/checkpoint-config.tar: failed to unmarshal"* ]]
}

@test "Run checkpointctl list with tar file with valid config.dump and spec.dump" {
Expand All @@ -691,10 +691,10 @@ function teardown() {
jq '.["annotations"]["io.kubernetes.pod.name"] = "modified-pod-name"' "$TEST_TMP_DIR1"/spec.dump > "$TEST_TMP_DIR1"/spec_modified.dump
mv "$TEST_TMP_DIR1"/spec_modified.dump "$TEST_TMP_DIR1"/spec.dump
( cd "$TEST_TMP_DIR1" && tar cf "$TEST_TMP_DIR2"/checkpoint-valid-config-modified.tar . )
checkpointctl list -p "$TEST_TMP_DIR2"
checkpointctl list "$TEST_TMP_DIR2"
[ "$status" -eq 0 ]
[[ "${lines[4]}" == *"| default | modified-pod-name | container-name | cri-o |"* ]]
[[ "${lines[4]}" == *"| default | modified-pod-name | container-name | CRI-O |"* ]]
[[ "${lines[4]}" == *"| checkpoint-valid-config-modified.tar |"* ]]
[[ "${lines[6]}" == *"| default | pod-name | container-name | cri-o |"* ]]
[[ "${lines[6]}" == *"| default | pod-name | container-name | CRI-O |"* ]]
[[ "${lines[6]}" == *"| checkpoint-valid-config.tar |"* ]]
}
7 changes: 4 additions & 3 deletions test/data/list_config_spec.dump/spec.dump
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"annotations": {
"io.container.manager": "cri-o",
"io.container.manager": "cri-o",
"io.kubernetes.container.hash": "1511917a",
"io.kubernetes.container.name": "container-name",
"io.kubernetes.pod.name": "pod-name",
"io.kubernetes.pod.namespace": "default"
"io.kubernetes.pod.namespace": "default",
"io.kubernetes.cri-o.Metadata": "{\"name\":\"container-name\"}"
}
}
}
Loading