Skip to content

Commit f8d0c80

Browse files
committed
merge from main after failure-domain merged
2 parents 31869ed + f2257db commit f8d0c80

12 files changed

+70
-41
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ bin
88
testbin/*
99

1010
nginx.conf
11+
nginx.conf.bak
1112

1213
pkg/mocks/*
1314
!pkg/mocks/.keep

api/v1beta1/cloudstackcluster_types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ type CloudStackClusterStatus struct {
116116
Zones ZoneStatusMap `json:"zones,omitempty"`
117117

118118
// CAPI recognizes failure domains as a method to spread machines.
119-
// CAPC sets failure domains per to indicate functionin Zones.
119+
// CAPC sets failure domains to indicate functioning Zones.
120120
// +optional
121121
FailureDomains clusterv1.FailureDomains `json:"failureDomains,omitempty"`
122122

api/v1beta1/cloudstackmachine_types.go

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ type CloudStackMachineSpec struct {
6363
// +optional
6464
ProviderID *string `json:"providerID,omitempty"`
6565

66+
// Optionally settable Zone ID to land the machine in.
67+
ZoneID string `json:"zoneID,omitempty"`
68+
69+
// Optionally settable Zone Name to land the machine in.
70+
ZoneName string `json:"zoneName,omitempty"`
71+
6672
// IdentityRef is a reference to a identity to be used when reconciling this cluster
6773
// +optional
6874
// +k8s:conversion-gen=false

api/v1beta1/cloudstackmachine_webhook_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,19 @@ var _ = Describe("CloudStackMachine webhook", func() {
8383
Should(MatchError(MatchRegexp(forbiddenRegex, "details")))
8484
})
8585

86-
It("should reject identity reference kind udpates to the CloudStackMachine", func() {
86+
It("should reject identity reference kind updates to the CloudStackMachine", func() {
8787
dummies.CSMachine1.Spec.IdentityRef.Kind = "ConfigMap"
8888
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
8989
Should(MatchError(MatchRegexp(forbiddenRegex, "identityRef\\.Kind")))
9090
})
9191

92-
It("should reject identity reference name udpates to the CloudStackMachine", func() {
92+
It("should reject identity reference name updates to the CloudStackMachine", func() {
9393
dummies.CSMachine1.Spec.IdentityRef.Name = "IdentityConfigMap"
9494
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
9595
Should(MatchError(MatchRegexp(forbiddenRegex, "identityRef\\.Name")))
9696
})
9797

98-
It("should reject udpates to the list of affinty groups of the CloudStackMachine", func() {
98+
It("should reject updates to the list of affinty groups of the CloudStackMachine", func() {
9999
dummies.CSMachine1.Spec.AffinityGroupIDs = []string{"28b907b8-75a7-4214-bd3d-6c61961fc2af"}
100100
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
101101
Should(MatchError(MatchRegexp(forbiddenRegex, "AffinityGroupIDs")))

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

+6
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ spec:
123123
description: Cloudstack resource Name
124124
type: string
125125
type: object
126+
zoneID:
127+
description: Optionally settable Zone ID to land the machine in.
128+
type: string
129+
zoneName:
130+
description: Optionally settable Zone Name to land the machine in.
131+
type: string
126132
required:
127133
- offering
128134
- template

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

+8
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ spec:
111111
description: Cloudstack resource Name
112112
type: string
113113
type: object
114+
zoneID:
115+
description: Optionally settable Zone ID to land the machine
116+
in.
117+
type: string
118+
zoneName:
119+
description: Optionally settable Zone Name to land the machine
120+
in.
121+
type: string
114122
required:
115123
- offering
116124
- template

controllers/cloudstackmachine_controller.go

+22-8
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,29 @@ func (r *CloudStackMachineReconciler) reconcile(
166166
// Set ZoneID on csMachine.
167167
if util.IsControlPlaneMachine(capiMachine) { // Use failure domain zone.
168168
csMachine.Status.ZoneID = *capiMachine.Spec.FailureDomain
169-
} else { // Random zone.
170-
zones := make([]string, len(csCluster.Status.Zones))
171-
zidx := 0
172-
for zoneID := range csCluster.Status.Zones {
173-
zones[zidx] = zoneID
174-
zidx++
169+
} else { // Specified by Machine Template or Random zone.
170+
if csMachine.Spec.ZoneID != "" {
171+
if zone, foundZone := csCluster.Status.Zones[csMachine.Spec.ZoneID]; foundZone { // ZoneID Specified.
172+
csMachine.Status.ZoneID = zone.ID
173+
} else {
174+
return ctrl.Result{}, errors.Errorf("could not find zone by zoneID: %s", csMachine.Spec.ZoneID)
175+
}
176+
} else if csMachine.Spec.ZoneName != "" {
177+
if zone := csCluster.Status.Zones.GetByName(csMachine.Spec.ZoneName); zone != nil { // ZoneName Specified.
178+
csMachine.Status.ZoneID = zone.ID
179+
} else {
180+
return ctrl.Result{}, errors.Errorf("could not find zone by zoneName: %s", csMachine.Spec.ZoneName)
181+
}
182+
} else { // No Zone Specified, pick a Random Zone.
183+
zones := make([]string, len(csCluster.Status.Zones))
184+
zidx := 0
185+
for zoneID := range csCluster.Status.Zones {
186+
zones[zidx] = zoneID
187+
zidx++
188+
}
189+
randNum := (rand.Int() % len(csCluster.Spec.Zones)) // #nosec G404 -- weak crypt rand doesn't matter here.
190+
csMachine.Status.ZoneID = zones[randNum]
175191
}
176-
randNum := (rand.Int() % len(csCluster.Spec.Zones)) // #nosec G404 -- weak crypt rand doesn't matter here.
177-
csMachine.Status.ZoneID = zones[randNum]
178192
}
179193

180194
secret := &corev1.Secret{}

hack/add_addresses.sh

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/bash
22

3+
# This is a very simple script that can be used to launch a load balancer and add addresses to it.
4+
5+
# It does so by querying kubernetes and parsing the output via jq.
6+
37
while true; do
48
ADDRESSES=$(kubectl get machine -o json | jq -r '.items[] | select(.metadata.labels."cluster.x-k8s.io/control-plane" != null) | .status | select(.addresses!=null) | .addresses[].address')
59
if [[ $ADDRESSES != $OLD_ADDRESSES ]]; then
@@ -10,8 +14,8 @@ while true; do
1014
echo $ADDRESS
1115
sed -i.bak '/upstream kubeendpoints/a\'$'\n'$'\t''server '$ADDRESS':6443 max_fails=3 fail_timeout=10s;'$'\n' nginx.conf
1216
done
13-
docker stop nginx-container || echo
14-
docker rm nginx-container || echo
17+
docker stop nginx-container &> /dev/null || echo
18+
docker rm nginx-container &> /dev/null || echo
1519
docker run --name=nginx-container --rm -p 8082:8082 -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf nginx &
1620
fi
1721
OLD_ADDRESSES=$ADDRESSES

hack/nginx.conf

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ http {
4444
stream {
4545
upstream kubeendpoints {
4646

47+
# The following is the format for adding a kubernetes upstream endpoint.
4748
# server <server1 IP>:6443 max_fails=3 fail_timeout=10s;
4849
# server <server2 IP>:6443 max_fails=3 fail_timeout=10s;
4950
}

hack/setup_for_dev.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
# Source this file for local dev.
4+
5+
export IMG=localhost:5000/cluster-api-provider-cloudstack:latest
6+
export PROJECT_DIR=`pwd`
7+
export KUBEBUILDER_ASSETS=$PROJECT_DIR/bin
8+
export PATH=$PROJECT_DIR/bin:$PATH
9+
export ACK_GINKGO_DEPRECATIONS=1.16.4

pkg/cloud/instance.go

+7-18
Original file line numberDiff line numberDiff line change
@@ -214,29 +214,18 @@ func (c *client) GetOrCreateVMInstance(
214214

215215
}
216216

217-
// DestroyVMInstance Destroy a VM instance. Assumes machine has been fetched prior and has an instance ID.
217+
// DestroyVMInstance Destroys a VM instance. Assumes machine has been fetched prior and has an instance ID.
218218
func (c *client) DestroyVMInstance(csMachine *infrav1.CloudStackMachine) error {
219219

220-
if err := c.ResolveVMInstanceDetails(csMachine); err == nil && csMachine.Status.InstanceState != "Running" {
221-
if csMachine.Status.InstanceState == "Stopping" ||
222-
csMachine.Status.InstanceState == "Stopped" {
223-
return errors.New("VM deletion in progress")
224-
} else if csMachine.Status.InstanceState == "Expunging" ||
225-
csMachine.Status.InstanceState == "Expunged" {
226-
// VM is stopped and getting expunged. So the desired state is getting satisfied. Let's move on.
227-
return nil
228-
}
229-
} else if err != nil && strings.Contains(strings.ToLower(err.Error()), "no match found") {
230-
// VM doesn't exist. So the desired state is in effect. Our work is done here.
231-
return nil
232-
}
233-
220+
// Attempt deletion regardless of machine state.
234221
p := c.cs.VirtualMachine.NewDestroyVirtualMachineParams(*csMachine.Spec.InstanceID)
235222
p.SetExpunge(true)
236-
_, err := c.csAsync.VirtualMachine.DestroyVirtualMachine(p)
237-
if err != nil && strings.Contains(err.Error(), "unable to find UUID for id") {
238-
// VM doesn't exist. So the desired state is in effect. Our work is done here.
223+
if _, err := c.csAsync.VirtualMachine.DestroyVirtualMachine(p); err != nil &&
224+
strings.Contains(strings.ToLower(err.Error()), "unable to find uuid for id") {
225+
// VM doesn't exist. Success...
239226
return nil
227+
} else if err != nil {
228+
return err
240229
}
241230
return errors.New("VM deletion in progress")
242231
}

pkg/cloud/network_test.go

-9
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,6 @@ var _ = Describe("Network", func() {
9999
It("correctly identifies an existing network from a network status", func() {
100100
Ω(cloud.NetworkExists(dummies.CSCluster.Status.Zones.GetOne().Network)).Should(BeTrue())
101101
})
102-
103-
// It("resolves network details with network ID instead of network name", func() {
104-
// ns.EXPECT().GetNetworkID(gomock.Any()).Return("", -1, errors.New("no match found for blah"))
105-
// ns.EXPECT().GetNetworkByID(fakeNetID).Return(&cloudstack.Network{Type: isolatedNetworkType}, 1, nil)
106-
// expectNetworkTags(fakeNetID, false)
107-
108-
// csCluster.Spec.Network = fakeNetID
109-
// Ω(client.GetOrCreateNetwork(csCluster)).Should(Succeed())
110-
// })
111102
})
112103

113104
Context("for a non-existent network", func() {

0 commit comments

Comments
 (0)