Skip to content

Commit

Permalink
restore: make IsCheckpointImage reusable
Browse files Browse the repository at this point in the history
Podman allows to store a container checkpoints as an images.
This patch makes the check that is used to recognise such checkpoint
images reusable by moving it in utils. This functionality will be reused
in a subsequent patch to extend the `podman run` command with support
for checkpoint images.

Signed-off-by: Radostin Stoyanov <[email protected]>
  • Loading branch information
rst0git committed Sep 29, 2022
1 parent f52fede commit b17d8ff
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
24 changes: 3 additions & 21 deletions cmd/podman/containers/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/cmd/podman/utils"
"github.com/containers/podman/v4/cmd/podman/validate"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/rootless"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -116,26 +115,9 @@ func restore(cmd *cobra.Command, args []string) error {

if !exists.Value {
// Find out if this is an image
inspectOpts := entities.InspectOptions{}
imgData, _, err := registry.ImageEngine().Inspect(context.Background(), args, inspectOpts)
if err != nil {
return err
}

hostInfo, err := registry.ContainerEngine().Info(context.Background())
if err != nil {
return err
}

for i := range imgData {
restoreOptions.CheckpointImage = true
checkpointRuntimeName, found := imgData[i].Annotations[define.CheckpointAnnotationRuntimeName]
if !found {
return fmt.Errorf("image is not a checkpoint: %s", imgData[i].ID)
}
if hostInfo.Host.OCIRuntime.Name != checkpointRuntimeName {
return fmt.Errorf("container image \"%s\" requires runtime: \"%s\"", imgData[i].ID, checkpointRuntimeName)
}
restoreOptions.CheckpointImage, e = utils.IsCheckpointImage(context.Background(), args)
if e != nil {
return e
}
}

Expand Down
40 changes: 40 additions & 0 deletions cmd/podman/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package utils

import (
"context"
"fmt"
"os"

"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/domain/entities/reports"
)
Expand Down Expand Up @@ -99,3 +102,40 @@ func PrintNetworkPruneResults(networkPruneReport []*entities.NetworkPruneReport,
}
return errs.PrintErrors()
}

// IsCheckpointImage returns true with no error only if all values in
// namesOrIDs correspond to checkpoint images AND these images are
// compatible with the container runtime that is currently in use,
// e.g., crun or runc.
//
// IsCheckpointImage returns false with no error when none of the values
// in namesOrIDs corresponds to an ID or name of an image.
//
// Otherwise, IsCheckpointImage returns false with appropriate error.
func IsCheckpointImage(ctx context.Context, namesOrIDs []string) (bool, error) {
inspectOpts := entities.InspectOptions{}
imgData, _, err := registry.ImageEngine().Inspect(ctx, namesOrIDs, inspectOpts)
if err != nil {
return false, err
}
if len(imgData) == 0 {
return false, nil
}
imgID := imgData[0].ID

hostInfo, err := registry.ContainerEngine().Info(ctx)
if err != nil {
return false, err
}

for i := range imgData {
checkpointRuntimeName, found := imgData[i].Annotations[define.CheckpointAnnotationRuntimeName]
if !found {
return false, fmt.Errorf("image is not a checkpoint: %s", imgID)
}
if hostInfo.Host.OCIRuntime.Name != checkpointRuntimeName {
return false, fmt.Errorf("container image \"%s\" requires runtime: \"%s\"", imgID, checkpointRuntimeName)
}
}
return true, nil
}

0 comments on commit b17d8ff

Please sign in to comment.