Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ ifndef HAS_YQ
endif
ifndef HAS_KUSTOMIZE
echo "installing kustomize"
go get sigs.k8s.io/kustomize/v3/cmd/kustomize
GO111MODULE=off go get sigs.k8s.io/kustomize/kustomize
endif
ifndef HAS_ENVSUBST
echo "installing envsubst"
Expand Down
7 changes: 7 additions & 0 deletions api/v1alpha2/openstackcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ type OpenStackClusterSpec struct {
// network, a subnet with NodeCIDR, and a router connected to this subnet.
// If you leave this empty, no network will be created.
NodeCIDR string `json:"nodeCidr,omitempty"`

// If NodeCIDR cannot be set this can be used to detect an existing network.
Network Filter `json:"network,omitempty"`

// If NodeCIDR cannot be set this can be used to detect an existing subnet.
Subnet SubnetFilter `json:"subnet,omitempty"`

// DNSNameservers is the list of nameservers for OpenStack Subnet being created.
DNSNameservers []string `json:"dnsNameservers,omitempty"`
// ExternalRouterIPs is an array of externalIPs on the respective subnets.
Expand Down
2 changes: 2 additions & 0 deletions api/v1alpha2/zz_generated.deepcopy.go

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
Expand Up @@ -192,6 +192,43 @@ spec:
and another one that allows all traffic to/from machines belonging
to that group. In the future, we could make this more flexible.
type: boolean
network:
description: If NodeCIDR cannot be set this can be used to detect an
existing network.
properties:
adminStateUp:
type: boolean
description:
type: string
id:
type: string
limit:
type: integer
marker:
type: string
name:
type: string
notTags:
type: string
notTagsAny:
type: string
projectId:
type: string
shared:
type: boolean
sortDir:
type: string
sortKey:
type: string
status:
type: string
tags:
type: string
tagsAny:
type: string
tenantId:
type: string
type: object
nodeCidr:
description: NodeCIDR is the OpenStack Subnet to be created. Cluster
actuator will create a network, a subnet with NodeCIDR, and a router
Expand All @@ -209,6 +246,53 @@ spec:
format: byte
type: string
type: object
subnet:
description: If NodeCIDR cannot be set this can be used to detect an
existing subnet.
properties:
cidr:
type: string
description:
type: string
enableDhcp:
type: boolean
gateway_ip:
type: string
id:
type: string
ipVersion:
type: integer
ipv6AddressMode:
type: string
ipv6RaMode:
type: string
limit:
type: integer
marker:
type: string
name:
type: string
networkId:
type: string
notTags:
type: string
notTagsAny:
type: string
projectId:
type: string
sortDir:
type: string
sortKey:
type: string
subnetpoolId:
type: string
tags:
type: string
tagsAny:
type: string
tenantId:
type: string
type: object
tags:
description: Tags for all resources in cluster
items:
Expand Down
32 changes: 31 additions & 1 deletion controllers/openstackcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package controllers
import (
"context"
"fmt"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/controller"

Expand Down Expand Up @@ -129,7 +131,35 @@ func (r *OpenStackClusterReconciler) reconcileCluster(logger logr.Logger, cluste

logger.Info("Reconciling network components")
if openStackCluster.Spec.NodeCIDR == "" {
logger.V(4).Info("No need to reconcile network")
logger.V(4).Info("No need to reconcile network, searching network and subnet instead")

netOpts := networks.ListOpts(openStackCluster.Spec.Network)
networkList, err := networkingService.GetNetworksByFilter(&netOpts)
if err != nil && len(networkList) == 0 {
return reconcile.Result{}, errors.Errorf("failed to find network: %v", err)
}
if len(networkList) > 1 {
return reconcile.Result{}, errors.Errorf("failed to find only one network (result: %v): %v", networkList, err)
}
openStackCluster.Status.Network = &infrav1.Network{
ID: networkList[0].ID,
Name: networkList[0].Name,
}

subnetOpts := subnets.ListOpts(openStackCluster.Spec.Subnet)
subnetOpts.NetworkID = networkList[0].ID
subnetList, err := networkingService.GetSubnetsByFilter(&subnetOpts)
if err != nil || len(subnetList) == 0 {
return reconcile.Result{}, errors.Errorf("failed to find subnet: %v", err)
}
if len(subnetList) > 1 {
return reconcile.Result{}, errors.Errorf("failed to find only one subnet (result: %v): %v", subnetList, err)
}
openStackCluster.Status.Network.Subnet = &infrav1.Subnet{
ID: subnetList[0].ID,
Name: subnetList[0].Name,
CIDR: subnetList[0].CIDR,
}
} else {
err := networkingService.ReconcileNetwork(clusterName, openStackCluster)
if err != nil {
Expand Down
18 changes: 0 additions & 18 deletions examples/controlplane/controlplane.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ spec:
image: <Image Name>
sshKeyName: cluster-api-provider-openstack
availabilityZone: nova
networks:
- filter:
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
subnets:
- filter:
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
# multi-node control-plane:
# * Disable the floatingIP property
# single-node control-plane:
Expand Down Expand Up @@ -148,12 +142,6 @@ spec:
image: <Image Name>
sshKeyName: cluster-api-provider-openstack
availabilityZone: nova
networks:
- filter:
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
subnets:
- filter:
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
cloudName: $CLOUD
cloudsSecret:
name: cloud-config
Expand Down Expand Up @@ -229,12 +217,6 @@ spec:
image: <Image Name>
sshKeyName: cluster-api-provider-openstack
availabilityZone: nova
networks:
- filter:
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
subnets:
- filter:
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
cloudName: $CLOUD
cloudsSecret:
name: cloud-config
Expand Down
6 changes: 0 additions & 6 deletions examples/machinedeployment/machinedeployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ spec:
namespace: ${CLUSTER_NAME}
flavor: m1.medium
image: <Image Name>
networks:
- filter:
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
subnets:
- filter:
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
sshKeyName: cluster-api-provider-openstack
---
apiVersion: bootstrap.cluster.x-k8s.io/v1alpha2
Expand Down
90 changes: 39 additions & 51 deletions pkg/cloud/services/compute/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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(".spec.networks not set in Machine and also no network was found in .status.network in OpenStackCluster")
}
if openStackCluster.Status.Network.Subnet == nil {
return nil, fmt.Errorf(".spec.networks not set in Machine and also no subnet was found in .status.network.subnet in OpenStackCluster")
}

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")
Expand Down Expand Up @@ -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,
Expand Down
Loading