@@ -23,6 +23,7 @@ import (
23
23
"strings"
24
24
25
25
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"
26
+ "github.com/Azure/go-autorest/autorest/to"
26
27
27
28
"k8s.io/klog/v2"
28
29
)
@@ -32,17 +33,18 @@ type AccountOptions struct {
32
33
Name , Type , Kind , ResourceGroup , Location string
33
34
EnableHTTPSTrafficOnly bool
34
35
Tags map [string ]string
36
+ VirtualNetworkResourceIDs []string
35
37
}
36
38
37
39
type accountWithLocation struct {
38
40
Name , StorageType , Location string
39
41
}
40
42
41
- // getStorageAccounts gets name, type, location of all storage accounts in a resource group which matches matchingAccountType, matchingLocation
42
- func (az * Cloud ) getStorageAccounts (matchingAccountType , matchingAccountKind , resourceGroup , matchingLocation string ) ([]accountWithLocation , error ) {
43
+ // getStorageAccounts get matching storage accounts
44
+ func (az * Cloud ) getStorageAccounts (accountOptions * AccountOptions ) ([]accountWithLocation , error ) {
43
45
ctx , cancel := getContextWithCancel ()
44
46
defer cancel ()
45
- result , rerr := az .StorageAccountClient .ListByResourceGroup (ctx , resourceGroup )
47
+ result , rerr := az .StorageAccountClient .ListByResourceGroup (ctx , accountOptions . ResourceGroup )
46
48
if rerr != nil {
47
49
return nil , rerr .Error ()
48
50
}
@@ -51,18 +53,39 @@ func (az *Cloud) getStorageAccounts(matchingAccountType, matchingAccountKind, re
51
53
for _ , acct := range result {
52
54
if acct .Name != nil && acct .Location != nil && acct .Sku != nil {
53
55
storageType := string ((* acct .Sku ).Name )
54
- if matchingAccountType != "" && ! strings .EqualFold (matchingAccountType , storageType ) {
56
+ if accountOptions . Type != "" && ! strings .EqualFold (accountOptions . Type , storageType ) {
55
57
continue
56
58
}
57
59
58
- if matchingAccountKind != "" && ! strings .EqualFold (matchingAccountKind , string (acct .Kind )) {
60
+ if accountOptions . Kind != "" && ! strings .EqualFold (accountOptions . Kind , string (acct .Kind )) {
59
61
continue
60
62
}
61
63
62
64
location := * acct .Location
63
- if matchingLocation != "" && ! strings .EqualFold (matchingLocation , location ) {
65
+ if accountOptions . Location != "" && ! strings .EqualFold (accountOptions . Location , location ) {
64
66
continue
65
67
}
68
+
69
+ if len (accountOptions .VirtualNetworkResourceIDs ) > 0 {
70
+ if acct .AccountProperties == nil || acct .AccountProperties .NetworkRuleSet == nil ||
71
+ acct .AccountProperties .NetworkRuleSet .VirtualNetworkRules == nil {
72
+ continue
73
+ }
74
+
75
+ found := false
76
+ for _ , subnetID := range accountOptions .VirtualNetworkResourceIDs {
77
+ for _ , rule := range * acct .AccountProperties .NetworkRuleSet .VirtualNetworkRules {
78
+ if strings .EqualFold (to .String (rule .VirtualNetworkResourceID ), subnetID ) && rule .Action == storage .Allow {
79
+ found = true
80
+ break
81
+ }
82
+ }
83
+ }
84
+ if ! found {
85
+ continue
86
+ }
87
+ }
88
+
66
89
accounts = append (accounts , accountWithLocation {Name : * acct .Name , StorageType : storageType , Location : location })
67
90
}
68
91
}
@@ -106,9 +129,10 @@ func (az *Cloud) EnsureStorageAccount(accountOptions *AccountOptions, genAccount
106
129
resourceGroup := accountOptions .ResourceGroup
107
130
location := accountOptions .Location
108
131
enableHTTPSTrafficOnly := accountOptions .EnableHTTPSTrafficOnly
132
+
109
133
if len (accountName ) == 0 {
110
134
// find a storage account that matches accountType
111
- accounts , err := az .getStorageAccounts (accountType , accountKind , resourceGroup , location )
135
+ accounts , err := az .getStorageAccounts (accountOptions )
112
136
if err != nil {
113
137
return "" , "" , fmt .Errorf ("could not list storage accounts for account type %s: %v" , accountType , err )
114
138
}
@@ -119,6 +143,24 @@ func (az *Cloud) EnsureStorageAccount(accountOptions *AccountOptions, genAccount
119
143
}
120
144
121
145
if len (accountName ) == 0 {
146
+ // set network rules for storage account
147
+ var networkRuleSet * storage.NetworkRuleSet
148
+ virtualNetworkRules := []storage.VirtualNetworkRule {}
149
+ for _ , subnetID := range accountOptions .VirtualNetworkResourceIDs {
150
+ vnetRule := storage.VirtualNetworkRule {
151
+ VirtualNetworkResourceID : & subnetID ,
152
+ Action : storage .Allow ,
153
+ }
154
+ virtualNetworkRules = append (virtualNetworkRules , vnetRule )
155
+ klog .V (4 ).Infof ("subnetID(%s) has been set" , subnetID )
156
+ }
157
+ if len (virtualNetworkRules ) > 0 {
158
+ networkRuleSet = & storage.NetworkRuleSet {
159
+ VirtualNetworkRules : & virtualNetworkRules ,
160
+ DefaultAction : storage .DefaultActionDeny ,
161
+ }
162
+ }
163
+
122
164
// not found a matching account, now create a new account in current resource group
123
165
accountName = generateStorageAccountName (genAccountNamePrefix )
124
166
if location == "" {
@@ -143,11 +185,14 @@ func (az *Cloud) EnsureStorageAccount(accountOptions *AccountOptions, genAccount
143
185
accountName , resourceGroup , location , accountType , kind , accountOptions .Tags )
144
186
145
187
cp := storage.AccountCreateParameters {
146
- Sku : & storage.Sku {Name : storage .SkuName (accountType )},
147
- Kind : kind ,
148
- AccountPropertiesCreateParameters : & storage.AccountPropertiesCreateParameters {EnableHTTPSTrafficOnly : & enableHTTPSTrafficOnly },
149
- Tags : tags ,
150
- Location : & location }
188
+ Sku : & storage.Sku {Name : storage .SkuName (accountType )},
189
+ Kind : kind ,
190
+ AccountPropertiesCreateParameters : & storage.AccountPropertiesCreateParameters {
191
+ EnableHTTPSTrafficOnly : & enableHTTPSTrafficOnly ,
192
+ NetworkRuleSet : networkRuleSet ,
193
+ },
194
+ Tags : tags ,
195
+ Location : & location }
151
196
152
197
ctx , cancel := getContextWithCancel ()
153
198
defer cancel ()
0 commit comments