File: list.go

package info (click to toggle)
golang-github-checkpoint-restore-checkpointctl 1.3.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 524 kB
  • sloc: ansic: 208; makefile: 172; sh: 40
file content (95 lines) | stat: -rw-r--r-- 1,902 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 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/"

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

	return cmd
}

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

	table := tablewriter.NewWriter(os.Stdout)
	header := []string{
		"Namespace",
		"Pod",
		"Container",
		"Engine",
		"Time Checkpointed",
		"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 {
			chkptConfig, err := internal.ExtractConfigDump(file)
			if err != nil {
				log.Printf("Error extracting information from %s: %v\n", file, err)
				continue
			}

			row := []string{
				chkptConfig.Namespace,
				chkptConfig.Pod,
				chkptConfig.Container,
				chkptConfig.ContainerManager,
				chkptConfig.Timestamp.Format(time.RFC822),
				filepath.Base(file),
			}

			table.Append(row)
		}
	}

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

	table.Render()
	return nil
}