Merge pull request #13943 from cdoern/clone

podman container clone -f
This commit is contained in:
OpenShift Merge Robot 2022-04-22 06:24:52 -04:00 committed by GitHub
commit 26a51b2900
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 2 deletions

View File

@ -38,6 +38,9 @@ func cloneFlags(cmd *cobra.Command) {
runFlagName := "run"
flags.BoolVar(&ctrClone.Run, runFlagName, false, "run the new container")
forceFlagName := "force"
flags.BoolVarP(&ctrClone.Force, forceFlagName, "f", false, "force the existing container to be destroyed")
common.DefineCreateFlags(cmd, &ctrClone.CreateOpts, false, true)
}
func init() {
@ -52,7 +55,7 @@ func init() {
func clone(cmd *cobra.Command, args []string) error {
switch len(args) {
case 0:
return errors.Wrapf(define.ErrInvalidArg, "Must Specify at least 1 argument")
return errors.Wrapf(define.ErrInvalidArg, "must specify at least 1 argument")
case 2:
ctrClone.CreateOpts.Name = args[1]
case 3:
@ -68,6 +71,10 @@ func clone(cmd *cobra.Command, args []string) error {
ctrClone.RawImageName = rawImageName
}
}
if ctrClone.Force && !ctrClone.Destroy {
return errors.Wrapf(define.ErrInvalidArg, "cannot set --force without --destroy")
}
ctrClone.ID = args[0]
ctrClone.CreateOpts.IsClone = true
rep, err := registry.ContainerEngine().ContainerClone(registry.GetContext(), ctrClone)

View File

@ -125,6 +125,10 @@ If none are specified, the original container's CPU memory nodes are used.
Remove the original container that we are cloning once used to mimic the configuration.
#### **--force**, **-f**
Force removal of the original container that we are cloning. Can only be used in conjunction with **--destroy**.
#### **--memory**, **-m**=*limit*
Memory limit (format: `<number>[<unit>]`, where unit = b (bytes), k (kilobytes), m (megabytes), or g (gigabytes))

View File

@ -477,4 +477,5 @@ type ContainerCloneOptions struct {
Image string
RawImageName string
Run bool
Force bool
}

View File

@ -1630,7 +1630,7 @@ func (ic *ContainerEngine) ContainerClone(ctx context.Context, ctrCloneOpts enti
if ctrCloneOpts.Destroy {
var time *uint
err := ic.Libpod.RemoveContainer(context.Background(), c, false, false, time)
err = ic.Libpod.RemoveContainer(context.Background(), c, ctrCloneOpts.Force, false, time)
if err != nil {
return nil, err
}

View File

@ -235,4 +235,36 @@ var _ = Describe("Podman container clone", func() {
Expect(ctrInspect.InspectContainerToJSON()[0].HostConfig.NetworkMode).Should(ContainSubstring("container:"))
})
It("podman container clone --destroy --force test", func() {
create := podmanTest.Podman([]string{"create", ALPINE})
create.WaitWithDefaultTimeout()
Expect(create).To(Exit(0))
clone := podmanTest.Podman([]string{"container", "clone", "--destroy", create.OutputToString()})
clone.WaitWithDefaultTimeout()
Expect(clone).To(Exit(0))
inspect := podmanTest.Podman([]string{"inspect", create.OutputToString()})
inspect.WaitWithDefaultTimeout()
Expect(inspect).ToNot(Exit(0))
run := podmanTest.Podman([]string{"run", "-dt", ALPINE})
run.WaitWithDefaultTimeout()
Expect(run).To(Exit(0))
clone = podmanTest.Podman([]string{"container", "clone", "--destroy", "-f", run.OutputToString()})
clone.WaitWithDefaultTimeout()
Expect(clone).To(Exit(0))
inspect = podmanTest.Podman([]string{"inspect", run.OutputToString()})
inspect.WaitWithDefaultTimeout()
Expect(inspect).ToNot(Exit(0))
run = podmanTest.Podman([]string{"run", "-dt", ALPINE})
run.WaitWithDefaultTimeout()
Expect(run).To(Exit(0))
clone = podmanTest.Podman([]string{"container", "clone", "-f", run.OutputToString()})
clone.WaitWithDefaultTimeout()
Expect(clone).ToNot(Exit(0))
})
})