Skip to content
This repository was archived by the owner on Mar 22, 2018. It is now read-only.

Commit a0c0ff8

Browse files
author
FengyunPan
committed
Implement GetZoneByProviderID and GetZoneByNodeName for openstack
This is part of #50926 cc @wlan0
1 parent 1e63225 commit a0c0ff8

1 file changed

Lines changed: 47 additions & 3 deletions

File tree

pkg/cloudprovider/providers/openstack/openstack.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ import (
4949
"k8s.io/kubernetes/pkg/controller"
5050
)
5151

52-
const ProviderName = "openstack"
52+
const (
53+
ProviderName = "openstack"
54+
AvailabilityZone = "availability_zone"
55+
)
5356

5457
var ErrNotFound = errors.New("Failed to find object")
5558
var ErrMultipleResults = errors.New("Multiple results where only one expected")
@@ -534,6 +537,7 @@ func (os *OpenStack) Zones() (cloudprovider.Zones, bool) {
534537

535538
return os, true
536539
}
540+
537541
func (os *OpenStack) GetZone() (cloudprovider.Zone, error) {
538542
md, err := getMetadata()
539543
if err != nil {
@@ -553,14 +557,54 @@ func (os *OpenStack) GetZone() (cloudprovider.Zone, error) {
553557
// This is particularly useful in external cloud providers where the kubelet
554558
// does not initialize node data.
555559
func (os *OpenStack) GetZoneByProviderID(providerID string) (cloudprovider.Zone, error) {
556-
return cloudprovider.Zone{}, errors.New("GetZoneByProviderID not implemented")
560+
instanceID, err := instanceIDFromProviderID(providerID)
561+
if err != nil {
562+
return cloudprovider.Zone{}, err
563+
}
564+
565+
compute, err := os.NewComputeV2()
566+
if err != nil {
567+
return cloudprovider.Zone{}, err
568+
}
569+
570+
srv, err := servers.Get(compute, instanceID).Extract()
571+
if err != nil {
572+
return cloudprovider.Zone{}, err
573+
}
574+
575+
zone := cloudprovider.Zone{
576+
FailureDomain: srv.Metadata[AvailabilityZone],
577+
Region: os.region,
578+
}
579+
glog.V(4).Infof("The instance %s in zone %v", srv.Name, zone)
580+
581+
return zone, nil
557582
}
558583

559584
// GetZoneByNodeName implements Zones.GetZoneByNodeName
560585
// This is particularly useful in external cloud providers where the kubelet
561586
// does not initialize node data.
562587
func (os *OpenStack) GetZoneByNodeName(nodeName types.NodeName) (cloudprovider.Zone, error) {
563-
return cloudprovider.Zone{}, errors.New("GetZoneByNodeName not imeplemented")
588+
compute, err := os.NewComputeV2()
589+
if err != nil {
590+
return cloudprovider.Zone{}, err
591+
}
592+
593+
srv, err := getServerByName(compute, nodeName)
594+
if err != nil {
595+
if err == ErrNotFound {
596+
return cloudprovider.Zone{}, cloudprovider.InstanceNotFound
597+
}
598+
return cloudprovider.Zone{}, err
599+
}
600+
601+
zone := cloudprovider.Zone{
602+
FailureDomain: srv.Metadata[AvailabilityZone],
603+
Region: os.region,
604+
}
605+
glog.V(4).Infof("The instance %s in zone %v", srv.Name, zone)
606+
607+
return zone, nil
564608
}
565609

566610
func (os *OpenStack) Routes() (cloudprovider.Routes, bool) {

0 commit comments

Comments
 (0)