From 22ae5fd608c11c2b50a69c573c90c6d392cb8108 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Thu, 20 Oct 2022 10:42:49 +0530 Subject: [PATCH 1/9] Add logging Signed-off-by: Shubham Sharma --- .../partner_extensions/Dapr.py | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py index e16a0cb1dee..c4f681335ef 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py @@ -6,13 +6,20 @@ # pylint: disable=unused-argument # pylint: disable=line-too-long # pylint: disable=too-many-locals +# pylint: disable=too-many-instance-attributes + +import copy +from knack.log import get_logger from .DefaultExtension import DefaultExtension from ..vendored_sdks.models import ( Extension, + PatchExtension, ) +logger = get_logger(__name__) + class Dapr(DefaultExtension): def __init__(self): @@ -23,7 +30,6 @@ def Create(self, cmd, client, resource_group_name, cluster_name, name, cluster_t extension_type, scope, auto_upgrade_minor_version, release_train, version, target_namespace, release_namespace, configuration_settings, configuration_protected_settings, configuration_settings_file, configuration_protected_settings_file): - """ExtensionType 'Microsoft.Dapr' specific validations & defaults for Create Must create and return a valid 'ExtensionInstance' object. """ @@ -44,3 +50,31 @@ def Create(self, cmd, client, resource_group_name, cluster_name, name, cluster_t location="" ) return extension_instance, name, create_identity + + def Update(self, cmd, resource_group_name: str, cluster_name: str, auto_upgrade_minor_version: bool, + release_train: str, version: str, configuration_settings: dict, + configuration_protected_settings: dict, original_extension: Extension, yes: bool) \ + -> PatchExtension: + + input_configuration_settings = copy.deepcopy(configuration_settings) + + # configuration_settings can be None, so we need to set it to an empty dict. + if configuration_settings is None: + configuration_settings = {} + + # If we are downgrading the extension, then we need to disable the apply-CRDs hook. + # This is because CRD updates while downgrading can cause issues. + original_version = original_extension.version + if original_version and version and version < original_version: + configuration_settings['hooks.applyCrds'] = 'false' + logger.debug("Downgrade detected. Setting hooks.applyCrds to false.") + + # If no changes were made, return the original dict (empty or None). + if len(configuration_settings) == 0: + configuration_settings = input_configuration_settings + + return PatchExtension(auto_upgrade_minor_version=auto_upgrade_minor_version, + release_train=release_train, + version=version, + configuration_settings=configuration_settings, + configuration_protected_settings=configuration_protected_settings) From 38152a7cdda8c7d11e014066329d0626685f51d2 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Thu, 20 Oct 2022 10:53:46 +0530 Subject: [PATCH 2/9] Lint Signed-off-by: Shubham Sharma --- .../azext_k8s_extension/partner_extensions/Dapr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py index c4f681335ef..08e6d3db674 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py @@ -53,7 +53,7 @@ def Create(self, cmd, client, resource_group_name, cluster_name, name, cluster_t def Update(self, cmd, resource_group_name: str, cluster_name: str, auto_upgrade_minor_version: bool, release_train: str, version: str, configuration_settings: dict, - configuration_protected_settings: dict, original_extension: Extension, yes: bool) \ + configuration_protected_settings: dict, original_extension: Extension, yes: bool = False) \ -> PatchExtension: input_configuration_settings = copy.deepcopy(configuration_settings) From 99e1cc6be87d4bb7e6c5b8982055020bfd8b18c2 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Thu, 20 Oct 2022 11:26:47 +0530 Subject: [PATCH 3/9] Update log Signed-off-by: Shubham Sharma --- .../azext_k8s_extension/partner_extensions/Dapr.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py index 08e6d3db674..021d0a98977 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py @@ -66,8 +66,9 @@ def Update(self, cmd, resource_group_name: str, cluster_name: str, auto_upgrade_ # This is because CRD updates while downgrading can cause issues. original_version = original_extension.version if original_version and version and version < original_version: + logger.debug("Downgrade detected from %s to %s. Setting hooks.applyCrds to false.", + original_version, version) configuration_settings['hooks.applyCrds'] = 'false' - logger.debug("Downgrade detected. Setting hooks.applyCrds to false.") # If no changes were made, return the original dict (empty or None). if len(configuration_settings) == 0: From ee52f77016a078e6f797d2cdbc889a0f8df22db6 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Thu, 20 Oct 2022 18:40:44 +0530 Subject: [PATCH 4/9] Revert applyCrds when not downgrading Signed-off-by: Shubham Sharma --- .../azext_k8s_extension/partner_extensions/Dapr.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py index 021d0a98977..118f7c77068 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py @@ -69,6 +69,11 @@ def Update(self, cmd, resource_group_name: str, cluster_name: str, auto_upgrade_ logger.debug("Downgrade detected from %s to %s. Setting hooks.applyCrds to false.", original_version, version) configuration_settings['hooks.applyCrds'] = 'false' + else: + # If we are not downgrading, then we need to make sure that the hooks.applyCrds is true. + # This is because the configuration_settings may have been set to false during a previous + # downgrade. + configuration_settings['hooks.applyCrds'] = 'true' # If no changes were made, return the original dict (empty or None). if len(configuration_settings) == 0: From 0d48ae102227baf7b96ee89abd31ca60d954afca Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Thu, 20 Oct 2022 19:45:02 +0530 Subject: [PATCH 5/9] Update logic for removing hooks.applyCrds Signed-off-by: Shubham Sharma --- .../azext_k8s_extension/partner_extensions/Dapr.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py index 118f7c77068..89d32e80a00 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py @@ -70,10 +70,11 @@ def Update(self, cmd, resource_group_name: str, cluster_name: str, auto_upgrade_ original_version, version) configuration_settings['hooks.applyCrds'] = 'false' else: - # If we are not downgrading, then we need to make sure that the hooks.applyCrds is true. - # This is because the configuration_settings may have been set to false during a previous - # downgrade. - configuration_settings['hooks.applyCrds'] = 'true' + # If we are not downgrading, remove the hooks.applyCrds setting if it exists from a previous downgrade. + if original_extension.configuration_settings and \ + 'hooks.applyCrds' in original_extension.configuration_settings: + logger.debug("Removing hooks.applyCrds from original_extension.configuration_settings.") + del original_extension.configuration_settings['hooks.applyCrds'] # If no changes were made, return the original dict (empty or None). if len(configuration_settings) == 0: From 931e9f3d31f995b06d1cd07d2070e9a4f9b23177 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Thu, 20 Oct 2022 19:51:03 +0530 Subject: [PATCH 6/9] Revert logic Signed-off-by: Shubham Sharma --- .../azext_k8s_extension/partner_extensions/Dapr.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py index 89d32e80a00..16e9eccdf18 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py @@ -70,11 +70,10 @@ def Update(self, cmd, resource_group_name: str, cluster_name: str, auto_upgrade_ original_version, version) configuration_settings['hooks.applyCrds'] = 'false' else: - # If we are not downgrading, remove the hooks.applyCrds setting if it exists from a previous downgrade. - if original_extension.configuration_settings and \ - 'hooks.applyCrds' in original_extension.configuration_settings: - logger.debug("Removing hooks.applyCrds from original_extension.configuration_settings.") - del original_extension.configuration_settings['hooks.applyCrds'] + # If we are not downgrading, set the hooks.applyCrds to true. + # This is because the value may have been set to false during a previous downgrade. + logger.debug("No downgrade detected. Setting hooks.applyCrds to true.") + configuration_settings['hooks.applyCrds'] = 'true' # If no changes were made, return the original dict (empty or None). if len(configuration_settings) == 0: From 12ca0c2432bce63760a386d26649bfd7fd8658c3 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Wed, 23 Nov 2022 15:07:56 +0530 Subject: [PATCH 7/9] Handle explicit hooks configuration Signed-off-by: Shubham Sharma --- .../partner_extensions/Dapr.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py index 71febb21339..7adc4440303 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py @@ -32,6 +32,7 @@ def __init__(self): # constants for configuration settings. self.CLUSTER_TYPE_KEY = 'global.clusterType' self.HA_KEY_ENABLED_KEY = 'global.ha.enabled' + self.APPLY_CRDS_HOOK_ENABLED_KEY = 'hooks.applyCrds' self.SKIP_EXISTING_DAPR_CHECK_KEY = 'skipExistingDaprCheck' self.EXISTING_DAPR_RELEASE_NAME_KEY = 'existingDaprReleaseName' self.EXISTING_DAPR_RELEASE_NAMESPACE_KEY = 'existingDaprReleaseNamespace' @@ -162,15 +163,18 @@ def Update(self, cmd, resource_group_name: str, cluster_name: str, auto_upgrade_ # If we are downgrading the extension, then we need to disable the apply-CRDs hook. # This is because CRD updates while downgrading can cause issues. original_version = original_extension.version - if original_version and version and version < original_version: - logger.debug("Downgrade detected from %s to %s. Setting hooks.applyCrds to false.", - original_version, version) - configuration_settings['hooks.applyCrds'] = 'false' + if self.APPLY_CRDS_HOOK_ENABLED_KEY in configuration_settings: + logger.debug("'%s' is set to '%s' in --configuration-settings, not overriding it.", + self.APPLY_CRDS_HOOK_ENABLED_KEY, configuration_settings[self.APPLY_CRDS_HOOK_ENABLED_KEY]) + elif original_version and version and version < original_version: + logger.debug("Downgrade detected from %s to %s. Setting %s to false.", + original_version, version, self.APPLY_CRDS_HOOK_ENABLED_KEY) + configuration_settings[self.APPLY_CRDS_HOOK_ENABLED_KEY] = 'false' else: - # If we are not downgrading, set the hooks.applyCrds to true. + # If we are not downgrading, enable the apply-CRDs hook explicitly. # This is because the value may have been set to false during a previous downgrade. - logger.debug("No downgrade detected. Setting hooks.applyCrds to true.") - configuration_settings['hooks.applyCrds'] = 'true' + logger.debug("No downgrade detected. Setting %s to true.", self.APPLY_CRDS_HOOK_ENABLED_KEY) + configuration_settings[self.APPLY_CRDS_HOOK_ENABLED_KEY] = 'true' # If no changes were made, return the original dict (empty or None). if len(configuration_settings) == 0: From 503d72741edf7f2d3f9592c81eb5766523ed6466 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Thu, 24 Nov 2022 11:22:40 +0530 Subject: [PATCH 8/9] Update comment Signed-off-by: Shubham Sharma --- src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py index 7adc4440303..cd693bb1336 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py @@ -162,6 +162,7 @@ def Update(self, cmd, resource_group_name: str, cluster_name: str, auto_upgrade_ # If we are downgrading the extension, then we need to disable the apply-CRDs hook. # This is because CRD updates while downgrading can cause issues. + # As CRDs are additive, skipping their removal while downgrading is safe. original_version = original_extension.version if self.APPLY_CRDS_HOOK_ENABLED_KEY in configuration_settings: logger.debug("'%s' is set to '%s' in --configuration-settings, not overriding it.", From c9a28cbb28e538dd472e1428d777bd7708f4984c Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Fri, 25 Nov 2022 10:16:39 +0530 Subject: [PATCH 9/9] re-trigger pipeline Signed-off-by: Shubham Sharma