-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce support for the list command to retrieve container
checkpoints - If applied, this commit will introduce support for the list command to retrieve container checkpoints, enhance user accessibility to container-related information. Signed-off-by: Parthiba-Hazra <[email protected]>
- Loading branch information
1 parent
d132ca0
commit 23e2986
Showing
8 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,99 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// This file is used to show the list of container checkpoints | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/checkpoint-restore/checkpointctl/internal" | ||
"github.com/olekukonko/tablewriter" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
defaultCheckpointPath = "/var/lib/kubelet/checkpoints/" | ||
additionalCheckpointPaths []string | ||
) | ||
|
||
func List() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Short: "List checkpoints stored in the default and additional paths", | ||
RunE: list, | ||
} | ||
|
||
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...) | ||
showTable := false | ||
|
||
table := tablewriter.NewWriter(os.Stdout) | ||
header := []string{ | ||
"Namespace", | ||
"Pod", | ||
"Container", | ||
"Time Created", | ||
"Checkpoint Name", | ||
} | ||
|
||
table.SetHeader(header) | ||
table.SetAutoMergeCells(false) | ||
table.SetRowLine(true) | ||
|
||
for _, checkpointPath := range allPaths { | ||
files, err := filepath.Glob(filepath.Join(checkpointPath, "checkpoint-*")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(files) == 0 { | ||
continue | ||
} | ||
|
||
showTable = true | ||
fmt.Printf("Listing checkpoints in path: %s\n", checkpointPath) | ||
|
||
for _, file := range files { | ||
namespace, pod, container, timestamp, err := internal.ExtractConfigDump(file) | ||
if err != nil { | ||
log.Printf("Error extracting information from %s: %v\n", file, err) | ||
continue | ||
} | ||
|
||
row := []string{ | ||
namespace, | ||
pod, | ||
container, | ||
timestamp.Format(time.RFC822), | ||
filepath.Base(file), | ||
} | ||
|
||
table.Append(row) | ||
} | ||
} | ||
|
||
if showTable { | ||
table.Render() | ||
} else { | ||
fmt.Println("No checkpoints found") | ||
} | ||
|
||
return nil | ||
} |
This file contains 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,26 @@ | ||
= checkpointctl-list(1) | ||
include::footer.adoc[] | ||
|
||
== Name | ||
|
||
*checkpointctl-list* - List checkpoints stored in the default and additional paths | ||
|
||
== Synopsis | ||
|
||
*checkpointctl list* [_OPTION_]... _FOLDER_... | ||
|
||
== Options | ||
|
||
*-h*, *--help*:: | ||
Show help for checkpointctl list | ||
|
||
*-p*, *--path*:: | ||
Specify additional paths to include in checkpoint listing | ||
|
||
== Default Path | ||
|
||
The default path for checking checkpoints is `/var/lib/kubelet/checkpoints/`. | ||
|
||
== See also | ||
|
||
checkpointctl(1) |
This file contains 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,55 @@ | ||
package internal | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
metadata "github.com/checkpoint-restore/checkpointctl/lib" | ||
) | ||
|
||
func ExtractConfigDump(checkpointPath string) (namespace, pod, container string, timestamp time.Time, err error) { | ||
tempDir, err := os.MkdirTemp("", "extracted-checkpoint") | ||
if err != nil { | ||
return "", "", "", time.Time{}, err | ||
} | ||
defer os.RemoveAll(tempDir) | ||
|
||
filesToExtract := []string{"config.dump"} | ||
if err := UntarFiles(checkpointPath, tempDir, filesToExtract); err != nil { | ||
log.Printf("Error extracting files from archive %s: %v\n", checkpointPath, err) | ||
return "", "", "", time.Time{}, err | ||
} | ||
|
||
configDumpPath := filepath.Join(tempDir, "config.dump") | ||
content, err := os.ReadFile(configDumpPath) | ||
if err != nil { | ||
log.Printf("Error reading config.dump file: %v\n", err) | ||
return "", "", "", time.Time{}, err | ||
} | ||
|
||
return extractConfigDumpContent(content) | ||
} | ||
|
||
func extractConfigDumpContent(content []byte) (namespace, pod, container string, timestamp time.Time, err error) { | ||
var c metadata.ContainerConfig | ||
if err := json.Unmarshal(content, &c); err != nil { | ||
return "", "", "", time.Time{}, err | ||
} | ||
|
||
parts := strings.Split(c.Name, "_") | ||
|
||
if len(parts) >= 4 { | ||
container = parts[1] | ||
pod = parts[2] | ||
namespace = parts[3] | ||
} else { | ||
return "", "", "", time.Time{}, fmt.Errorf("invalid name format") | ||
} | ||
|
||
return namespace, pod, container, c.CheckpointedAt, nil | ||
} |
This file contains 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,51 @@ | ||
package internal | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestExtractConfigDump(t *testing.T) { | ||
tempDir, err := os.MkdirTemp("", "test-extract-config-dump") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.RemoveAll(tempDir) | ||
|
||
checkpointFile := filepath.Join(tempDir, "checkpoint-123.tar") | ||
if err := CreateTarWithFile(checkpointFile, "../test/data/list_config.dump/config.dump"); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
namespace, pod, container, timestamp, err := ExtractConfigDump(checkpointFile) | ||
if err != nil { | ||
t.Fatalf("ExtractConfigDump failed: %v", err) | ||
} | ||
|
||
expectedNamespace := "default" | ||
expectedPod := "deployment-name" | ||
expectedContainer := "container-name" | ||
expectedTimestamp := time.Date(2024, 1, 28, 0, 10, 45, 673538606, time.FixedZone("", 19800)) | ||
if namespace != expectedNamespace || pod != expectedPod || container != expectedContainer || !timestamp.Equal(expectedTimestamp) { | ||
t.Errorf("ExtractConfigDump returned unexpected values: namespace=%s, pod=%s, container=%s, timestamp=%v", namespace, pod, container, timestamp) | ||
} | ||
} | ||
|
||
func TestExtractConfigDumpContent(t *testing.T) { | ||
content := []byte(`{"id":"6924be1bd90c23f10e2667102b0ee0f74f09bba78b6661871e733cb3b1737821","name":"k8s_container-name_deployment-name_default_6975ee47-6765-45dc-9a2b-1e38d51031f7_0","checkpointedTime":"2024-01-28T00:10:45.673538606+05:30"}`) | ||
|
||
namespace, pod, container, timestamp, err := extractConfigDumpContent(content) | ||
if err != nil { | ||
t.Fatalf("ExtractConfigDumpContent failed: %v", err) | ||
} | ||
|
||
expectedNamespace := "default" | ||
expectedPod := "deployment-name" | ||
expectedContainer := "container-name" | ||
expectedTimestamp := time.Date(2024, 1, 28, 0, 10, 45, 673538606, time.FixedZone("", 19800)) | ||
if namespace != expectedNamespace || pod != expectedPod || container != expectedContainer || !timestamp.Equal(expectedTimestamp) { | ||
t.Errorf("ExtractConfigDumpContent returned unexpected values: namespace=%s, pod=%s, container=%s, timestamp=%v", namespace, pod, container, timestamp) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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,12 @@ | ||
{ | ||
"id": "6924be1bd90c23f10e2667102b0ee0f74f09bba78b6661871e733cb3b1737821", | ||
"name": "k8s_container-name_deployment-name_default_6975ee47-6765-45dc-9a2b-1e38d51031f7_0", | ||
"rootfsImage": "docker.io/library/nginx@sha256:161ef4b1bf7effb350a2a9625cb2b59f69d54ec6059a8a155a1438d0439c593c", | ||
"rootfsImageRef": "a8758716bb6aa4d90071160d27028fe4eaee7ce8166221a97d30440c8eac2be6", | ||
"rootfsImageName": "docker.io/library/nginx:latest", | ||
"runtime": "runc", | ||
"createdTime": "2024-01-27T14:45:26.083444055Z", | ||
"checkpointedTime": "2024-01-28T00:10:45.673538606+05:30", | ||
"restoredTime": "0001-01-01T00:00:00Z", | ||
"restored": false | ||
} |