Skip to content

Commit dec2240

Browse files
authored
[AKS] fix the nextLink issue (#22001)
1 parent fe8fdd5 commit dec2240

File tree

5 files changed

+57
-47
lines changed

5 files changed

+57
-47
lines changed

src/Aks/Aks/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fixed the issue of handling `nextLink` in `Set-AzAksCluster`. [#21846]
22+
* Fixed the issue of parameter `AcrNameToDetach` in `Set-AzAksCluster` due to role assignment name is a guid.
2123
* Added breaking change message for parameter `DockerBridgeCidr` in `New-AzAksCluster`.
2224
* Supported the value `AzureLinux` for parameter `-NodeOsSKU` in `New-AzAksCluster` and parameter `-OsSKU` in `New-AzAksNodePool`.
2325
* Fixed the issue of `-DisableLocalAccount` for `Set-AzAksCluster`. [#21835]

src/Aks/Aks/Commands/CreateOrUpdateKubeBase.cs

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,14 @@ private AcsServicePrincipal BuildServicePrincipal(string name, string clientSecr
356356
return new AcsServicePrincipal { SpId = app.AppId, ClientSecret = clientSecret, ObjectId = sp.Id };
357357
}
358358

359-
protected RoleAssignment GetRoleAssignmentWithRoleDefinitionId(string roleDefinitionId)
359+
protected RoleAssignment GetRoleAssignmentWithRoleDefinitionId(string roleDefinitionId, string acrResourceId, string acsServicePrincipalObjectId)
360360
{
361361
RoleAssignment roleAssignment = null;
362362
var actionSuccess = RetryAction(() =>
363363
{
364-
roleAssignment = AuthClient.RoleAssignments.List().Where(x => x.Properties.RoleDefinitionId == roleDefinitionId && x.Name == Name).FirstOrDefault();
364+
roleAssignment = AuthClient.RoleAssignments.ListForScope(acrResourceId)
365+
.Where(x => (x.Properties.RoleDefinitionId == roleDefinitionId && (x.Name == Name || x.Properties.PrincipalId == acsServicePrincipalObjectId)))
366+
.FirstOrDefault();
365367
});
366368
if (!actionSuccess)
367369
{
@@ -374,29 +376,33 @@ protected RoleAssignment GetRoleAssignmentWithRoleDefinitionId(string roleDefini
374376

375377
protected void AddAcrRoleAssignment(string acrName, string acrParameterName, AcsServicePrincipal acsServicePrincipal)
376378
{
377-
string acrResourceId = null;
378-
try
379-
{
380-
//Find Acr resourceId first
381-
var acrQuery = new ODataQuery<GenericResourceFilter>($"$filter=resourceType eq 'Microsoft.ContainerRegistry/registries' and name eq '{acrName}'");
382-
var acrObjects = RmClient.Resources.List(acrQuery);
383-
acrResourceId = acrObjects.First().Id;
384-
}
385-
catch (Exception)
386-
{
387-
throw new AzPSArgumentException(
388-
string.Format(Resources.CouldNotFindSpecifiedAcr, acrName),
389-
acrParameterName,
390-
string.Format(Resources.CouldNotFindSpecifiedAcr, "*"));
391-
}
379+
string acrResourceId = getSpecifiedAcr(acrName, acrParameterName);
380+
381+
var roleDefinitionId = GetRoleId("acrpull", acrResourceId);
382+
var spObjectId = getSPObjectId(acsServicePrincipal);
392383

393-
var roleId = GetRoleId("acrpull", acrResourceId);
394-
RoleAssignment roleAssignment = GetRoleAssignmentWithRoleDefinitionId(roleId);
384+
RoleAssignment roleAssignment = GetRoleAssignmentWithRoleDefinitionId(roleDefinitionId, acrResourceId, spObjectId);
395385
if (roleAssignment != null)
396386
{
397387
WriteWarning(string.Format(Resources.AcrRoleAssignmentIsAlreadyExist, acrResourceId));
398388
return;
399389
}
390+
391+
var success = RetryAction(() =>
392+
AuthClient.RoleAssignments.Create(acrResourceId, Guid.NewGuid().ToString(), new RoleAssignmentCreateParameters()
393+
{
394+
Properties = new RoleAssignmentProperties(roleDefinitionId, spObjectId)
395+
}), Resources.AddRoleAssignment);
396+
397+
if (!success)
398+
{
399+
throw new AzPSInvalidOperationException(
400+
Resources.CouldNotAddAcrRoleAssignment,
401+
desensitizedMessage: Resources.CouldNotAddAcrRoleAssignment);
402+
}
403+
}
404+
405+
protected string getSPObjectId(AcsServicePrincipal acsServicePrincipal) {
400406
var spObjectId = acsServicePrincipal.ObjectId;
401407
if (spObjectId == null)
402408
{
@@ -414,17 +420,31 @@ protected void AddAcrRoleAssignment(string acrName, string acrParameterName, Acs
414420
string.Format(Resources.CouldNotFindObjectIdForServicePrincipal, "*"));
415421
}
416422
}
417-
var success = RetryAction(() =>
418-
AuthClient.RoleAssignments.Create(acrResourceId, Guid.NewGuid().ToString(), new RoleAssignmentCreateParameters()
419-
{
420-
Properties = new RoleAssignmentProperties(roleId, spObjectId)
421-
}), Resources.AddRoleAssignment);
423+
return spObjectId;
424+
}
422425

423-
if (!success)
426+
protected string getSpecifiedAcr(string acrName, string acrParameterName) {
427+
try
424428
{
425-
throw new AzPSInvalidOperationException(
426-
Resources.CouldNotAddAcrRoleAssignment,
427-
desensitizedMessage: Resources.CouldNotAddAcrRoleAssignment);
429+
//Find Acr resourceId first
430+
var acrQuery = new ODataQuery<GenericResourceFilter>($"$filter=resourceType eq 'Microsoft.ContainerRegistry/registries' and name eq '{acrName}'");
431+
var acrObjects = RmClient.Resources.List(acrQuery);
432+
while (acrObjects.Count() == 0 && acrObjects.NextPageLink != null)
433+
{
434+
acrObjects = RmClient.Resources.ListNext(acrObjects.NextPageLink);
435+
}
436+
if (acrObjects.Count() == 0)
437+
{
438+
throw new AzPSArgumentException(
439+
string.Format(Resources.CouldNotFindSpecifiedAcr, acrName),
440+
acrParameterName,
441+
string.Format(Resources.CouldNotFindSpecifiedAcr, "*"));
442+
}
443+
return acrObjects.First().Id;
444+
}
445+
catch (Exception ex)
446+
{
447+
throw new AzPSArgumentException(string.Format(Resources.CouldNotFindSpecifiedAcr, acrName), ex, string.Format(Resources.CouldNotFindSpecifiedAcr, "*"));
428448
}
429449
}
430450

src/Aks/Aks/Commands/SetAzureRmAks.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -463,24 +463,12 @@ public override void ExecuteCmdlet()
463463

464464
private void RemoveAcrRoleAssignment(string acrName, string acrParameterName, AcsServicePrincipal acsServicePrincipal)
465465
{
466-
string acrResourceId = null;
467-
try
468-
{
469-
//Find Acr resourceId first
470-
var acrQuery = new ODataQuery<GenericResourceFilter>($"$filter=resourceType eq 'Microsoft.ContainerRegistry/registries' and name eq '{acrName}'");
471-
var acrObjects = RmClient.Resources.List(acrQuery);
472-
acrResourceId = acrObjects.First().Id;
473-
}
474-
catch (Exception)
475-
{
476-
throw new AzPSArgumentException(
477-
string.Format(Resources.CouldNotFindSpecifiedAcr, acrName),
478-
acrParameterName,
479-
string.Format(Resources.CouldNotFindSpecifiedAcr, "*"));
480-
}
466+
string acrResourceId = getSpecifiedAcr(acrName, acrParameterName);
481467

482468
var roleDefinitionId = GetRoleId("acrpull", acrResourceId);
483-
RoleAssignment roleAssignment = GetRoleAssignmentWithRoleDefinitionId(roleDefinitionId);
469+
var spObjectId = getSPObjectId(acsServicePrincipal);
470+
471+
RoleAssignment roleAssignment = GetRoleAssignmentWithRoleDefinitionId(roleDefinitionId, acrResourceId, spObjectId);
484472
if (roleAssignment == null)
485473
{
486474
throw new AzPSInvalidOperationException(

src/Aks/Aks/Properties/Resources.Designer.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Aks/Aks/Properties/Resources.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@
349349
<value>Could not find object id of service principal : {0}, please make sure you have graph directory.read permission which is required for grant acrpull permission.</value>
350350
</data>
351351
<data name="CouldNotFindSpecifiedAcr" xml:space="preserve">
352-
<value>Could not find specified Acr '{0}' to attach.</value>
352+
<value>Could not find specified Acr '{0}' to attach or detach.</value>
353353
</data>
354354
<data name="AgentPoolAlreadyExistsError" xml:space="preserve">
355355
<value>The node pool already exists. Please use Update-AzAksNodePool for update.</value>

0 commit comments

Comments
 (0)