-
Notifications
You must be signed in to change notification settings - Fork 312
Pr use existing networks #477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
037e5b3
f36169a
fc76ab8
7a42de1
5405e7b
d357bf1
d253098
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,6 @@ import ( | |
|
|
||
| "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups" | ||
|
|
||
| "github.com/gophercloud/gophercloud" | ||
| "github.com/gophercloud/gophercloud/openstack/common/extensions" | ||
| "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/attachinterfaces" | ||
| "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/bootfromvolume" | ||
|
|
@@ -41,7 +40,6 @@ import ( | |
| "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" | ||
| "github.com/gophercloud/gophercloud/openstack/networking/v2/ports" | ||
| "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets" | ||
| "github.com/gophercloud/gophercloud/pagination" | ||
| infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha2" | ||
| "sigs.k8s.io/cluster-api/util" | ||
| ) | ||
|
|
@@ -99,36 +97,51 @@ func (s *Service) InstanceCreate(clusterName string, machine *clusterv1.Machine, | |
| } | ||
| // Get all network UUIDs | ||
| var nets []ServerNetwork | ||
| for _, net := range openStackMachine.Spec.Networks { | ||
| opts := networks.ListOpts(net.Filter) | ||
| opts.ID = net.UUID | ||
| ids, err := getNetworkIDsByFilter(s.networkClient, &opts) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| for _, netID := range ids { | ||
| if net.Subnets == nil { | ||
| nets = append(nets, ServerNetwork{ | ||
| networkID: netID, | ||
| }) | ||
| if len(openStackMachine.Spec.Networks) > 0 { | ||
| for _, net := range openStackMachine.Spec.Networks { | ||
| opts := networks.ListOpts(net.Filter) | ||
| opts.ID = net.UUID | ||
| ids, err := networking.GetNetworkIDsByFilter(s.networkClient, &opts) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| for _, subnet := range net.Subnets { | ||
| subnetOpts := subnets.ListOpts(subnet.Filter) | ||
| subnetOpts.ID = subnet.UUID | ||
| subnetOpts.NetworkID = netID | ||
| subnetsByFilter, err := networking.GetSubnetsByFilter(s.networkClient, &subnetOpts) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| for _, subnetByFilter := range subnetsByFilter { | ||
| for _, netID := range ids { | ||
| if net.Subnets == nil { | ||
| nets = append(nets, ServerNetwork{ | ||
| networkID: subnetByFilter.NetworkID, | ||
| subnetID: subnetByFilter.ID, | ||
| networkID: netID, | ||
| }) | ||
| continue | ||
| } | ||
|
|
||
| for _, subnet := range net.Subnets { | ||
| subnetOpts := subnets.ListOpts(subnet.Filter) | ||
| subnetOpts.ID = subnet.UUID | ||
| subnetOpts.NetworkID = netID | ||
| subnetsByFilter, err := networking.GetSubnetsByFilter(s.networkClient, &subnetOpts) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| for _, subnetByFilter := range subnetsByFilter { | ||
| nets = append(nets, ServerNetwork{ | ||
| networkID: subnetByFilter.NetworkID, | ||
| subnetID: subnetByFilter.ID, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| if openStackCluster.Status.Network == nil { | ||
| return nil, fmt.Errorf("no network was found in OpenStackCluster.Status.Network") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we need add some info that because the machine doesn't have network spec and cluster network is also nil
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree. Fixed |
||
| } | ||
| if openStackCluster.Status.Network.Subnet == nil { | ||
| return nil, fmt.Errorf("no subnet was found in OpenStackCluster.Status.Network") | ||
| } | ||
|
|
||
| nets = []ServerNetwork{{ | ||
| networkID: openStackCluster.Status.Network.ID, | ||
| subnetID: openStackCluster.Status.Network.Subnet.ID, | ||
| }} | ||
| } | ||
| if len(nets) == 0 { | ||
| return nil, fmt.Errorf("no network was found or provided. Please check your machine configuration and try again") | ||
|
|
@@ -324,31 +337,6 @@ func isDuplicate(list []string, name string) bool { | |
| return false | ||
| } | ||
|
|
||
| // A function for getting the id of a network by querying openstack with filters | ||
| func getNetworkIDsByFilter(networkClient *gophercloud.ServiceClient, opts networks.ListOptsBuilder) ([]string, error) { | ||
| if opts == nil { | ||
| return []string{}, fmt.Errorf("no Filters were passed") | ||
| } | ||
| pager := networks.List(networkClient, opts) | ||
| var uuids []string | ||
| err := pager.EachPage(func(page pagination.Page) (bool, error) { | ||
| networkList, err := networks.ExtractNetworks(page) | ||
| if err != nil { | ||
| return false, err | ||
| } else if len(networkList) == 0 { | ||
| return false, fmt.Errorf("no networks could be found with the filters provided") | ||
| } | ||
| for _, network := range networkList { | ||
| uuids = append(uuids, network.ID) | ||
| } | ||
| return true, nil | ||
| }) | ||
| if err != nil { | ||
| return []string{}, err | ||
| } | ||
| return uuids, nil | ||
| } | ||
|
|
||
| func createPort(is *Service, name string, net ServerNetwork, securityGroups *[]string) (ports.Port, error) { | ||
| portCreateOpts := ports.CreateOpts{ | ||
| Name: name, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.