Merge pull request #19436 from rhatdan/remote

Cleanup CIDFile on podman-remote run --rm command
This commit is contained in:
OpenShift Merge Robot 2023-08-02 12:00:27 +02:00 committed by GitHub
commit 2252ed1241
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 13 deletions

View File

@ -4,4 +4,5 @@
####> are applicable to all of those.
#### **--cidfile**=*file*
Write the container ID to *file*. The file is removed along with the container.
Write the container ID to *file*. The file is removed along with the container, except
when used with podman --remote run on detached containers.

View File

@ -70,6 +70,7 @@ Valid placeholders for the Go template are listed below:
| **Placeholder** | **Description** |
|--------------------|----------------------------------------------|
| .AutoRemove | If true, containers are removed on exit |
| .CIDFile | Container ID File |
| .Command | Quoted command used |
| .Created | Creation time for container, Y-M-D H:M:S |
| .CreatedAt | Creation time for container (same as above) |

View File

@ -20,6 +20,8 @@ type ListContainer struct {
Created time.Time
// Human-readable container creation time.
CreatedAt string
// CIDFile specified at creation time.
CIDFile string
// If container has exited/stopped
Exited bool
// Time container exited

View File

@ -693,7 +693,12 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
}
}
removeOptions := new(containers.RemoveOptions).WithVolumes(true).WithForce(false)
removeContainer := func(id string) {
removeContainer := func(id, CIDFile string) {
if CIDFile != "" {
if err := os.Remove(CIDFile); err != nil && !errors.Is(err, os.ErrNotExist) {
logrus.Warnf("Cleaning up CID file: %s", err)
}
}
reports, err := containers.Remove(ic.ClientCtx, id, removeOptions)
logIfRmError(id, err, reports)
}
@ -722,7 +727,7 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
if err != nil {
if ctr.AutoRemove {
removeContainer(ctr.ID)
removeContainer(ctr.ID, ctr.CIDFile)
}
report.ExitCode = define.ExitCode(report.Err)
report.Err = err
@ -741,7 +746,7 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
logrus.Errorf("Should restart: %v", shouldRestart)
if !shouldRestart && ctr.AutoRemove {
removeContainer(ctr.ID)
removeContainer(ctr.ID, ctr.CIDFile)
}
}()
}
@ -827,7 +832,13 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta
for _, w := range con.Warnings {
fmt.Fprintf(os.Stderr, "%s\n", w)
}
removeContainer := func(id string, force bool) error {
removeContainer := func(id, CIDFile string, force bool) error {
if CIDFile != "" {
if err := os.Remove(CIDFile); err != nil && !errors.Is(err, os.ErrNotExist) {
logrus.Warnf("Cleaning up CID file: %s", err)
}
}
removeOptions := new(containers.RemoveOptions).WithVolumes(true).WithForce(force)
reports, err := containers.Remove(ic.ClientCtx, id, removeOptions)
logIfRmError(id, err, reports)
@ -837,7 +848,7 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta
if opts.CIDFile != "" {
if err := util.CreateIDFile(opts.CIDFile, con.ID); err != nil {
// If you fail to create CIDFile then remove the container
_ = removeContainer(con.ID, true)
_ = removeContainer(con.ID, opts.CIDFile, true)
return nil, err
}
}
@ -850,9 +861,7 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta
if err != nil {
report.ExitCode = define.ExitCode(err)
if opts.Rm {
if rmErr := removeContainer(con.ID, true); rmErr != nil && !errors.Is(rmErr, define.ErrNoSuchCtr) {
logrus.Errorf("Container %s failed to be removed", con.ID)
}
_ = removeContainer(con.ID, opts.CIDFile, true)
}
}
return &report, err
@ -873,7 +882,7 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta
report.ExitCode = define.ExitCode(err)
if opts.Rm {
_ = removeContainer(con.ID, false)
_ = removeContainer(con.ID, opts.CIDFile, false)
}
return &report, err
}
@ -889,7 +898,7 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta
}
if !shouldRestart {
_ = removeContainer(con.ID, false)
_ = removeContainer(con.ID, opts.CIDFile, false)
}
}()
}

View File

@ -237,10 +237,11 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
ps := entities.ListContainer{
AutoRemove: ctr.AutoRemove(),
CIDFile: conConfig.Spec.Annotations[define.InspectAnnotationCIDFile],
Command: conConfig.Command,
Created: conConfig.CreatedTime,
Exited: exited,
ExitCode: exitCode,
Exited: exited,
ExitedAt: exitedTime.Unix(),
ID: conConfig.ID,
Image: conConfig.RootfsImageName,
@ -253,11 +254,11 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
Pid: pid,
Pod: conConfig.Pod,
Ports: portMappings,
Restarts: restartCount,
Size: size,
StartedAt: startedTime.Unix(),
State: conState.String(),
Status: healthStatus,
Restarts: restartCount,
}
if opts.Pod && len(conConfig.Pod) > 0 {
podName, err := rt.GetPodName(conConfig.Pod)