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 2 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
21 changes: 15 additions & 6 deletions cli_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,28 @@ func (p *CliPlugin) Read(b []byte) (n int, err error) {
}

func (p *CliPlugin) Close() error {
if err := p.wrapper.KillAndClean(p.containerName); err != nil {
return err
containerExists, err := p.wrapper.ContainerExists(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)
}
if err != nil || containerExists {
if err := p.wrapper.KillAndClean(p.containerName); err != nil {
return err
}
}

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")
}
return nil
}
Expand Down
26 changes: 19 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) ContainerExists(containerID string) (bool, error) {
dbutenhof marked this conversation as resolved.
Show resolved Hide resolved
outStr, err := p.runPodmanCmd(
"checking whether container exists",
"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 @@ -83,20 +96,19 @@ func (p *cliWrapper) Deploy(image string, podmanArgs []string, containerArgs []s
}

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)
_, 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)
}

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 {
p.logger.Infof("successfully removed container %s", containerName)
p.logger.Debugf("successfully removed container %s", containerName)
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions internal/cliwrapper/cliwrapper_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type CliWrapper interface {
ImageExists(image string) (*bool, error)
ContainerExists(image string) (bool, error)
dbutenhof marked this conversation as resolved.
Show resolved Hide resolved
PullImage(image string, platform *string) error
Deploy(
image string,
Expand Down
Loading