1919from azure .cli .core .util import send_raw_request , sdk_no_wait
2020from knack .log import get_logger
2121from knack .util import CLIError
22+ from packaging .version import InvalidVersion , Version
2223from ..vendored_sdks .models import Extension , PatchExtension , Scope , ScopeCluster
2324from .._client_factory import cf_k8s_extension_types
2425from .DefaultExtension import DefaultExtension
@@ -104,7 +105,6 @@ class ChaosStudio(DefaultExtension):
104105 DEFAULT_CLUSTER_TYPE = "managedclusters"
105106 DEFAULT_RELEASE_NAMESPACE = "chaos-infrastructure"
106107 DEFAULT_RELEASE_TRAIN = "dev"
107- DEFAULT_VERSION = "0.1.6"
108108 WORKSPACE_ID_KEY = "chaos-workspace-id"
109109 EXISTING_ROLE_KEY = "chaos-existing-role-definition-id"
110110
@@ -191,7 +191,12 @@ def Create(
191191 )
192192 if not workspace_id :
193193 raise InvalidArgumentValueError ("'chaos-workspace-id' is required." )
194- self ._validate_version (version or self .DEFAULT_VERSION )
194+ release_train = release_train or self .DEFAULT_RELEASE_TRAIN
195+ if not version :
196+ # auto-upgrade is forced off, so the extension RP needs an explicit version.
197+ version = self ._latest_registered_version (
198+ cmd , resource_group_name , cluster_rp , cluster_type , cluster_name , release_train
199+ )
195200 release_namespace = self ._resolve_release_namespace (
196201 release_namespace
197202 )
@@ -207,23 +212,33 @@ def Create(
207212 extension_type = extension_type ,
208213 auto_upgrade_minor_version = False ,
209214 auto_upgrade_mode = None ,
210- release_train = release_train or self . DEFAULT_RELEASE_TRAIN ,
211- version = version or self . DEFAULT_VERSION ,
215+ release_train = release_train ,
216+ version = version ,
212217 scope = extension_scope ,
213218 configuration_settings = configuration_settings ,
214219 configuration_protected_settings = configuration_protected_settings ,
215220 )
216221 return extension , name , False
217222
218- @classmethod
219- def _validate_version (cls , version ):
220- # Only the coordinated bootstrap contract is known compatible. Availability
221- # remains an extension-RP registration check, not a version-number inference.
222- if version != cls .DEFAULT_VERSION :
223+ @staticmethod
224+ def _latest_registered_version (cmd , resource_group_name , cluster_rp , cluster_type , cluster_name , release_train ):
225+ versions = cf_k8s_extension_types (cmd .cli_ctx ).cluster_list_versions (
226+ resource_group_name , cluster_rp , cluster_type , cluster_name ,
227+ "Microsoft.ChaosStudio" , release_train = release_train ,
228+ )
229+ candidates = []
230+ for item in versions or []:
231+ value = getattr (getattr (item , "properties" , None ), "version" , None )
232+ try :
233+ candidates .append ((Version (value ), value ))
234+ except (InvalidVersion , TypeError ):
235+ continue
236+ if not candidates :
223237 raise InvalidArgumentValueError (
224- "Microsoft.ChaosStudio staged installation requires chart {} . "
225- "It must be published and registered before installation ." .format (cls . DEFAULT_VERSION )
238+ "No Microsoft.ChaosStudio version is registered for this cluster on release train '{}' . "
239+ "Pass --version once one is available ." .format (release_train )
226240 )
241+ return max (candidates )[1 ]
227242
228243 @classmethod
229244 def _reject_managed_overrides (cls , * settings ):
@@ -264,7 +279,6 @@ def _stage(cls, extension):
264279 "The existing extension is not a staged platform-identity installation. "
265280 "Automatic migration of older manual identities is not supported."
266281 )
267- cls ._validate_version (extension .version )
268282 return value
269283
270284 def Install (self , cmd , client , resource_group_name , cluster_rp , cluster_type ,
@@ -451,7 +465,6 @@ def Update(
451465 configuration_protected_settings or {}
452466 )
453467 self ._reject_managed_overrides (supplied_settings , configuration_protected_settings )
454- self ._validate_version (version or original_extension .version )
455468 if self ._stage (original_extension ) != "true" :
456469 raise InvalidArgumentValueError ("Resume bootstrap with create before updating settings." )
457470 self ._require_success (original_extension )
@@ -504,11 +517,7 @@ def Update(
504517 or getattr (original_extension , "release_train" , None )
505518 or self .DEFAULT_RELEASE_TRAIN
506519 ),
507- version = (
508- version
509- or getattr (original_extension , "version" , None )
510- or self .DEFAULT_VERSION
511- ),
520+ version = version or getattr (original_extension , "version" , None ),
512521 configuration_settings = configuration_settings ,
513522 configuration_protected_settings = configuration_protected_settings ,
514523 )
0 commit comments