Skip to content

Commit

Permalink
[Dapr] Update version comparison logic to use semver based comparison (
Browse files Browse the repository at this point in the history
…#219)

* Update semver comparison

Signed-off-by: Shubham Sharma <shubhash@microsoft.com>

* Add log

Signed-off-by: Shubham Sharma <shubhash@microsoft.com>

---------

Signed-off-by: Shubham Sharma <shubhash@microsoft.com>
  • Loading branch information
shubham1172 authored Feb 17, 2023
1 parent 2473565 commit 0845f61
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/k8s-extension/azext_k8s_extension/partner_extensions/Dapr.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from copy import deepcopy
from knack.log import get_logger
from knack.prompting import prompt, prompt_y_n
from packaging import version as packaging_version

from ..vendored_sdks.models import Extension, PatchExtension, Scope, ScopeCluster
from .DefaultExtension import DefaultExtension
Expand Down Expand Up @@ -173,7 +174,7 @@ def Update(self, cmd, resource_group_name: str, cluster_name: str, auto_upgrade_
logger.debug("Auto-upgrade is disabled and version is pinned to %s. Setting '%s' to false.",
version, self.APPLY_CRDS_HOOK_ENABLED_KEY)
configuration_settings[self.APPLY_CRDS_HOOK_ENABLED_KEY] = 'false'
elif original_version and version and version < original_version:
elif original_version and version and Dapr._is_downgrade(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'
Expand All @@ -196,3 +197,14 @@ def Update(self, cmd, resource_group_name: str, cluster_name: str, auto_upgrade_
version=version,
configuration_settings=configuration_settings,
configuration_protected_settings=configuration_protected_settings)

@staticmethod
def _is_downgrade(v1: str, v2: str) -> bool:
"""
Returns True if version v1 is less than version v2.
"""
try:
return packaging_version.Version(v1) < packaging_version.Version(v2)
except packaging_version.InvalidVersion:
logger.debug("Warning: Unable to compare versions %s and %s.", v1, v2)
return True # This will cause the apply-CRDs hook to be disabled, which is safe.

0 comments on commit 0845f61

Please sign in to comment.