Skip to content

Commit

Permalink
Separate kill and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredoconnell committed Sep 30, 2024
1 parent 04c3c73 commit 1df6222
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
18 changes: 14 additions & 4 deletions cli_plugin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package podman

import (
"fmt"
"io"

log "go.arcalot.io/log/v2"
Expand All @@ -26,19 +27,21 @@ func (p *CliPlugin) Read(b []byte) (n int, err error) {
}

func (p *CliPlugin) Close() error {
containerExists, err := p.wrapper.ContainerExists(p.containerImage)
containerExists, err := p.wrapper.ContainerRunning(p.containerImage)
if err != nil {
p.logger.Warningf("error while checking if container exists (%s);"+
" killing container in case it still exists", err.Error())
} else if containerExists {
p.logger.Infof("container %s still exists; killing container", p.containerName)
}
var killErr error
if err != nil || containerExists {
if err := p.wrapper.KillAndClean(p.containerName); err != nil {
return err
}
killErr = p.wrapper.Kill(p.containerName)
}

// Still clean up even if the kill fails.
cleanErr := p.wrapper.Clean(p.containerName)

if err := p.stdin.Close(); err != nil {
p.logger.Warningf("failed to close stdin pipe")
} else {
Expand All @@ -49,6 +52,13 @@ func (p *CliPlugin) Close() error {
} else {
p.logger.Debugf("stdout pipe successfully closed")
}
if killErr != nil && cleanErr != nil {

Check failure on line 55 in cli_plugin.go

View workflow job for this annotation

GitHub Actions / lint and test / golangci-lint

ifElseChain: rewrite if-else to switch statement (gocritic)
return fmt.Errorf("error while killing pod (%s) and cleaning up pod (%s)", killErr, cleanErr)

Check failure on line 56 in cli_plugin.go

View workflow job for this annotation

GitHub Actions / lint and test / golangci-lint

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
} else if killErr != nil {
return killErr
} else if cleanErr != nil {
return cleanErr
}
return nil
}

Expand Down
11 changes: 7 additions & 4 deletions internal/cliwrapper/cliwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func (p *cliWrapper) ImageExists(image string) (*bool, error) {
return &exists, nil
}

func (p *cliWrapper) ContainerExists(containerID string) (bool, error) {
func (p *cliWrapper) ContainerRunning(containerID string) (bool, error) {
outStr, err := p.runPodmanCmd(
"checking whether container exists",
"checking whether container is running",
"container", "ls", "--format", "{{.Names}}",
)
if err != nil {
Expand Down Expand Up @@ -95,16 +95,19 @@ func (p *cliWrapper) Deploy(image string, podmanArgs []string, containerArgs []s
return stdin, stdout, nil
}

func (p *cliWrapper) KillAndClean(containerName string) error {
func (p *cliWrapper) Kill(containerName string) error {
_, err := p.runPodmanCmd("killing container "+containerName, "kill", containerName)
if err != nil {
p.logger.Warningf("failed to kill pod %s (%s); it may have exited earlier", containerName, err.Error())
} else {
p.logger.Debugf("successfully killed container %s", containerName)
}
return nil
}

func (p *cliWrapper) Clean(containerName string) error {
msg := "removing container " + containerName
_, err = p.runPodmanCmd(msg, "rm", "--force", containerName)
_, err := p.runPodmanCmd(msg, "rm", "--force", containerName)
if err != nil {
p.logger.Errorf(err.Error())
} else {
Expand Down
5 changes: 3 additions & 2 deletions internal/cliwrapper/cliwrapper_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (

type CliWrapper interface {
ImageExists(image string) (*bool, error)
ContainerExists(image string) (bool, error)
ContainerRunning(image string) (bool, error)
PullImage(image string, platform *string) error
Deploy(
image string,
podmanArgs []string,
containerArgs []string,
) (io.WriteCloser, io.ReadCloser, error)
KillAndClean(containerName string) error
Kill(containerName string) error
Clean(containerName string) error
}

0 comments on commit 1df6222

Please sign in to comment.