Skip to content

Commit 554994e

Browse files
sbueringerk8s-ci-robot
authored andcommitted
Pr use existing networks (#477)
* use existing networks, fallback to network/subnet defined in cluster status * linter fixes * remove the network filters which are not required anymore * regen code/crds * review fixes * fix tests, by fixing kustomize * fix tests
1 parent 484127d commit 554994e

File tree

8 files changed

+208
-76
lines changed

8 files changed

+208
-76
lines changed

api/v1alpha2/openstackcluster_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ type OpenStackClusterSpec struct {
4242
// network, a subnet with NodeCIDR, and a router connected to this subnet.
4343
// If you leave this empty, no network will be created.
4444
NodeCIDR string `json:"nodeCidr,omitempty"`
45+
46+
// If NodeCIDR cannot be set this can be used to detect an existing network.
47+
Network Filter `json:"network,omitempty"`
48+
49+
// If NodeCIDR cannot be set this can be used to detect an existing subnet.
50+
Subnet SubnetFilter `json:"subnet,omitempty"`
51+
4552
// DNSNameservers is the list of nameservers for OpenStack Subnet being created.
4653
DNSNameservers []string `json:"dnsNameservers,omitempty"`
4754
// ExternalRouterIPs is an array of externalIPs on the respective subnets.

api/v1alpha2/zz_generated.deepcopy.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclusters.yaml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,43 @@ spec:
192192
and another one that allows all traffic to/from machines belonging
193193
to that group. In the future, we could make this more flexible.
194194
type: boolean
195+
network:
196+
description: If NodeCIDR cannot be set this can be used to detect an
197+
existing network.
198+
properties:
199+
adminStateUp:
200+
type: boolean
201+
description:
202+
type: string
203+
id:
204+
type: string
205+
limit:
206+
type: integer
207+
marker:
208+
type: string
209+
name:
210+
type: string
211+
notTags:
212+
type: string
213+
notTagsAny:
214+
type: string
215+
projectId:
216+
type: string
217+
shared:
218+
type: boolean
219+
sortDir:
220+
type: string
221+
sortKey:
222+
type: string
223+
status:
224+
type: string
225+
tags:
226+
type: string
227+
tagsAny:
228+
type: string
229+
tenantId:
230+
type: string
231+
type: object
195232
nodeCidr:
196233
description: NodeCIDR is the OpenStack Subnet to be created. Cluster
197234
actuator will create a network, a subnet with NodeCIDR, and a router
@@ -209,6 +246,53 @@ spec:
209246
format: byte
210247
type: string
211248
type: object
249+
subnet:
250+
description: If NodeCIDR cannot be set this can be used to detect an
251+
existing subnet.
252+
properties:
253+
cidr:
254+
type: string
255+
description:
256+
type: string
257+
enableDhcp:
258+
type: boolean
259+
gateway_ip:
260+
type: string
261+
id:
262+
type: string
263+
ipVersion:
264+
type: integer
265+
ipv6AddressMode:
266+
type: string
267+
ipv6RaMode:
268+
type: string
269+
limit:
270+
type: integer
271+
marker:
272+
type: string
273+
name:
274+
type: string
275+
networkId:
276+
type: string
277+
notTags:
278+
type: string
279+
notTagsAny:
280+
type: string
281+
projectId:
282+
type: string
283+
sortDir:
284+
type: string
285+
sortKey:
286+
type: string
287+
subnetpoolId:
288+
type: string
289+
tags:
290+
type: string
291+
tagsAny:
292+
type: string
293+
tenantId:
294+
type: string
295+
type: object
212296
tags:
213297
description: Tags for all resources in cluster
214298
items:

controllers/openstackcluster_controller.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package controllers
1818
import (
1919
"context"
2020
"fmt"
21+
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
22+
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
2123
"k8s.io/client-go/tools/record"
2224
"sigs.k8s.io/controller-runtime/pkg/controller"
2325

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

130132
logger.Info("Reconciling network components")
131133
if openStackCluster.Spec.NodeCIDR == "" {
132-
logger.V(4).Info("No need to reconcile network")
134+
logger.V(4).Info("No need to reconcile network, searching network and subnet instead")
135+
136+
netOpts := networks.ListOpts(openStackCluster.Spec.Network)
137+
networkList, err := networkingService.GetNetworksByFilter(&netOpts)
138+
if err != nil && len(networkList) == 0 {
139+
return reconcile.Result{}, errors.Errorf("failed to find network: %v", err)
140+
}
141+
if len(networkList) > 1 {
142+
return reconcile.Result{}, errors.Errorf("failed to find only one network (result: %v): %v", networkList, err)
143+
}
144+
openStackCluster.Status.Network = &infrav1.Network{
145+
ID: networkList[0].ID,
146+
Name: networkList[0].Name,
147+
}
148+
149+
subnetOpts := subnets.ListOpts(openStackCluster.Spec.Subnet)
150+
subnetOpts.NetworkID = networkList[0].ID
151+
subnetList, err := networkingService.GetSubnetsByFilter(&subnetOpts)
152+
if err != nil || len(subnetList) == 0 {
153+
return reconcile.Result{}, errors.Errorf("failed to find subnet: %v", err)
154+
}
155+
if len(subnetList) > 1 {
156+
return reconcile.Result{}, errors.Errorf("failed to find only one subnet (result: %v): %v", subnetList, err)
157+
}
158+
openStackCluster.Status.Network.Subnet = &infrav1.Subnet{
159+
ID: subnetList[0].ID,
160+
Name: subnetList[0].Name,
161+
CIDR: subnetList[0].CIDR,
162+
}
133163
} else {
134164
err := networkingService.ReconcileNetwork(clusterName, openStackCluster)
135165
if err != nil {

examples/controlplane/controlplane.yaml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ spec:
3333
image: <Image Name>
3434
sshKeyName: cluster-api-provider-openstack
3535
availabilityZone: nova
36-
networks:
37-
- filter:
38-
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
39-
subnets:
40-
- filter:
41-
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
4236
# multi-node control-plane:
4337
# * Disable the floatingIP property
4438
# single-node control-plane:
@@ -148,12 +142,6 @@ spec:
148142
image: <Image Name>
149143
sshKeyName: cluster-api-provider-openstack
150144
availabilityZone: nova
151-
networks:
152-
- filter:
153-
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
154-
subnets:
155-
- filter:
156-
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
157145
cloudName: $CLOUD
158146
cloudsSecret:
159147
name: cloud-config
@@ -229,12 +217,6 @@ spec:
229217
image: <Image Name>
230218
sshKeyName: cluster-api-provider-openstack
231219
availabilityZone: nova
232-
networks:
233-
- filter:
234-
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
235-
subnets:
236-
- filter:
237-
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
238220
cloudName: $CLOUD
239221
cloudsSecret:
240222
name: cloud-config

examples/machinedeployment/machinedeployment.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@ spec:
4949
namespace: ${CLUSTER_NAME}
5050
flavor: m1.medium
5151
image: <Image Name>
52-
networks:
53-
- filter:
54-
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
55-
subnets:
56-
- filter:
57-
name: k8s-clusterapi-cluster-${CLUSTER_NAME}-${CLUSTER_NAME}
5852
sshKeyName: cluster-api-provider-openstack
5953
---
6054
apiVersion: bootstrap.cluster.x-k8s.io/v1alpha2

pkg/cloud/services/compute/instance.go

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727

2828
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
2929

30-
"github.com/gophercloud/gophercloud"
3130
"github.com/gophercloud/gophercloud/openstack/common/extensions"
3231
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/attachinterfaces"
3332
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/bootfromvolume"
@@ -41,7 +40,6 @@ import (
4140
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
4241
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
4342
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
44-
"github.com/gophercloud/gophercloud/pagination"
4543
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha2"
4644
"sigs.k8s.io/cluster-api/util"
4745
)
@@ -99,36 +97,51 @@ func (s *Service) InstanceCreate(clusterName string, machine *clusterv1.Machine,
9997
}
10098
// Get all network UUIDs
10199
var nets []ServerNetwork
102-
for _, net := range openStackMachine.Spec.Networks {
103-
opts := networks.ListOpts(net.Filter)
104-
opts.ID = net.UUID
105-
ids, err := getNetworkIDsByFilter(s.networkClient, &opts)
106-
if err != nil {
107-
return nil, err
108-
}
109-
for _, netID := range ids {
110-
if net.Subnets == nil {
111-
nets = append(nets, ServerNetwork{
112-
networkID: netID,
113-
})
100+
if len(openStackMachine.Spec.Networks) > 0 {
101+
for _, net := range openStackMachine.Spec.Networks {
102+
opts := networks.ListOpts(net.Filter)
103+
opts.ID = net.UUID
104+
ids, err := networking.GetNetworkIDsByFilter(s.networkClient, &opts)
105+
if err != nil {
106+
return nil, err
114107
}
115-
116-
for _, subnet := range net.Subnets {
117-
subnetOpts := subnets.ListOpts(subnet.Filter)
118-
subnetOpts.ID = subnet.UUID
119-
subnetOpts.NetworkID = netID
120-
subnetsByFilter, err := networking.GetSubnetsByFilter(s.networkClient, &subnetOpts)
121-
if err != nil {
122-
return nil, err
123-
}
124-
for _, subnetByFilter := range subnetsByFilter {
108+
for _, netID := range ids {
109+
if net.Subnets == nil {
125110
nets = append(nets, ServerNetwork{
126-
networkID: subnetByFilter.NetworkID,
127-
subnetID: subnetByFilter.ID,
111+
networkID: netID,
128112
})
113+
continue
114+
}
115+
116+
for _, subnet := range net.Subnets {
117+
subnetOpts := subnets.ListOpts(subnet.Filter)
118+
subnetOpts.ID = subnet.UUID
119+
subnetOpts.NetworkID = netID
120+
subnetsByFilter, err := networking.GetSubnetsByFilter(s.networkClient, &subnetOpts)
121+
if err != nil {
122+
return nil, err
123+
}
124+
for _, subnetByFilter := range subnetsByFilter {
125+
nets = append(nets, ServerNetwork{
126+
networkID: subnetByFilter.NetworkID,
127+
subnetID: subnetByFilter.ID,
128+
})
129+
}
129130
}
130131
}
131132
}
133+
} else {
134+
if openStackCluster.Status.Network == nil {
135+
return nil, fmt.Errorf(".spec.networks not set in Machine and also no network was found in .status.network in OpenStackCluster")
136+
}
137+
if openStackCluster.Status.Network.Subnet == nil {
138+
return nil, fmt.Errorf(".spec.networks not set in Machine and also no subnet was found in .status.network.subnet in OpenStackCluster")
139+
}
140+
141+
nets = []ServerNetwork{{
142+
networkID: openStackCluster.Status.Network.ID,
143+
subnetID: openStackCluster.Status.Network.Subnet.ID,
144+
}}
132145
}
133146
if len(nets) == 0 {
134147
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 {
324337
return false
325338
}
326339

327-
// A function for getting the id of a network by querying openstack with filters
328-
func getNetworkIDsByFilter(networkClient *gophercloud.ServiceClient, opts networks.ListOptsBuilder) ([]string, error) {
329-
if opts == nil {
330-
return []string{}, fmt.Errorf("no Filters were passed")
331-
}
332-
pager := networks.List(networkClient, opts)
333-
var uuids []string
334-
err := pager.EachPage(func(page pagination.Page) (bool, error) {
335-
networkList, err := networks.ExtractNetworks(page)
336-
if err != nil {
337-
return false, err
338-
} else if len(networkList) == 0 {
339-
return false, fmt.Errorf("no networks could be found with the filters provided")
340-
}
341-
for _, network := range networkList {
342-
uuids = append(uuids, network.ID)
343-
}
344-
return true, nil
345-
})
346-
if err != nil {
347-
return []string{}, err
348-
}
349-
return uuids, nil
350-
}
351-
352340
func createPort(is *Service, name string, net ServerNetwork, securityGroups *[]string) (ports.Port, error) {
353341
portCreateOpts := ports.CreateOpts{
354342
Name: name,

0 commit comments

Comments
 (0)