Skip to content

Commit 6b3cb6f

Browse files
address comments
1 parent 61fefa1 commit 6b3cb6f

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

src/fleet/azext_fleet/_helpers.py

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from knack.prompting import NoTTYException, prompt_y_n
1515
from knack.util import CLIError
1616
from azure.cli.command_modules.acs._roleassignments import add_role_assignment
17-
from azure.mgmt.core.tools import is_valid_resource_id, parse_resource_id
17+
from azure.mgmt.core.tools import parse_resource_id
1818

1919

2020
from azext_fleet._client_factory import get_provider_client
@@ -156,32 +156,24 @@ def _load_kubernetes_configuration(filename):
156156
raise CLIError(f'Error parsing {filename} ({str(ex)})') from ex
157157

158158

159-
def assign_network_contributor_role_to_subnet(cmd, objectId, subnet_id):
160-
resource_client = get_provider_client(cmd.cli_ctx)
161-
provider = resource_client.providers.get("Microsoft.ContainerService")
162-
163-
# provider registration state being is checked to ensure that the Fleet service principal is available
164-
# to create the role assignment on the subnet
165-
if provider.registration_state != 'Registered':
166-
raise CLIError("The Microsoft.ContainerService resource provider is not registered."
167-
"Run `az provider register -n Microsoft.ContainerService --wait`.")
168-
169-
if not add_role_assignment(cmd, 'Network Contributor', objectId, scope=subnet_id):
159+
def assign_network_contributor_role_to_subnet(cmd, object_id, subnet_id):
160+
if not add_role_assignment(cmd, 'Network Contributor', object_id, scope=subnet_id):
170161
logger.warning("Failed to create Network Contributor role assignment on the subnet.\n"
171162
"Please ensure you have sufficient permissions to assign roles on subnet %s.", subnet_id)
172163

173164

174165
def get_msi_object_id(cmd, msi_resource_id):
175-
try:
176-
if not is_valid_resource_id(msi_resource_id):
177-
raise CLIError(f"The provided managed identity resource ID '{msi_resource_id}' is not valid.")
178-
parsed = parse_resource_id(msi_resource_id)
179-
subscription_id = parsed['subscription']
180-
resource_group_name = parsed['resource_group']
181-
msi_name = parsed['resource_name']
182-
msi_client = get_msi_client(cmd.cli_ctx, subscription_id=subscription_id)
183-
msi = msi_client.user_assigned_identities.get(resource_name=msi_name,
184-
resource_group_name=resource_group_name)
185-
return msi.principal_id
186-
except Exception as ex:
187-
raise CLIError(f"Failed to get object ID for managed identity {msi_resource_id}: {str(ex)}") from ex
166+
parsed = parse_resource_id(msi_resource_id)
167+
subscription_id = parsed['subscription']
168+
resource_group_name = parsed['resource_group']
169+
msi_name = parsed['resource_name']
170+
msi_client = get_msi_client(cmd.cli_ctx, subscription_id=subscription_id)
171+
msi = msi_client.user_assigned_identities.get(resource_name=msi_name,
172+
resource_group_name=resource_group_name)
173+
return msi.principal_id
174+
175+
176+
def is_rp_registered(cmd):
177+
resource_client = get_provider_client(cmd.cli_ctx)
178+
provider = resource_client.providers.get("Microsoft.ContainerService")
179+
return provider.registration_state == 'Registered'

src/fleet/azext_fleet/custom.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from azure.cli.core.util import sdk_no_wait
1313

1414
from azext_fleet._client_factory import CUSTOM_MGMT_FLEET
15-
from azext_fleet._helpers import print_or_merge_credentials
15+
from azext_fleet._helpers import is_rp_registered, print_or_merge_credentials
1616
from azext_fleet._helpers import assign_network_contributor_role_to_subnet
1717
from azext_fleet._helpers import get_msi_object_id
1818
from azext_fleet.constants import UPGRADE_TYPE_CONTROLPLANEONLY
@@ -91,6 +91,7 @@ def create_fleet(cmd,
9191
resource_type=CUSTOM_MGMT_FLEET,
9292
operation_group="fleets"
9393
)
94+
9495
managed_service_identity = fleet_managed_service_identity_model(type="None")
9596
if enable_managed_identity:
9697
managed_service_identity.type = "SystemAssigned"
@@ -105,6 +106,11 @@ def create_fleet(cmd,
105106
elif assign_identity is not None:
106107
raise CLIError("Cannot assign identity without enabling managed identity.")
107108

109+
if enable_vnet_integration:
110+
if not enable_managed_identity and assign_identity is None:
111+
raise CLIError("When vnet integration is enabled, either system-assigned or "
112+
"user-assigned identity must be provided.")
113+
108114
fleet = fleet_model(
109115
location=location,
110116
tags=tags,
@@ -113,11 +119,17 @@ def create_fleet(cmd,
113119
)
114120

115121
if enable_private_cluster:
122+
# provider registration state being is checked to ensure that the Fleet service principal is available
123+
# to create the role assignment on the subnet
124+
if not is_rp_registered(cmd):
125+
raise CLIError("The Microsoft.ContainerService resource provider is not registered."
126+
"Run `az provider register -n Microsoft.ContainerService --wait`.")
116127
assign_network_contributor_role_to_subnet(cmd, FLEET_1P_APP_ID, agent_subnet_id)
117128

118-
if enable_vnet_integration:
119-
assign_network_contributor_role_to_subnet(cmd, get_msi_object_id(cmd, assign_identity), apiserver_subnet_id)
120-
assign_network_contributor_role_to_subnet(cmd, get_msi_object_id(cmd, assign_identity), agent_subnet_id)
129+
if enable_vnet_integration and assign_identity is not None:
130+
object_id = get_msi_object_id(cmd, assign_identity)
131+
assign_network_contributor_role_to_subnet(cmd, object_id, apiserver_subnet_id)
132+
assign_network_contributor_role_to_subnet(cmd, object_id, agent_subnet_id)
121133

122134
return sdk_no_wait(no_wait,
123135
client.begin_create_or_update,

0 commit comments

Comments
 (0)