Skip to content

Commit 72b7a0c

Browse files
add polling
1 parent 044a407 commit 72b7a0c

4 files changed

Lines changed: 44 additions & 9 deletions

File tree

src/fleet/azext_fleet/_client_factory.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from azure.cli.core.commands.client_factory import get_mgmt_service_client
77
from azure.mgmt.msi import ManagedServiceIdentityClient
8+
from azure.mgmt.authorization import AuthorizationManagementClient
89
from azure.cli.core.profiles import (
910
CustomResourceType,
1011
ResourceType
@@ -56,5 +57,9 @@ def get_provider_client(cli_ctx):
5657
cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES)
5758

5859

60+
def get_role_assignments_client(cli_ctx):
61+
return get_mgmt_service_client(cli_ctx, AuthorizationManagementClient).role_assignments
62+
63+
5964
def get_msi_client(cli_ctx, subscription_id=None):
6065
return get_mgmt_service_client(cli_ctx, ManagedServiceIdentityClient, subscription_id=subscription_id)

src/fleet/azext_fleet/_helpers.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
from knack.util import CLIError
1717
from azure.cli.command_modules.acs._roleassignments import add_role_assignment
1818
from azure.mgmt.core.tools import parse_resource_id
19-
19+
from azext_fleet.constants import NETWORK_CONTRIBUTOR_ROLE_ID
2020

2121
from azext_fleet._client_factory import get_provider_client
2222
from azext_fleet._client_factory import get_msi_client
23+
from azext_fleet._client_factory import get_role_assignments_client
2324

2425
logger = get_logger(__name__)
2526

@@ -159,13 +160,38 @@ def _load_kubernetes_configuration(filename):
159160

160161
def assign_network_contributor_role_to_subnet(cmd, object_id, subnet_id):
161162
if not add_role_assignment(cmd, 'Network Contributor', object_id, scope=subnet_id):
162-
logger.warning("Failed to create Network Contributor role assignment on the subnet %s.\n"
163-
"This role assignment is required for the managed identity to access the subnet.\n"
164-
"Please ensure you have sufficient permissions, or ask an administrator to run:\n"
165-
"az role assignment create --assignee-principal-type ServicePrincipal --assignee-object-id %s "
166-
"--role 'Network Contributor' --scope %s",
167-
subnet_id, object_id, subnet_id)
168-
time.sleep(3)
163+
logger.warning(
164+
"Failed to create Network Contributor role assignment on the subnet %s.\n"
165+
"This role assignment is required for the managed identity to access the subnet.\n"
166+
"Please ensure you have sufficient permissions, or ask an administrator to run:\n"
167+
"az role assignment create --assignee-principal-type ServicePrincipal --assignee-object-id %s "
168+
"--role 'Network Contributor' --scope %s",
169+
subnet_id, object_id, subnet_id)
170+
return
171+
172+
auth_client = get_role_assignments_client(cmd.cli_ctx)
173+
max_attempts = 3
174+
interval = 3
175+
for _ in range(max_attempts):
176+
if _is_assignment_present(auth_client, subnet_id, object_id):
177+
logger.warning("Role assignment for Network Contributor on subnet %s was detected.", subnet_id)
178+
return
179+
time.sleep(interval)
180+
logger.warning(
181+
"Role assignment for Network Contributor on subnet %s was not detected after %s seconds. "
182+
"There may be a delay in propagation.",
183+
subnet_id, max_attempts * interval)
184+
185+
186+
def _is_assignment_present(auth_client, subnet_id, object_id):
187+
filter_query = f"assignedTo('{object_id}') and atScope()"
188+
for assignment in auth_client.list_for_scope(subnet_id, filter=filter_query):
189+
if assignment.role_definition_id.lower().endswith(NETWORK_CONTRIBUTOR_ROLE_ID) and \
190+
assignment.scope.lower() == subnet_id.lower():
191+
return True
192+
logger.warning("No matching role assignment found for scope %s and subnet %s",
193+
assignment.scope.lower(), subnet_id.lower())
194+
return False
169195

170196

171197
def get_msi_object_id(cmd, msi_resource_id):

src/fleet/azext_fleet/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
UPGRADE_TYPE_FULL = "Full"
88
UPGRADE_TYPE_NODEIMAGEONLY = "NodeImageOnly"
99
FLEET_1P_APP_ID = "609d2f62-527f-4451-bfd2-ac2c7850822c"
10+
NETWORK_CONTRIBUTOR_ROLE_ID = "4d97b98b-1d4f-4787-a291-c67834d212e7"
1011

1112
SUPPORTED_GATE_STATES_FILTERS = ["Pending", "Skipped", "Completed"]
1213
SUPPORTED_GATE_STATES_PATCH = ["Completed"]

src/fleet/azext_fleet/custom.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
from azure.cli.core.commands.client_factory import get_subscription_id
1111
from azure.cli.core.util import sdk_no_wait, get_file_json, shell_safe_json_parse
12+
from azure.cli.command_modules.acs._graph import resolve_object_id
13+
1214

1315
from azext_fleet._client_factory import CUSTOM_MGMT_FLEET
1416
from azext_fleet._helpers import is_rp_registered, print_or_merge_credentials
@@ -119,7 +121,8 @@ def create_fleet(cmd,
119121
if not is_rp_registered(cmd):
120122
raise CLIError("The Microsoft.ContainerService resource provider is not registered."
121123
"Run `az provider register -n Microsoft.ContainerService --wait`.")
122-
assign_network_contributor_role_to_subnet(cmd, FLEET_1P_APP_ID, agent_subnet_id)
124+
object_id = resolve_object_id(cmd.cli_ctx, FLEET_1P_APP_ID)
125+
assign_network_contributor_role_to_subnet(cmd, object_id, agent_subnet_id)
123126

124127
if enable_vnet_integration and assign_identity is not None:
125128
object_id = get_msi_object_id(cmd, assign_identity)

0 commit comments

Comments
 (0)