Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#16 from zetaab/feat/gophercloudv2
Browse files Browse the repository at this point in the history
update to gophercloud v2
  • Loading branch information
k8s-ci-robot authored Oct 28, 2024
2 parents 1064757 + 744026e commit ecdaeff
Show file tree
Hide file tree
Showing 125 changed files with 12,474 additions and 2,870 deletions.
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/blang/semver/v4 v4.0.0
github.com/digitalocean/godo v1.118.0
github.com/golang/protobuf v1.5.4
github.com/gophercloud/gophercloud v1.12.0
github.com/gophercloud/gophercloud/v2 v2.2.0
github.com/hetznercloud/hcloud-go v1.56.0
github.com/prometheus/client_golang v1.19.1
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.28
Expand Down Expand Up @@ -83,6 +83,7 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.4 // indirect
github.com/gophercloud/gophercloud v1.12.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.6 // indirect
github.com/jmespath/go-jmespath v0.4.1-0.20220621161143-b0104c826a24 // indirect
Expand All @@ -104,9 +105,9 @@ require (
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.19.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240610135401-a8a62080eff3 // indirect
Expand Down
14 changes: 8 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ github.com/googleapis/gax-go/v2 v2.12.4 h1:9gWcmF85Wvq4ryPFvGFaOgPIs1AQX0d0bcbGw
github.com/googleapis/gax-go/v2 v2.12.4/go.mod h1:KYEYLorsnIGDi/rPC8b5TdlB9kbKoFubselGIoBMCwI=
github.com/gophercloud/gophercloud v1.12.0 h1:Jrz16vPAL93l80q16fp8NplrTCp93y7rZh2P3Q4Yq7g=
github.com/gophercloud/gophercloud v1.12.0/go.mod h1:aAVqcocTSXh2vYFZ1JTvx4EQmfgzxRcNupUfxZbBNDM=
github.com/gophercloud/gophercloud/v2 v2.2.0 h1:STqqnSXuhcg1OPBOZ14z6JDm8fKIN13H2bJg6bBuHp8=
github.com/gophercloud/gophercloud/v2 v2.2.0/go.mod h1:f2hMRC7Kakbv5vM7wSGHrIPZh6JZR60GVHryJlF/K44=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
Expand Down Expand Up @@ -276,8 +278,8 @@ golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
Expand Down Expand Up @@ -327,14 +329,14 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
11 changes: 5 additions & 6 deletions pkg/volumes/openstack/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ go_library(
deps = [
"//pkg/privateapi/discovery",
"//pkg/volumes",
"//vendor/github.com/gophercloud/gophercloud",
"//vendor/github.com/gophercloud/gophercloud/openstack",
"//vendor/github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes",
"//vendor/github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones",
"//vendor/github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach",
"//vendor/github.com/gophercloud/gophercloud/openstack/compute/v2/servers",
"//vendor/github.com/gophercloud/gophercloud/v2:gophercloud",
"//vendor/github.com/gophercloud/gophercloud/v2/openstack",
"//vendor/github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v3/volumes",
"//vendor/github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers",
"//vendor/github.com/gophercloud/gophercloud/v2/openstack/compute/v2/volumeattach",
"//vendor/github.com/prometheus/client_golang/prometheus",
"//vendor/gopkg.in/gcfg.v1:gcfg_v1",
"//vendor/k8s.io/klog/v2:klog",
Expand Down
9 changes: 6 additions & 3 deletions pkg/volumes/openstack/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package openstack

import (
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"context"

"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
"k8s.io/klog/v2"
"sigs.k8s.io/etcd-manager/pkg/privateapi/discovery"
"sigs.k8s.io/etcd-manager/pkg/volumes"
Expand All @@ -27,8 +29,9 @@ import (
var _ discovery.Interface = &OpenstackVolumes{}

func (os *OpenstackVolumes) Poll() (map[string]discovery.Node, error) {
ctx := context.TODO()

allVolumes, err := os.findVolumes(false)
allVolumes, err := os.findVolumes(ctx, false)
if err != nil {
return nil, err
}
Expand All @@ -42,7 +45,7 @@ func (os *OpenstackVolumes) Poll() (map[string]discovery.Node, error) {
}
for i, volume := range instanceToVolumeMap {
mc := NewMetricContext("server", "get")
server, err := servers.Get(os.computeClient, i).Extract()
server, err := servers.Get(ctx, os.computeClient, i).Extract()
if mc.ObserveRequest(err) != nil {
klog.Warningf("Could not find server with id '%s': %v", i, err)
continue
Expand Down
48 changes: 22 additions & 26 deletions pkg/volumes/openstack/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package openstack

import (
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -27,12 +28,11 @@ import (
"strings"
"time"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
cinderv3 "github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack"
cinderv3 "github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v3/volumes"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/volumeattach"
"k8s.io/klog/v2"
"k8s.io/mount-utils"
utilexec "k8s.io/utils/exec"
Expand Down Expand Up @@ -112,6 +112,8 @@ func NewOpenstackVolumes(clusterName string, volumeTags []string, nameTag string
networkCIDR: networkCIDR,
}

ctx := context.Background()

for _, volumeTag := range volumeTags {
tokens := strings.SplitN(volumeTag, "=", 2)
if len(tokens) == 1 {
Expand All @@ -121,12 +123,12 @@ func NewOpenstackVolumes(clusterName string, volumeTags []string, nameTag string
}
}

err = stack.getClients()
err = stack.getClients(ctx)
if err != nil {
return nil, fmt.Errorf("could not build OpenstackVolumes: %v", err)
}

err = stack.discoverTags()
err = stack.discoverTags(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -293,34 +295,34 @@ func getCredential() (gophercloud.AuthOptions, string, bool, error) {
}, cfg.Global.Region, cfg.BlockStorage.IgnoreVolumeAZ, nil
}

func (stack *OpenstackVolumes) getClients() error {
func (stack *OpenstackVolumes) getClients(ctx context.Context) error {
authOption, region, ignoreAZ, err := getCredential()
if err != nil {
return fmt.Errorf("error building openstack credentials: %v", err)
}
stack.ignoreAZ = ignoreAZ
provider, err := openstack.NewClient(authOption.IdentityEndpoint)
client, err := openstack.NewClient(authOption.IdentityEndpoint)
if err != nil {
return fmt.Errorf("error building openstack storage client: %v", err)
}
ua := gophercloud.UserAgent{}
ua.Prepend("etcd-manager")
provider.UserAgent = ua
client.UserAgent = ua
klog.V(4).Infof("Using user-agent %s", ua.Join())

err = openstack.Authenticate(provider, authOption)
err = openstack.Authenticate(ctx, client, authOption)
if err != nil {
return fmt.Errorf("error authenticating openstack client: %v", err)
}

cinderClient, err := openstack.NewBlockStorageV3(provider, gophercloud.EndpointOpts{
cinderClient, err := openstack.NewBlockStorageV3(client, gophercloud.EndpointOpts{
Type: "volumev3",
Region: region,
})
if err != nil {
return fmt.Errorf("error building storage client: %v", err)
}
computeClient, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
computeClient, err := openstack.NewComputeV2(client, gophercloud.EndpointOpts{
Type: "compute",
Region: region,
})
Expand All @@ -337,7 +339,7 @@ func (stack *OpenstackVolumes) InternalIP() net.IP {
return stack.internalIP
}

func (stack *OpenstackVolumes) discoverTags() error {
func (stack *OpenstackVolumes) discoverTags(ctx context.Context) error {

// Project ID
{
Expand All @@ -359,14 +361,8 @@ func (stack *OpenstackVolumes) discoverTags() error {

// Internal IP & zone
{

var extendedServer struct {
servers.Server
availabilityzones.ServerAvailabilityZoneExt
}

mc := NewMetricContext("server", "get")
err := servers.Get(stack.computeClient, strings.TrimSpace(stack.meta.ServerID)).ExtractInto(&extendedServer)
extendedServer, err := servers.Get(ctx, stack.computeClient, strings.TrimSpace(stack.meta.ServerID)).Extract()
if mc.ObserveRequest(err) != nil {
return fmt.Errorf("failed to retrieve server information from cloud: %v", err)
}
Expand Down Expand Up @@ -444,18 +440,18 @@ func (stack *OpenstackVolumes) matchesTags(d *cinderv3.Volume, filterByAZ bool)
}

func (stack *OpenstackVolumes) FindVolumes() ([]*volumes.Volume, error) {
return stack.findVolumes(true)
return stack.findVolumes(context.TODO(), true)
}

func (stack *OpenstackVolumes) findVolumes(filterByAZ bool) ([]*volumes.Volume, error) {
func (stack *OpenstackVolumes) findVolumes(ctx context.Context, filterByAZ bool) ([]*volumes.Volume, error) {
var volumes []*volumes.Volume

klog.V(2).Infof("Listing Openstack disks in %s/%s", stack.project, stack.meta.AvailabilityZone)

mc := NewMetricContext("volumes", "list")
pages, err := cinderv3.List(stack.volumeClient, cinderv3.ListOpts{
TenantID: stack.project,
}).AllPages()
}).AllPages(ctx)
if mc.ObserveRequest(err) != nil {
return volumes, fmt.Errorf("FindVolumes: Failed to list volumes: %v", err)
}
Expand Down Expand Up @@ -585,7 +581,7 @@ func (stack *OpenstackVolumes) AttachVolume(volume *volumes.Volume) error {
VolumeID: volume.ProviderID,
}
mc := NewMetricContext("volume", "attach")
volumeAttachment, err := volumeattach.Create(stack.computeClient, stack.meta.ServerID, opts).Extract()
volumeAttachment, err := volumeattach.Create(context.TODO(), stack.computeClient, stack.meta.ServerID, opts).Extract()
if mc.ObserveRequest(err) != nil {
return fmt.Errorf("error attaching volume %s to server %s: %v", opts.VolumeID, stack.meta.ServerID, err)
}
Expand Down

This file was deleted.

Loading

0 comments on commit ecdaeff

Please sign in to comment.