Skip to content

Commit

Permalink
[occm]: remove zones method (#2732)
Browse files Browse the repository at this point in the history
Since InstancesV1 was removed, we need to clean up the zone methods.

Signed-off-by: Serge Logvinov <[email protected]>
  • Loading branch information
sergelogvinov authored Dec 11, 2024
1 parent dc8e21c commit bfc2ab8
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 113 deletions.
75 changes: 2 additions & 73 deletions pkg/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ import (
"time"

"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/portsecurity"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/trunk_details"
neutronports "github.com/gophercloud/gophercloud/v2/openstack/networking/v2/ports"
"github.com/spf13/pflag"
gcfg "gopkg.in/gcfg.v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/klog/v2"
Expand All @@ -45,7 +43,6 @@ import (
"k8s.io/cloud-provider-openstack/pkg/client"
"k8s.io/cloud-provider-openstack/pkg/metrics"
"k8s.io/cloud-provider-openstack/pkg/util"
"k8s.io/cloud-provider-openstack/pkg/util/errors"
"k8s.io/cloud-provider-openstack/pkg/util/metadata"
openstackutil "k8s.io/cloud-provider-openstack/pkg/util/openstack"
)
Expand Down Expand Up @@ -379,77 +376,9 @@ func (os *OpenStack) LoadBalancer() (cloudprovider.LoadBalancer, bool) {
}

// Zones indicates that we support zones
// DEPRECATED: Zones is deprecated in favor of retrieving zone/region information from InstancesV2.
func (os *OpenStack) Zones() (cloudprovider.Zones, bool) {
klog.V(1).Info("Claiming to support Zones")
return os, true
}

// GetZone returns the current zone
func (os *OpenStack) GetZone(ctx context.Context) (cloudprovider.Zone, error) {
md, err := metadata.Get(os.metadataOpts.SearchOrder)
if err != nil {
return cloudprovider.Zone{}, err
}

zone := cloudprovider.Zone{
FailureDomain: md.AvailabilityZone,
Region: os.epOpts.Region,
}
klog.V(4).Infof("Current zone is %v", zone)
return zone, nil
}

// GetZoneByProviderID implements Zones.GetZoneByProviderID
// This is particularly useful in external cloud providers where the kubelet
// does not initialize node data.
func (os *OpenStack) GetZoneByProviderID(ctx context.Context, providerID string) (cloudprovider.Zone, error) {
instanceID, _, err := instanceIDFromProviderID(providerID)
if err != nil {
return cloudprovider.Zone{}, err
}

compute, err := client.NewComputeV2(os.provider, os.epOpts)
if err != nil {
return cloudprovider.Zone{}, err
}

mc := metrics.NewMetricContext("server", "get")
server, err := servers.Get(ctx, compute, instanceID).Extract()
if mc.ObserveRequest(err) != nil {
return cloudprovider.Zone{}, err
}

zone := cloudprovider.Zone{
FailureDomain: server.AvailabilityZone,
Region: os.epOpts.Region,
}
klog.V(4).Infof("The instance %s in zone %v", server.Name, zone)
return zone, nil
}

// GetZoneByNodeName implements Zones.GetZoneByNodeName
// This is particularly useful in external cloud providers where the kubelet
// does not initialize node data.
func (os *OpenStack) GetZoneByNodeName(ctx context.Context, nodeName types.NodeName) (cloudprovider.Zone, error) {
compute, err := client.NewComputeV2(os.provider, os.epOpts)
if err != nil {
return cloudprovider.Zone{}, err
}

srv, err := getServerByName(ctx, compute, string(nodeName))
if err != nil {
if err == errors.ErrNotFound {
return cloudprovider.Zone{}, cloudprovider.InstanceNotFound
}
return cloudprovider.Zone{}, err
}

zone := cloudprovider.Zone{
FailureDomain: srv.AvailabilityZone,
Region: os.epOpts.Region,
}
klog.V(4).Infof("The instance %s in zone %v", srv.Name, zone)
return zone, nil
return nil, false
}

// Routes initializes routes support
Expand Down
40 changes: 0 additions & 40 deletions pkg/openstack/openstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"testing"
"time"

"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
neutronports "github.com/gophercloud/gophercloud/v2/openstack/networking/v2/ports"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -938,45 +937,6 @@ func TestLoadBalancer(t *testing.T) {
}
}

var FakeMetadata = metadata.Metadata{
UUID: "83679162-1378-4288-a2d4-70e13ec132aa",
Name: "test",
AvailabilityZone: "nova",
}

func TestZones(t *testing.T) {
metadata.Set(&FakeMetadata)
defer metadata.Clear()

os := OpenStack{
provider: &gophercloud.ProviderClient{
IdentityBase: "http://auth.url/",
},
epOpts: &gophercloud.EndpointOpts{
Region: "myRegion",
Availability: gophercloud.AvailabilityPublic,
},
}

z, ok := os.Zones()
if !ok {
t.Fatalf("Zones() returned false")
}

zone, err := z.GetZone(context.TODO())
if err != nil {
t.Fatalf("GetZone() returned error: %v", err)
}

if zone.Region != "myRegion" {
t.Fatalf("GetZone() returned wrong region (%s)", zone.Region)
}

if zone.FailureDomain != "nova" {
t.Fatalf("GetZone() returned wrong failure domain (%s)", zone.FailureDomain)
}
}

func TestInstanceIDFromProviderID(t *testing.T) {
testCases := []struct {
providerID string
Expand Down

0 comments on commit bfc2ab8

Please sign in to comment.