@@ -33,6 +33,7 @@ type NetworkIface interface {
33
33
ResolvePublicIPDetails (* infrav1.CloudStackCluster ) (* cloudstack.PublicIpAddress , error )
34
34
ResolveLoadBalancerRuleDetails (* infrav1.CloudStackCluster ) error
35
35
GetOrCreateLoadBalancerRule (* infrav1.CloudStackCluster ) error
36
+ GetOrCreateIsolatedNetwork (* infrav1.CloudStackCluster ) error
36
37
}
37
38
38
39
const (
@@ -48,11 +49,13 @@ const (
48
49
// usesIsolatedNetwork returns true if this cluster is specs an isolated network.
49
50
// Assumes that the a fetch has been done on network statuses prior.
50
51
func usesIsolatedNetwork (csCluster * infrav1.CloudStackCluster ) bool {
51
- firstNetStatus := csCluster .Status .Zones [csCluster .Spec .Zones [0 ].Network .Name ].Network
52
52
// Check for Isolated network use case.
53
53
if len (csCluster .Spec .Zones ) == 1 { // Where the only specced network
54
- if firstNetStatus .Type == "" || // doesn't exist or
55
- firstNetStatus .Type == NetworkTypeIsolated { // exists and is an isolated network.
54
+ zoneStatus := infrav1.Zone {}
55
+ for _ , zoneStatus = range csCluster .Status .Zones {
56
+ }
57
+ if zoneStatus .Network .Type == "" || // doesn't exist or
58
+ zoneStatus .Network .Type == NetworkTypeIsolated { // exists and is an isolated network.
56
59
return true
57
60
}
58
61
}
@@ -71,24 +74,26 @@ func networkExists(net infrav1.Network) bool {
71
74
// ResolveNetworks fetches networks' Id, Name, and Type.
72
75
func (c * client ) ResolveNetwork (csCluster * infrav1.CloudStackCluster , net * infrav1.Network ) (retErr error ) {
73
76
netName := net .Name
74
- netId , count , err := c .cs .Network .GetNetworkID (netName )
77
+ netDetails , count , err := c .cs .Network .GetNetworkByName (netName )
75
78
if err != nil {
76
79
retErr = multierror .Append (retErr , errors .Wrapf (err , "Could not get Network ID from %s." , netName ))
77
80
} else if count != 1 {
78
81
retErr = multierror .Append (retErr , errors .Errorf (
79
82
"Expected 1 Network with name %s, but got %d." , netName , count ))
80
83
} else { // Got netId from the network's name.
81
- netId = net .Id
84
+ net .Id = netDetails .Id
85
+ net .Type = netDetails .Type
86
+ return nil
82
87
}
83
88
84
89
// Now get network details.
85
- netDetails , count , err : = c .cs .Network .GetNetworkByID (netId )
90
+ netDetails , count , err = c .cs .Network .GetNetworkByID (net . Id )
86
91
if err != nil {
87
92
return multierror .Append (retErr , errors .Wrapf (
88
- err , "Could not get Network by ID %s." , netId ))
93
+ err , "Could not get Network by ID %s." , net . Id ))
89
94
} else if count != 1 {
90
95
return multierror .Append (retErr , errors .Errorf (
91
- "Expected 1 Network with UUID %s, but got %d." , netId , count ))
96
+ "Expected 1 Network with UUID %s, but got %d." , net . Id , count ))
92
97
}
93
98
net .Name = netDetails .Name
94
99
net .Id = netDetails .Id
@@ -155,10 +160,11 @@ func (c *client) ResolveNetworkStatuses(csCluster *infrav1.CloudStackCluster) (r
155
160
}
156
161
157
162
// At this point network status should have been populated (copied) from the spec.
158
- for _ , zoneStatus := range csCluster .Status .Zones {
163
+ for zoneName , zoneStatus := range csCluster .Status .Zones {
159
164
if retErr = c .ResolveNetwork (csCluster , & zoneStatus .Network ); retErr == nil { // Found network
165
+ csCluster .Status .Zones [zoneName ] = zoneStatus
166
+ //zone.Network = zoneStatus.DeepCopy().Network
160
167
c .addClusterTags (csCluster , zoneStatus .Network , doNotAddCreatedByTag )
161
- continue
162
168
} else if ! strings .Contains (retErr .Error (), "No match found" ) { // Some other error.
163
169
return retErr
164
170
} // Network not found, so create it.
@@ -362,3 +368,21 @@ func (c *client) AssignVMToLoadBalancerRule(csCluster *infrav1.CloudStackCluster
362
368
_ , retErr = c .cs .LoadBalancer .AssignToLoadBalancerRule (p )
363
369
return retErr
364
370
}
371
+
372
+ // GetOrCreateIsolatedNetwork fetches or builds out the necessary structures for isolated network use.
373
+ func (c * client ) GetOrCreateIsolatedNetwork (csCluster * infrav1.CloudStackCluster ) error {
374
+ onlyNetStatus := csCluster .Status .Zones [csCluster .Spec .Zones [0 ].Network .Name ].Network
375
+ if ! networkExists (onlyNetStatus ) { // create isolated network.
376
+
377
+ }
378
+
379
+ if csCluster .Status .PublicIPID == "" { // Don't try to get public IP again it's already been fetched.
380
+ if err := c .AssociatePublicIpAddress (csCluster ); err != nil {
381
+ return err
382
+ }
383
+ }
384
+ if err := c .GetOrCreateLoadBalancerRule (csCluster ); err != nil {
385
+ return err
386
+ }
387
+ return nil
388
+ }
0 commit comments