Browse Source

Add --openstack-ip-version option

This option allows users to specify IP version.

Signed-off-by: Hironobu Saitoh <hiro@hironobu.org>
Hironobu Saitoh 3 years ago
parent
commit
59545198ab
3 changed files with 19 additions and 1 deletions
  1. 2
    0
      docs/drivers/openstack.md
  2. 8
    0
      drivers/openstack/client.go
  3. 9
    1
      drivers/openstack/openstack.go

+ 2
- 0
docs/drivers/openstack.md View File

@@ -34,6 +34,7 @@ Options:
34 34
  - `--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
35 35
    IP address already allocated but not assigned to any machine, this IP will be chosen and assigned to the machine. If
36 36
    there is no IP address already allocated a new IP will be allocated and assigned to the machine.
37
+ - `--openstack-ip-version`: If the instance has both IPv4 and IPv6 address, you can select IP version. If not provided `4` will be used.
37 38
  - `--openstack-ssh-user`: The username to use for SSH into the machine. If not provided `root` will be used.
38 39
  - `--openstack-ssh-port`: Customize the SSH port if the SSH server on the machine does not listen on the default port.
39 40
  - `--openstack-active-timeout`: The timeout in seconds until the OpenStack instance must be active.
@@ -61,6 +62,7 @@ Environment variables and default values:
61 62
 | `--openstack-net-id`             | -                      | -       |
62 63
 | `--openstack-sec-groups`         | -                      | -       |
63 64
 | `--openstack-floatingip-pool`    | -                      | -       |
65
+| `--openstack-ip-version`         | `OS_IP_VERSION`        | `4`     |
64 66
 | `--openstack-ssh-user`           | -                      | `root`  |
65 67
 | `--openstack-ssh-port`           | -                      | `22`    |
66 68
 | `--openstack-active-timeout`     | -                      | `200`   |

+ 8
- 0
drivers/openstack/client.go View File

@@ -89,6 +89,7 @@ type IpAddress struct {
89 89
 	Network     string
90 90
 	AddressType string
91 91
 	Address     string
92
+	Version     int
92 93
 	Mac         string
93 94
 }
94 95
 
@@ -163,10 +164,16 @@ func (c *GenericClient) GetInstanceIpAddresses(d *Driver) ([]IpAddress, error) {
163 164
 	for network, networkAddresses := range server.Addresses {
164 165
 		for _, element := range networkAddresses.([]interface{}) {
165 166
 			address := element.(map[string]interface{})
167
+			version, ok := address["version"].(float64)
168
+			if !ok {
169
+				// Assume IPv4 if no version present.
170
+				version = 4
171
+			}
166 172
 
167 173
 			addr := IpAddress{
168 174
 				Network: network,
169 175
 				Address: address["addr"].(string),
176
+				Version: int(version),
170 177
 			}
171 178
 
172 179
 			if tp, ok := address["OS-EXT-IPS:type"]; ok {
@@ -179,6 +186,7 @@ func (c *GenericClient) GetInstanceIpAddresses(d *Driver) ([]IpAddress, error) {
179 186
 			addresses = append(addresses, addr)
180 187
 		}
181 188
 	}
189
+
182 190
 	return addresses, nil
183 191
 }
184 192
 

+ 9
- 1
drivers/openstack/openstack.go View File

@@ -39,6 +39,7 @@ type Driver struct {
39 39
 	SecurityGroups   []string
40 40
 	FloatingIpPool   string
41 41
 	FloatingIpPoolId string
42
+	IpVersion        int
42 43
 	client           Client
43 44
 }
44 45
 
@@ -160,6 +161,12 @@ func GetCreateFlags() []cli.Flag {
160 161
 			Usage: "OpenStack floating IP pool to get an IP from to assign to the instance",
161 162
 			Value: "",
162 163
 		},
164
+		cli.IntFlag{
165
+			EnvVar: "OS_IP_VERSION",
166
+			Name:   "openstack-ip-version",
167
+			Usage:  "OpenStack version of IP address assigned for the machine",
168
+			Value:  4,
169
+		},
163 170
 		cli.StringFlag{
164 171
 			Name:  "openstack-ssh-user",
165 172
 			Usage: "OpenStack SSH user",
@@ -226,6 +233,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
226 233
 		d.SecurityGroups = strings.Split(flags.String("openstack-sec-groups"), ",")
227 234
 	}
228 235
 	d.FloatingIpPool = flags.String("openstack-floatingip-pool")
236
+	d.IpVersion = flags.Int("openstack-ip-version")
229 237
 	d.SSHUser = flags.String("openstack-ssh-user")
230 238
 	d.SSHPort = flags.Int("openstack-ssh-port")
231 239
 	d.SwarmMaster = flags.Bool("swarm-master")
@@ -269,7 +277,7 @@ func (d *Driver) GetIP() (string, error) {
269 277
 			return "", err
270 278
 		}
271 279
 		for _, a := range addresses {
272
-			if a.AddressType == addressType {
280
+			if a.AddressType == addressType && a.Version == d.IpVersion {
273 281
 				return a.Address, nil
274 282
 			}
275 283
 		}

Loading…
Cancel
Save