Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove container/pod id file along with container/pod #16394

Merged
merged 1 commit into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/podman/containers/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ func rm(cmd *cobra.Command, args []string) error {
for _, cidFile := range rmCidFiles {
content, err := os.ReadFile(cidFile)
if err != nil {
if rmOptions.Ignore && errors.Is(err, os.ErrNotExist) {
continue
}
return fmt.Errorf("reading CIDFile: %w", err)
}
id := strings.Split(string(content), "\n")[0]
Expand Down
3 changes: 2 additions & 1 deletion cmd/podman/pods/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,5 +301,6 @@ func replacePod(name string) error {
Force: true, // stop and remove pod
Ignore: true, // ignore if pod doesn't exist
}
return removePods([]string{name}, rmOptions, false)
errs := removePods([]string{name}, rmOptions, false)
return errs.PrintErrors()
}
35 changes: 26 additions & 9 deletions cmd/podman/pods/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"os"
"strings"

"github.com/containers/common/pkg/completion"
Expand Down Expand Up @@ -72,23 +73,38 @@ func init() {
}

func rm(cmd *cobra.Command, args []string) error {
ids, err := specgenutil.ReadPodIDFiles(rmOptions.PodIDFiles)
if err != nil {
return err
}
args = append(args, ids...)
var errs utils.OutputErrors

if cmd.Flag("time").Changed {
if !rmOptions.Force {
return errors.New("--force option must be specified to use the --time option")
}
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
// 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

responses, err := registry.ContainerEngine().PodRm(context.Background(), namesOrIDs, rmOptions)
Expand All @@ -97,7 +113,7 @@ func removePods(namesOrIDs []string, rmOptions entities.PodRmOptions, printIDs b
return nil
}
setExitCode(err)
return err
return append(errs, err)
}

// in the cli, first we print out all the successful attempts
Expand All @@ -114,8 +130,9 @@ func removePods(namesOrIDs []string, rmOptions entities.PodRmOptions, printIDs b
errs = append(errs, r.Err)
}
}
return errs.PrintErrors()
return errs
}

func setExitCode(err error) {
if errors.Is(err, define.ErrNoSuchPod) || strings.Contains(err.Error(), define.ErrNoSuchPod.Error()) {
registry.SetExitCode(1)
Expand Down
2 changes: 1 addition & 1 deletion docs/source/markdown/options/cidfile.write.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
####> are applicable to all of those.
#### **--cidfile**=*file*

Write the container ID to *file*.
Write the container ID to *file*. The file will be removed along with the container.
1 change: 1 addition & 0 deletions docs/source/markdown/podman-pod-rm.1.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -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)

@@option pod-id-file.pod
If specified, the pod-id-file will be removed along with the pod.

@@option time

Expand Down
2 changes: 2 additions & 0 deletions docs/source/markdown/podman-rm.1.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ In addition, forcing can be used to remove unusable containers, e.g. containers
whose OCI runtime has become unavailable.

@@option ignore
Further ignore when the specified `--cidfile` does not exist as it may have
already been removed along with the container.

#### **--latest**, **-l**

Expand Down
10 changes: 10 additions & 0 deletions libpod/runtime_ctr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
if c.config.Pod != "" {
// If we're removing the pod, the container will be evicted
Expand Down
3 changes: 3 additions & 0 deletions test/system/030-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ echo $rand | 0 | $rand

# All OK. Kill container.
run_podman rm -f $cid
if [[ -e $cidfile ]]; then
die "cidfile $cidfile should be removed along with container"
fi
}

@test "podman run docker-archive" {
Expand Down
5 changes: 4 additions & 1 deletion test/system/200-pod.bats
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ EOF

# Clean up
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
}

Expand Down