Skip to content

Commit

Permalink
Enables Users to use more complex filters to select networks (openshi…
Browse files Browse the repository at this point in the history
…ft#68)

* A filter can be passed in machines.yaml that allows networks to be searched for in openstack

* Dependancies Updated and Gophercloud Networking dependancies added
  • Loading branch information
iamemilio authored and flaper87 committed Jan 9, 2019
1 parent 43aff4a commit 29023e3
Show file tree
Hide file tree
Showing 99 changed files with 2,824 additions and 17,875 deletions.
246 changes: 24 additions & 222 deletions Gopkg.lock

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions pkg/apis/openstackproviderconfig/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ type NetworkParam struct {
UUID string `json:"uuid,omitempty"`
// A fixed IPv4 address for the NIC.
FixedIp string `json:"fixed_ip,omitempty"`
// Filters for optional network query
Filter Filter `json:"filter,omitempty"`
}

type Filter struct {
Status string `json:"status,omitempty"`
Name string `json:"name,omitempty"`
AdminStateUp *bool `json:"admin_state_up,omitempty"`
TenantID string `json:"tenant_id,omitempty"`
ProjectID string `json:"project_id,omitempty"`
Shared *bool `json:"shared,omitempty"`
ID string `json:"id,omitempty"`
Marker string `json:"marker,omitempty"`
Limit int `json:"limit,omitempty"`
SortKey string `json:"sort_key,omitempty"`
SortDir string `json:"sort_dir,omitempty"`
Tags string `json:"tags,omitempty"`
TagsAny string `json:"tags-any,omitempty"`
NotTags string `json:"not-tags,omitempty"`
NotTagsAny string `json:"not-tags-any,omitempty"`
}

type RootVolume struct {
Expand Down
71 changes: 63 additions & 8 deletions pkg/cloud/openstack/clients/machineservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/pagination"
"github.com/gophercloud/utils/openstack/clientconfig"
openstackconfigv1 "sigs.k8s.io/cluster-api-provider-openstack/pkg/apis/openstackproviderconfig/v1alpha1"
)
Expand All @@ -35,6 +37,7 @@ type InstanceService struct {
provider *gophercloud.ProviderClient
computeClient *gophercloud.ServiceClient
identityClient *gophercloud.ServiceClient
networkClient *gophercloud.ServiceClient
}

type Instance struct {
Expand Down Expand Up @@ -91,11 +94,18 @@ func NewInstanceService() (*InstanceService, error) {
if err != nil {
return nil, fmt.Errorf("Create serviceClient err: %v", err)
}
networkingClient, err := openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{
Region: clientOpts.RegionName,
})
if err != nil {
return nil, fmt.Errorf("Create networkingClient err: %v", err)
}

return &InstanceService{
provider: provider,
identityClient: identityClient,
computeClient: serverClient,
networkClient: networkingClient,
}, nil
}

Expand Down Expand Up @@ -138,26 +148,71 @@ func (is *InstanceService) GetAcceptableFloatingIP() (string, error) {
return floatingIP.IP, nil
}
}
return "", fmt.Errorf("Don't have acceptable floating IP.")
return "", fmt.Errorf("Don't have acceptable floating IP")
}

// A function for getting the id of a network by querying openstack with filters
func getNetworkIDsByFilter(is *InstanceService, opts *networks.ListOpts) ([]string, error) {
if opts == nil {
return []string{}, fmt.Errorf("No Filters were passed")
}
if opts == nil {
return []string{}, fmt.Errorf("Network filters must be provided")
}
pager := networks.List(is.networkClient, opts)
var uids []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 {
uids = append(uids, network.ID)
}
return true, nil
})
if err != nil {
return uids, err
}
return []string{}, nil
}

func (is *InstanceService) InstanceCreate(name string, config *openstackconfigv1.OpenstackProviderConfig, cmd string, keyName string) (instance *Instance, err error) {
var createOpts servers.CreateOpts
if config == nil {
return nil, fmt.Errorf("create Options need be specified to create instace.")
return nil, fmt.Errorf("create Options need be specified to create instace")
}
var nets []servers.Network
for _, net := range config.Networks {
if net.UUID == "" {
opts := networks.ListOpts(net.Filter)
ids, err := getNetworkIDsByFilter(is, &opts)
if err != nil {
return nil, err
}
for _, netID := range ids {
nets = append(nets, servers.Network{
UUID: netID,
})
}
} else {
nets = append(nets, servers.Network{
UUID: net.UUID,
})
}
}
userData := base64.StdEncoding.EncodeToString([]byte(cmd))
createOpts = servers.CreateOpts{
Name: name,
ImageName: config.Image,
FlavorName: config.Flavor,
AvailabilityZone: config.AvailabilityZone,
Networks: []servers.Network{{
UUID: config.Networks[0].UUID,
}},
UserData: []byte(userData),
SecurityGroups: config.SecurityGroups,
ServiceClient: is.computeClient,
Networks: nets,
UserData: []byte(userData),
SecurityGroups: config.SecurityGroups,
ServiceClient: is.computeClient,
}
server, err := servers.Create(is.computeClient, keypairs.CreateOptsExt{
CreateOptsBuilder: createOpts,
Expand Down
15 changes: 0 additions & 15 deletions vendor/cloud.google.com/go/AUTHORS

This file was deleted.

40 changes: 0 additions & 40 deletions vendor/cloud.google.com/go/CONTRIBUTORS

This file was deleted.

Loading

0 comments on commit 29023e3

Please sign in to comment.