Skip to content

Commit

Permalink
added clean of zombie dind containers at the startup of the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Bianco95 committed Aug 27, 2024
1 parent b8be319 commit 8f13315
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func main() {
if err != nil {
log.G(Ctx).Fatal(err)
}
dindHandler.CleanDindContainers()
dindHandler.BuildDindContainers(int8(availableDindsInt))

SidecarAPIs := docker.SidecarHandler{
Expand Down
47 changes: 47 additions & 0 deletions pkg/docker/dindmanager/DindHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

type DindManagerInterface interface {
CleanDindContainers() error
BuildDindContainers(nDindContainer int8) error
PrintDindList() error
GetAvailableDind() (string, error)
Expand Down Expand Up @@ -51,6 +52,52 @@ func GenerateUUIDv4() (string, error) {
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:16]), nil
}

func (a *DindManager) CleanDindContainers() error {

// print the number of DIND containers to be created
log.G(a.Ctx).Info(fmt.Sprintf("\u2705 Start cleaning zombie DIND containers"))

// exec this command docker ps -a --format "{{.Names}}" | grep '_dind$' | wc -l
shell := exec.ExecTask{
Command: "docker",
Args: []string{"ps", "-a", "--format", "{{.Names}}", "|", "grep", "_dind$", "|", "wc", "-l"},
Shell: true,
}
execReturn, err := shell.Execute()
if err != nil {
return err
}

// log the number of zombie DIND containers (remove the \n at the end of the string)
log.G(a.Ctx).Info(fmt.Sprintf("\u2705 %s zombie DIND containers found", strings.ReplaceAll(execReturn.Stdout, "\n", "")))

shell = exec.ExecTask{
Command: "docker",
Args: []string{"ps", "-a", "--format", "{{.Names}}", "|", "grep", "_dind$", "|", "xargs", "-I", "{}", "docker", "rm", "-f", "{}"},
Shell: true,
}
_, err = shell.Execute()
if err != nil {
return err
}

// exec this command docker network ls --filter name=_dind_network$ --format "{{.ID}}" | xargs -r docker network rm

shell = exec.ExecTask{
Command: "docker",
Args: []string{"network", "ls", "--filter", "name=_dind_network$", "--format", "{{.ID}}", "|", "xargs", "-r", "docker", "network", "rm"},
Shell: true,
}
_, err = shell.Execute()
if err != nil {
return err
}

log.G(a.Ctx).Info(fmt.Sprintf("\u2705 DIND zombie containers cleaned"))

return nil
}

func (a *DindManager) BuildDindContainers(nDindContainer int8) error {

// print the number of DIND containers to be created
Expand Down

0 comments on commit 8f13315

Please sign in to comment.