@@ -26,6 +26,15 @@ import (
26
26
"github.com/pkg/errors"
27
27
)
28
28
29
+ type NetworkIface interface {
30
+ ResolveNetworkStatuses (* infrav1.CloudStackCluster ) error
31
+ CreateIsolatedNewtork (* infrav1.CloudStackCluster ) error
32
+ OpenFirewallRules (* infrav1.CloudStackCluster ) error
33
+ ResolvePublicIPDetails (* infrav1.CloudStackCluster ) (* cloudstack.PublicIpAddress , error )
34
+ ResolveLoadBalancerRuleDetails (* infrav1.CloudStackCluster ) error
35
+ GetOrCreateLoadBalancerRule (* infrav1.CloudStackCluster ) error
36
+ }
37
+
29
38
const (
30
39
NetOffering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
31
40
K8sDefaultAPIPort = 6443
@@ -36,6 +45,29 @@ const (
36
45
doNotAddCreatedByTag = false
37
46
)
38
47
48
+ // usesIsolatedNetwork returns true if this cluster is specs an isolated network.
49
+ // Assumes that the a fetch has been done on network statuses prior.
50
+ func usesIsolatedNetwork (csCluster * infrav1.CloudStackCluster ) bool {
51
+ firstNetStatus := csCluster .Status .Zones [csCluster .Spec .Zones [0 ].Network .Name ].Network
52
+ // Check for Isolated network use case.
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.
56
+ return true
57
+ }
58
+ }
59
+ return false
60
+ }
61
+
62
+ // networkExists checks that the network already exists based on the presence of all fields.
63
+ // Assumes that the a fetch has been done on network statuses prior.
64
+ func networkExists (net infrav1.Network ) bool {
65
+ if net .Name != "" && net .Type != "" && net .Id != "" {
66
+ return true
67
+ }
68
+ return false
69
+ }
70
+
39
71
// ResolveNetworks fetches networks' Id, Name, and Type.
40
72
func (c * client ) ResolveNetwork (csCluster * infrav1.CloudStackCluster , net * infrav1.Network ) (retErr error ) {
41
73
netName := net .Name
@@ -79,8 +111,39 @@ func (c *client) getOfferingId() (string, error) {
79
111
return offeringId , nil
80
112
}
81
113
82
- // GetOrCreateNetworks fetches the details of or creates networks per specs.
83
- func (c * client ) GetOrCreateNetworks (csCluster * infrav1.CloudStackCluster ) (retErr error ) {
114
+ // CreateIsolatedNewtork creates an isolated network in the relevant Zone.
115
+ // Assumes that there is only the one zone in the cluster.
116
+ func (c * client ) CreateIsolatedNewtork (csCluster * infrav1.CloudStackCluster ) (retErr error ) {
117
+ zoneStatus := csCluster .Status .Zones [csCluster .Spec .Zones [0 ].Network .Name ]
118
+ netStatus := zoneStatus .Network
119
+
120
+ // Fetch offering Id.
121
+ offeringId , err := c .getOfferingId ()
122
+ if err != nil {
123
+ return err
124
+ }
125
+
126
+ // Do creation.
127
+ p := c .cs .Network .NewCreateNetworkParams (netStatus .Name , netStatus .Name , offeringId , zoneStatus .Id )
128
+ setIfNotEmpty (csCluster .Spec .Account , p .SetAccount )
129
+ setIfNotEmpty (csCluster .Status .DomainID , p .SetDomainid )
130
+ resp , err := c .cs .Network .CreateNetwork (p )
131
+ if err != nil {
132
+ return err
133
+ }
134
+ c .addClusterTags (csCluster , zoneStatus .Network , addCreatedByTag )
135
+
136
+ // Update Zone/Network status accordingly.
137
+ netStatus .Id = resp .Id
138
+ netStatus .Type = resp .Type
139
+ zoneStatus .Network = netStatus
140
+ csCluster .Status .Zones [zoneStatus .Name ] = zoneStatus
141
+
142
+ return nil
143
+ }
144
+
145
+ // ResolveNetworkStatuses fetches details on all networks specced, but will not modify ACS settings.
146
+ func (c * client ) ResolveNetworkStatuses (csCluster * infrav1.CloudStackCluster ) (retErr error ) {
84
147
// Copy network spec to status in preparation for network resolution or creation.
85
148
for _ , specZone := range csCluster .Spec .Zones {
86
149
zone , ok := csCluster .Status .Zones [specZone .Name ]
@@ -93,35 +156,12 @@ func (c *client) GetOrCreateNetworks(csCluster *infrav1.CloudStackCluster) (retE
93
156
94
157
// At this point network status should have been populated (copied) from the spec.
95
158
for _ , zoneStatus := range csCluster .Status .Zones {
96
- netStatus := zoneStatus .Network
97
159
if retErr = c .ResolveNetwork (csCluster , & zoneStatus .Network ); retErr == nil { // Found network
98
160
c .addClusterTags (csCluster , zoneStatus .Network , doNotAddCreatedByTag )
99
161
continue
100
162
} else if ! strings .Contains (retErr .Error (), "No match found" ) { // Some other error.
101
163
return retErr
102
164
} // Network not found, so create it.
103
-
104
- // Fetch offering Id.
105
- offeringId , err := c .getOfferingId ()
106
- if err != nil {
107
- return err
108
- }
109
-
110
- // Do creation
111
- p := c .cs .Network .NewCreateNetworkParams (netStatus .Name , netStatus .Name , offeringId , zoneStatus .Id )
112
- setIfNotEmpty (csCluster .Spec .Account , p .SetAccount )
113
- setIfNotEmpty (csCluster .Status .DomainID , p .SetDomainid )
114
- resp , err := c .cs .Network .CreateNetwork (p )
115
- if err != nil {
116
- return err
117
- }
118
- c .addClusterTags (csCluster , zoneStatus .Network , addCreatedByTag )
119
-
120
- // Update Zone/Network status accordingly.
121
- netStatus .Id = resp .Id
122
- netStatus .Type = resp .Type
123
- zoneStatus .Network = netStatus
124
- csCluster .Status .Zones [zoneStatus .Name ] = zoneStatus
125
165
}
126
166
127
167
return nil
0 commit comments