From 0487616b3d1c822f170a4788ef22785db0b68466 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Mon, 28 Nov 2022 21:52:56 +0530 Subject: [PATCH] [Dapr] Disable applying CRDs during a downgrade (#193) * Add logging Signed-off-by: Shubham Sharma * Lint Signed-off-by: Shubham Sharma * Update log Signed-off-by: Shubham Sharma * Revert applyCrds when not downgrading Signed-off-by: Shubham Sharma * Update logic for removing hooks.applyCrds Signed-off-by: Shubham Sharma * Revert logic Signed-off-by: Shubham Sharma * Handle explicit hooks configuration Signed-off-by: Shubham Sharma * Update comment Signed-off-by: Shubham Sharma * re-trigger pipeline Signed-off-by: Shubham Sharma Signed-off-by: Shubham Sharma --- .../partner_extensions/Dapr.py | 44 ++++++++++++++++++- 1 file changed, 43 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 f80f8540f33..cd693bb1336 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py @@ -10,10 +10,11 @@ from typing import Tuple from azure.cli.core.azclierror import InvalidArgumentValueError +from copy import deepcopy from knack.log import get_logger from knack.prompting import prompt, prompt_y_n -from ..vendored_sdks.models import Extension, Scope, ScopeCluster +from ..vendored_sdks.models import Extension, PatchExtension, Scope, ScopeCluster from .DefaultExtension import DefaultExtension logger = get_logger(__name__) @@ -31,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' @@ -144,3 +146,43 @@ def Create(self, cmd, client, resource_group_name: str, cluster_name: str, name: location="" ) return extension_instance, release_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 = False) \ + -> PatchExtension: + """ExtensionType 'Microsoft.Dapr' specific validations & defaults for Update. + Must create and return a valid 'PatchExtension' object. + """ + input_configuration_settings = 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. + # 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.", + 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, 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 %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: + 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)