From f659e817b4429bb5adaa935e7a6442c1f5a963ec Mon Sep 17 00:00:00 2001 From: Hironobu Saitoh Date: Sun, 4 Oct 2015 23:43:42 +0900 Subject: [PATCH] Add --openstack-ip-version option This option allows users to specify IP version. Signed-off-by: Hironobu Saitoh --- docs/drivers/openstack.md | 2 ++ drivers/openstack/client.go | 8 ++++++++ drivers/openstack/openstack.go | 10 +++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/drivers/openstack.md b/docs/drivers/openstack.md index 6b1cb75a0e..1bfcded6b6 100644 --- a/docs/drivers/openstack.md +++ b/docs/drivers/openstack.md @@ -34,6 +34,7 @@ Options: - `--openstack-floatingip-pool`: The IP pool that will be used to get a public IP can assign it to the machine. If there is an IP address already allocated but not assigned to any machine, this IP will be chosen and assigned to the machine. If there is no IP address already allocated a new IP will be allocated and assigned to the machine. + - `--openstack-ip-version`: If the instance has both IPv4 and IPv6 address, you can select IP version. If not provided `4` will be used. - `--openstack-ssh-user`: The username to use for SSH into the machine. If not provided `root` will be used. - `--openstack-ssh-port`: Customize the SSH port if the SSH server on the machine does not listen on the default port. - `--openstack-active-timeout`: The timeout in seconds until the OpenStack instance must be active. @@ -61,6 +62,7 @@ Environment variables and default values: | `--openstack-net-id` | - | - | | `--openstack-sec-groups` | - | - | | `--openstack-floatingip-pool` | - | - | +| `--openstack-ip-version` | `OS_IP_VERSION` | `4` | | `--openstack-ssh-user` | - | `root` | | `--openstack-ssh-port` | - | `22` | | `--openstack-active-timeout` | - | `200` | diff --git a/drivers/openstack/client.go b/drivers/openstack/client.go index 00421be7a4..8a99233405 100644 --- a/drivers/openstack/client.go +++ b/drivers/openstack/client.go @@ -89,6 +89,7 @@ type IpAddress struct { Network string AddressType string Address string + Version int Mac string } @@ -163,10 +164,16 @@ func (c *GenericClient) GetInstanceIpAddresses(d *Driver) ([]IpAddress, error) { for network, networkAddresses := range server.Addresses { for _, element := range networkAddresses.([]interface{}) { address := element.(map[string]interface{}) + version, ok := address["version"].(float64) + if !ok { + // Assume IPv4 if no version present. + version = 4 + } addr := IpAddress{ Network: network, Address: address["addr"].(string), + Version: int(version), } if tp, ok := address["OS-EXT-IPS:type"]; ok { @@ -179,6 +186,7 @@ func (c *GenericClient) GetInstanceIpAddresses(d *Driver) ([]IpAddress, error) { addresses = append(addresses, addr) } } + return addresses, nil } diff --git a/drivers/openstack/openstack.go b/drivers/openstack/openstack.go index e93ecfd3a3..616434b5f5 100644 --- a/drivers/openstack/openstack.go +++ b/drivers/openstack/openstack.go @@ -39,6 +39,7 @@ type Driver struct { SecurityGroups []string FloatingIpPool string FloatingIpPoolId string + IpVersion int client Client } @@ -160,6 +161,12 @@ func GetCreateFlags() []cli.Flag { Usage: "OpenStack floating IP pool to get an IP from to assign to the instance", Value: "", }, + cli.IntFlag{ + EnvVar: "OS_IP_VERSION", + Name: "openstack-ip-version", + Usage: "OpenStack version of IP address assigned for the machine", + Value: 4, + }, cli.StringFlag{ Name: "openstack-ssh-user", Usage: "OpenStack SSH user", @@ -226,6 +233,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error { d.SecurityGroups = strings.Split(flags.String("openstack-sec-groups"), ",") } d.FloatingIpPool = flags.String("openstack-floatingip-pool") + d.IpVersion = flags.Int("openstack-ip-version") d.SSHUser = flags.String("openstack-ssh-user") d.SSHPort = flags.Int("openstack-ssh-port") d.SwarmMaster = flags.Bool("swarm-master") @@ -269,7 +277,7 @@ func (d *Driver) GetIP() (string, error) { return "", err } for _, a := range addresses { - if a.AddressType == addressType { + if a.AddressType == addressType && a.Version == d.IpVersion { return a.Address, nil } }