8000 Add option '--ipv4' · docker/cli@6a2cde6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6a2cde6

Browse files
committed
Add option '--ipv4'
Signed-off-by: Rob Murray <rob.murray@docker.com>
1 parent 5c896c9 commit 6a2cde6

File tree

5 files changed

+77
-6
lines changed

5 files changed

+77
-6
lines changed

cli/command/network/create.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type createOptions struct {
2424
driverOpts opts.MapOpts
2525
labels opts.ListOpts
2626
internal bool
27+
ipv4 *bool
2728
ipv6 *bool
2829
attachable bool
2930
ingress bool
@@ -42,7 +43,7 @@ type ipamOptions struct {
4243
}
4344

4445
func newCreateCommand(dockerCLI command.Cli) *cobra.Command {
45-
var ipv6 bool
46+
var ipv4, ipv6 bool
4647
options := createOptions{
4748
driverOpts: *opts.NewMapOpts(nil, nil),
4849
labels: opts.NewListOpts(opts.ValidateLabel),
@@ -59,6 +60,9 @@ func newCreateCommand(dockerCLI command.Cli) *cobra.Command {
5960
RunE: func(cmd *cobra.Command, args []string) error {
6061
options.name = args[0]
6162

63+
if cmd.Flag("ipv4").Changed {
64+
options.ipv4 = &ipv4
65+
}
6266
if cmd.Flag("ipv6").Changed {
6367
options.ipv6 = &ipv6
6468
}
@@ -73,7 +77,8 @@ func newCreateCommand(dockerCLI command.Cli) *cobra.Command {
7377
flags.VarP(&options.driverOpts, "opt", "o", "Set driver specific options")
7478
flags.Var(&options.labels, "label", "Set metadata on a network")
7579
flags.BoolVar(&options.internal, "internal", false, "Restrict external access to the network")
76-
flags.BoolVar(&ipv6, "ipv6", false, "Enable or disable IPv6 networking")
80+
flags.BoolVar(&ipv4, "ipv4", true, "Enable or disable IPv4 address assignment")
81+
flags.BoolVar(&ipv6, "ipv6", false, "Enable or disable IPv6 address assignment")
7782
flags.BoolVar(&options.attachable, "attachable", false, "Enable manual container attachment")
7883
flags.SetAnnotation("attachable", "version", []string{"1.25"})
7984
flags.BoolVar(&options.ingress, "ingress", false, "Create swarm routing-mesh network")
@@ -113,6 +118,7 @@ func runCreate(ctx context.Context, apiClient client.NetworkAPIClient, output io
113118
Options: options.driverOpts.GetAll(),
114119
IPAM: ipamCfg,
115120
Internal: options.internal,
121+
EnableIPv4: options.ipv4,
116122
EnableIPv6: options.ipv6,
117123
Attachable: options.attachable,
118124
Ingress: options.ingress,

cli/command/network/create_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,60 @@ func TestNetworkCreateWithFlags(t *testing.T) {
182182
assert.Check(t, is.Equal("banana", strings.TrimSpace(cli.OutBuffer().String())))
183183
}
184184

185+
// TestNetworkCreateIPv4 verifies behavior of the "--ipv4" option. This option
186+
// is an optional bool, and must default to "nil", not "true" or "false".
187+
func TestNetworkCreateIPv4(t *testing.T) {
188+
boolPtr := func(val bool) *bool { return &val }
189+
190+
tests := []struct {
191+
doc, name string
192+
flags []string
193+
expected *bool
194+
}{
195+
{
196+
doc: "IPv4 default",
197+
name: "ipv4-default",
198+
expected: nil,
199+
},
200+
{
201+
doc: "IPv4 enabled",
202+
name: "ipv4-enabled",
203+
flags: []string{"--ipv4=true"},
204+
expected: boolPtr(true),
205+
},
206+
{
207+
doc: "IPv4 enabled (shorthand)",
208+
name: "ipv4-enabled-shorthand",
209+
flags: []string{"--ipv4"},
210+
expected: boolPtr(true),
211+
},
212+
{
213+
doc: "IPv4 disabled",
214+
name: "ipv4-disabled",
215+
flags: []string{"--ipv4=false"},
216+
expected: boolPtr(false),
217+
},
218+
}
219+
220+
for _, tc := range tests {
221+
t.Run(tc.doc, func(t *testing.T) {
222+
cli := test.NewFakeCli(&fakeClient{
223+
networkCreateFunc: func(ctx context.Context, name string, createBody network.CreateOptions) (network.CreateResponse, error) {
224+
assert.Check(t, is.DeepEqual(createBody.EnableIPv4, tc.expected))
225+
return network.CreateResponse{ID: name}, nil
226+
},
227+
})
228+
cmd := newCreateCommand(cli)
229+
cmd.SetArgs([]string{tc.name})
230+
if tc.expected != nil {
231+
assert.Check(t, cmd.ParseFlags(tc.flags))
232+
}
233+
assert.NilError(t, cmd.Execute())
234+
assert.Check(t, is.Equal(tc.name, st 2D08 rings.TrimSpace(cli.OutBuffer().String())))
235+
})
236+
}
237+
}
238+
185239
// TestNetworkCreateIPv6 verifies behavior of the "--ipv6" option. This option
186240
// is an optional bool, and must default to "nil", not "true" or "false".
187241
func TestNetworkCreateIPv6(t *testing.T) {

cli/command/network/formatter.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const (
1313
defaultNetworkTableFormat = "table {{.ID}}\t{{.Name}}\t{{.Driver}}\t{{.Scope}}"
1414

1515
networkIDHeader = "NETWORK ID"
16+
ipv4Header = "IPV4"
1617
ipv6Header = "IPV6"
1718
internalHeader = "INTERNAL"
1819
)
@@ -51,6 +52,7 @@ func FormatWrite(ctx formatter.Context, networks []network.Summary) error {
5152
"Name": formatter.NameHeader,
5253
"Driver": formatter.DriverHeader,
5354
"Scope": formatter.ScopeHeader,
55+
"IPv4": ipv4Header,
5456
"IPv6": ipv6Header,
5557
"Internal": internalHeader,
5658
"Labels": formatter.LabelsHeader,
@@ -88,6 +90,10 @@ func (c *networkContext) Scope() string {
8890
return c.n.Scope
8991
}
9092

93+
func (c *networkContext) IPv4() string {
94+
return strconv.FormatBool(c.n.EnableIPv4)
95+
}
96+
9197
func (c *networkContext) IPv6() string {
9298
return strconv.FormatBool(c.n.EnableIPv6)
9399
}

cli/command/network/formatter_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ func TestNetworkContext(t *testing.T) {
4242
{networkContext{
4343
n: network.Summary{Driver: "driver_name"},
4444
}, "driver_name", ctx.Driver},
45+
{networkContext{
46+
n: network.Summary{EnableIPv4: true},
47+
}, "true", ctx.IPv4},
4548
{networkContext{
4649
n: network.Summary{EnableIPv6: true},
4750
}, "true", ctx.IPv6},
@@ -180,8 +183,8 @@ func TestNetworkContextWriteJSON(t *testing.T) {
180183
{ID: "networkID2", Name: "foobar_bar"},
181184
}
182185
expectedJSONs := []map[string]any{
183-
{"Driver": "", "ID": "networkID1", "IPv6": "false", "Internal": "false", "Labels": "", "Name": "foobar_baz", "Scope": "", "CreatedAt": "0001-01-01 00:00:00 +0000 UTC"},
184-
{"Driver": "", "ID": "networkID2", "IPv6": "false", "Internal": "false", "Labels": "", "Name": "foobar_bar", "Scope": "", "CreatedAt": "0001-01-01 00:00:00 +0000 UTC"},
186+
{"Driver": "", "ID": "networkID1", "IPv4": "false", "IPv6": "false", "Internal": "false", "Labels": "", "Name": "foobar_baz", "Scope": "", "CreatedAt": "0001-01-01 00:00:00 +0000 UTC"},
187+
{"Driver": "", "ID": "networkID2", "IPv4": "false", "IPv6": "false", "Internal": "false", "Labels": "", "Name": "foobar_bar", "Scope": "", "CreatedAt": "0001-01-01 00:00:00 +0000 UTC"},
185188
}
186189

187190
out := bytes.NewBufferString("")

docs/reference/commandline/network_create.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ Create a network
1818
| `--ip-range` | `stringSlice` | | Allocate container ip from a sub-range |
1919
| `--ipam-driver` | `string` | `default` | IP Address Management Driver |
2020
| `--ipam-opt` | `map` | `map[]` | Set IPAM driver specific options |
21-
| `--ipv6` | `bool` | | Enable or disable IPv6 networking |
21+
| `--ipv4` | `bool` | `true` | Enable or disable IPv4 address assignment |
22+
| `--ipv6` | `bool` | | Enable or disable IPv6 address assignment |
2223
| `--label` | `list` | | Set metadata on a network |
2324
| `-o`, `--opt` | `map` | `map[]` | Set driver specific options |
2425
| `--scope` | `string` | | Control the network's scope |
@@ -170,7 +171,8 @@ flags used for the `docker0` bridge:
170171
| `--gateway` | - | IPv4 or IPv6 Gateway for the master subnet |
171172
| `--ip-range` | `--fixed-cidr`, `--fixed-cidr-v6` | Allocate IP addresses from a range |
172173
| `--internal` | - 5DA0 | Restrict external access to the network |
173-
| `--ipv6` | `--ipv6` | Enable or disable IPv6 networking |
174+
| `--ipv4` | - | Enable or disable IPv4 address assignment |
175+
| `--ipv6` | `--ipv6` | Enable or disable IPv6 address assignment |
174176
| `--subnet` | `--bip`, `--bip6` | Subnet for network |
175177

176178
For example, let's use `-o` or `--opt` options to specify an IP address binding

0 commit comments

Comments
 (0)
0