Skip to content

Commit 8b6864d

Browse files
Update CLI to create role assignment (Azure#8636)
* create role assignment for Fleet RP on subnet when creating private fleet * lint * update * update * update * comment * rename func
1 parent 6c076ea commit 8b6864d

File tree

8 files changed

+2716
-1454
lines changed

8 files changed

+2716
-1454
lines changed

src/fleet/HISTORY.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,8 @@ Release History
118118

119119
1.5.0
120120
++++++
121-
* Upgrade SDK version to 2025-03-01
121+
* Upgrade SDK version to 2025-03-01
122+
123+
1.5.1
124+
++++++
125+
* create_fleet now creates a role assignment when fleet type is private

src/fleet/azext_fleet/_client_factory.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,8 @@ def cf_auto_upgrade_profiles(cli_ctx, *_):
4444

4545
def cf_auto_upgrade_profile_operations(cli_ctx, *_):
4646
return get_container_service_client(cli_ctx).auto_upgrade_profile_operations
47+
48+
49+
def get_provider_client(cli_ctx):
50+
return get_mgmt_service_client(
51+
cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES)

src/fleet/azext_fleet/_helpers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
from knack.log import get_logger
1414
from knack.prompting import NoTTYException, prompt_y_n
1515
from knack.util import CLIError
16+
from azure.cli.command_modules.acs._roleassignments import add_role_assignment
17+
18+
from azext_fleet.constants import FLEET_1P_APP_ID
19+
from azext_fleet._client_factory import get_provider_client
1620

1721
logger = get_logger(__name__)
1822

@@ -148,3 +152,17 @@ def _load_kubernetes_configuration(filename):
148152
raise
149153
except (yaml.parser.ParserError, UnicodeDecodeError) as ex:
150154
raise CLIError(f'Error parsing {filename} ({str(ex)})') from ex
155+
156+
157+
def assign_network_contributor_role_to_subnet(cmd, subnet_id):
158+
resource_client = get_provider_client(cmd.cli_ctx)
159+
provider = resource_client.providers.get("Microsoft.ContainerService")
160+
161+
# provider registration state being is checked to ensure that the Fleet service principal is available
162+
# to create the role assignment on the subnet
163+
if provider.registration_state != 'Registered':
164+
raise CLIError("The Microsoft.ContainerService resource provider is not registered."
165+
"Run `az provider register -n Microsoft.ContainerService --wait`.")
166+
if not add_role_assignment(cmd, 'Network Contributor', FLEET_1P_APP_ID, scope=subnet_id):
167+
raise CLIError("failed to create role assignment for Fleet RP.\n"
168+
f"Do you have owner permissions on the subnet {subnet_id}?\n")

src/fleet/azext_fleet/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
UPGRADE_TYPE_CONTROLPLANEONLY = "ControlPlaneOnly"
77
UPGRADE_TYPE_FULL = "Full"
88
UPGRADE_TYPE_NODEIMAGEONLY = "NodeImageOnly"
9+
FLEET_1P_APP_ID = "609d2f62-527f-4451-bfd2-ac2c7850822c"
910

1011
UPGRADE_TYPE_ERROR_MESSAGES = {
1112
UPGRADE_TYPE_CONTROLPLANEONLY: f"Please set kubernetes version when upgrade type is '{UPGRADE_TYPE_CONTROLPLANEONLY}'.", # pylint: disable=line-too-long

src/fleet/azext_fleet/custom.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from azext_fleet._client_factory import CUSTOM_MGMT_FLEET
1515
from azext_fleet._helpers import print_or_merge_credentials
16+
from azext_fleet._helpers import assign_network_contributor_role_to_subnet
1617
from azext_fleet.constants import UPGRADE_TYPE_CONTROLPLANEONLY
1718
from azext_fleet.constants import UPGRADE_TYPE_FULL
1819
from azext_fleet.constants import UPGRADE_TYPE_NODEIMAGEONLY
@@ -109,6 +110,9 @@ def create_fleet(cmd,
109110
identity=managed_service_identity
110111
)
111112

113+
if enable_private_cluster:
114+
assign_network_contributor_role_to_subnet(cmd, resource_group_name, agent_subnet_id)
115+
112116
return sdk_no_wait(no_wait,
113117
client.begin_create_or_update,
114118
resource_group_name,

src/fleet/azext_fleet/tests/latest/recordings/test_fleet_hubful.yaml

Lines changed: 2073 additions & 998 deletions
Large diffs are not rendered by default.

src/fleet/azext_fleet/tests/latest/recordings/test_fleet_hubless.yaml

Lines changed: 609 additions & 454 deletions
Large diffs are not rendered by default.

src/fleet/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
# TODO: Confirm this is the right version number you want and it matches your
1818
# HISTORY.rst entry.
19-
VERSION = '1.5.0'
19+
VERSION = '1.5.1'
2020

2121
# The full list of classifiers is available at
2222
# https://pypi.python.org/pypi?%3Aaction=list_classifiers

0 commit comments

Comments
 (0)