mirror of https://github.com/containers/podman.git
Merge pull request #16394 from vrothberg/fix-16387
remove container/pod id file along with container/pod
This commit is contained in:
commit
013bd81417
|
@ -110,6 +110,9 @@ func rm(cmd *cobra.Command, args []string) error {
|
||||||
for _, cidFile := range rmCidFiles {
|
for _, cidFile := range rmCidFiles {
|
||||||
content, err := os.ReadFile(cidFile)
|
content, err := os.ReadFile(cidFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if rmOptions.Ignore && errors.Is(err, os.ErrNotExist) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
return fmt.Errorf("reading CIDFile: %w", err)
|
return fmt.Errorf("reading CIDFile: %w", err)
|
||||||
}
|
}
|
||||||
id := strings.Split(string(content), "\n")[0]
|
id := strings.Split(string(content), "\n")[0]
|
||||||
|
|
|
@ -301,5 +301,6 @@ func replacePod(name string) error {
|
||||||
Force: true, // stop and remove pod
|
Force: true, // stop and remove pod
|
||||||
Ignore: true, // ignore if pod doesn't exist
|
Ignore: true, // ignore if pod doesn't exist
|
||||||
}
|
}
|
||||||
return removePods([]string{name}, rmOptions, false)
|
errs := removePods([]string{name}, rmOptions, false)
|
||||||
|
return errs.PrintErrors()
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/containers/common/pkg/completion"
|
"github.com/containers/common/pkg/completion"
|
||||||
|
@ -72,23 +73,38 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func rm(cmd *cobra.Command, args []string) error {
|
func rm(cmd *cobra.Command, args []string) error {
|
||||||
ids, err := specgenutil.ReadPodIDFiles(rmOptions.PodIDFiles)
|
var errs utils.OutputErrors
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
args = append(args, ids...)
|
|
||||||
if cmd.Flag("time").Changed {
|
if cmd.Flag("time").Changed {
|
||||||
if !rmOptions.Force {
|
if !rmOptions.Force {
|
||||||
return errors.New("--force option must be specified to use the --time option")
|
return errors.New("--force option must be specified to use the --time option")
|
||||||
}
|
}
|
||||||
rmOptions.Timeout = &stopTimeout
|
rmOptions.Timeout = &stopTimeout
|
||||||
}
|
}
|
||||||
return removePods(args, rmOptions.PodRmOptions, true)
|
|
||||||
|
errs = append(errs, removePods(args, rmOptions.PodRmOptions, true)...)
|
||||||
|
|
||||||
|
for _, idFile := range rmOptions.PodIDFiles {
|
||||||
|
id, err := specgenutil.ReadPodIDFile(idFile)
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
rmErrs := removePods([]string{id}, rmOptions.PodRmOptions, true)
|
||||||
|
errs = append(errs, rmErrs...)
|
||||||
|
if len(rmErrs) == 0 {
|
||||||
|
if err := os.Remove(idFile); err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return errs.PrintErrors()
|
||||||
}
|
}
|
||||||
|
|
||||||
// removePods removes the specified pods (names or IDs). Allows for sharing
|
// removePods removes the specified pods (names or IDs). Allows for sharing
|
||||||
// pod-removal logic across commands.
|
// pod-removal logic across commands.
|
||||||
func removePods(namesOrIDs []string, rmOptions entities.PodRmOptions, printIDs bool) error {
|
func removePods(namesOrIDs []string, rmOptions entities.PodRmOptions, printIDs bool) utils.OutputErrors {
|
||||||
var errs utils.OutputErrors
|
var errs utils.OutputErrors
|
||||||
|
|
||||||
responses, err := registry.ContainerEngine().PodRm(context.Background(), namesOrIDs, rmOptions)
|
responses, err := registry.ContainerEngine().PodRm(context.Background(), namesOrIDs, rmOptions)
|
||||||
|
@ -97,7 +113,7 @@ func removePods(namesOrIDs []string, rmOptions entities.PodRmOptions, printIDs b
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
setExitCode(err)
|
setExitCode(err)
|
||||||
return err
|
return append(errs, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// in the cli, first we print out all the successful attempts
|
// in the cli, first we print out all the successful attempts
|
||||||
|
@ -114,8 +130,9 @@ func removePods(namesOrIDs []string, rmOptions entities.PodRmOptions, printIDs b
|
||||||
errs = append(errs, r.Err)
|
errs = append(errs, r.Err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return errs.PrintErrors()
|
return errs
|
||||||
}
|
}
|
||||||
|
|
||||||
func setExitCode(err error) {
|
func setExitCode(err error) {
|
||||||
if errors.Is(err, define.ErrNoSuchPod) || strings.Contains(err.Error(), define.ErrNoSuchPod.Error()) {
|
if errors.Is(err, define.ErrNoSuchPod) || strings.Contains(err.Error(), define.ErrNoSuchPod.Error()) {
|
||||||
registry.SetExitCode(1)
|
registry.SetExitCode(1)
|
||||||
|
|
|
@ -4,4 +4,4 @@
|
||||||
####> are applicable to all of those.
|
####> are applicable to all of those.
|
||||||
#### **--cidfile**=*file*
|
#### **--cidfile**=*file*
|
||||||
|
|
||||||
Write the container ID to *file*.
|
Write the container ID to *file*. The file will be removed along with the container.
|
||||||
|
|
|
@ -26,6 +26,7 @@ Stop running containers and delete all stopped containers before removal of pod.
|
||||||
Instead of providing the pod name or ID, remove the last created pod. (This option is not available with the remote Podman client, including Mac and Windows (excluding WSL2) machines)
|
Instead of providing the pod name or ID, remove the last created pod. (This option is not available with the remote Podman client, including Mac and Windows (excluding WSL2) machines)
|
||||||
|
|
||||||
@@option pod-id-file.pod
|
@@option pod-id-file.pod
|
||||||
|
If specified, the pod-id-file will be removed along with the pod.
|
||||||
|
|
||||||
@@option time
|
@@option time
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,8 @@ In addition, forcing can be used to remove unusable containers, e.g. containers
|
||||||
whose OCI runtime has become unavailable.
|
whose OCI runtime has become unavailable.
|
||||||
|
|
||||||
@@option ignore
|
@@option ignore
|
||||||
|
Further ignore when the specified `--cidfile` does not exist as it may have
|
||||||
|
already been removed along with the container.
|
||||||
|
|
||||||
#### **--latest**, **-l**
|
#### **--latest**, **-l**
|
||||||
|
|
||||||
|
|
|
@ -777,6 +777,16 @@ func (r *Runtime) removeContainer(ctx context.Context, c *Container, force, remo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove the container's CID file on container removal.
|
||||||
|
if cidFile, ok := c.config.Spec.Annotations[define.InspectAnnotationCIDFile]; ok {
|
||||||
|
if err := os.Remove(cidFile); err != nil {
|
||||||
|
if cleanupErr == nil {
|
||||||
|
cleanupErr = err
|
||||||
|
} else {
|
||||||
|
logrus.Errorf("Cleaning up CID file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// Remove the container from the state
|
// Remove the container from the state
|
||||||
if c.config.Pod != "" {
|
if c.config.Pod != "" {
|
||||||
// If we're removing the pod, the container will be evicted
|
// If we're removing the pod, the container will be evicted
|
||||||
|
|
|
@ -222,6 +222,9 @@ echo $rand | 0 | $rand
|
||||||
|
|
||||||
# All OK. Kill container.
|
# All OK. Kill container.
|
||||||
run_podman rm -f $cid
|
run_podman rm -f $cid
|
||||||
|
if [[ -e $cidfile ]]; then
|
||||||
|
die "cidfile $cidfile should be removed along with container"
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "podman run docker-archive" {
|
@test "podman run docker-archive" {
|
||||||
|
|
|
@ -314,7 +314,10 @@ EOF
|
||||||
|
|
||||||
# Clean up
|
# Clean up
|
||||||
run_podman rm $cid
|
run_podman rm $cid
|
||||||
run_podman pod rm -t 0 -f mypod
|
run_podman pod rm -t 0 -f --pod-id-file $pod_id_file
|
||||||
|
if [[ -e $pod_id_file ]]; then
|
||||||
|
die "pod-id-file $pod_id_file should be removed along with pod"
|
||||||
|
fi
|
||||||
run_podman rmi $infra_image
|
run_podman rmi $infra_image
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue