Skip to content

Commit d1a9d16

Browse files
authored
Support updating SSH public key with az aks update --ssh-key-value (#5464)
1 parent d177573 commit d1a9d16

File tree

9 files changed

+2708
-1
lines changed

9 files changed

+2708
-1
lines changed

src/aks-preview/HISTORY.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ To release a new version, please select a new version number (usually plus 1 to
1212
Pending
1313
+++++++
1414

15+
0.5.111
16+
+++++++
17+
18+
* Support updating SSH public key with `az aks update --ssh-key-value`.
19+
1520
0.5.110
1621
+++++++
1722

src/aks-preview/azext_aks_preview/_help.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,10 @@
894894
- name: --cluster-snapshot-id
895895
type: string
896896
short-summary: The source cluster snapshot id is used to update existing cluster.
897+
- name: --ssh-key-value
898+
type: string
899+
short-summary: Public key path or key contents to install on node VMs for SSH access. For example,
900+
'ssh-rsa AAAAB...snip...UcyupgH azureuser@linuxvm'.
897901
examples:
898902
- name: Reconcile the cluster back to its current state.
899903
text: az aks update -g MyResourceGroup -n MyManagedCluster

src/aks-preview/azext_aks_preview/_params.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
validate_snapshot_name,
117117
validate_spot_max_price,
118118
validate_ssh_key,
119+
validate_ssh_key_for_update,
119120
validate_taints,
120121
validate_user,
121122
validate_vm_set_type,
@@ -388,6 +389,7 @@ def load_arguments(self, _):
388389
c.argument('disable_defender', action='store_true', validator=validate_defender_disable_and_enable_parameters)
389390
c.argument('enable_defender', action='store_true')
390391
c.argument('defender_config', validator=validate_defender_config_parameter)
392+
c.argument('ssh_key_value', type=file_type, completer=FilesCompleter(), validator=validate_ssh_key_for_update)
391393
# addons
392394
c.argument('enable_secret_rotation', action='store_true')
393395
c.argument('disable_secret_rotation', action='store_true')

src/aks-preview/azext_aks_preview/_validators.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ def validate_ssh_key(namespace):
6464
namespace.ssh_key_value = content
6565

6666

67+
def validate_ssh_key_for_update(namespace):
68+
string_or_file = namespace.ssh_key_value
69+
if not string_or_file:
70+
return
71+
content = string_or_file
72+
if os.path.exists(string_or_file):
73+
logger.info('Use existing SSH public key file: %s', string_or_file)
74+
with open(string_or_file, 'r') as f:
75+
content = f.read()
76+
elif not keys.is_valid_ssh_rsa_public_key(content):
77+
raise InvalidArgumentValueError('An RSA key file or key value must be supplied to SSH Key Value')
78+
namespace.ssh_key_value = content
79+
80+
6781
def validate_create_parameters(namespace):
6882
if not namespace.name:
6983
raise CLIError('--name has no value')

src/aks-preview/azext_aks_preview/custom.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ def aks_update(
810810
enable_vpa=False,
811811
disable_vpa=False,
812812
cluster_snapshot_id=None,
813+
ssh_key_value=None,
813814
):
814815
# DO NOT MOVE: get all the original parameters and save them as a dictionary
815816
raw_parameters = locals()

src/aks-preview/azext_aks_preview/managed_cluster_decorator.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from azure.cli.command_modules.acs._helpers import (
1515
check_is_msi_cluster,
1616
format_parameter_name_to_option_name,
17+
safe_list_get,
1718
safe_lower,
1819
)
1920
from azure.cli.command_modules.acs._validators import (
@@ -2080,6 +2081,24 @@ def get_disable_vpa(self) -> bool:
20802081
"""
20812082
return self._get_disable_vpa(enable_validation=True)
20822083

2084+
def get_ssh_key_value_for_update(self) -> Tuple[str, bool]:
2085+
"""Obtain the value of ssh_key_value for "az aks update".
2086+
2087+
Note: no_ssh_key will not be decorated into the `mc` object.
2088+
2089+
If the user provides a string-like input for --ssh-key-value, the validator function "validate_ssh_key_for_update" will
2090+
check whether it is a file path, if so, read its content and return; if it is a valid public key, return it.
2091+
Otherwise, raise error.
2092+
2093+
:return: ssh_key_value of string type
2094+
"""
2095+
# read the original value passed by the command
2096+
ssh_key_value = self.raw_param.get("ssh_key_value")
2097+
2098+
# this parameter does not need dynamic completion
2099+
# this parameter does not need validation
2100+
return ssh_key_value
2101+
20832102

20842103
class AKSPreviewManagedClusterCreateDecorator(AKSManagedClusterCreateDecorator):
20852104
def __init__(
@@ -2988,6 +3007,27 @@ def update_creation_data(self, mc: ManagedCluster) -> ManagedCluster:
29883007
source_resource_id=snapshot_id
29893008
)
29903009
mc.creation_data = creation_data
3010+
3011+
return mc
3012+
3013+
def update_linux_profile(self, mc: ManagedCluster) -> ManagedCluster:
3014+
"""Update Linux profile for the ManagedCluster object.
3015+
3016+
:return: the ManagedCluster object
3017+
"""
3018+
self._ensure_mc(mc)
3019+
3020+
ssh_key_value = self.context.get_ssh_key_value_for_update()
3021+
3022+
if ssh_key_value:
3023+
mc.linux_profile.ssh = self.models.ContainerServiceSshConfiguration(
3024+
public_keys=[
3025+
self.models.ContainerServiceSshPublicKey(
3026+
key_data=ssh_key_value
3027+
)
3028+
]
3029+
)
3030+
29913031
return mc
29923032

29933033
def update_mc_profile_preview(self) -> ManagedCluster:
@@ -3032,5 +3072,7 @@ def update_mc_profile_preview(self) -> ManagedCluster:
30323072
mc = self.update_vpa(mc)
30333073
# update creation data
30343074
mc = self.update_creation_data(mc)
3075+
# update linux profile
3076+
mc = self.update_linux_profile(mc)
30353077

30363078
return mc

0 commit comments

Comments
 (0)