Skip to content

Commit b861a47

Browse files
author
Joshua Reed
committed
Added ability to specify zone on machine.
1 parent 7f13afa commit b861a47

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ spec:
109109
template:
110110
description: CloudStack template to use.
111111
type: string
112+
zoneID:
113+
description: Optionally settable Zone ID to land the machine in.
114+
type: string
115+
zoneName:
116+
description: Optionally settable Zone Name to land the machine in.
117+
type: string
112118
required:
113119
- offering
114120
- 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
@@ -97,6 +97,14 @@ spec:
9797
template:
9898
description: CloudStack template to use.
9999
type: string
100+
zoneID:
101+
description: Optionally settable Zone ID to land the machine
102+
in.
103+
type: string
104+
zoneName:
105+
description: Optionally settable Zone Name to land the machine
106+
in.
107+
type: string
100108
required:
101109
- offering
102110
- 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.ZoneID); 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{}

0 commit comments

Comments
 (0)