Skip to content

Commit 04bf536

Browse files
authored
Allow custom region from config (#83)
1 parent abddd58 commit 04bf536

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
@@ -48,6 +48,7 @@ type CSConfig struct {
4848
SSLNoVerify bool `gcfg:"ssl-no-verify"`
4949
ProjectID string `gcfg:"project-id"`
5050
Zone string `gcfg:"zone"`
51+
Region string `gcfg:"region"`
5152
}
5253
}
5354

@@ -56,6 +57,7 @@ type CSCloud struct {
5657
client *cloudstack.CloudStackClient
5758
projectID string // If non-"", all resources will be created within this project
5859
zone string
60+
region string
5961
version semver.Version
6062
clientBuilder cloudprovider.ControllerClientBuilder
6163
}
@@ -90,6 +92,7 @@ func newCSCloud(cfg *CSConfig) (*CSCloud, error) {
9092
cs := &CSCloud{
9193
projectID: cfg.Global.ProjectID,
9294
zone: cfg.Global.Zone,
95+
region: cfg.Global.Region,
9396
version: semver.Version{},
9497
}
9598

@@ -225,7 +228,8 @@ func (cs *CSCloud) GetZone(ctx context.Context) (cloudprovider.Zone, error) {
225228

226229
klog.V(2).Infof("Current zone is %v", cs.zone)
227230
zone.FailureDomain = cs.zone
228-
zone.Region = cs.zone
231+
232+
zone.Region = cs.getRegionFromZone(cs.zone)
229233

230234
return zone, nil
231235
}
@@ -247,7 +251,7 @@ func (cs *CSCloud) GetZoneByProviderID(ctx context.Context, providerID string) (
247251

248252
klog.V(2).Infof("Current zone is %v", cs.zone)
249253
zone.FailureDomain = instance.Zonename
250-
zone.Region = instance.Zonename
254+
zone.Region = cs.getRegionFromZone(instance.Zonename)
251255

252256
return zone, nil
253257
}
@@ -269,7 +273,7 @@ func (cs *CSCloud) GetZoneByNodeName(ctx context.Context, nodeName types.NodeNam
269273

270274
klog.V(2).Infof("Current zone is %v", cs.zone)
271275
zone.FailureDomain = instance.Zonename
272-
zone.Region = instance.Zonename
276+
zone.Region = cs.getRegionFromZone(instance.Zonename)
273277

274278
return zone, nil
275279
}
@@ -324,3 +328,10 @@ func (cs *CSCloud) getNodeNameFromPod(ctx context.Context) (string, error) {
324328
klog.V(4).Infof("found node name %s for pod %s/%s", pod.Spec.NodeName, namespace, podName)
325329
return pod.Spec.NodeName, nil
326330
}
331+
332+
func (cs *CSCloud) getRegionFromZone(zone string) string {
333+
if cs.region != "" {
334+
return cs.region
335+
}
336+
return zone
337+
}

0 commit comments

Comments
 (0)