Skip to content

Commit 2e9bb6a

Browse files
committed
feat: add vnet setting in storage class parmaters
fix trivy
1 parent a0feb42 commit 2e9bb6a

File tree

6 files changed

+46
-16
lines changed

6 files changed

+46
-16
lines changed

.github/workflows/trivy.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ jobs:
99
name: Build
1010
runs-on: ubuntu-18.04
1111
steps:
12+
- name: Set up Go 1.x
13+
uses: actions/setup-go@v2
14+
with:
15+
go-version: ^1.16
16+
id: go
17+
1218
- name: Checkout code
1319
uses: actions/checkout@v2
1420

docs/driver-parameters.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ volumeAttributes.secretName | secret name that stores storage account name and k
6262
volumeAttributes.secretNamespace | secret namespace | `default`,`kube-system`, etc | No | `default`
6363
nodeStageSecretRef.name | secret name that stores(check below examples):<br>`azurestorageaccountkey`<br>`azurestorageaccountsastoken`<br>`msisecret`<br>`azurestoragespnclientsecret` | existing Kubernetes secret name | No |
6464
nodeStageSecretRef.namespace | secret namespace | k8s namespace | Yes |
65+
--- | **Following parameters are only for NFS vnet setting** | --- | --- |
66+
vnetResourceGroup | specify vnet resource group where virtual network is | existing resource group name | No | if empty, driver will use the `vnetResourceGroup` value in azure cloud config file
67+
vnetName | virtual network name | existing virtual network name | No | if empty, driver will use the `vnetName` value in azure cloud config file
68+
subnetName | subnet name | existing subnet name of the agent node | No | if empty, driver will use the `subnetName` value in azure cloud config file
6569
--- | **Following parameters are only for feature: blobfuse [Managed Identity and Service Principal Name auth](https://github.com/Azure/azure-storage-fuse#environment-variables)** | --- | --- |
6670
volumeAttributes.AzureStorageAuthType | Authentication Type | `Key`, `SAS`, `MSI`, `SPN` | No | `Key`
6771
volumeAttributes.AzureStorageIdentityClientID | Identity Client ID | | No |

pkg/blob/azure.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,29 +182,36 @@ func (d *Driver) getKeyvaultToken() (authorizer autorest.Authorizer, err error)
182182
return authorizer, nil
183183
}
184184

185-
func (d *Driver) updateSubnetServiceEndpoints(ctx context.Context) error {
185+
func (d *Driver) updateSubnetServiceEndpoints(ctx context.Context, vnetResourceGroup, vnetName, subnetName string) error {
186186
if d.cloud.SubnetsClient == nil {
187187
return fmt.Errorf("SubnetsClient is nil")
188188
}
189189

190-
resourceGroup := d.cloud.ResourceGroup
191-
if len(d.cloud.VnetResourceGroup) > 0 {
192-
resourceGroup = d.cloud.VnetResourceGroup
190+
if vnetResourceGroup == "" {
191+
vnetResourceGroup = d.cloud.ResourceGroup
192+
if len(d.cloud.VnetResourceGroup) > 0 {
193+
vnetResourceGroup = d.cloud.VnetResourceGroup
194+
}
193195
}
196+
194197
location := d.cloud.Location
195-
vnetName := d.cloud.VnetName
196-
subnetName := d.cloud.SubnetName
198+
if vnetName == "" {
199+
vnetName = d.cloud.VnetName
200+
}
201+
if subnetName == "" {
202+
subnetName = d.cloud.SubnetName
203+
}
197204

198205
klog.V(2).Infof("updateSubnetServiceEndpoints on vnetName: %s, subnetName: %s, location: %s", vnetName, subnetName, location)
199206
if subnetName == "" || vnetName == "" || location == "" {
200207
return fmt.Errorf("value of subnetName, vnetName or location is empty")
201208
}
202209

203-
lockKey := resourceGroup + vnetName + subnetName
210+
lockKey := vnetResourceGroup + vnetName + subnetName
204211
d.subnetLockMap.LockEntry(lockKey)
205212
defer d.subnetLockMap.UnlockEntry(lockKey)
206213

207-
subnet, err := d.cloud.SubnetsClient.Get(ctx, resourceGroup, vnetName, subnetName, "")
214+
subnet, err := d.cloud.SubnetsClient.Get(ctx, vnetResourceGroup, vnetName, subnetName, "")
208215
if err != nil {
209216
return fmt.Errorf("failed to get the subnet %s under vnet %s: %v", subnetName, vnetName, err)
210217
}
@@ -233,7 +240,7 @@ func (d *Driver) updateSubnetServiceEndpoints(ctx context.Context) error {
233240
serviceEndpoints = append(serviceEndpoints, storageServiceEndpoint)
234241
subnet.SubnetPropertiesFormat.ServiceEndpoints = &serviceEndpoints
235242

236-
if err := d.cloud.SubnetsClient.CreateOrUpdate(ctx, resourceGroup, vnetName, subnetName, subnet); err != nil {
243+
if err := d.cloud.SubnetsClient.CreateOrUpdate(ctx, vnetResourceGroup, vnetName, subnetName, subnet); err != nil {
237244
return fmt.Errorf("failed to update the subnet %s under vnet %s: %v", subnetName, vnetName, err)
238245
}
239246
klog.V(2).Infof("serviceEndpoint(%s) is appended in subnet(%s)", storageService, subnetName)

pkg/blob/azure_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func TestUpdateSubnetServiceEndpoints(t *testing.T) {
285285
retErr := retry.NewError(false, fmt.Errorf("the subnet does not exist"))
286286
mockSubnetClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(network.Subnet{}, retErr).Times(1)
287287
expectedErr := fmt.Errorf("failed to get the subnet %s under vnet %s: %v", config.SubnetName, config.VnetName, retErr)
288-
err := d.updateSubnetServiceEndpoints(ctx)
288+
err := d.updateSubnetServiceEndpoints(ctx, "", "", "")
289289
if !reflect.DeepEqual(err, expectedErr) {
290290
t.Errorf("Unexpected error: %v", err)
291291
}
@@ -297,7 +297,7 @@ func TestUpdateSubnetServiceEndpoints(t *testing.T) {
297297
mockSubnetClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(network.Subnet{}, nil).Times(1)
298298
mockSubnetClient.EXPECT().CreateOrUpdate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
299299

300-
err := d.updateSubnetServiceEndpoints(ctx)
300+
err := d.updateSubnetServiceEndpoints(ctx, "", "", "")
301301
if !reflect.DeepEqual(err, nil) {
302302
t.Errorf("Unexpected error: %v", err)
303303
}
@@ -313,7 +313,7 @@ func TestUpdateSubnetServiceEndpoints(t *testing.T) {
313313
mockSubnetClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fakeSubnet, nil).Times(1)
314314
mockSubnetClient.EXPECT().CreateOrUpdate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
315315

316-
err := d.updateSubnetServiceEndpoints(ctx)
316+
err := d.updateSubnetServiceEndpoints(ctx, "", "", "")
317317
if !reflect.DeepEqual(err, nil) {
318318
t.Errorf("Unexpected error: %v", err)
319319
}
@@ -331,7 +331,7 @@ func TestUpdateSubnetServiceEndpoints(t *testing.T) {
331331
mockSubnetClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fakeSubnet, nil).Times(1)
332332
mockSubnetClient.EXPECT().CreateOrUpdate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
333333

334-
err := d.updateSubnetServiceEndpoints(ctx)
334+
err := d.updateSubnetServiceEndpoints(ctx, "", "", "")
335335
if !reflect.DeepEqual(err, nil) {
336336
t.Errorf("Unexpected error: %v", err)
337337
}
@@ -352,7 +352,7 @@ func TestUpdateSubnetServiceEndpoints(t *testing.T) {
352352

353353
mockSubnetClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fakeSubnet, nil).Times(1)
354354

355-
err := d.updateSubnetServiceEndpoints(ctx)
355+
err := d.updateSubnetServiceEndpoints(ctx, "", "", "")
356356
if !reflect.DeepEqual(err, nil) {
357357
t.Errorf("Unexpected error: %v", err)
358358
}
@@ -363,7 +363,7 @@ func TestUpdateSubnetServiceEndpoints(t *testing.T) {
363363
testFunc: func(t *testing.T) {
364364
d.cloud.SubnetsClient = nil
365365
expectedErr := fmt.Errorf("SubnetsClient is nil")
366-
err := d.updateSubnetServiceEndpoints(ctx)
366+
err := d.updateSubnetServiceEndpoints(ctx, "", "", "")
367367
if !reflect.DeepEqual(err, expectedErr) {
368368
t.Errorf("Unexpected error: %v", err)
369369
}

pkg/blob/blob.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ const (
8080
defaultSecretAccountKey = "azurestorageaccountkey"
8181
fuse = "fuse"
8282
nfs = "nfs"
83+
vnetResourceGroupField = "vnetresourcegroup"
84+
vnetNameField = "vnetname"
85+
subnetNameField = "subnetname"
8386

8487
// See https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata#container-names
8588
containerNameMinLength = 3

pkg/blob/controllerserver.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
6868
}
6969
var storageAccountType, resourceGroup, location, account, containerName, protocol, customTags, secretName, secretNamespace string
7070
var isHnsEnabled *bool
71+
var vnetResourceGroup, vnetName, subnetName string
7172
// set allowBlobPublicAccess as false by default
7273
allowBlobPublicAccess := to.BoolPtr(false)
7374

@@ -123,6 +124,12 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
123124
// no op, only used in NodeStageVolume
124125
case storageEndpointSuffixField:
125126
// no op, only used in NodeStageVolume
127+
case vnetResourceGroupField:
128+
vnetResourceGroup = v
129+
case vnetNameField:
130+
vnetName = v
131+
case subnetNameField:
132+
subnetName = v
126133
default:
127134
return nil, fmt.Errorf("invalid parameter %s in storage class", k)
128135
}
@@ -153,7 +160,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
153160
vnetResourceID := d.getSubnetResourceID()
154161
klog.V(2).Infof("set vnetResourceID(%s) for NFS protocol", vnetResourceID)
155162
vnetResourceIDs = []string{vnetResourceID}
156-
if err := d.updateSubnetServiceEndpoints(ctx); err != nil {
163+
if err := d.updateSubnetServiceEndpoints(ctx, vnetResourceGroup, vnetName, subnetName); err != nil {
157164
return nil, status.Errorf(codes.Internal, "update service endpoints failed with error: %v", err)
158165
}
159166
// NFS protocol does not need account key
@@ -187,6 +194,9 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
187194
IsHnsEnabled: isHnsEnabled,
188195
EnableNfsV3: enableNfsV3,
189196
AllowBlobPublicAccess: allowBlobPublicAccess,
197+
VNetResourceGroup: vnetResourceGroup,
198+
VNetName: vnetName,
199+
SubnetName: subnetName,
190200
}
191201

192202
var accountKey string

0 commit comments

Comments
 (0)