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

refactor: move table rendering into own funcs #60

Merged
merged 2 commits into from
Jun 8, 2023
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
24 changes: 12 additions & 12 deletions checkpointctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
)

var (
name string
version string
printStats bool
showMounts bool
fullPaths bool
showAll bool
name string
version string
stats bool
mounts bool
fullPaths bool
showAll bool
)

func main() {
Expand Down Expand Up @@ -47,19 +47,19 @@ func setupShow() *cobra.Command {
}
flags := cmd.Flags()
flags.BoolVar(
&printStats,
&stats,
"print-stats",
false,
"Print checkpointing statistics if available",
)
flags.BoolVar(
&printStats,
&stats,
"stats",
false,
"Print checkpointing statistics if available",
)
flags.BoolVar(
&showMounts,
&mounts,
"mounts",
false,
"Print overview about mounts used in the checkpoints",
Expand All @@ -86,10 +86,10 @@ func setupShow() *cobra.Command {

func show(cmd *cobra.Command, args []string) error {
if showAll {
printStats = true
showMounts = true
stats = true
mounts = true
}
if fullPaths && !showMounts {
if fullPaths && !mounts {
return fmt.Errorf("Cannot use --full-paths without --mounts/--all option")
}

Expand Down
141 changes: 76 additions & 65 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

metadata "github.com/checkpoint-restore/checkpointctl/lib"
"github.com/checkpoint-restore/go-criu/v6/crit"
"github.com/checkpoint-restore/go-criu/v6/crit/images"
"github.com/containers/storage/pkg/archive"
"github.com/olekukonko/tablewriter"
spec "github.com/opencontainers/runtime-spec/specs-go"
Expand Down Expand Up @@ -66,10 +67,6 @@ func getCRIOInfo(_ *metadata.ContainerConfig, specDump *spec.Spec) (*containerIn
}

func showContainerCheckpoint(checkpointDirectory, input string) error {
var (
row []string
ci *containerInfo
)
containerConfig, _, err := metadata.ReadContainerCheckpointConfigDump(checkpointDirectory)
if err != nil {
return err
Expand All @@ -79,11 +76,15 @@ func showContainerCheckpoint(checkpointDirectory, input string) error {
return err
}

var ci *containerInfo
switch m := specDump.Annotations["io.container.manager"]; m {
case "libpod":
ci = getPodmanInfo(containerConfig, specDump)
case "cri-o":
ci, err = getCRIOInfo(containerConfig, specDump)
if err != nil {
return fmt.Errorf("getting container checkpoint information failed: %w", err)
}
default:
containerdStatus, _, _ := metadata.ReadContainerCheckpointStatusFile(checkpointDirectory)
if containerdStatus == nil {
Expand All @@ -92,12 +93,40 @@ func showContainerCheckpoint(checkpointDirectory, input string) error {
ci = getContainerdInfo(containerdStatus, specDump)
}

// Fetch root fs diff size if available
archiveSizes, err := getArchiveSizes(input)
if err != nil {
return fmt.Errorf("getting container checkpoint information failed: %w", err)
return fmt.Errorf("failed to get archive sizes: %w", err)
}

fmt.Printf("\nDisplaying container checkpoint data from %s\n\n", input)

renderCheckpoint(ci, containerConfig, archiveSizes)

if mounts {
renderMounts(specDump)
}

if stats {
cpDir, err := os.Open(checkpointDirectory)
if err != nil {
return err
}
defer cpDir.Close()

// Get dump statistics with crit
dumpStats, err := crit.GetDumpStats(cpDir.Name())
if err != nil {
return fmt.Errorf("unable to display checkpointing statistics: %w", err)
}

renderDumpStats(dumpStats)
}

return nil
}

func renderCheckpoint(ci *containerInfo, containerConfig *metadata.ContainerConfig, archiveSizes *archiveSizes) {
table := tablewriter.NewWriter(os.Stdout)
header := []string{
"Container",
Expand All @@ -108,6 +137,7 @@ func showContainerCheckpoint(checkpointDirectory, input string) error {
"Engine",
}

var row []string
row = append(row, ci.Name)
row = append(row, containerConfig.RootfsImageName)
if len(containerConfig.ID) > 12 {
Expand All @@ -129,11 +159,6 @@ func showContainerCheckpoint(checkpointDirectory, input string) error {
row = append(row, ci.MAC)
}

archiveSizes, err := getArchiveSizes(input)
if err != nil {
return err
}

header = append(header, "CHKPT Size")
row = append(row, metadata.ByteToString(archiveSizes.checkpointSize))

Expand All @@ -148,66 +173,52 @@ func showContainerCheckpoint(checkpointDirectory, input string) error {
table.SetHeader(header)
table.Append(row)
table.Render()
}

if showMounts {
table = tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"Destination",
"Type",
"Source",
})
// Get overview of mounts from spec.dump
for _, data := range specDump.Mounts {
table.Append([]string{
data.Destination,
data.Type,
func() string {
if fullPaths {
return data.Source
}
return shortenPath(data.Source)
}(),
})
}
fmt.Println("\nOverview of Mounts")
table.Render()
}

if printStats {
cpDir, err := os.Open(checkpointDirectory)
if err != nil {
return err
}
defer cpDir.Close()

// Get dump statistics with crit
dumpStatistics, err := crit.GetDumpStats(cpDir.Name())
if err != nil {
return fmt.Errorf("unable to display checkpointing statistics: %w", err)
}

table = tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"Freezing Time",
"Frozen Time",
"Memdump Time",
"Memwrite Time",
"Pages Scanned",
"Pages Written",
})
func renderMounts(specDump *spec.Spec) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"Destination",
"Type",
"Source",
})
// Get overview of mounts from spec.dump
for _, data := range specDump.Mounts {
table.Append([]string{
fmt.Sprintf("%d us", dumpStatistics.GetFreezingTime()),
fmt.Sprintf("%d us", dumpStatistics.GetFrozenTime()),
fmt.Sprintf("%d us", dumpStatistics.GetMemdumpTime()),
fmt.Sprintf("%d us", dumpStatistics.GetMemwriteTime()),
fmt.Sprintf("%d", dumpStatistics.GetPagesScanned()),
fmt.Sprintf("%d", dumpStatistics.GetPagesWritten()),
data.Destination,
data.Type,
func() string {
if fullPaths {
return data.Source
}
return shortenPath(data.Source)
}(),
})
fmt.Println("\nCRIU dump statistics")
table.Render()
}
fmt.Println("\nOverview of Mounts")
table.Render()
}

return nil
func renderDumpStats(dumpStats *images.DumpStatsEntry) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"Freezing Time",
"Frozen Time",
"Memdump Time",
"Memwrite Time",
"Pages Scanned",
"Pages Written",
})
table.Append([]string{
fmt.Sprintf("%d us", dumpStats.GetFreezingTime()),
fmt.Sprintf("%d us", dumpStats.GetFrozenTime()),
fmt.Sprintf("%d us", dumpStats.GetMemdumpTime()),
fmt.Sprintf("%d us", dumpStats.GetMemwriteTime()),
fmt.Sprintf("%d", dumpStats.GetPagesScanned()),
fmt.Sprintf("%d", dumpStats.GetPagesWritten()),
})
fmt.Println("\nCRIU dump statistics")
table.Render()
}

func hasPrefix(path, prefix string) bool {
Expand Down