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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 22 additions & 8 deletions
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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

0 commit comments

Comments
 (0)