Use functions and defines from checkpointctl

No functional changes.

[NO TESTS NEEDED] - only moving code around

Signed-off-by: Adrian Reber <areber@redhat.com>
This commit is contained in:
Adrian Reber 2021-02-25 16:43:18 +00:00
parent bf92e21113
commit 91b2f07d5b
No known key found for this signature in database
GPG Key ID: 82C9378ED3C4906A
3 changed files with 32 additions and 69 deletions

View File

@ -13,6 +13,7 @@ import (
"strings" "strings"
"time" "time"
metadata "github.com/checkpoint-restore/checkpointctl/lib"
"github.com/containers/buildah/copier" "github.com/containers/buildah/copier"
"github.com/containers/common/pkg/secrets" "github.com/containers/common/pkg/secrets"
"github.com/containers/podman/v3/libpod/define" "github.com/containers/podman/v3/libpod/define"
@ -135,7 +136,7 @@ func (c *Container) ControlSocketPath() string {
// CheckpointPath returns the path to the directory containing the checkpoint // CheckpointPath returns the path to the directory containing the checkpoint
func (c *Container) CheckpointPath() string { func (c *Container) CheckpointPath() string {
return filepath.Join(c.bundlePath(), "checkpoint") return filepath.Join(c.bundlePath(), metadata.CheckpointDirectory)
} }
// PreCheckpointPath returns the path to the directory containing the pre-checkpoint-images // PreCheckpointPath returns the path to the directory containing the pre-checkpoint-images
@ -2141,26 +2142,11 @@ func (c *Container) canWithPrevious() error {
return err return err
} }
// writeJSONFile marshalls and writes the given data to a JSON file
// in the bundle path
func (c *Container) writeJSONFile(v interface{}, file string) error {
fileJSON, err := json.MarshalIndent(v, "", " ")
if err != nil {
return errors.Wrapf(err, "error writing JSON to %s for container %s", file, c.ID())
}
file = filepath.Join(c.bundlePath(), file)
if err := ioutil.WriteFile(file, fileJSON, 0644); err != nil {
return err
}
return nil
}
// prepareCheckpointExport writes the config and spec to // prepareCheckpointExport writes the config and spec to
// JSON files for later export // JSON files for later export
func (c *Container) prepareCheckpointExport() error { func (c *Container) prepareCheckpointExport() error {
// save live config // save live config
if err := c.writeJSONFile(c.Config(), "config.dump"); err != nil { if _, err := metadata.WriteJSONFile(c.Config(), c.bundlePath(), metadata.ConfigDumpFile); err != nil {
return err return err
} }
@ -2171,7 +2157,7 @@ func (c *Container) prepareCheckpointExport() error {
logrus.Debugf("generating spec for container %q failed with %v", c.ID(), err) logrus.Debugf("generating spec for container %q failed with %v", c.ID(), err)
return err return err
} }
if err := c.writeJSONFile(g.Config, "spec.dump"); err != nil { if _, err := metadata.WriteJSONFile(g.Config, c.bundlePath(), metadata.SpecDumpFile); err != nil {
return err return err
} }

View File

@ -19,6 +19,7 @@ import (
"syscall" "syscall"
"time" "time"
metadata "github.com/checkpoint-restore/checkpointctl/lib"
cnitypes "github.com/containernetworking/cni/pkg/types/current" cnitypes "github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/plugins/pkg/ns" "github.com/containernetworking/plugins/pkg/ns"
"github.com/containers/buildah/pkg/chrootuser" "github.com/containers/buildah/pkg/chrootuser"
@ -885,12 +886,13 @@ func (c *Container) exportCheckpoint(options ContainerCheckpointOptions) error {
logrus.Debugf("Exporting checkpoint image of container %q to %q", c.ID(), options.TargetFile) logrus.Debugf("Exporting checkpoint image of container %q to %q", c.ID(), options.TargetFile)
includeFiles := []string{ includeFiles := []string{
"checkpoint",
"artifacts", "artifacts",
"ctr.log", "ctr.log",
"config.dump", metadata.CheckpointDirectory,
"spec.dump", metadata.ConfigDumpFile,
"network.status"} metadata.SpecDumpFile,
metadata.NetworkStatusFile,
}
if options.PreCheckPoint { if options.PreCheckPoint {
includeFiles[0] = "pre-checkpoint" includeFiles[0] = "pre-checkpoint"
@ -1031,11 +1033,7 @@ func (c *Container) checkpoint(ctx context.Context, options ContainerCheckpointO
// Save network.status. This is needed to restore the container with // Save network.status. This is needed to restore the container with
// the same IP. Currently limited to one IP address in a container // the same IP. Currently limited to one IP address in a container
// with one interface. // with one interface.
formatJSON, err := json.MarshalIndent(c.state.NetworkStatus, "", " ") if _, err := metadata.WriteJSONFile(c.state.NetworkStatus, c.bundlePath(), metadata.NetworkStatusFile); err != nil {
if err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(c.bundlePath(), "network.status"), formatJSON, 0644); err != nil {
return err return err
} }
@ -1051,7 +1049,7 @@ func (c *Container) checkpoint(ctx context.Context, options ContainerCheckpointO
} }
if options.TargetFile != "" { if options.TargetFile != "" {
if err = c.exportCheckpoint(options); err != nil { if err := c.exportCheckpoint(options); err != nil {
return err return err
} }
} }
@ -1071,8 +1069,8 @@ func (c *Container) checkpoint(ctx context.Context, options ContainerCheckpointO
cleanup := []string{ cleanup := []string{
"dump.log", "dump.log",
"stats-dump", "stats-dump",
"config.dump", metadata.ConfigDumpFile,
"spec.dump", metadata.SpecDumpFile,
} }
for _, del := range cleanup { for _, del := range cleanup {
file := filepath.Join(c.bundlePath(), del) file := filepath.Join(c.bundlePath(), del)
@ -1165,7 +1163,7 @@ func (c *Container) restore(ctx context.Context, options ContainerCheckpointOpti
// Read network configuration from checkpoint // Read network configuration from checkpoint
// Currently only one interface with one IP is supported. // Currently only one interface with one IP is supported.
networkStatusFile, err := os.Open(filepath.Join(c.bundlePath(), "network.status")) networkStatus, _, err := metadata.ReadContainerCheckpointNetworkStatus(c.bundlePath())
// If the restored container should get a new name, the IP address of // If the restored container should get a new name, the IP address of
// the container will not be restored. This assumes that if a new name is // the container will not be restored. This assumes that if a new name is
// specified, the container is restored multiple times. // specified, the container is restored multiple times.
@ -1175,43 +1173,14 @@ func (c *Container) restore(ctx context.Context, options ContainerCheckpointOpti
if err == nil && options.Name == "" && (!options.IgnoreStaticIP || !options.IgnoreStaticMAC) { if err == nil && options.Name == "" && (!options.IgnoreStaticIP || !options.IgnoreStaticMAC) {
// The file with the network.status does exist. Let's restore the // The file with the network.status does exist. Let's restore the
// container with the same IP address / MAC address as during checkpointing. // container with the same IP address / MAC address as during checkpointing.
defer networkStatusFile.Close()
var networkStatus []*cnitypes.Result
networkJSON, err := ioutil.ReadAll(networkStatusFile)
if err != nil {
return err
}
if err := json.Unmarshal(networkJSON, &networkStatus); err != nil {
return err
}
if !options.IgnoreStaticIP { if !options.IgnoreStaticIP {
// Take the first IP address if IP := metadata.GetIPFromNetworkStatus(networkStatus); IP != nil {
var IP net.IP
if len(networkStatus) > 0 {
if len(networkStatus[0].IPs) > 0 {
IP = networkStatus[0].IPs[0].Address.IP
}
}
if IP != nil {
// Tell CNI which IP address we want. // Tell CNI which IP address we want.
c.requestedIP = IP c.requestedIP = IP
} }
} }
if !options.IgnoreStaticMAC { if !options.IgnoreStaticMAC {
// Take the first device with a defined sandbox. if MAC := metadata.GetMACFromNetworkStatus(networkStatus); MAC != nil {
var MAC net.HardwareAddr
if len(networkStatus) > 0 {
for _, n := range networkStatus[0].Interfaces {
if n.Sandbox != "" {
MAC, err = net.ParseMAC(n.Mac)
if err != nil {
return err
}
break
}
}
}
if MAC != nil {
// Tell CNI which MAC address we want. // Tell CNI which MAC address we want.
c.requestedMAC = MAC c.requestedMAC = MAC
} }
@ -1349,7 +1318,15 @@ func (c *Container) restore(ctx context.Context, options ContainerCheckpointOpti
if err != nil { if err != nil {
logrus.Debugf("Non-fatal: removal of pre-checkpoint directory (%s) failed: %v", c.PreCheckPointPath(), err) logrus.Debugf("Non-fatal: removal of pre-checkpoint directory (%s) failed: %v", c.PreCheckPointPath(), err)
} }
cleanup := [...]string{"restore.log", "dump.log", "stats-dump", "stats-restore", "network.status", "rootfs-diff.tar", "deleted.files"} cleanup := [...]string{
"restore.log",
"dump.log",
"stats-dump",
"stats-restore",
metadata.NetworkStatusFile,
metadata.RootFsDiffTar,
metadata.DeletedFilesFile,
}
for _, del := range cleanup { for _, del := range cleanup {
file := filepath.Join(c.bundlePath(), del) file := filepath.Join(c.bundlePath(), del)
err = os.Remove(file) err = os.Remove(file)

View File

@ -32,13 +32,13 @@ func CRImportCheckpoint(ctx context.Context, runtime *libpod.Runtime, restoreOpt
options := &archive.TarOptions{ options := &archive.TarOptions{
// Here we only need the files config.dump and spec.dump // Here we only need the files config.dump and spec.dump
ExcludePatterns: []string{ ExcludePatterns: []string{
"checkpoint",
"artifacts",
"ctr.log",
"rootfs-diff.tar",
"network.status",
"deleted.files",
"volumes", "volumes",
"ctr.log",
"artifacts",
metadata.RootFsDiffTar,
metadata.DeletedFilesFile,
metadata.NetworkStatusFile,
metadata.CheckpointDirectory,
}, },
} }
dir, err := ioutil.TempDir("", "checkpoint") dir, err := ioutil.TempDir("", "checkpoint")