forked from kubernetes-sigs/azurefile-csi-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44cf32f
commit ffce2a8
Showing
12 changed files
with
404 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package azurefile | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to" | ||
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage" | ||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/cloud-provider-azure/pkg/azclient" | ||
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/fileshareclient" | ||
azure "sigs.k8s.io/cloud-provider-azure/pkg/provider" | ||
) | ||
|
||
type azureFileMgmtClient struct { | ||
fileShareClient fileshareclient.Interface | ||
accountOptions *azure.AccountOptions | ||
} | ||
|
||
func newAzureFileMgmtClient(clientFactory azclient.ClientFactory, accountOptions *azure.AccountOptions) (azureFileClient, error) { | ||
if clientFactory == nil { | ||
return nil, fmt.Errorf("clientFactory is nil") | ||
} | ||
if accountOptions == nil { | ||
return nil, fmt.Errorf("accountOptions is nil") | ||
} | ||
fileShareClient, err := clientFactory.GetFileShareClientForSub(accountOptions.SubscriptionID) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get file share client for subscription %s: %w", accountOptions.SubscriptionID, err) | ||
} | ||
|
||
return &azureFileMgmtClient{ | ||
accountOptions: accountOptions, | ||
fileShareClient: fileShareClient, | ||
}, nil | ||
|
||
} | ||
|
||
// CreateFileShare creates a file share, using a matching storage account type, account kind, etc. | ||
// storage account will be created if specified account is not found | ||
func (az *azureFileMgmtClient) CreateFileShare(ctx context.Context, shareOptions *ShareOptions) error { | ||
if shareOptions == nil { | ||
return fmt.Errorf("shareOptions of account(%s) is nil", az.accountOptions.Name) | ||
} | ||
az.accountOptions.EnableHTTPSTrafficOnly = true | ||
if shareOptions.Protocol == armstorage.EnabledProtocolsNFS { | ||
az.accountOptions.EnableHTTPSTrafficOnly = false | ||
} | ||
shareOps := armstorage.FileShare{ | ||
Name: to.Ptr(shareOptions.Name), | ||
FileShareProperties: &armstorage.FileShareProperties{}, | ||
} | ||
if shareOptions.RequestGiB > 0 { | ||
quota := int32(shareOptions.RequestGiB) | ||
shareOps.FileShareProperties.ShareQuota = "a | ||
} | ||
if shareOptions.Protocol == armstorage.EnabledProtocolsNFS { | ||
shareOps.FileShareProperties.EnabledProtocols = to.Ptr(shareOptions.Protocol) | ||
} | ||
if shareOptions.AccessTier != "" { | ||
shareOps.FileShareProperties.AccessTier = to.Ptr(armstorage.ShareAccessTier(shareOptions.AccessTier)) | ||
} | ||
if shareOptions.RootSquash != "" { | ||
shareOps.FileShareProperties.RootSquash = to.Ptr(armstorage.RootSquashType(shareOptions.RootSquash)) | ||
} | ||
if shareOptions.Metadata != nil { | ||
shareOps.FileShareProperties.Metadata = shareOptions.Metadata | ||
} | ||
if _, err := az.fileShareClient.Create(ctx, az.accountOptions.ResourceGroup, az.accountOptions.Name, shareOptions.Name, shareOps); err != nil { | ||
return fmt.Errorf("failed to create share %s in account %s: %w", shareOptions.Name, az.accountOptions.Name, err) | ||
} | ||
klog.V(4).Infof("created share %s in account %s", shareOptions.Name, az.accountOptions.Name) | ||
return nil | ||
} | ||
|
||
// DeleteFileShare deletes a file share using storage account name and key | ||
func (az *azureFileMgmtClient) DeleteFileShare(ctx context.Context, shareName string) error { | ||
if err := az.fileShareClient.Delete(ctx, az.accountOptions.ResourceGroup, az.accountOptions.Name, shareName); err != nil { | ||
return err | ||
} | ||
klog.V(4).Infof("share %s deleted", shareName) | ||
return nil | ||
} | ||
|
||
// ResizeFileShare resizes a file share | ||
func (az *azureFileMgmtClient) ResizeFileShare(ctx context.Context, name string, sizeGiB int) error { | ||
fileShare, err := az.fileShareClient.Get(ctx, az.accountOptions.ResourceGroup, az.accountOptions.Name, name) | ||
if err != nil { | ||
return err | ||
} | ||
if int(*fileShare.FileShareProperties.ShareQuota) >= sizeGiB { | ||
klog.Warningf("file share size(%dGi) is already greater or equal than requested size(%dGi), accountName: %s, shareName: %s", | ||
*fileShare.FileShareProperties.ShareQuota, sizeGiB, az.accountOptions.Name, name) | ||
return nil | ||
} | ||
|
||
fileShare.FileShareProperties.ShareQuota = to.Ptr(int32(sizeGiB)) | ||
_, err = az.fileShareClient.Update(ctx, az.accountOptions.ResourceGroup, az.accountOptions.Name, name, *fileShare) | ||
return err | ||
} | ||
|
||
// GetFileShare gets a file share | ||
func (az *azureFileMgmtClient) GetFileShareQuota(ctx context.Context, name string) (int, error) { | ||
share, err := az.fileShareClient.Get(ctx, az.accountOptions.ResourceGroup, az.accountOptions.Name, name) | ||
if err != nil { | ||
return -1, err | ||
} | ||
return int(*share.FileShareProperties.ShareQuota), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package azurefile | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"go.uber.org/mock/gomock" | ||
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/fileshareclient/mock_fileshareclient" | ||
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/mock_azclient" | ||
"sigs.k8s.io/cloud-provider-azure/pkg/provider" | ||
) | ||
|
||
func TestNewAzureMgmtFileClientClientFactoryNil(t *testing.T) { | ||
_, actualErr := newAzureFileMgmtClient(nil, nil) | ||
if actualErr != nil { | ||
expectedErr := fmt.Errorf("clientFactory is nil") | ||
if !reflect.DeepEqual(actualErr, expectedErr) { | ||
t.Errorf("actualErr: (%v), expectedErr: (%v)", actualErr, expectedErr) | ||
} | ||
} | ||
} | ||
func TestNewAzureMgmtFileClientAccountOptionNil(t *testing.T) { | ||
cntl := gomock.NewController(t) | ||
defer cntl.Finish() | ||
_, actualErr := newAzureFileMgmtClient(mock_azclient.NewMockClientFactory(cntl), nil) | ||
if actualErr != nil { | ||
expectedErr := fmt.Errorf("accountOptions is nil") | ||
if !reflect.DeepEqual(actualErr, expectedErr) { | ||
t.Errorf("actualErr: (%v), expectedErr: (%v)", actualErr, expectedErr) | ||
} | ||
} | ||
} | ||
|
||
func TestNewAzureMgmtFileClient(t *testing.T) { | ||
cntl := gomock.NewController(t) | ||
defer cntl.Finish() | ||
clientFactory := mock_azclient.NewMockClientFactory(cntl) | ||
fileClient := mock_fileshareclient.NewMockInterface(cntl) | ||
clientFactory.EXPECT().GetFileShareClientForSub("testsub").Return(fileClient, nil) | ||
_, actualErr := newAzureFileMgmtClient(clientFactory, &provider.AccountOptions{ | ||
SubscriptionID: "testsub", | ||
ResourceGroup: "testrg", | ||
}) | ||
if actualErr != nil { | ||
expectedErr := fmt.Errorf("accountOptions is nil") | ||
if !reflect.DeepEqual(actualErr, expectedErr) { | ||
t.Errorf("actualErr: (%v), expectedErr: (%v)", actualErr, expectedErr) | ||
} | ||
} | ||
} |
Oops, something went wrong.