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

Detect running containers to remove unnecessary error messages #81

Merged
merged 6 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
31 changes: 25 additions & 6 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,37 @@
}

func (p *CliPlugin) Close() error {
if err := p.wrapper.KillAndClean(p.containerName); err != nil {
return err
containerExists, err := p.wrapper.ContainerRunning(p.containerImage)
dbutenhof marked this conversation as resolved.
Show resolved Hide resolved
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 {
killErr = p.wrapper.Kill(p.containerName)
}

// Still clean up even if the kill fails.
dbutenhof marked this conversation as resolved.
Show resolved Hide resolved
cleanErr := p.wrapper.Clean(p.containerName)

if err := p.stdin.Close(); err != nil {
p.logger.Errorf("failed to close stdin pipe")
p.logger.Warningf("failed to close stdin pipe")
} else {
p.logger.Infof("stdin pipe successfully closed")
p.logger.Debugf("stdin pipe successfully closed")
dbutenhof marked this conversation as resolved.
Show resolved Hide resolved
}
if err := p.stdout.Close(); err != nil {
p.logger.Infof("failed to close stdout pipe")
p.logger.Warningf("failed to close stdout pipe")
} else {
p.logger.Infof("stdout pipe successfully closed")
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
29 changes: 22 additions & 7 deletions internal/cliwrapper/cliwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ func (p *cliWrapper) ImageExists(image string) (*bool, error) {
return &exists, nil
}

func (p *cliWrapper) ContainerRunning(containerID string) (bool, error) {
outStr, err := p.runPodmanCmd(
"checking whether container is running",
"container", "ls", "--format", "{{.Names}}",
)
if err != nil {
return true, err
}
outSlice := strings.Split(outStr, "\n")
exists := util.SliceContains(outSlice, containerID)
return exists, nil
}

func (p *cliWrapper) PullImage(image string, platform *string) error {
commandArgs := []string{"pull"}
if platform != nil {
Expand Down Expand Up @@ -82,21 +95,23 @@ func (p *cliWrapper) Deploy(image string, podmanArgs []string, containerArgs []s
return stdin, stdout, nil
}

func (p *cliWrapper) KillAndClean(containerName string) error {
cmdKill := p.getPodmanCmd("kill", containerName)
p.logger.Debugf("Killing with command %v", cmdKill.Args)
if err := cmdKill.Run(); err != nil {
p.logger.Warningf("failed to kill pod %s, probably the execution terminated earlier", containerName)
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())
dbutenhof marked this conversation as resolved.
Show resolved Hide resolved
} else {
p.logger.Warningf("successfully killed container %s", containerName)
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)
if err != nil {
p.logger.Errorf(err.Error())
} else {
p.logger.Infof("successfully removed container %s", containerName)
p.logger.Debugf("successfully removed container %s", containerName)
}
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion internal/cliwrapper/cliwrapper_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (

type CliWrapper interface {
ImageExists(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
}
Loading