Skip to content

Commit ff2c11a

Browse files
Refactoring code
1 parent 4caa565 commit ff2c11a

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

pkg/cloud/instance.go

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -312,20 +312,31 @@ func (c *client) CheckLimits(
312312
return nil
313313
}
314314

315-
func (c *client) isIpAvailableInNetwork(ip, networkID string) (bool, error) {
315+
func (c *client) listPublicIPsInNetwork(networkID, ip string) ([]*cloudstack.PublicIpAddress, error) {
316316
params := c.cs.Address.NewListPublicIpAddressesParams()
317317
params.SetNetworkid(networkID)
318-
params.SetIpaddress(ip)
319318
params.SetAllocatedonly(false)
320319
params.SetForvirtualnetwork(false)
321320
params.SetListall(true)
321+
if ip != "" {
322+
params.SetIpaddress(ip)
323+
}
322324

323325
resp, err := c.cs.Address.ListPublicIpAddresses(params)
324326
if err != nil {
325-
return false, errors.Wrapf(err, "failed to list public IP addresses for network %q", networkID)
327+
return nil, errors.Wrapf(err, "failed to list public IP addresses for network %q", networkID)
328+
}
329+
330+
return resp.PublicIpAddresses, nil
331+
}
332+
333+
func (c *client) isIPAvailableInNetwork(ip, networkID string) (bool, error) {
334+
publicIPs, err := c.listPublicIPsInNetwork(networkID, ip)
335+
if err != nil {
336+
return false, err
326337
}
327338

328-
for _, addr := range resp.PublicIpAddresses {
339+
for _, addr := range publicIPs {
329340
if addr.State == "Free" {
330341
return true, nil
331342
}
@@ -335,18 +346,12 @@ func (c *client) isIpAvailableInNetwork(ip, networkID string) (bool, error) {
335346
}
336347

337348
func (c *client) hasFreeIPInNetwork(resolvedNet *cloudstack.Network) (bool, error) {
338-
params := c.cs.Address.NewListPublicIpAddressesParams()
339-
params.SetNetworkid(resolvedNet.Id)
340-
params.SetAllocatedonly(false)
341-
params.SetForvirtualnetwork(false)
342-
params.SetListall(true)
343-
344-
resp, err := c.cs.Address.ListPublicIpAddresses(params)
349+
publicIPs, err := c.listPublicIPsInNetwork(resolvedNet.Id, "")
345350
if err != nil {
346-
return false, errors.Wrapf(err, "failed to check free IPs for network %q", resolvedNet.Id)
351+
return false, err
347352
}
348353

349-
for _, addr := range resp.PublicIpAddresses {
354+
for _, addr := range publicIPs {
350355
if addr.State == "Free" {
351356
return true, nil
352357
}
@@ -355,13 +360,14 @@ func (c *client) hasFreeIPInNetwork(resolvedNet *cloudstack.Network) (bool, erro
355360
return false, nil
356361
}
357362

358-
func (c *client) buildStaticIPEntry(ip, networkID string, resolvedNet *cloudstack.Network) (map[string]string, error) {
363+
func (c *client) buildStaticIPEntry(ip string, resolvedNet *cloudstack.Network) (map[string]string, error) {
364+
networkID := resolvedNet.Id
359365
if err := c.validateIPInCIDR(ip, resolvedNet, networkID); err != nil {
360366
return nil, err
361367
}
362368

363369
if resolvedNet.Type == "Shared" {
364-
isAvailable, err := c.isIpAvailableInNetwork(ip, networkID)
370+
isAvailable, err := c.isIPAvailableInNetwork(ip, networkID)
365371
if err != nil {
366372
return nil, err
367373
}
@@ -407,14 +413,14 @@ func (c *client) buildIPToNetworkList(csMachine *infrav1.CloudStackMachine) ([]m
407413
var ipToNetworkList []map[string]string
408414

409415
for _, net := range csMachine.Spec.Networks {
410-
networkID, resolvedNet, err := c.resolveNetworkReference(net)
416+
resolvedNet, err := c.resolveNetwork(net)
411417
if err != nil {
412418
return nil, err
413419
}
414420

415421
var entry map[string]string
416422
if net.IP != "" {
417-
entry, err = c.buildStaticIPEntry(net.IP, networkID, resolvedNet)
423+
entry, err = c.buildStaticIPEntry(net.IP, resolvedNet)
418424
if err != nil {
419425
return nil, err
420426
}
@@ -431,27 +437,19 @@ func (c *client) buildIPToNetworkList(csMachine *infrav1.CloudStackMachine) ([]m
431437
return ipToNetworkList, nil
432438
}
433439

434-
func (c *client) resolveNetworkReference(net infrav1.NetworkSpec) (string, *cloudstack.Network, error) {
440+
func (c *client) resolveNetwork(net infrav1.NetworkSpec) (*cloudstack.Network, error) {
435441
if net.ID == "" {
436-
resolvedNet, err := c.resolveNetworkByName(net.Name)
437-
if err != nil {
438-
return "", nil, err
439-
}
440-
return resolvedNet.Id, resolvedNet, nil
442+
return c.resolveNetworkByName(net.Name)
441443
}
442444

443445
resolvedNet, _, err := c.cs.Network.GetNetworkByID(net.ID, cloudstack.WithProject(c.user.Project.ID))
444446
if err != nil {
445-
return "", nil, errors.Wrapf(err, "failed to get network %q by ID", net.ID)
447+
return nil, errors.Wrapf(err, "failed to get network %q by ID", net.ID)
446448
}
447-
return net.ID, resolvedNet, nil
449+
return resolvedNet, nil
448450
}
449451

450452
func (c *client) validateIPInCIDR(ipStr string, net *cloudstack.Network, netID string) error {
451-
if net == nil {
452-
return errors.Errorf("network details not found for validation")
453-
}
454-
455453
ip := netpkg.ParseIP(ipStr)
456454
if ip == nil {
457455
return errors.Errorf("invalid IP address %q", ipStr)

templates/cluster-template-multiple-networks.yaml

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,15 @@ spec:
7474
offering:
7575
name: ${CLOUDSTACK_CONTROL_PLANE_MACHINE_OFFERING}
7676
networks:
77-
- name: ${CLOUDSTACK_NETWORK_NAME} # default primary network and should match with failureDomains.zone.network.name
78-
ip: 10.1.1.21 # optional ip address in the network
79-
- name: # extra network, it can be network name or ID
80-
ip: 10.1.1.31 # optional ip address in the network
81-
- id: # extra network, it can be network name or ID
77+
- name: ${CLOUDSTACK_NETWORK_NAME} # (optional) default primary network; must match failureDomains.zone.network.name
78+
# ip: 10.1.1.21 # (optional) static IP in the primary network
79+
80+
# Additional (extra) networks can be specified below. Use either 'name' or 'id', and optionally an 'ip'.
81+
# - name: isolated-net2 # (optional) extra network by name
82+
# ip: 10.1.1.31 # (optional) static IP in this network
83+
84+
# - id: a1b2c3d4-5678-90ef-gh12-3456789ijklm # (optional) extra network by ID
85+
# ip: 10.1.1.32 # (optional) static IP in this network
8286
template:
8387
name: ${CLOUDSTACK_TEMPLATE_NAME}
8488
---
@@ -118,11 +122,15 @@ spec:
118122
template:
119123
name: ${CLOUDSTACK_TEMPLATE_NAME}
120124
networks:
121-
- name: ${CLOUDSTACK_NETWORK_NAME} # default primary network and should match with failureDomains.zone.network.name
122-
ip: 10.1.1.41 # optional ip address in the network
123-
- name: # extra network, it can be network name or ID
124-
ip: 10.1.1.51 # optional ip address in the network
125-
- id: # extra network, it can be network name or ID
125+
- name: ${CLOUDSTACK_NETWORK_NAME} # (optional) default primary network; must match failureDomains.zone.network.name
126+
# ip: 10.1.1.41 # (optional) static IP in the primary network
127+
128+
# Additional (extra) networks can be specified below. Use either 'name' or 'id', and optionally an 'ip'.
129+
# - name: isolated-net3 # (optional) extra network by name
130+
# ip: 10.1.1.51 # (optional) static IP in this network
131+
132+
# - id: f5c93c15-d27b-4c93-b27c-00f48226b9a9 # (optional) extra network by ID
133+
# ip: 10.1.1.52 # (optional) static IP in this network
126134
---
127135
apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
128136
kind: KubeadmConfigTemplate

0 commit comments

Comments
 (0)