Browse Source

feat(openstack): add flag for using nova networks

Signed-off-by: Jeremy White <dudymas@gmail.com>
Jeremy White 3 years ago
parent
commit
2abe8969aa
2 changed files with 77 additions and 19 deletions
  1. 62
    3
      drivers/openstack/client.go
  2. 15
    16
      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
- 16
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",
@@ -182,16 +188,6 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
182 188
 			Usage:  "OpenStack SSH port",
183 189
 			Value:  defaultSSHPort,
184 190
 		},
185
-		mcnflag.StringFlag{
186
-			Name:  "openstack-ssh-user",
187
-			Usage: "OpenStack SSH user",
188
-			Value: defaultSSHUser,
189
-		},
190
-		mcnflag.IntFlag{
191
-			Name:  "openstack-ssh-port",
192
-			Usage: "OpenStack SSH port",
193
-			Value: defaultSSHPort,
194
-		},
195 191
 		mcnflag.IntFlag{
196 192
 			EnvVar: "OS_ACTIVE_TIMEOUT",
197 193
 			Name:   "openstack-active-timeout",
@@ -250,6 +246,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
250 246
 	}
251 247
 	d.FloatingIpPool = flags.String("openstack-floatingip-pool")
252 248
 	d.IpVersion = flags.Int("openstack-ip-version")
249
+	d.ComputeNetwork = flags.Bool("openstack-nova-network")
253 250
 	d.SSHUser = flags.String("openstack-ssh-user")
254 251
 	d.SSHPort = flags.Int("openstack-ssh-port")
255 252
 	d.SwarmMaster = flags.Bool("swarm-master")
@@ -468,7 +465,7 @@ func (d *Driver) checkConfig() error {
468 465
 }
469 466
 
470 467
 func (d *Driver) resolveIds() error {
471
-	if d.NetworkName != "" {
468
+	if d.NetworkName != "" && !d.ComputeNetwork {
472 469
 		if err := d.initNetwork(); err != nil {
473 470
 			return err
474 471
 		}
@@ -532,7 +529,7 @@ func (d *Driver) resolveIds() error {
532 529
 		}).Debug("Found image id using its name")
533 530
 	}
534 531
 
535
-	if d.FloatingIpPool != "" {
532
+	if d.FloatingIpPool != "" && !d.ComputeNetwork {
536 533
 		if err := d.initNetwork(); err != nil {
537 534
 			return err
538 535
 		}
@@ -613,12 +610,14 @@ func (d *Driver) createMachine() error {
613 610
 }
614 611
 
615 612
 func (d *Driver) assignFloatingIp() error {
613
+	var err error
616 614
 
617
-	if err := d.initNetwork(); err != nil {
618
-		return err
615
+	if d.ComputeNetwork {
616
+		err = d.initCompute()
617
+	} else {
618
+		err = d.initNetwork()
619 619
 	}
620 620
 
621
-	portId, err := d.client.GetInstancePortId(d)
622 621
 	if err != nil {
623 622
 		return err
624 623
 	}
@@ -653,7 +652,7 @@ func (d *Driver) assignFloatingIp() error {
653 652
 		log.WithField("MachineId", d.MachineId).Debugf("Assigning floating IP to the instance")
654 653
 	}
655 654
 
656
-	if err := d.client.AssignFloatingIP(d, floatingIp, portId); err != nil {
655
+	if err := d.client.AssignFloatingIP(d, floatingIp); err != nil {
657 656
 		return err
658 657
 	}
659 658
 	d.IPAddress = floatingIp.Ip

Loading…
Cancel
Save