Skip to content

Commit

Permalink
add network in use check to compose down
Browse files Browse the repository at this point in the history
Signed-off-by: Yuruosheng <[email protected]>
  • Loading branch information
Retrospection committed Feb 25, 2023
1 parent e09d7f8 commit 3a122ea
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
40 changes: 40 additions & 0 deletions cmd/nerdctl/compose_down_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,46 @@ import (
"github.com/containerd/nerdctl/pkg/testutil"
)

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

if testutil.GetTarget() == testutil.Docker {
t.Skip("test skipped for docker")
}

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 := 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)

Expand Down
15 changes: 15 additions & 0 deletions pkg/cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ 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 {
for _, net := range v {
if net == 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
2 changes: 1 addition & 1 deletion pkg/cmd/image/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,5 @@ func Remove(ctx context.Context, client *containerd.Client, args []string, optio
}
logrus.Error(msg)
}
return nil
return err
}
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

0 comments on commit 3a122ea

Please sign in to comment.