remove container/pod id file along with container/pod

Remove the container/pod ID file along with the container/pod.  It's
primarily used in the context of systemd and are not useful nor needed
once a container/pod has ceased to exist.

Fixes: #16387
Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2022-11-03 13:16:16 +01:00
parent 6428ff180a
commit 3fee351c35
9 changed files with 52 additions and 12 deletions

View File

@ -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]

View File

@ -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()
}

View File

@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"os"
"strings"
"github.com/containers/common/pkg/completion"
@ -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)
@ -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
@ -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)