Browse Source

Merge pull request #850 from dudymas/feat/openstack-nova-ip-pools

feature to fix #737 - openstack driver can use nova to allocate floating ips
Nathan LeClaire 3 years ago
parent
commit
15d213dcd7
2 changed files with 77 additions and 9 deletions
  1. 62
    3
      drivers/openstack/client.go
  2. 15
    6
      drivers/openstack/openstack.go

+ 62
- 3
drivers/openstack/client.go View File

@@ -11,6 +11,7 @@ import (
11 11
 	"github.com/docker/machine/libmachine/version"
12 12
 	"github.com/rackspace/gophercloud"
13 13
 	"github.com/rackspace/gophercloud/openstack"
14
+	compute_ips "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/floatingip"
14 15
 	"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
15 16
 	"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/startstop"
16 17
 	"github.com/rackspace/gophercloud/openstack/compute/v2/flavors"
@@ -40,7 +41,7 @@ type Client interface {
40 41
 	GetNetworkId(d *Driver) (string, error)
41 42
 	GetFlavorId(d *Driver) (string, error)
42 43
 	GetImageId(d *Driver) (string, error)
43
-	AssignFloatingIP(d *Driver, floatingIp *FloatingIp, portId string) error
44
+	AssignFloatingIP(d *Driver, floatingIp *FloatingIp) error
44 45
 	GetFloatingIPs(d *Driver) ([]FloatingIp, error)
45 46
 	GetFloatingIpPoolId(d *Driver) (string, error)
46 47
 	GetInstancePortId(d *Driver) (string, error)
@@ -98,6 +99,8 @@ type FloatingIp struct {
98 99
 	Ip        string
99 100
 	NetworkId string
100 101
 	PortId    string
102
+	Pool      string
103
+	MachineId string
101 104
 }
102 105
 
103 106
 func (c *GenericClient) GetInstanceState(d *Driver) (string, error) {
@@ -296,7 +299,33 @@ func (c *GenericClient) GetServerDetail(d *Driver) (*servers.Server, error) {
296 299
 	return server, nil
297 300
 }
298 301
 
299
-func (c *GenericClient) AssignFloatingIP(d *Driver, floatingIp *FloatingIp, portId string) error {
302
+func (c *GenericClient) AssignFloatingIP(d *Driver, floatingIp *FloatingIp) error {
303
+	if d.ComputeNetwork {
304
+		return c.assignNovaFloatingIP(d, floatingIp)
305
+	} else {
306
+		return c.assignNeutronFloatingIP(d, floatingIp)
307
+	}
308
+}
309
+
310
+func (c *GenericClient) assignNovaFloatingIP(d *Driver, floatingIp *FloatingIp) error {
311
+	if floatingIp.Ip == "" {
312
+		f, err := compute_ips.Create(c.Compute, compute_ips.CreateOpts{
313
+			Pool: d.FloatingIpPool,
314
+		}).Extract()
315
+		if err != nil {
316
+			return err
317
+		}
318
+		floatingIp.Ip = f.IP
319
+		floatingIp.Pool = f.Pool
320
+	}
321
+	return compute_ips.Associate(c.Compute, d.MachineId, floatingIp.Ip).Err
322
+}
323
+
324
+func (c *GenericClient) assignNeutronFloatingIP(d *Driver, floatingIp *FloatingIp) error {
325
+	portId, err := c.GetInstancePortId(d)
326
+	if err != nil {
327
+		return err
328
+	}
300 329
 	if floatingIp.Id == "" {
301 330
 		f, err := floatingips.Create(c.Network, floatingips.CreateOpts{
302 331
 			FloatingNetworkID: d.FloatingIpPoolId,
@@ -311,7 +340,7 @@ func (c *GenericClient) AssignFloatingIP(d *Driver, floatingIp *FloatingIp, port
311 340
 		floatingIp.PortId = f.PortID
312 341
 		return nil
313 342
 	}
314
-	_, err := floatingips.Update(c.Network, floatingIp.Id, floatingips.UpdateOpts{
343
+	_, err = floatingips.Update(c.Network, floatingIp.Id, floatingips.UpdateOpts{
315 344
 		PortID: portId,
316 345
 	}).Extract()
317 346
 	if err != nil {
@@ -321,6 +350,36 @@ func (c *GenericClient) AssignFloatingIP(d *Driver, floatingIp *FloatingIp, port
321 350
 }
322 351
 
323 352
 func (c *GenericClient) GetFloatingIPs(d *Driver) ([]FloatingIp, error) {
353
+	if d.ComputeNetwork {
354
+		return c.getNovaNetworkFloatingIPs(d)
355
+	} else {
356
+		return c.getNeutronNetworkFloatingIPs(d)
357
+	}
358
+}
359
+
360
+func (c *GenericClient) getNovaNetworkFloatingIPs(d *Driver) ([]FloatingIp, error) {
361
+	pager := compute_ips.List(c.Compute)
362
+
363
+	ips := []FloatingIp{}
364
+	err := pager.EachPage(func(page pagination.Page) (continue_paging bool, err error) {
365
+		continue_paging, err = true, nil
366
+		ip_listing, err := compute_ips.ExtractFloatingIPs(page)
367
+
368
+		for _, ip := range ip_listing {
369
+			if ip.InstanceID == "" && ip.Pool == d.FloatingIpPool {
370
+				ips = append(ips, FloatingIp{
371
+					Id:   ip.ID,
372
+					Ip:   ip.IP,
373
+					Pool: ip.Pool,
374
+				})
375
+			}
376
+		}
377
+		return
378
+	})
379
+	return ips, err
380
+}
381
+
382
+func (c *GenericClient) getNeutronNetworkFloatingIPs(d *Driver) ([]FloatingIp, error) {
324 383
 	pager := floatingips.List(c.Network, floatingips.ListOpts{
325 384
 		FloatingNetworkID: d.FloatingIpPoolId,
326 385
 	})

+ 15
- 6
drivers/openstack/openstack.go View File

@@ -38,6 +38,7 @@ type Driver struct {
38 38
 	NetworkId        string
39 39
 	SecurityGroups   []string
40 40
 	FloatingIpPool   string
41
+	ComputeNetwork   bool
41 42
 	FloatingIpPoolId string
42 43
 	IpVersion        int
43 44
 	client           Client
@@ -158,6 +159,11 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
158 159
 			Usage:  "OpenStack comma separated security groups for the machine",
159 160
 			Value:  "",
160 161
 		},
162
+		mcnflag.BoolFlag{
163
+			EnvVar: "OS_NOVA_NETWORK",
164
+			Name:   "openstack-nova-network",
165
+			Usage:  "Use the nova networking services instead of neutron.",
166
+		},
161 167
 		mcnflag.StringFlag{
162 168
 			EnvVar: "OS_FLOATINGIP_POOL",
163 169
 			Name:   "openstack-floatingip-pool",
@@ -240,6 +246,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
240 246
 	}
241 247
 	d.FloatingIpPool = flags.String("openstack-floatingip-pool")
242 248
 	d.IpVersion = flags.Int("openstack-ip-version")
249
+	d.ComputeNetwork = flags.Bool("openstack-nova-network")
243 250
 	d.SSHUser = flags.String("openstack-ssh-user")
244 251
 	d.SSHPort = flags.Int("openstack-ssh-port")
245 252
 	d.SwarmMaster = flags.Bool("swarm-master")
@@ -454,7 +461,7 @@ func (d *Driver) checkConfig() error {
454 461
 }
455 462
 
456 463
 func (d *Driver) resolveIds() error {
457
-	if d.NetworkName != "" {
464
+	if d.NetworkName != "" && !d.ComputeNetwork {
458 465
 		if err := d.initNetwork(); err != nil {
459 466
 			return err
460 467
 		}
@@ -518,7 +525,7 @@ func (d *Driver) resolveIds() error {
518 525
 		}).Debug("Found image id using its name")
519 526
 	}
520 527
 
521
-	if d.FloatingIpPool != "" {
528
+	if d.FloatingIpPool != "" && !d.ComputeNetwork {
522 529
 		if err := d.initNetwork(); err != nil {
523 530
 			return err
524 531
 		}
@@ -599,12 +606,14 @@ func (d *Driver) createMachine() error {
599 606
 }
600 607
 
601 608
 func (d *Driver) assignFloatingIp() error {
609
+	var err error
602 610
 
603
-	if err := d.initNetwork(); err != nil {
604
-		return err
611
+	if d.ComputeNetwork {
612
+		err = d.initCompute()
613
+	} else {
614
+		err = d.initNetwork()
605 615
 	}
606 616
 
607
-	portId, err := d.client.GetInstancePortId(d)
608 617
 	if err != nil {
609 618
 		return err
610 619
 	}
@@ -639,7 +648,7 @@ func (d *Driver) assignFloatingIp() error {
639 648
 		log.WithField("MachineId", d.MachineId).Debugf("Assigning floating IP to the instance")
640 649
 	}
641 650
 
642
-	if err := d.client.AssignFloatingIP(d, floatingIp, portId); err != nil {
651
+	if err := d.client.AssignFloatingIP(d, floatingIp); err != nil {
643 652
 		return err
644 653
 	}
645 654
 	d.IPAddress = floatingIp.Ip

Loading…
Cancel
Save