mirror of
https://github.com/containers/podman.git
synced 2025-05-17 23:26:08 +08:00
Merge pull request #18917 from Luap99/ip-range
network create --ip-range allow for custom range
This commit is contained in:
@ -245,6 +245,23 @@ func parseRoute(routeStr string) (*types.Route, error) {
|
||||
}
|
||||
|
||||
func parseRange(iprange string) (*types.LeaseRange, error) {
|
||||
split := strings.SplitN(iprange, "-", 2)
|
||||
if len(split) > 1 {
|
||||
// range contains dash so assume form is start-end
|
||||
start := net.ParseIP(split[0])
|
||||
if start == nil {
|
||||
return nil, fmt.Errorf("range start ip %q is not a ip address", split[0])
|
||||
}
|
||||
end := net.ParseIP(split[1])
|
||||
if end == nil {
|
||||
return nil, fmt.Errorf("range end ip %q is not a ip address", split[1])
|
||||
}
|
||||
return &types.LeaseRange{
|
||||
StartIP: start,
|
||||
EndIP: end,
|
||||
}, nil
|
||||
}
|
||||
// no dash, so assume CIDR is given
|
||||
_, subnet, err := net.ParseCIDR(iprange)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -66,8 +66,9 @@ Restrict external access of this network. Note when using this option, the dnsna
|
||||
|
||||
#### **--ip-range**=*range*
|
||||
|
||||
Allocate container IP from a range. The range must be a complete subnet and in CIDR notation. The *ip-range* option
|
||||
must be used with a *subnet* option. Can be specified multiple times.
|
||||
Allocate container IP from a range. The range must be a either a complete subnet in CIDR notation or be in
|
||||
the `<startIP>-<endIP>` syntax which allows for a more flexible range compared to the CIDR subnet.
|
||||
The *ip-range* option must be used with a *subnet* option. Can be specified multiple times.
|
||||
The argument order of the **--subnet**, **--gateway** and **--ip-range** options must match.
|
||||
|
||||
#### **--ipam-driver**=*driver*
|
||||
|
@ -680,7 +680,9 @@ This is equivalent to the Podman `--ipam-driver` option
|
||||
|
||||
### `IPRange=`
|
||||
|
||||
Allocate container IP from a range. The range must be a complete subnet and in CIDR notation. The ip-range option must be used with a subnet option.
|
||||
Allocate container IP from a range. The range must be a either a complete subnet in CIDR notation or be
|
||||
in the `<startIP>-<endIP>` syntax which allows for a more flexible range compared to the CIDR subnet.
|
||||
The ip-range option must be used with a subnet option.
|
||||
|
||||
This is equivalent to the Podman `--ip-range` option
|
||||
|
||||
|
@ -696,4 +696,41 @@ var _ = Describe("Podman network create", func() {
|
||||
Expect(bridge.Name).To(Equal(bridgeName))
|
||||
}
|
||||
})
|
||||
|
||||
It("podman network create --ip-range sip-eip", func() {
|
||||
netName := "subnet-" + stringid.GenerateRandomID()
|
||||
nc := podmanTest.Podman([]string{"network", "create", "--subnet", "10.11.16.0/24", "--ip-range", "10.11.16.11-10.11.16.12", netName})
|
||||
nc.WaitWithDefaultTimeout()
|
||||
defer podmanTest.removeNetwork(netName)
|
||||
Expect(nc).Should(Exit(0))
|
||||
|
||||
// Inspect the network configuration
|
||||
inspect := podmanTest.Podman([]string{"network", "inspect", netName})
|
||||
inspect.WaitWithDefaultTimeout()
|
||||
Expect(inspect).Should(Exit(0))
|
||||
|
||||
// JSON the network configuration into something usable
|
||||
var results []types.Network
|
||||
err := json.Unmarshal([]byte(inspect.OutputToString()), &results)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(results).To(HaveLen(1))
|
||||
result := results[0]
|
||||
Expect(result).To(HaveField("Name", netName))
|
||||
Expect(result.Subnets).To(HaveLen(1))
|
||||
Expect(result.Subnets[0].Subnet.String()).To(Equal("10.11.16.0/24"))
|
||||
Expect(result.Subnets[0].Gateway.String()).To(Equal("10.11.16.1"))
|
||||
Expect(result.Subnets[0].LeaseRange).ToNot(BeNil())
|
||||
Expect(result.Subnets[0].LeaseRange.StartIP.String()).To(Equal("10.11.16.11"))
|
||||
Expect(result.Subnets[0].LeaseRange.EndIP.String()).To(Equal("10.11.16.12"))
|
||||
|
||||
try := podmanTest.Podman([]string{"run", "--rm", "--network", netName, ALPINE, "sh", "-c", "ip addr show eth0 | awk ' /inet / {print $2}'"})
|
||||
try.WaitWithDefaultTimeout()
|
||||
Expect(try).To(Exit(0))
|
||||
|
||||
containerIP, _, err := net.ParseCIDR(try.OutputToString())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
// Note as of today (June 2023) we always get the first ip from netavark and cni but let's not depend on that.
|
||||
// All we care about is the ip is from the range which allows for both.
|
||||
Expect(containerIP.String()).To(Or(Equal("10.11.16.11"), Equal("10.11.16.12")), "ip address must be in --ip-range")
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user