Skip to content

Commit

Permalink
sync csi changes PR Azure#4865 & PR Azure#4868
Browse files Browse the repository at this point in the history
  • Loading branch information
FumingZhang committed Jun 13, 2022
1 parent ab4b1d0 commit f0bb79a
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 15 deletions.
51 changes: 43 additions & 8 deletions src/aks-preview/azext_aks_preview/managed_cluster_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
)
from azure.cli.command_modules.acs._helpers import (
check_is_msi_cluster,
safe_list_get,
safe_lower,
)
from azure.cli.command_modules.acs._validators import extract_comma_separated_string
from azure.cli.command_modules.acs._validators import (
extract_comma_separated_string,
)
from azure.cli.command_modules.acs.managed_cluster_decorator import (
AKSManagedClusterContext,
AKSManagedClusterCreateDecorator,
Expand All @@ -26,6 +27,7 @@
)
from azure.cli.core import AzCommandsLoader
from azure.cli.core.azclierror import (
ArgumentUsageError,
InvalidArgumentValueError,
MutuallyExclusiveArgumentError,
RequiredArgumentMissingError,
Expand All @@ -39,7 +41,9 @@

from azext_aks_preview._helpers import get_cluster_snapshot_by_snapshot_id
from azext_aks_preview._loadbalancer import create_load_balancer_profile
from azext_aks_preview._loadbalancer import update_load_balancer_profile as _update_load_balancer_profile
from azext_aks_preview._loadbalancer import (
update_load_balancer_profile as _update_load_balancer_profile,
)
from azext_aks_preview._podidentity import (
_fill_defaults_for_pod_identity_profile,
_is_pod_identity_addon_enabled,
Expand Down Expand Up @@ -816,13 +820,15 @@ def get_kubernetes_version(self) -> str:
return self._get_kubernetes_version(read_only=False)

def get_disk_driver(self) -> Optional[ManagedClusterStorageProfileDiskCSIDriver]:
"""Obtrain the value of storage_profile.disk_csi_driver
"""Obtain the value of storage_profile.disk_csi_driver
:return: Optional[ManagedClusterStorageProfileDiskCSIDriver]
"""
enable_disk_driver = self.raw_param.get("enable_disk_driver")
disable_disk_driver = self.raw_param.get("disable_disk_driver")
if not enable_disk_driver and not disable_disk_driver:
disk_driver_version = self.raw_param.get("disk_driver_version")

if not enable_disk_driver and not disable_disk_driver and not disk_driver_version:
return None
profile = self.models.ManagedClusterStorageProfileDiskCSIDriver()

Expand All @@ -832,25 +838,45 @@ def get_disk_driver(self) -> Optional[ManagedClusterStorageProfileDiskCSIDriver]
"--disable-disk-driver at the same time."
)

if disable_disk_driver and disk_driver_version:
raise ArgumentUsageError(
"The parameter --disable-disk-driver cannot be used "
"when --disk-driver-version is specified.")

if self.decorator_mode == DecoratorMode.UPDATE and disk_driver_version and not enable_disk_driver:
raise ArgumentUsageError(
"Parameter --enable-disk-driver is required "
"when --disk-driver-version is specified during update.")

if self.decorator_mode == DecoratorMode.CREATE:
if disable_disk_driver:
profile.enabled = False
else:
profile.enabled = True
if disk_driver_version:
profile.version = disk_driver_version

if self.decorator_mode == DecoratorMode.UPDATE:
if enable_disk_driver:
profile.enabled = True
if disk_driver_version:
profile.version = disk_driver_version
elif disable_disk_driver:
msg = "Please make sure there are no existing PVs and PVCs that are used by AzureDisk CSI driver before disabling."
if not self.get_yes() and not prompt_y_n(msg, default="n"):
raise DecoratorEarlyExitException()
profile.enabled = False

return profile

def get_file_driver(self) -> Optional[ManagedClusterStorageProfileFileCSIDriver]:
"""Obtrain the value of storage_profile.file_csi_driver
"""Obtain the value of storage_profile.file_csi_driver
:return: Optional[ManagedClusterStorageProfileFileCSIDriver]
"""
enable_file_driver = self.raw_param.get("enable_file_driver")
disable_file_driver = self.raw_param.get("disable_file_driver")

if not enable_file_driver and not disable_file_driver:
return None
profile = self.models.ManagedClusterStorageProfileFileCSIDriver()
Expand All @@ -869,19 +895,24 @@ def get_file_driver(self) -> Optional[ManagedClusterStorageProfileFileCSIDriver]
if enable_file_driver:
profile.enabled = True
elif disable_file_driver:
msg = "Please make sure there are no existing PVs and PVCs that are used by AzureFile CSI driver before disabling."
if not self.get_yes() and not prompt_y_n(msg, default="n"):
raise DecoratorEarlyExitException()
profile.enabled = False

return profile

def get_snapshot_controller(self) -> Optional[ManagedClusterStorageProfileSnapshotController]:
"""Obtrain the value of storage_profile.snapshot_controller
"""Obtain the value of storage_profile.snapshot_controller
:return: Optional[ManagedClusterStorageProfileSnapshotController]
"""
enable_snapshot_controller = self.raw_param.get("enable_snapshot_controller")
disable_snapshot_controller = self.raw_param.get("disable_snapshot_controller")

if not enable_snapshot_controller and not disable_snapshot_controller:
return None

profile = self.models.ManagedClusterStorageProfileSnapshotController()

if enable_snapshot_controller and disable_snapshot_controller:
Expand All @@ -898,12 +929,16 @@ def get_snapshot_controller(self) -> Optional[ManagedClusterStorageProfileSnapsh
if enable_snapshot_controller:
profile.enabled = True
elif disable_snapshot_controller:
msg = "Please make sure there are no existing VolumeSnapshots, VolumeSnapshotClasses and VolumeSnapshotContents " \
"that are used by the snapshot controller before disabling."
if not self.get_yes() and not prompt_y_n(msg, default="n"):
raise DecoratorEarlyExitException()
profile.enabled = False

return profile

def get_storage_profile(self) -> Optional[ManagedClusterStorageProfile]:
"""Obtrain the value of storage_profile.
"""Obtain the value of storage_profile.
:return: Optional[ManagedClusterStorageProfile]
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
CONST_CONFCOM_ADDON_NAME,
CONST_DEFAULT_NODE_OS_TYPE,
CONST_DEFAULT_NODE_VM_SIZE,
CONST_DISK_DRIVER_V2,
CONST_GITOPS_ADDON_NAME,
CONST_HTTP_APPLICATION_ROUTING_ADDON_NAME,
CONST_INGRESS_APPGW_ADDON_NAME,
Expand Down Expand Up @@ -47,12 +48,21 @@
AKSPreviewManagedClusterUpdateDecorator,
)
from azext_aks_preview.tests.latest.utils import get_test_data_file_path
from azure.cli.command_modules.acs._consts import AgentPoolDecoratorMode, DecoratorEarlyExitException, DecoratorMode
from azure.cli.command_modules.acs.managed_cluster_decorator import AKSManagedClusterParamDict
from azure.cli.command_modules.acs.tests.latest.mocks import MockCLI, MockClient, MockCmd
from azure.cli.command_modules.acs._consts import (
AgentPoolDecoratorMode,
DecoratorEarlyExitException,
DecoratorMode,
)
from azure.cli.command_modules.acs.managed_cluster_decorator import (
AKSManagedClusterParamDict,
)
from azure.cli.command_modules.acs.tests.latest.mocks import (
MockCLI,
MockClient,
MockCmd,
)
from azure.cli.core.azclierror import (
AzCLIError,
AzureInternalError,
ArgumentUsageError,
CLIInternalError,
InvalidArgumentValueError,
MutuallyExclusiveArgumentError,
Expand Down Expand Up @@ -1226,6 +1236,119 @@ def test_get_disk_driver(self):
with self.assertRaises(MutuallyExclusiveArgumentError):
ctx_1.get_disk_driver()

ctx_2 = AKSPreviewManagedClusterContext(
self.cmd,
AKSManagedClusterParamDict(
{
"enable_disk_driver": True,
"disk_driver_version": "v2",
"disable_disk_driver": False,
}
),
self.models,
decorator_mode=DecoratorMode.UPDATE,
)
storage_profile_2 = self.models.ManagedClusterStorageProfile(
disk_csi_driver=self.models.ManagedClusterStorageProfileDiskCSIDriver(
enabled=False,
version=None,
),
file_csi_driver=None,
snapshot_controller=None,
)
mc_2 = self.models.ManagedCluster(
location="test_location",
storage_profile=storage_profile_2,
)
ctx_2.attach_mc(mc_2)
ground_truth_disk_csi_driver_2 = (
self.models.ManagedClusterStorageProfileDiskCSIDriver(
enabled=True,
version="v2",
)
)
self.assertEqual(
ctx_2.get_disk_driver(), ground_truth_disk_csi_driver_2
)

# fail with enable-disk-driver as false and value passed for disk_driver_version
ctx_3 = AKSPreviewManagedClusterContext(
self.cmd,
AKSManagedClusterParamDict({
"disable_disk_driver": True,
"disk_driver_version": "v2",
}),
self.models,
decorator_mode=DecoratorMode.UPDATE,
)

# fail on argument usage error
with self.assertRaises(ArgumentUsageError):
ctx_3.get_disk_driver()

# fail with enable-disk-driver as false and value passed for disk_driver_version
ctx_4 = AKSPreviewManagedClusterContext(
self.cmd,
AKSManagedClusterParamDict({
"disk_driver_version": "v2",
}),
self.models,
decorator_mode=DecoratorMode.UPDATE,
)
# fail on argument usage error
with self.assertRaises(ArgumentUsageError):
ctx_4.get_disk_driver()

# fail on prompt_y_n not specified when disabling disk driver
ctx_5 = AKSPreviewManagedClusterContext(
self.cmd,
AKSManagedClusterParamDict({
"disable_disk_driver": True,
}),
self.models,
decorator_mode=DecoratorMode.UPDATE,
)
with patch(
"azext_aks_preview.managed_cluster_decorator.prompt_y_n",
return_value=False,
), self.assertRaises(DecoratorEarlyExitException):
ctx_5.get_disk_driver()

ctx_6 = AKSPreviewManagedClusterContext(
self.cmd,
AKSManagedClusterParamDict({
"disable_disk_driver": True,
}),
self.models,
decorator_mode=DecoratorMode.CREATE,
)
ground_truth_disk_csi_driver_6 = (
self.models.ManagedClusterStorageProfileDiskCSIDriver(
enabled=False,
)
)
self.assertEqual(
ctx_6.get_disk_driver(), ground_truth_disk_csi_driver_6
)

ctx_7 = AKSPreviewManagedClusterContext(
self.cmd,
AKSManagedClusterParamDict({
"disk_driver_version": CONST_DISK_DRIVER_V2,
}),
self.models,
decorator_mode=DecoratorMode.CREATE,
)
ground_truth_disk_csi_driver_7 = (
self.models.ManagedClusterStorageProfileDiskCSIDriver(
enabled=True,
version=CONST_DISK_DRIVER_V2,
)
)
self.assertEqual(
ctx_7.get_disk_driver(), ground_truth_disk_csi_driver_7
)

def test_get_file_driver(self):
ctx_1 = AKSPreviewManagedClusterContext(
self.cmd,
Expand All @@ -1242,6 +1365,21 @@ def test_get_file_driver(self):
with self.assertRaises(MutuallyExclusiveArgumentError):
ctx_1.get_file_driver()

# fail on prompt_y_n not specified when disabling file driver
ctx_2 = AKSPreviewManagedClusterContext(
self.cmd,
AKSManagedClusterParamDict({
"disable_file_driver": True,
}),
self.models,
decorator_mode=DecoratorMode.UPDATE,
)
with patch(
"azext_aks_preview.managed_cluster_decorator.prompt_y_n",
return_value=False,
), self.assertRaises(DecoratorEarlyExitException):
ctx_2.get_file_driver()

def test_get_snapshot_controller(self):
ctx_1 = AKSPreviewManagedClusterContext(
self.cmd,
Expand All @@ -1258,6 +1396,21 @@ def test_get_snapshot_controller(self):
with self.assertRaises(MutuallyExclusiveArgumentError):
ctx_1.get_snapshot_controller()

# fail on prompt_y_n not specified when disabling snapshot controller
ctx_2 = AKSPreviewManagedClusterContext(
self.cmd,
AKSManagedClusterParamDict({
"disable_snapshot_controller": True,
}),
self.models,
decorator_mode=DecoratorMode.UPDATE,
)
with patch(
"azext_aks_preview.managed_cluster_decorator.prompt_y_n",
return_value=False,
), self.assertRaises(DecoratorEarlyExitException):
ctx_2.get_snapshot_controller()

def test_get_storage_profile(self):
# create
ctx_1 = AKSPreviewManagedClusterContext(
Expand All @@ -1266,7 +1419,7 @@ def test_get_storage_profile(self):
"disable_disk_driver": True,
}),
self.models,
decorator_mode=DecoratorMode.UPDATE,
decorator_mode=DecoratorMode.CREATE,
)
mc_1 = self.models.ManagedCluster(
location="test_location",
Expand All @@ -1287,6 +1440,7 @@ def test_get_storage_profile(self):
AKSManagedClusterParamDict({
"enable_file_driver": True,
"disable_snapshot_controller": True,
"yes": True,
}),
self.models,
decorator_mode=DecoratorMode.UPDATE,
Expand Down Expand Up @@ -2968,7 +3122,7 @@ def test_update_storage_profile(self):
dec_1 = AKSPreviewManagedClusterUpdateDecorator(
self.cmd,
self.client,
{"disable_disk_driver": True, "disable_file_driver": True, "disable_snapshot_controller": True},
{"disable_disk_driver": True, "disable_file_driver": True, "disable_snapshot_controller": True, "yes": True},
CUSTOM_MGMT_AKS_PREVIEW,
)
storage_profile_1 = self.models.ManagedClusterStorageProfile(
Expand Down

0 comments on commit f0bb79a

Please sign in to comment.