summaryrefslogtreecommitdiff
path: root/cmd/podman/pods
diff options
context:
space:
mode:
authorcdoern <cbdoer23@g.holycross.edu>2022-05-13 10:52:57 -0400
committercdoern <cdoern@redhat.com>2022-06-10 14:23:19 -0400
commit958759a71955860b01b17bd3bebf38f9dae1018e (patch)
tree34872e81fb126f854c7612ca39f11932ccd3f8bb /cmd/podman/pods
parent6a2c0e96011d36e5b459a010d472facd26c67389 (diff)
downloadpodman-958759a71955860b01b17bd3bebf38f9dae1018e.tar.gz
podman-958759a71955860b01b17bd3bebf38f9dae1018e.tar.bz2
podman-958759a71955860b01b17bd3bebf38f9dae1018e.zip
podman pod clone
implement podman pod clone, a command to create an exact copy of a pod while changing certain config elements current supported flags are: --name change the pod name --destroy remove the original pod --start run the new pod on creation and all infra-container related flags from podman pod create (namespaces etc) resolves #12843 Signed-off-by: cdoern <cdoern@redhat.com>
Diffstat (limited to 'cmd/podman/pods')
-rw-r--r--cmd/podman/pods/clone.go87
-rw-r--r--cmd/podman/pods/create.go1
2 files changed, 88 insertions, 0 deletions
diff --git a/cmd/podman/pods/clone.go b/cmd/podman/pods/clone.go
new file mode 100644
index 000000000..d95d74b05
--- /dev/null
+++ b/cmd/podman/pods/clone.go
@@ -0,0 +1,87 @@
+package pods
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/containers/common/pkg/completion"
+ "github.com/containers/podman/v4/cmd/podman/common"
+ "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/pkg/errors"
+ "github.com/spf13/cobra"
+)
+
+var (
+ podCloneDescription = `Create an exact copy of a pod and the containers within it`
+
+ podCloneCommand = &cobra.Command{
+ Use: "clone [options] POD NAME",
+ Short: "Clone an existing pod",
+ Long: podCloneDescription,
+ RunE: clone,
+ Args: cobra.RangeArgs(1, 2),
+ ValidArgsFunction: common.AutocompleteClone,
+ Example: `podman pod clone pod_name new_name`,
+ }
+)
+
+var (
+ podClone entities.PodCloneOptions
+)
+
+func cloneFlags(cmd *cobra.Command) {
+ flags := cmd.Flags()
+
+ destroyFlagName := "destroy"
+ flags.BoolVar(&podClone.Destroy, destroyFlagName, false, "destroy the original pod")
+
+ startFlagName := "start"
+ flags.BoolVar(&podClone.Start, startFlagName, false, "start the new pod")
+
+ nameFlagName := "name"
+ flags.StringVarP(&podClone.CreateOpts.Name, nameFlagName, "n", "", "name the new pod")
+ _ = podCloneCommand.RegisterFlagCompletionFunc(nameFlagName, completion.AutocompleteNone)
+
+ common.DefineCreateDefaults(&podClone.InfraOptions)
+ common.DefineCreateFlags(cmd, &podClone.InfraOptions, true, false)
+ podClone.InfraOptions.MemorySwappiness = -1 // this is not implemented for pods yet, need to set -1 default manually
+
+ // need to fill an empty ctr create option for each container for sane defaults
+ // for now, these cannot be used. The flag names conflict too much
+ // this makes sense since this is a pod command not a container command
+ // TODO: add support for container specific arguments/flags
+ common.DefineCreateDefaults(&podClone.PerContainerOptions)
+}
+func init() {
+ registry.Commands = append(registry.Commands, registry.CliCommand{
+ Command: podCloneCommand,
+ Parent: podCmd,
+ })
+
+ cloneFlags(podCloneCommand)
+}
+
+func clone(cmd *cobra.Command, args []string) error {
+ switch len(args) {
+ case 0:
+ return errors.Wrapf(define.ErrInvalidArg, "must specify at least 1 argument")
+ case 2:
+ podClone.CreateOpts.Name = args[1]
+ }
+
+ podClone.ID = args[0]
+ podClone.PerContainerOptions.IsClone = true
+ rep, err := registry.ContainerEngine().PodClone(context.Background(), podClone)
+ if err != nil {
+ if rep != nil {
+ fmt.Printf("pod %s created but error after creation\n", rep.Id)
+ }
+ return err
+ }
+
+ fmt.Println(rep.Id)
+
+ return nil
+}
diff --git a/cmd/podman/pods/create.go b/cmd/podman/pods/create.go
index e2f80bdbc..ca9d60174 100644
--- a/cmd/podman/pods/create.go
+++ b/cmd/podman/pods/create.go
@@ -64,6 +64,7 @@ func init() {
})
flags := createCommand.Flags()
flags.SetInterspersed(false)
+ common.DefineCreateDefaults(&infraOptions)
common.DefineCreateFlags(createCommand, &infraOptions, true, false)
common.DefineNetFlags(createCommand)