Skip to content

Commit

Permalink
migrate azfile data plane client to track2 one
Browse files Browse the repository at this point in the history
Signed-off-by: Fan Shang Xiang <[email protected]>
  • Loading branch information
MartinForReal committed Sep 26, 2024
1 parent 180793b commit 480d729
Show file tree
Hide file tree
Showing 63 changed files with 75 additions and 9,841 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ require (
github.com/Azure/go-autorest/autorest/to v0.4.0
github.com/container-storage-interface/spec v1.9.0
github.com/golang/protobuf v1.5.4
github.com/google/uuid v1.6.0
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1
github.com/kubernetes-csi/csi-lib-utils v0.14.1
github.com/kubernetes-csi/csi-proxy/client v1.0.1
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0
github.com/onsi/ginkgo/v2 v2.20.2
github.com/onsi/gomega v1.34.2
github.com/pborman/uuid v1.2.0
github.com/rubiojr/go-vhd v0.0.0-20200706105327-02e210299021
github.com/stretchr/testify v1.9.0
go.uber.org/mock v0.4.0
Expand Down Expand Up @@ -87,7 +87,6 @@ require (
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
Expand All @@ -97,7 +96,6 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
Expand Down
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,6 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0=
github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI=
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
Expand Down Expand Up @@ -839,8 +837,6 @@ github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1v
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
Expand Down Expand Up @@ -946,7 +942,6 @@ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm4
github.com/google/s2a-go v0.1.0/go.mod h1:OJpEgntRZo8ugHpF9hkoLJbS5dSI20XZeXJ9JVywLlM=
github.com/google/s2a-go v0.1.3/go.mod h1:Ej+mSEMGRnqRzjc7VtF+jdBwYG5fuJfiZ8ELkjEwM0A=
github.com/google/s2a-go v0.1.4/go.mod h1:Ej+mSEMGRnqRzjc7VtF+jdBwYG5fuJfiZ8ELkjEwM0A=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand Down Expand Up @@ -1131,8 +1126,6 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/selinux v1.11.0 h1:+5Zbo97w3Lbmb3PeqQtpmTkMwsW5nRI3YaLpt7tQ7oU=
github.com/opencontainers/selinux v1.11.0/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec=
github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY=
github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
Expand Down
29 changes: 17 additions & 12 deletions pkg/azurefile/azurefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-09-01/storage"
"github.com/Azure/azure-storage-file-go/azfile"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/google/uuid"
grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/pborman/uuid"
"github.com/rubiojr/go-vhd/vhd"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -632,7 +632,7 @@ func getValidFileShareName(volumeName string) string {
fileShareName = fileShareName[0:fileShareNameMaxLength]
}
if !checkShareNameBeginAndEnd(fileShareName) || len(fileShareName) < fileShareNameMinLength {
fileShareName = util.GenerateVolumeName("pvc-file", uuid.NewUUID().String(), fileShareNameMaxLength)
fileShareName = util.GenerateVolumeName("pvc-file", uuid.NewString(), fileShareNameMaxLength)
klog.Warningf("the requested volume name (%q) is invalid, so it is regenerated as (%q)", volumeName, fileShareName)
}
fileShareName = strings.Replace(fileShareName, "--", "-", -1)
Expand Down Expand Up @@ -916,22 +916,27 @@ func (d *Driver) CreateFileShare(ctx context.Context, accountOptions *azure.Acco
return wait.ExponentialBackoff(d.cloud.RequestBackoff(), func() (bool, error) {
var err error
if len(secrets) > 0 {
accountName, accountKey, rerr := getStorageAccount(secrets)
if rerr != nil {
return true, rerr
var accountName, accountKey string
accountName, accountKey, err = getStorageAccount(secrets)
if err != nil {
return true, err
}
fileClient, rerr := newAzureFileClient(accountName, accountKey, d.getStorageEndPointSuffix(), &retry.Backoff{Steps: 1})
if rerr != nil {
return true, rerr
var fileClient azureFileClient
fileClient, err = newAzureFileClient(accountName, accountKey, d.getStorageEndPointSuffix(), &retry.Backoff{Steps: 1})
if err != nil {
return true, err
}
err = fileClient.CreateFileShare(ctx, shareOptions)
} else {
_, err = d.cloud.FileClient.WithSubscriptionID(accountOptions.SubscriptionID).CreateFileShare(ctx, accountOptions.ResourceGroup, accountOptions.Name, shareOptions, "")
}
if isRetriableError(err) {
klog.Warningf("CreateFileShare(%s) on account(%s) failed with error(%v), waiting for retrying", shareOptions.Name, accountOptions.Name, err)
sleepIfThrottled(err, fileOpThrottlingSleepSec)
return false, nil
if err != nil {
if isRetriableError(err) {
klog.Warningf("CreateFileShare(%s) on account(%s) failed with error(%v), waiting for retrying", shareOptions.Name, accountOptions.Name, err)
sleepIfThrottled(err, fileOpThrottlingSleepSec)
return false, nil
}
klog.Errorf("CreateFileShare(%s) on account(%s) failed with error(%v)", shareOptions.Name, accountOptions.Name, err)
}
return true, err
})
Expand Down
88 changes: 49 additions & 39 deletions pkg/azurefile/azurefile_dataplane_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ package azurefile

import (
"context"
"errors"
"fmt"
"net/http"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
azs "github.com/Azure/azure-sdk-for-go/storage"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/share"
"k8s.io/klog/v2"

"sigs.k8s.io/cloud-provider-azure/pkg/azclient/utils"
"sigs.k8s.io/cloud-provider-azure/pkg/azureclients/fileclient"
"sigs.k8s.io/cloud-provider-azure/pkg/retry"
)

const (
useHTTPS = true
)

var (
// refer https://github.com/Azure/azure-sdk-for-go/blob/master/storage/client.go#L88.
defaultValidStatusCodes = []int{
Expand All @@ -47,78 +47,88 @@ var (
type azureFileDataplaneClient struct {
accountName string
accountKey string
*azs.FileServiceClient
*service.Client
}

func newAzureFileClient(accountName, accountKey, storageEndpointSuffix string, backoff *retry.Backoff) (azureFileClient, error) {
if storageEndpointSuffix == "" {
storageEndpointSuffix = defaultStorageEndPointSuffix
}

fileClient, err := azs.NewClient(accountName, accountKey, storageEndpointSuffix, azs.DefaultAPIVersion, useHTTPS)
keyCred, err := service.NewSharedKeyCredential(accountName, accountKey)
if err != nil {
return nil, fmt.Errorf("error creating azure client: %v", err)
}

storageEndpoint := fmt.Sprintf("https://%s.file."+storageEndpointSuffix, accountName)
clientOps := utils.GetDefaultAzCoreClientOption()
if backoff != nil {
fileClient.Sender = &azs.DefaultSender{
RetryAttempts: backoff.Steps,
ValidStatusCodes: defaultValidStatusCodes,
RetryDuration: backoff.Duration,
}
clientOps.Retry.MaxRetries = int32(backoff.Steps)
clientOps.Retry.StatusCodes = defaultValidStatusCodes
clientOps.Retry.RetryDelay = backoff.Duration
}
fileClient, err := service.NewClientWithSharedKeyCredential(storageEndpoint, keyCred, &service.ClientOptions{
ClientOptions: clientOps,
})
if err != nil {
return nil, fmt.Errorf("error creating azure client: %v", err)
}

return &azureFileDataplaneClient{
accountName: accountName,
accountKey: accountKey,
FileServiceClient: to.Ptr(fileClient.GetFileService()),
accountName: accountName,
accountKey: accountKey,
Client: fileClient,
}, nil
}

func (f *azureFileDataplaneClient) CreateFileShare(_ context.Context, shareOptions *fileclient.ShareOptions) error {
func (f *azureFileDataplaneClient) CreateFileShare(ctx context.Context, shareOptions *fileclient.ShareOptions) error {
if shareOptions == nil {
return fmt.Errorf("shareOptions of account(%s) is nil", f.accountName)
}
share := f.FileServiceClient.GetShareReference(shareOptions.Name)
share.Properties.Quota = shareOptions.RequestGiB
newlyCreated, err := share.CreateIfNotExists(nil)
shareClient := f.Client.NewShareClient(shareOptions.Name)
_, err := shareClient.Create(ctx, &share.CreateOptions{
Quota: to.Ptr(int32(shareOptions.RequestGiB)),
})

if err != nil {
return fmt.Errorf("failed to create file share, err: %v", err)
}
if !newlyCreated {
klog.V(2).Infof("file share(%s) under account(%s) already exists", shareOptions.Name, f.accountName)
}
return nil
}

// delete a file share
func (f *azureFileDataplaneClient) DeleteFileShare(_ context.Context, shareName string) error {
return f.FileServiceClient.GetShareReference(shareName).Delete(nil)
func (f *azureFileDataplaneClient) DeleteFileShare(ctx context.Context, shareName string) error {
_, err := f.Client.NewShareClient(shareName).Delete(ctx, nil)
return err
}

func (f *azureFileDataplaneClient) ResizeFileShare(_ context.Context, shareName string, sizeGiB int) error {
share := f.FileServiceClient.GetShareReference(shareName)
if share.Properties.Quota >= sizeGiB {
func (f *azureFileDataplaneClient) ResizeFileShare(ctx context.Context, shareName string, sizeGiB int) error {
shareClient := f.Client.NewShareClient(shareName)
shareProps, err := shareClient.GetProperties(ctx, nil)
if err != nil {
return fmt.Errorf("failed to set quota on file share %s, err: %v", shareName, err)
}
if *shareProps.Quota >= int32(sizeGiB) {
klog.Warningf("file share size(%dGi) is already greater or equal than requested size(%dGi), accountName: %s, shareName: %s",
share.Properties.Quota, sizeGiB, f.accountName, shareName)
*shareProps.Quota, sizeGiB, f.accountName, shareName)
return nil
}
share.Properties.Quota = sizeGiB
if err := share.SetProperties(nil); err != nil {
if _, err := shareClient.SetProperties(ctx, &share.SetPropertiesOptions{
Quota: to.Ptr(int32(sizeGiB)),
}); err != nil {
return fmt.Errorf("failed to set quota on file share %s, err: %v", shareName, err)
}
klog.V(4).Infof("resize file share completed, accountName: %s, shareName: %s, sizeGiB: %d", f.accountName, shareName, sizeGiB)
return nil
}

func (f *azureFileDataplaneClient) GetFileShareQuota(_ context.Context, name string) (int, error) {
share := f.FileServiceClient.GetShareReference(name)
exists, err := share.Exists()
func (f *azureFileDataplaneClient) GetFileShareQuota(ctx context.Context, name string) (int, error) {
shareClient := f.Client.NewShareClient(name)
shareProps, err := shareClient.GetProperties(ctx, nil)
if err != nil {
var respErr *azcore.ResponseError
if errors.As(err, &respErr) && respErr != nil && respErr.StatusCode == http.StatusNotFound {
return -1, nil
}
return -1, err
}
if !exists {
return -1, nil
}
return share.Properties.Quota, nil
return int(*shareProps.Quota), nil
}
2 changes: 1 addition & 1 deletion pkg/azurefile/azurefile_dataplane_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestCreateFileShare(t *testing.T) {
func TestNewAzureFileClient(t *testing.T) {
_, actualErr := newAzureFileClient("ut", "ut", "ut", nil)
if actualErr != nil {
expectedErr := fmt.Errorf("error creating azure client: azure: account name is not valid: it must be between 3 and 24 characters, and only may contain numbers and lowercase letters: ut")
expectedErr := fmt.Errorf("error creating azure client: decode account key: illegal base64 data at input byte 0")
if !reflect.DeepEqual(actualErr, expectedErr) {
t.Errorf("actualErr: (%v), expectedErr: (%v)", actualErr, expectedErr)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/azurefile/azurefile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ func TestGetFileShareQuota(t *testing.T) {
mockedFileShareResp: storage.FileShare{},
mockedFileShareErr: nil,
expectedQuota: -1,
expectedError: fmt.Errorf("error creating azure client: azure: account name is not valid: it must be between 3 and 24 characters, and only may contain numbers and lowercase letters: ut"),
expectedError: fmt.Errorf("error creating azure client: decode account key: illegal base64 data at input byte 4"),
},
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/azurefile/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-09-01/storage"
"github.com/Azure/azure-storage-file-go/azfile"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/pborman/uuid"
"github.com/google/uuid"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -623,7 +623,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
diskName = validFileShareName + vhdSuffix
} else {
// use uuid as vhd disk name if file share specified
diskName = uuid.NewUUID().String() + vhdSuffix
diskName = uuid.NewString() + vhdSuffix
}
diskSizeBytes := volumehelper.GiBToBytes(requestGiB)
klog.V(2).Infof("begin to create vhd file(%s) size(%d) on share(%s) on account(%s) type(%s) rg(%s) location(%s)",
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
"strings"
"testing"

"github.com/google/uuid"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/reporters"
"github.com/onsi/gomega"
"github.com/pborman/uuid"
"k8s.io/kubernetes/test/e2e/framework"
"k8s.io/kubernetes/test/e2e/framework/config"
"sigs.k8s.io/azurefile-csi-driver/pkg/azurefile"
Expand Down Expand Up @@ -143,7 +143,7 @@ var _ = ginkgo.BeforeSuite(func(ctx ginkgo.SpecContext) {
driverOptions := azurefile.DriverOptions{
NodeID: os.Getenv("nodeid"),
DriverName: azurefile.DefaultDriverName,
Endpoint: fmt.Sprintf("unix:///tmp/csi-%s.sock", uuid.NewUUID().String()),
Endpoint: fmt.Sprintf("unix:///tmp/csi-%s.sock", uuid.NewString()),
KubeConfig: kubeconfig,
}
azurefileDriver = azurefile.NewDriver(&driverOptions)
Expand Down
4 changes: 2 additions & 2 deletions test/utils/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"html/template"
"os"

"github.com/pborman/uuid"
"github.com/google/uuid"
)

const (
Expand Down Expand Up @@ -124,7 +124,7 @@ func CreateAzureCredentialFile(isAzureChinaCloud bool) (*Credentials, error) {
}

if resourceGroup == "" {
resourceGroup = ResourceGroupPrefix + uuid.NewUUID().String()
resourceGroup = ResourceGroupPrefix + uuid.NewString()
}

if location == "" {
Expand Down
23 changes: 0 additions & 23 deletions vendor/github.com/Azure/azure-sdk-for-go/storage/README.md

This file was deleted.

Loading

0 comments on commit 480d729

Please sign in to comment.