2016-09-08 13:11:39 -04:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2024-05-31 11:28:10 -04:00
|
|
|
"github.com/docker/docker/api/types/network"
|
2022-04-08 22:43:28 -04:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2022-04-08 22:43:28 -04:00
|
|
|
type removeOptions struct {
|
|
|
|
force bool
|
|
|
|
}
|
|
|
|
|
2017-06-21 05:25:25 -04:00
|
|
|
func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
|
2022-04-08 22:43:28 -04:00
|
|
|
var opts removeOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-09-08 13:11:39 -04:00
|
|
|
Use: "rm NETWORK [NETWORK...]",
|
|
|
|
Aliases: []string{"remove"},
|
|
|
|
Short: "Remove one or more networks",
|
|
|
|
Args: cli.RequiresMinArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-09-09 18:27:44 -04:00
|
|
|
return runRemove(cmd.Context(), dockerCli, args, &opts)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: completion.NetworkNames(dockerCli),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2022-04-08 22:43:28 -04:00
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&opts.force, "force", "f", false, "Do not error if the network does not exist")
|
|
|
|
return cmd
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-03-09 14:52:25 -05:00
|
|
|
const ingressWarning = "WARNING! Before removing the routing-mesh network, " +
|
|
|
|
"make sure all the nodes in your swarm run the same docker engine version. " +
|
|
|
|
"Otherwise, removal may not be effective and functionality of newly create " +
|
|
|
|
"ingress networks will be impaired.\nAre you sure you want to continue?"
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
func runRemove(ctx context.Context, dockerCli command.Cli, networks []string, opts *removeOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
2023-09-09 18:27:44 -04:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
status := 0
|
|
|
|
|
|
|
|
for _, name := range networks {
|
2024-05-31 11:28:10 -04:00
|
|
|
nw, _, err := client.NetworkInspectWithRaw(ctx, name, network.InspectOptions{})
|
2024-02-21 10:36:17 -05:00
|
|
|
if err == nil && nw.Ingress {
|
|
|
|
r, err := command.PromptForConfirmation(ctx, dockerCli.In(), dockerCli.Out(), ingressWarning)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !r {
|
|
|
|
continue
|
|
|
|
}
|
2017-03-09 14:52:25 -05:00
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
if err := client.NetworkRemove(ctx, name); err != nil {
|
2022-04-08 22:43:28 -04:00
|
|
|
if opts.force && errdefs.IsNotFound(err) {
|
|
|
|
continue
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
fmt.Fprintf(dockerCli.Err(), "%s\n", err)
|
|
|
|
status = 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s\n", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != 0 {
|
|
|
|
return cli.StatusError{StatusCode: status}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|