Skip to content

Commit

Permalink
Cleanup using labels instead of name prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Aug 9, 2024
1 parent d14a609 commit ee1fed7
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions dockerutil/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,36 @@ package dockerutil

import (
"context"
"os"
"strings"

dockerapitypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
)

// KillAllInterchaintestContainers kills all containers that are prefixed with interchaintest specific container names.
// This can be called during cleanup, which is especially useful when running tests which fail to cleanup after themselves.
// Specifically, on failed ic.Build(...) calls.
func KillAllInterchaintestContainers(ctx context.Context) []string {
if os.Getenv("KEEP_CONTAINERS") != "" {
return nil
}

removedContainers := []string{}

cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}

containers, err := cli.ContainerList(ctx, dockerapitypes.ContainerListOptions{})
containers, err := cli.ContainerList(ctx, dockerapitypes.ContainerListOptions{
// grabs only running containers
All: false,
Filters: filters.NewArgs(
filters.Arg("label", CleanupLabel),
),
})
if err != nil {
panic(err)
}
Expand All @@ -32,11 +44,6 @@ func KillAllInterchaintestContainers(ctx context.Context) []string {
name := strings.ToLower(container.Names[0])
name = strings.TrimPrefix(name, "/")

// leave non ict relayed containers running
if !(strings.HasPrefix(name, ICTDockerPrefix) || strings.HasPrefix(name, RelayerDockerPrefix)) {
continue
}

inspected, err := cli.ContainerInspect(ctx, container.ID)
if err != nil {
panic(err)
Expand Down

0 comments on commit ee1fed7

Please sign in to comment.