Skip to content

Commit

Permalink
[AKS-preview] az aks upgrade: Add forceupgrade settings to aks previe…
Browse files Browse the repository at this point in the history
…w cli (Azure#7531)
  • Loading branch information
reneeli123 authored Apr 28, 2024
1 parent 2a1898f commit cb7abc8
Show file tree
Hide file tree
Showing 6 changed files with 3,076 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/aks-preview/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ To release a new version, please select a new version number (usually plus 1 to
Pending
+++++++
* Add option `WindowsAnnual` to `--os-sku` for `az aks nodepool add`.
* Add option `--enable-force-upgrade`, `--disable-force-upgrade` and `--upgrade-override-until` to `az aks upgrade`.

3.0.0b9
+++++++
Expand Down
10 changes: 10 additions & 0 deletions src/aks-preview/azext_aks_preview/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,16 @@
- name: --aks-custom-headers
type: string
short-summary: Send custom headers. When specified, format should be Key1=Value1,Key2=Value2
- name: --enable-force-upgrade
type: bool
short-summary: Enable forceUpgrade cluster upgrade settings override.
- name: --disable-force-upgrade
type: bool
short-summary: Disable forceUpgrade cluster upgrade settings override.
- name: --upgrade-override-until
type: string
short-summary: Until when the cluster upgradeSettings overrides are effective.
long-summary: It needs to be in a valid date-time format that's within the next 30 days. For example, 2023-04-01T13:00:00Z. Note that if --force-upgrade is set to true and --upgrade-override-until is not set, by default it will be set to 3 days from now.
examples:
- name: Upgrade a existing managed cluster to a managed cluster snapshot.
text: az aks upgrade -g MyResourceGroup -n MyManagedCluster --cluster-snapshot-id "/subscriptions/00000/resourceGroups/AnotherResourceGroup/providers/Microsoft.ContainerService/managedclustersnapshots/mysnapshot1"
Expand Down
6 changes: 6 additions & 0 deletions src/aks-preview/azext_aks_preview/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,12 @@ def load_arguments(self, _):
help="Do not prompt for confirmation.",
action="store_true",
)
c.argument('enable_force_upgrade', action='store_true')
c.argument(
'disable_force_upgrade', action='store_true',
validator=validate_force_upgrade_disable_and_enable_parameters
)
c.argument('upgrade_override_until')

with self.argument_context("aks scale") as c:
c.argument(
Expand Down
62 changes: 62 additions & 0 deletions src/aks-preview/azext_aks_preview/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,9 @@ def aks_upgrade(cmd,
node_image_only=False,
cluster_snapshot_id=None,
aks_custom_headers=None,
enable_force_upgrade=False,
disable_force_upgrade=False,
upgrade_override_until=None,
yes=False):
msg = 'Kubernetes may be unavailable during cluster upgrades.\n Are you sure you want to perform this operation?'
if not yes and not prompt_y_n(msg, default="n"):
Expand Down Expand Up @@ -1077,6 +1080,13 @@ def aks_upgrade(cmd,
mcsnapshot = get_cluster_snapshot_by_snapshot_id(cmd.cli_ctx, cluster_snapshot_id)
kubernetes_version = mcsnapshot.managed_cluster_properties_read_only.kubernetes_version

instance = _update_upgrade_settings(
cmd,
instance,
enable_force_upgrade=enable_force_upgrade,
disable_force_upgrade=disable_force_upgrade,
upgrade_override_until=upgrade_override_until)

if instance.kubernetes_version == kubernetes_version:
if instance.provisioning_state == "Succeeded":
logger.warning("The cluster is already on version %s and is not in a failed state. No operations "
Expand Down Expand Up @@ -1129,6 +1139,58 @@ def aks_upgrade(cmd,
return sdk_no_wait(no_wait, client.begin_create_or_update, resource_group_name, name, instance, headers=headers)


def _update_upgrade_settings(cmd, instance,
enable_force_upgrade=False,
disable_force_upgrade=False,
upgrade_override_until=None):
existing_until = None
if (instance.upgrade_settings is not None and instance.upgrade_settings.override_settings is not None
and instance.upgrade_settings.override_settings.until is not None):
existing_until = instance.upgrade_settings.override_settings.until

force_upgrade = None
if enable_force_upgrade is False and disable_force_upgrade is False:
force_upgrade = None
elif enable_force_upgrade is not None:
force_upgrade = enable_force_upgrade
elif disable_force_upgrade is not None:
force_upgrade = not disable_force_upgrade

ClusterUpgradeSettings = cmd.get_models(
"ClusterUpgradeSettings",
resource_type=CUSTOM_MGMT_AKS_PREVIEW,
operation_group="managed_clusters",
)

UpgradeOverrideSettings = cmd.get_models(
"UpgradeOverrideSettings",
resource_type=CUSTOM_MGMT_AKS_PREVIEW,
operation_group="managed_clusters",
)

if force_upgrade is not None or upgrade_override_until is not None:
if instance.upgrade_settings is None:
instance.upgrade_settings = ClusterUpgradeSettings()
if instance.upgrade_settings.override_settings is None:
instance.upgrade_settings.override_settings = UpgradeOverrideSettings()
# sets force_upgrade
if force_upgrade is not None:
instance.upgrade_settings.override_settings.force_upgrade = force_upgrade
# sets until
if upgrade_override_until is not None:
try:
instance.upgrade_settings.override_settings.until = parse(upgrade_override_until)
except Exception: # pylint: disable=broad-except
raise InvalidArgumentValueError(
f"{upgrade_override_until} is not a valid datatime format."
)
elif force_upgrade:
default_extended_until = datetime.datetime.utcnow() + datetime.timedelta(days=3)
if existing_until is None or existing_until.timestamp() < default_extended_until.timestamp():
instance.upgrade_settings.override_settings.until = default_extended_until
return instance


def _upgrade_single_nodepool_image_version(
no_wait, client, resource_group_name, cluster_name, nodepool_name, snapshot_id=None
):
Expand Down
Loading

0 comments on commit cb7abc8

Please sign in to comment.