Skip to content

Commit f4b523f

Browse files
author
Joshua Reed
committed
Breakout Isolated network creation path. Use failureDomains in instance creation.
1 parent 7ff5ecd commit f4b523f

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

pkg/cloud/client_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
package cloud_test
1818

1919
import (
20-
"fmt"
21-
2220
. "github.com/onsi/ginkgo"
2321
. "github.com/onsi/gomega"
2422
"gopkg.in/ini.v1"
@@ -49,7 +47,6 @@ var _ = Describe("Instance", func() {
4947
cfg := &Global{}
5048
rawCfg, err := ini.Load("../../cloud-config")
5149
Ω(rawCfg.Section("Global")).ShouldNot(BeNil())
52-
fmt.Println(rawCfg.Section("Global"))
5350
Ω(err).ShouldNot(HaveOccurred())
5451
Ω(rawCfg.Section("Global").MapTo(cfg)).Should(Succeed())
5552
Ω(cfg.VerifySSL).Should(BeFalse())

pkg/cloud/cluster.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
infrav1 "github.com/aws/cluster-api-provider-cloudstack/api/v1beta1"
2121
"github.com/hashicorp/go-multierror"
2222
"github.com/pkg/errors"
23+
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2324
)
2425

2526
type ClusterIface interface {
@@ -63,6 +64,11 @@ func (c *client) GetOrCreateCluster(csCluster *infrav1.CloudStackCluster) (retEr
6364
return errors.Wrapf(retErr, "Error resolving Zone details for Cluster %s.", csCluster.Name)
6465
}
6566

67+
csCluster.Status.FailureDomains = capiv1.FailureDomains{}
68+
for _, zone := range csCluster.Status.Zones {
69+
csCluster.Status.FailureDomains[zone.Id] = capiv1.FailureDomainSpec{ControlPlane: true}
70+
}
71+
6672
// If provided, translate Domain name to Domain ID.
6773
if csCluster.Spec.Domain != "" {
6874
domainID, count, retErr := c.cs.Domain.GetDomainID(csCluster.Spec.Domain)
@@ -82,20 +88,9 @@ func (c *client) GetOrCreateCluster(csCluster *infrav1.CloudStackCluster) (retEr
8288
}
8389

8490
if usesIsolatedNetwork(csCluster) {
85-
onlyNetStatus := csCluster.Status.Zones[csCluster.Spec.Zones[0].Network.Name].Network
86-
if !networkExists(onlyNetStatus) { // create isolated network.
87-
88-
}
89-
90-
if csCluster.Status.PublicIPID == "" { // Don't try to get public IP again it's already been fetched.
91-
if retErr = c.AssociatePublicIpAddress(csCluster); retErr != nil {
92-
return retErr
93-
}
94-
}
95-
if retErr = c.GetOrCreateLoadBalancerRule(csCluster); retErr != nil {
96-
return retErr
97-
}
91+
c.GetOrCreateIsolatedNetwork(csCluster)
9892
}
93+
9994
return nil
10095
}
10196

pkg/cloud/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (c *client) GetOrCreateVMInstance(
203203

204204
// If this VM instance is a control plane, consider setting its IP.
205205
_, isControlPlanceMachine := capiMachine.ObjectMeta.Labels["cluster.x-k8s.io/control-plane"]
206-
if isControlPlanceMachine && zone.Network.Type == NetworkTypeShared {
206+
if isControlPlanceMachine && zone.Network.Type == NetworkTypeIsolated {
207207
// If the specified control plane endpoint is an IP address, specify the IP address of this VM instance.
208208
if net.ParseIP(csCluster.Spec.ControlPlaneEndpoint.Host) != nil {
209209
p.SetIpaddress(csCluster.Spec.ControlPlaneEndpoint.Host)

pkg/cloud/network.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type NetworkIface interface {
3333
ResolvePublicIPDetails(*infrav1.CloudStackCluster) (*cloudstack.PublicIpAddress, error)
3434
ResolveLoadBalancerRuleDetails(*infrav1.CloudStackCluster) error
3535
GetOrCreateLoadBalancerRule(*infrav1.CloudStackCluster) error
36+
GetOrCreateIsolatedNetwork(*infrav1.CloudStackCluster) error
3637
}
3738

3839
const (
@@ -48,11 +49,13 @@ const (
4849
// usesIsolatedNetwork returns true if this cluster is specs an isolated network.
4950
// Assumes that the a fetch has been done on network statuses prior.
5051
func usesIsolatedNetwork(csCluster *infrav1.CloudStackCluster) bool {
51-
firstNetStatus := csCluster.Status.Zones[csCluster.Spec.Zones[0].Network.Name].Network
5252
// Check for Isolated network use case.
5353
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.
5659
return true
5760
}
5861
}
@@ -71,24 +74,26 @@ func networkExists(net infrav1.Network) bool {
7174
// ResolveNetworks fetches networks' Id, Name, and Type.
7275
func (c *client) ResolveNetwork(csCluster *infrav1.CloudStackCluster, net *infrav1.Network) (retErr error) {
7376
netName := net.Name
74-
netId, count, err := c.cs.Network.GetNetworkID(netName)
77+
netDetails, count, err := c.cs.Network.GetNetworkByName(netName)
7578
if err != nil {
7679
retErr = multierror.Append(retErr, errors.Wrapf(err, "Could not get Network ID from %s.", netName))
7780
} else if count != 1 {
7881
retErr = multierror.Append(retErr, errors.Errorf(
7982
"Expected 1 Network with name %s, but got %d.", netName, count))
8083
} 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
8287
}
8388

8489
// Now get network details.
85-
netDetails, count, err := c.cs.Network.GetNetworkByID(netId)
90+
netDetails, count, err = c.cs.Network.GetNetworkByID(net.Id)
8691
if err != nil {
8792
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))
8994
} else if count != 1 {
9095
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))
9297
}
9398
net.Name = netDetails.Name
9499
net.Id = netDetails.Id
@@ -155,10 +160,11 @@ func (c *client) ResolveNetworkStatuses(csCluster *infrav1.CloudStackCluster) (r
155160
}
156161

157162
// 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 {
159164
if retErr = c.ResolveNetwork(csCluster, &zoneStatus.Network); retErr == nil { // Found network
165+
csCluster.Status.Zones[zoneName] = zoneStatus
166+
//zone.Network = zoneStatus.DeepCopy().Network
160167
c.addClusterTags(csCluster, zoneStatus.Network, doNotAddCreatedByTag)
161-
continue
162168
} else if !strings.Contains(retErr.Error(), "No match found") { // Some other error.
163169
return retErr
164170
} // Network not found, so create it.
@@ -362,3 +368,21 @@ func (c *client) AssignVMToLoadBalancerRule(csCluster *infrav1.CloudStackCluster
362368
_, retErr = c.cs.LoadBalancer.AssignToLoadBalancerRule(p)
363369
return retErr
364370
}
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

Comments
 (0)