Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 43 additions & 24 deletions drivers/openstack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Client interface {
GetPublicKey(keyPairName string) ([]byte, error)
CreateKeyPair(d *Driver, name string, publicKey string) error
DeleteKeyPair(d *Driver, name string) error
GetNetworkID(d *Driver) (string, error)
GetNetworkIDList(d *Driver) ([]string, error)
GetFlavorID(d *Driver) (string, error)
GetImageID(d *Driver) (string, error)
AssignFloatingIP(d *Driver, floatingIP *FloatingIP) error
Expand All @@ -70,11 +70,12 @@ func (c *GenericClient) CreateInstance(d *Driver) (string, error) {
AvailabilityZone: d.AvailabilityZone,
ConfigDrive: d.ConfigDrive,
}
if d.NetworkId != "" {
serverOpts.Networks = []servers.Network{
{
UUID: d.NetworkId,
},
if len(d.NetworkIds) > 0 {
serverOpts.Networks = make([]servers.Network, len(d.NetworkIds))
for i, nID := range d.NetworkIds {
serverOpts.Networks[i] = servers.Network{
UUID: nID,
}
}
}

Expand Down Expand Up @@ -203,8 +204,20 @@ func (c *GenericClient) GetInstanceIPAddresses(d *Driver) ([]IPAddress, error) {
return addresses, nil
}

func (c *GenericClient) GetNetworkID(d *Driver) (string, error) {
return c.getNetworkID(d, d.NetworkName)
func (c *GenericClient) GetNetworkIDList(d *Driver) ([]string, error) {
networkIDs := make([]string, len(d.NetworkNames))
for index, networkName := range d.NetworkNames {
nID, err := c.getNetworkID(d, networkName)
if err != nil {
return nil, err
}
log.Debug("Found network id using its name", map[string]string{
"Name": networkName,
"ID": nID,
})
networkIDs[index] = nID
}
return networkIDs, nil
}

func (c *GenericClient) GetFloatingIPPoolID(d *Driver) (string, error) {
Expand Down Expand Up @@ -451,26 +464,32 @@ func (c *GenericClient) getNeutronNetworkFloatingIPs(d *Driver) ([]FloatingIP, e
}

func (c *GenericClient) GetInstancePortID(d *Driver) (string, error) {
pager := ports.List(c.Network, ports.ListOpts{
DeviceID: d.MachineId,
NetworkID: d.NetworkId,
})

var portID string
err := pager.EachPage(func(page pagination.Page) (bool, error) {
portList, err := ports.ExtractPorts(page)
for _, networkID := range d.NetworkIds {
pager := ports.List(c.Network, ports.ListOpts{
DeviceID: d.MachineId,
NetworkID: networkID,
})

err := pager.EachPage(func(page pagination.Page) (bool, error) {
portList, err := ports.ExtractPorts(page)
if err != nil {
return false, err
}
for _, port := range portList {
portID = port.ID
return false, nil
}
return true, nil
})

if err != nil {
return false, err
return "", err
}
for _, port := range portList {
portID = port.ID
return false, nil
}
return true, nil
})

if err != nil {
return "", err
if portID != "" {
break
}
}
return portID, nil
}
Expand Down
34 changes: 17 additions & 17 deletions drivers/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type Driver struct {
ImageName string
ImageId string
KeyPairName string
NetworkName string
NetworkId string
NetworkNames []string
NetworkIds []string
UserData []byte
PrivateKeyFile string
SecurityGroups []string
Expand Down Expand Up @@ -166,7 +166,7 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
mcnflag.StringFlag{

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could convert these to mcnflag.StringSlice and avoid all the splits.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried mcnflag.StringSliceFlag but it is not working as expected. Does not split with comma.

diff --git a/drivers/openstack/openstack.go b/drivers/openstack/openstack.go
index 48a78a99..b5e99f20 100644
--- a/drivers/openstack/openstack.go
+++ b/drivers/openstack/openstack.go
@@ -163,11 +163,10 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
                        Usage:  "OpenStack keypair to use to SSH to the instance",
                        Value:  "",
                },
-               mcnflag.StringFlag{
+               mcnflag.StringSliceFlag{
                        EnvVar: "OS_NETWORK_ID",
                        Name:   "openstack-net-id",
                        Usage:  "OpenStack comma seperated network id(s) the machine will be connected on. (If floating ip pool is given, driver tries to find connected port in order of given networks to update floating ip)",
-                       Value:  "",
                },
                mcnflag.StringFlag{
                        EnvVar: "OS_PRIVATE_KEY_FILE",
@@ -181,11 +180,10 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
                        Usage:  "File containing an openstack userdata script",
                        Value:  "",
                },
-               mcnflag.StringFlag{
+               mcnflag.StringSliceFlag{
                        EnvVar: "OS_NETWORK_NAME",
                        Name:   "openstack-net-name",
                        Usage:  "OpenStack comma seperated network name(s) the machine will be connected on. (If floating ip pool is given, driver tries to find connected port in order of given networks to update floating ip)",
-                       Value:  "",
                },
                mcnflag.StringFlag{
                        EnvVar: "OS_SECURITY_GROUPS",
@@ -284,12 +282,8 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
        d.FlavorName = flags.String("openstack-flavor-name")
        d.ImageId = flags.String("openstack-image-id")
        d.ImageName = flags.String("openstack-image-name")
-       if flags.String("openstack-net-id") != "" {
-               d.NetworkIds = strings.Split(flags.String("openstack-net-id"), ",")
-       }
-       if flags.String("openstack-net-name") != "" {
-               d.NetworkNames = strings.Split(flags.String("openstack-net-name"), ",")
-       }
+       d.NetworkIds = flags.StringSlice("openstack-net-id")
+       d.NetworkNames = flags.StringSlice("openstack-net-name")
        if flags.String("openstack-sec-groups") != "" {
                d.SecurityGroups = strings.Split(flags.String("openstack-sec-groups"), ",")
        }

Also on this driver openstack-sec-groups are done with splitting way before. Don't know why it is not working as expected

@luthermonson luthermonson Nov 11, 2019

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alkimake the string slice let's you pass in the same param multiple times. instead of doing ---openstack-net-id="id1,id2,id3" you do --openstack-net-id=id1 --openstack-net-id=id2 --openstack-net-id=id3. it just eliminates the string split on comma and more drivers use that standard than comma sep list

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But on the UI of Rancher we have only 1 input for each network names and ids. So , separated argument makes more sense.

EnvVar: "OS_NETWORK_ID",
Name: "openstack-net-id",
Usage: "OpenStack network id the machine will be connected on",
Usage: "OpenStack comma seperated network id(s) the machine will be connected on. (If floating ip pool is given, driver tries to find connected port in order of given networks to update floating ip)",
Value: "",
},
mcnflag.StringFlag{
Expand All @@ -184,7 +184,7 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
mcnflag.StringFlag{
EnvVar: "OS_NETWORK_NAME",
Name: "openstack-net-name",
Usage: "OpenStack network name the machine will be connected on",
Usage: "OpenStack comma seperated network name(s) the machine will be connected on. (If floating ip pool is given, driver tries to find connected port in order of given networks to update floating ip)",
Value: "",
},
mcnflag.StringFlag{
Expand Down Expand Up @@ -284,8 +284,12 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.FlavorName = flags.String("openstack-flavor-name")
d.ImageId = flags.String("openstack-image-id")
d.ImageName = flags.String("openstack-image-name")
d.NetworkId = flags.String("openstack-net-id")
d.NetworkName = flags.String("openstack-net-name")
if flags.String("openstack-net-id") != "" {
d.NetworkIds = strings.Split(flags.String("openstack-net-id"), ",")
}
if flags.String("openstack-net-name") != "" {
d.NetworkNames = strings.Split(flags.String("openstack-net-name"), ",")
}
if flags.String("openstack-sec-groups") != "" {
d.SecurityGroups = strings.Split(flags.String("openstack-sec-groups"), ",")
}
Expand Down Expand Up @@ -520,8 +524,8 @@ func (d *Driver) checkConfig() error {
return fmt.Errorf(errorExclusiveOptions, "Image name", "Image id")
}

if d.NetworkName != "" && d.NetworkId != "" {
return fmt.Errorf(errorExclusiveOptions, "Network name", "Network id")
if len(d.NetworkNames) > 0 && len(d.NetworkIds) > 0 {
return fmt.Errorf(errorExclusiveOptions, "Network name(s)", "Network id(s)")
}
if d.EndpointType != "" && (d.EndpointType != "publicURL" && d.EndpointType != "adminURL" && d.EndpointType != "internalURL") {
return fmt.Errorf(errorWrongEndpointType)
Expand All @@ -533,26 +537,22 @@ func (d *Driver) checkConfig() error {
}

func (d *Driver) resolveIds() error {
if d.NetworkName != "" && !d.ComputeNetwork {
if len(d.NetworkNames) > 0 && !d.ComputeNetwork {
if err := d.initNetwork(); err != nil {
return err
}

networkID, err := d.client.GetNetworkID(d)
networkIDs, err := d.client.GetNetworkIDList(d)

if err != nil {
return err
}

if networkID == "" {
return fmt.Errorf(errorUnknownNetworkName, d.NetworkName)
if len(networkIDs) == 0 {
return fmt.Errorf(errorUnknownNetworkName, d.NetworkNames)
}

d.NetworkId = networkID
log.Debug("Found network id using its name", map[string]string{
"Name": d.NetworkName,
"ID": d.NetworkId,
})
d.NetworkIds = networkIDs
}

if d.FlavorName != "" {
Expand Down