@@ -116,61 +116,15 @@ public abstract class CreateOrUpdateKubeBase : KubeCmdletBase
116
116
[ Alias ( "SshKeyPath" ) ]
117
117
public string SshKeyValue { get ; set ; }
118
118
119
+ [ Parameter ( Mandatory = false , HelpMessage = "Grant the 'acrpull' role of the specified ACR to AKS Service Principal, e.g. myacr" ) ]
120
+ public string AcrNameToAttach { get ; set ; }
121
+
119
122
[ Parameter ( Mandatory = false , HelpMessage = "Run cmdlet in the background" ) ]
120
123
public SwitchParameter AsJob { get ; set ; }
121
124
122
125
[ Parameter ( Mandatory = false ) ]
123
126
public Hashtable Tag { get ; set ; }
124
127
125
- protected virtual ManagedCluster BuildNewCluster ( )
126
- {
127
- BeforeBuildNewCluster ( ) ;
128
-
129
- var defaultAgentPoolProfile = new ManagedClusterAgentPoolProfile (
130
- name : NodeName ?? "default" ,
131
- count : NodeCount ,
132
- vmSize : NodeVmSize ,
133
- osDiskSizeGB : NodeOsDiskSize ) ;
134
-
135
- if ( this . IsParameterBound ( c => c . NodeMinCount ) )
136
- {
137
- defaultAgentPoolProfile . MinCount = NodeMinCount ;
138
- }
139
- if ( this . IsParameterBound ( c => c . NodeMaxCount ) )
140
- {
141
- defaultAgentPoolProfile . MaxCount = NodeMaxCount ;
142
- }
143
- if ( EnableNodeAutoScaling . IsPresent )
144
- {
145
- defaultAgentPoolProfile . EnableAutoScaling = EnableNodeAutoScaling . ToBool ( ) ;
146
- }
147
-
148
- var pubKey =
149
- new List < ContainerServiceSshPublicKey > { new ContainerServiceSshPublicKey ( SshKeyValue ) } ;
150
-
151
- var linuxProfile =
152
- new ContainerServiceLinuxProfile ( LinuxProfileAdminUserName ,
153
- new ContainerServiceSshConfiguration ( pubKey ) ) ;
154
-
155
- var acsServicePrincipal = EnsureServicePrincipal ( ServicePrincipalIdAndSecret ? . UserName , ServicePrincipalIdAndSecret ? . Password ? . ConvertToString ( ) ) ;
156
-
157
- var spProfile = new ManagedClusterServicePrincipalProfile (
158
- acsServicePrincipal . SpId ,
159
- acsServicePrincipal . ClientSecret ) ;
160
-
161
- WriteVerbose ( string . Format ( Resources . DeployingYourManagedKubeCluster , AcsSpFilePath ) ) ;
162
- var managedCluster = new ManagedCluster (
163
- Location ,
164
- name : Name ,
165
- tags : TagsConversionHelper . CreateTagDictionary ( Tag , true ) ,
166
- dnsPrefix : DnsNamePrefix ,
167
- kubernetesVersion : KubernetesVersion ,
168
- agentPoolProfiles : new List < ManagedClusterAgentPoolProfile > { defaultAgentPoolProfile } ,
169
- linuxProfile : linuxProfile ,
170
- servicePrincipalProfile : spProfile ) ;
171
- return managedCluster ;
172
- }
173
-
174
128
protected void BeforeBuildNewCluster ( )
175
129
{
176
130
if ( ! string . IsNullOrEmpty ( ResourceGroupName ) && string . IsNullOrEmpty ( Location ) )
@@ -271,17 +225,15 @@ protected AcsServicePrincipal EnsureServicePrincipal(string spId = null, string
271
225
{
272
226
clientSecret = RandomBase64String ( 16 ) ;
273
227
}
274
- var salt = RandomBase64String ( 3 ) ;
275
- var url = $ "http://{ salt } .{ DnsNamePrefix } .{ Location } .cloudapp.azure.com";
276
228
277
- acsServicePrincipal = BuildServicePrincipal ( Name , url , clientSecret ) ;
229
+ acsServicePrincipal = BuildServicePrincipal ( Name , clientSecret ) ;
278
230
WriteVerbose ( Resources . CreatedANewServicePrincipalAndAssignedTheContributorRole ) ;
279
231
StoreServicePrincipal ( acsServicePrincipal ) ;
280
232
}
281
233
return acsServicePrincipal ;
282
234
}
283
235
284
- private AcsServicePrincipal BuildServicePrincipal ( string name , string url , string clientSecret )
236
+ private AcsServicePrincipal BuildServicePrincipal ( string name , string clientSecret )
285
237
{
286
238
var pwCreds = new PasswordCredential (
287
239
value : clientSecret ,
@@ -291,8 +243,8 @@ private AcsServicePrincipal BuildServicePrincipal(string name, string url, strin
291
243
var app = GraphClient . Applications . Create ( new ApplicationCreateParameters (
292
244
false ,
293
245
name ,
294
- new List < string > { url } ,
295
- url ,
246
+ new List < string > { } ,
247
+ null ,
296
248
passwordCredentials : new List < PasswordCredential > { pwCreds } ) ) ;
297
249
298
250
ServicePrincipal sp = null ;
@@ -316,6 +268,22 @@ private AcsServicePrincipal BuildServicePrincipal(string name, string url, strin
316
268
return new AcsServicePrincipal { SpId = app . AppId , ClientSecret = clientSecret , ObjectId = app . ObjectId } ;
317
269
}
318
270
271
+ protected RoleAssignment GetRoleAssignmentWithRoleDefinitionId ( string roleDefinitionId )
272
+ {
273
+ RoleAssignment roleAssignment = null ;
274
+ var actionSuccess = RetryAction ( ( ) =>
275
+ {
276
+ roleAssignment = AuthClient . RoleAssignments . List ( ) . Where ( x => x . Properties . RoleDefinitionId == roleDefinitionId && x . Name == Name ) . FirstOrDefault ( ) ;
277
+ } ) ;
278
+ if ( ! actionSuccess )
279
+ {
280
+ throw new AzPSInvalidOperationException (
281
+ Resources . CouldNotGetAcrRoleAssignment ,
282
+ desensitizedMessage : Resources . CouldNotGetAcrRoleAssignment ) ;
283
+ }
284
+ return roleAssignment ;
285
+ }
286
+
319
287
protected void AddAcrRoleAssignment ( string acrName , string acrParameterName , AcsServicePrincipal acsServicePrincipal )
320
288
{
321
289
string acrResourceId = null ;
@@ -335,8 +303,14 @@ protected void AddAcrRoleAssignment(string acrName, string acrParameterName, Acs
335
303
}
336
304
337
305
var roleId = GetRoleId ( "acrpull" , acrResourceId ) ;
306
+ RoleAssignment roleAssignment = GetRoleAssignmentWithRoleDefinitionId ( roleId ) ;
307
+ if ( roleAssignment != null )
308
+ {
309
+ WriteWarning ( string . Format ( Resources . AcrRoleAssignmentIsAlreadyExist , acrResourceId ) ) ;
310
+ return ;
311
+ }
338
312
var spObjectId = acsServicePrincipal . ObjectId ;
339
- if ( spObjectId == null )
313
+ if ( spObjectId == null )
340
314
{
341
315
try
342
316
{
0 commit comments