Skip to content

Commit c9ba7e3

Browse files
committed
Allow custom region from config
1 parent 96f1b18 commit c9ba7e3

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

cloudstack.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type CSConfig struct {
4646
SSLNoVerify bool `gcfg:"ssl-no-verify"`
4747
ProjectID string `gcfg:"project-id"`
4848
Zone string `gcfg:"zone"`
49+
Region string `gcfg:"region"`
4950
}
5051
}
5152

@@ -54,6 +55,7 @@ type CSCloud struct {
5455
client *cloudstack.CloudStackClient
5556
projectID string // If non-"", all resources will be created within this project
5657
zone string
58+
region string
5759
clientBuilder cloudprovider.ControllerClientBuilder
5860
}
5961

@@ -87,6 +89,7 @@ func newCSCloud(cfg *CSConfig) (*CSCloud, error) {
8789
cs := &CSCloud{
8890
projectID: cfg.Global.ProjectID,
8991
zone: cfg.Global.Zone,
92+
region: cfg.Global.Region,
9093
}
9194

9295
if cfg.Global.APIURL != "" && cfg.Global.APIKey != "" && cfg.Global.SecretKey != "" {
@@ -198,7 +201,8 @@ func (cs *CSCloud) GetZone(ctx context.Context) (cloudprovider.Zone, error) {
198201

199202
klog.V(2).Infof("Current zone is %v", cs.zone)
200203
zone.FailureDomain = cs.zone
201-
zone.Region = cs.zone
204+
205+
zone.Region = cs.getRegionFromZone(cs.zone)
202206

203207
return zone, nil
204208
}
@@ -220,7 +224,7 @@ func (cs *CSCloud) GetZoneByProviderID(ctx context.Context, providerID string) (
220224

221225
klog.V(2).Infof("Current zone is %v", cs.zone)
222226
zone.FailureDomain = instance.Zonename
223-
zone.Region = instance.Zonename
227+
zone.Region = cs.getRegionFromZone(instance.Zonename)
224228

225229
return zone, nil
226230
}
@@ -242,7 +246,7 @@ func (cs *CSCloud) GetZoneByNodeName(ctx context.Context, nodeName types.NodeNam
242246

243247
klog.V(2).Infof("Current zone is %v", cs.zone)
244248
zone.FailureDomain = instance.Zonename
245-
zone.Region = instance.Zonename
249+
zone.Region = cs.getRegionFromZone(instance.Zonename)
246250

247251
return zone, nil
248252
}
@@ -297,3 +301,10 @@ func (cs *CSCloud) getNodeNameFromPod(ctx context.Context) (string, error) {
297301
klog.V(4).Infof("found node name %s for pod %s/%s", pod.Spec.NodeName, namespace, podName)
298302
return pod.Spec.NodeName, nil
299303
}
304+
305+
func (cs *CSCloud) getRegionFromZone(zone string) string {
306+
if cs.region != "" {
307+
return cs.region
308+
}
309+
return zone
310+
}

0 commit comments

Comments
 (0)