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

feat: add network in use check to compose down #2051

Merged
merged 1 commit into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 40 additions & 1 deletion cmd/nerdctl/compose_down_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import (
"github.com/containerd/nerdctl/pkg/testutil"
)

func TestComposeDownRemoveOrphans(t *testing.T) {
func TestComposeDownRemoveUsedNetwork(t *testing.T) {
base := testutil.NewBase(t)

// The error output is different with docker
testutil.DockerIncompatible(t)

var (
dockerComposeYAMLOrphan = fmt.Sprintf(`
version: '3.1'
Expand All @@ -53,6 +56,42 @@ services:
projectName := fmt.Sprintf("nerdctl-compose-test-%d", time.Now().Unix())
t.Logf("projectName=%q", projectName)

base.ComposeCmd("-p", projectName, "-f", compFull.YAMLFullPath(), "up", "-d").AssertOK()
defer base.ComposeCmd("-p", projectName, "-f", compFull.YAMLFullPath(), "down", "--remove-orphans").AssertOK()

base.ComposeCmd("-p", projectName, "-f", compOrphan.YAMLFullPath(), "down", "-v").AssertCombinedOutContains("is in use")

}

func TestComposeDownRemoveOrphans(t *testing.T) {
base := testutil.NewBase(t)

var (
dockerComposeYAMLOrphan = fmt.Sprintf(`
version: '3.1'
services:
test:
image: %s
command: "sleep infinity"
`, testutil.AlpineImage)

dockerComposeYAMLFull = fmt.Sprintf(`
%s
orphan:
image: %s
command: "sleep infinity"
`, dockerComposeYAMLOrphan, testutil.AlpineImage)
)

compOrphan := testutil.NewComposeDir(t, dockerComposeYAMLOrphan)
defer compOrphan.CleanUp()
compFull := testutil.NewComposeDir(t, dockerComposeYAMLFull)
defer compFull.CleanUp()

projectName := compFull.ProjectName()
t.Logf("projectName=%q", projectName)

orphanContainer := fmt.Sprintf("%s_orphan_1", projectName)

base.ComposeCmd("-p", projectName, "-f", compFull.YAMLFullPath(), "up", "-d").AssertOK()
Expand Down
14 changes: 14 additions & 0 deletions pkg/cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/containerd/nerdctl/pkg/ipfs"
"github.com/containerd/nerdctl/pkg/netutil"
"github.com/containerd/nerdctl/pkg/referenceutil"
"github.com/containerd/nerdctl/pkg/strutil"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/sirupsen/logrus"
)
Expand All @@ -58,6 +59,19 @@ func New(client *containerd.Client, globalOptions types.GlobalCommandOptions, op
return false, nil
}

options.NetworkInUse = func(ctx context.Context, netName string) (bool, error) {
networkUsedByNsMap, err := netutil.UsedNetworks(ctx, client)
if err != nil {
return false, err
}
for _, v := range networkUsedByNsMap {
if strutil.InStringSlice(v, netName) {
return true, nil
}
}
return false, nil
}

volStore, err := volume.Store(globalOptions.Namespace, globalOptions.DataRoot, globalOptions.Address)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions pkg/composer/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Options struct {
EnvFile string
NerdctlCmd string
NerdctlArgs []string
NetworkInUse func(ctx context.Context, netName string) (bool, error)
NetworkExists func(string) (bool, error)
VolumeExists func(string) (bool, error)
ImageExists func(ctx context.Context, imageName string) (bool, error)
Expand Down
8 changes: 8 additions & 0 deletions pkg/composer/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ func (c *Composer) downNetwork(ctx context.Context, shortName string) error {
if err != nil {
return err
} else if netExists {
netUsed, err := c.NetworkInUse(ctx, fullName)
if err != nil {
return err
}
if netUsed {
return fmt.Errorf("network %s is in use", fullName)
}

logrus.Infof("Removing network %s", fullName)
if err := c.runNerdctlCmd(ctx, "network", "rm", fullName); err != nil {
logrus.Warn(err)
Expand Down