Skip to content

Commit

Permalink
feat: add network in use check to compose down
Browse files Browse the repository at this point in the history
Signed-off-by: Retros <[email protected]>
  • Loading branch information
Retrospection committed Jan 28, 2023
1 parent b0701ba commit eb44739
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
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) {
usedMap, err := netutil.UsedNetworks(ctx, client)
if err != nil {
return false, err
}
for _, v := range usedMap {
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
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
11 changes: 11 additions & 0 deletions pkg/composer/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ func (c *Composer) downNetwork(ctx context.Context, shortName string) error {
netExists, err := c.NetworkExists(fullName)
if err != nil {
return err
}

if netExists {
netUsed, err := c.NetworkInUse(ctx, fullName)
if err != nil {
return err
}

if netUsed {
logrus.Errorf("network %s is in use", fullName)
}
} else if netExists {
logrus.Infof("Removing network %s", fullName)
if err := c.runNerdctlCmd(ctx, "network", "rm", fullName); err != nil {
Expand Down

0 comments on commit eb44739

Please sign in to comment.