def cluster_update(data: dict, op_ctx: ctx.OperationContext): """Request handler for cluster resize operation. Validate data before actual resize is delegated to cluster service. :return: Defined entity of the native cluster :rtype: container_service_extension.def_.models.DefEntity """ # TODO Reject request if rde_in_use is less than 2.0.0 # TODO find out the RDE version from the request spec # TODO Insert RDE converters and validators cluster_id = data[RequestKey.CLUSTER_ID] rde_in_use = server_utils.get_rde_version_in_use() svc = cluster_service_factory.ClusterServiceFactory(op_ctx). \ get_cluster_service(rde_in_use) NativeEntityClass = rde_factory.get_rde_model(rde_in_use) cluster_entity_spec: AbstractNativeEntity = \ NativeEntityClass(**data[RequestKey.INPUT_SPEC]) # noqa: E501 curr_entity = svc.entity_svc.get_entity(cluster_id) is_upgrade_operation = \ request_utils.validate_cluster_update_request_and_check_cluster_upgrade( # noqa: E501 asdict(cluster_entity_spec.spec), asdict(curr_entity.entity.spec)) if is_upgrade_operation: return asdict(svc.upgrade_cluster(cluster_id, cluster_entity_spec)) return asdict(svc.resize_cluster(cluster_id, cluster_entity_spec))
def __init__(self, entity: AbstractNativeEntity, entityType: str, name: str = None, id: str = None, externalId: str = None, state: str = None, owner: Owner = None, org: Org = None): self.id = id self.entityType = entityType # Get the entity type version from entity type urn entity_type_version = self.entityType.split(":")[-1] self.entity = entity if isinstance(entity, dict): # Parse the entity to the right entity class NativeEntityClass = get_rde_model(entity_type_version) self.entity = NativeEntityClass.from_dict(entity) self.name = name if not self.name: if entity_type_version == RDEVersion.RDE_1_0_0: self.name = self.entity.metadata.cluster_name else: self.name = self.entity.metadata.name self.externalId = externalId self.state = state self.owner = Owner(**owner) if isinstance(owner, dict) else owner self.org = Org(**org) if isinstance(org, dict) else org
def convert_runtime_rde_to_input_rde_version_format( runtime_rde: AbstractNativeEntity, input_rde_version): # noqa: E501 InputNativeEntityClass: Type[ AbstractNativeEntity] = rde_factory.get_rde_model( input_rde_version) # noqa: E501 output_entity = InputNativeEntityClass.from_native_entity(runtime_rde) return output_entity
def cluster_update(data: dict, op_ctx: ctx.OperationContext): """Request handler for cluster update operation. :return: Defined entity of the native cluster :rtype: container_service_extension.def_.models.DefEntity """ cluster_id = data[RequestKey.CLUSTER_ID] input_entity: dict = data[RequestKey.INPUT_SPEC] # Validate the input # ToDo: Should the validation be done using a v36 client? rde_validator_factory.get_validator( rde_version=rde_constants.RDEVersion.RDE_2_0_0). \ validate(cloudapi_client=op_ctx.cloudapi_client, entity_id=cluster_id, entity=input_entity, operation=BehaviorOperation.UPDATE_CLUSTER) # TODO Insert RDE converter if needed. # Call the backend to initiate the cluster update operation. rde_in_use = server_utils.get_rde_version_in_use() svc = cluster_service_factory.ClusterServiceFactory(op_ctx). \ get_cluster_service(rde_in_use) NativeEntityClass: Type[AbstractNativeEntity] = rde_factory.get_rde_model( rde_in_use) # noqa: E501 cluster_entity: AbstractNativeEntity = \ NativeEntityClass.from_dict(input_entity) return svc.update_cluster(cluster_id, cluster_entity).to_dict()
def cluster_upgrade(data, op_ctx: ctx.OperationContext): """Request handler for cluster upgrade operation. Validate data before actual upgrade is delegated to cluster service. :return: Dict """ rde_in_use = server_utils.get_rde_version_in_use() svc = cluster_service_factory.ClusterServiceFactory(op_ctx). \ get_cluster_service(rde_in_use) # TODO find out the RDE version from the request spec # TODO Insert RDE converters and validators NativeEntityClass = rde_factory.get_rde_model(rde_in_use) cluster_entity_spec: AbstractNativeEntity = \ NativeEntityClass(**data[RequestKey.INPUT_SPEC]) cluster_id = data[RequestKey.CLUSTER_ID] curr_entity = svc.entity_svc.get_entity(cluster_id) request_utils.validate_request_payload( asdict(cluster_entity_spec.spec), asdict(curr_entity.entity.spec), exclude_fields=[ FlattenedClusterSpecKey.TEMPLATE_NAME.value, FlattenedClusterSpecKey.TEMPLATE_REVISION.value ]) return asdict(svc.upgrade_cluster(cluster_id, cluster_entity_spec))
def cluster_resize(data: dict, op_ctx: ctx.OperationContext): """Request handler for cluster resize operation. Validate data before actual resize is delegated to cluster service. :return: Defined entity of the native cluster :rtype: container_service_extension.def_.models.DefEntity """ rde_in_use = server_utils.get_rde_version_in_use() svc = cluster_service_factory.ClusterServiceFactory(op_ctx). \ get_cluster_service(rde_in_use) cluster_id = data[RequestKey.CLUSTER_ID] # TODO find out the RDE version from the request spec # TODO Insert RDE converters and validators NativeEntityClass = rde_factory.get_rde_model(rde_in_use) cluster_entity_spec: AbstractNativeEntity = \ NativeEntityClass(**data[RequestKey.INPUT_SPEC]) # noqa: E501 curr_entity = svc.entity_svc.get_entity(cluster_id) request_utils.validate_request_payload( asdict(cluster_entity_spec.spec), asdict(curr_entity.entity.spec), exclude_fields=[ FlattenedClusterSpecKey.WORKERS_COUNT.value, FlattenedClusterSpecKey.NFS_COUNT.value ]) return asdict(svc.resize_cluster(cluster_id, cluster_entity_spec))
def convert(entity, src_rde_version: str, target_rde_version: str): """Convert the Cluster Entity from one RDE version to another. :param entity: Input cluster entity :param src_rde_version: The current RDE version of the entity :param str target_rde_version: The target RDE version of the entity. :return data: """ target_model = get_rde_model(target_rde_version) return target_model(**entity)
def convert_input_rde_to_runtime_rde_format(input_entity: dict): """Convert input entity to runtime rde format. :param AbstractNativeEntity input_entity: entity to be converted :return: Entity in runtime rde format :rtype: AbstractNativeEntity """ payload_version = input_entity.get( def_constants.PayloadKey.PAYLOAD_VERSION) # noqa: E501 runtime_rde_version = server_utils.get_rde_version_in_use() RuntimeNativeEntityClass: Type[ AbstractNativeEntity] = rde_factory.get_rde_model( runtime_rde_version) # noqa: E501 InputNativeEntityClass: Type[ AbstractNativeEntity] = rde_factory.get_rde_model( # noqa: E501 def_constants.MAP_INPUT_PAYLOAD_VERSION_TO_RDE_VERSION[ payload_version]) # noqa: E501 input_rde: AbstractNativeEntity = InputNativeEntityClass( **input_entity) # noqa: E501 return RuntimeNativeEntityClass.from_native_entity(input_rde)
def _get_sample_cluster_configuration_by_k8_runtime( k8_runtime, server_rde_in_use): # noqa: E501 NativeEntityClass = rde_factory.get_rde_model(server_rde_in_use) sample_native_entity = NativeEntityClass.sample_native_entity( k8_runtime.value) # noqa: E501 native_entity_dict = sample_native_entity.to_dict() # remove status part of the entity dict del native_entity_dict['status'] sample_apply_spec = yaml.dump(native_entity_dict) CLIENT_LOGGER.info(sample_apply_spec) return sample_apply_spec
def cluster_create(data: dict, op_ctx: ctx.OperationContext): """Request handler for cluster create operation. :return: Defined entity of the native cluster :rtype: container_service_extension.def_.models.DefEntity """ rde_in_use = server_utils.get_rde_version_in_use() svc = cluster_service_factory.ClusterServiceFactory(op_ctx). \ get_cluster_service(rde_in_use) # TODO find out the RDE version from the request spec # TODO Insert RDE converters and validators NativeEntityClass = rde_factory.get_rde_model(rde_in_use) cluster_entity_spec: AbstractNativeEntity = \ NativeEntityClass(**data[RequestKey.INPUT_SPEC]) return asdict(svc.create_cluster(cluster_entity_spec))
def nfs_node_delete(data, op_ctx: ctx.OperationContext): """Request handler for node delete operation. Required data: cluster_name, node_names_list Optional data and default values: org_name=None, ovdc_name=None (data validation handled in broker) :return: Dict """ rde_in_use = server_utils.get_rde_version_in_use() if semantic_version.Version(rde_in_use) >= semantic_version.Version( rde_constants.RDEVersion.RDE_2_0_0.value): # noqa: E501 cluster_dict = cluster_handler.nfs_node_delete( data=data, op_ctx=op_ctx) # noqa: E501 # NOTE: cluster_handler is always expected to return RDE version # 2.0.0 or more. Hence a convertion to 1.0 is needed rde_class = rde_factory.get_rde_model(rde_in_use) cluster_entity: AbstractNativeEntity = rde_class.from_dict( cluster_dict['entity']) # noqa: E501 new_native_entity: AbstractNativeEntity = rde_utils.convert_runtime_rde_to_input_rde_version_format( # noqa: E501 cluster_entity, rde_constants.RDEVersion.RDE_1_0_0) cluster_dict['entity'] = new_native_entity.to_dict() cluster_dict[ 'entityType'] = common_models.EntityType.NATIVE_ENTITY_TYPE_1_0_0.value.id # noqa: E501 return cluster_dict svc = cluster_service_factory.ClusterServiceFactory(op_ctx). \ get_cluster_service(rde_in_use) cluster_id = data[RequestKey.CLUSTER_ID] node_name = data[RequestKey.NODE_NAME] telemetry_handler.record_user_action_details( cse_operation=telemetry_constants.CseOperation.V35_NODE_DELETE, cse_params={ telemetry_constants.PayloadKey.CLUSTER_ID: cluster_id, telemetry_constants.PayloadKey.NODE_NAME: node_name, telemetry_constants.PayloadKey.SOURCE_DESCRIPTION: thread_local_data.get_thread_local_data( ThreadLocalData.USER_AGENT) # noqa: E501 }) return svc.delete_nodes(cluster_id, [node_name]).to_dict()
def _get_sample_cluster_configuration_by_k8_runtime( k8_runtime, server_rde_in_use): # noqa: E501 NativeEntityClass = rde_factory.get_rde_model(server_rde_in_use) sample_native_entity = NativeEntityClass.sample_native_entity( k8_runtime.value) # noqa: E501 native_entity_dict = sample_native_entity.to_dict() # remove status part of the entity dict del native_entity_dict['status'] # Hiding the network spec section for Andromeda (CSE 3.1) # spec.settings.network is targeted for CSE 3.1.1 to accommodate CNI=Antrea # Below line can be deleted post Andromeda (CSE 3.1) del native_entity_dict['spec']['settings']['network'] sample_apply_spec = yaml.dump(native_entity_dict) CLIENT_LOGGER.info(sample_apply_spec) return sample_apply_spec
def __init__(self, name: str, org: Org, entityType: str, entity, owner: Owner, **kwargs): self.name = name self.org = Org(**org) if isinstance(org, dict) else org self.entityType = entityType entity_dict = entity.to_dict() if not isinstance(entity, dict) else entity # noqa: E501 if entity_dict['kind'] in \ [shared_constants.ClusterEntityKind.NATIVE.value, shared_constants.ClusterEntityKind.TKG_PLUS.value, shared_constants.ClusterEntityKind.TKG_M.value]: # Get the entity type version from entity type urn entity_type_version = self.entityType.split(":")[-1] # Parse the entity to the right entity class NativeEntityClass = get_rde_model(entity_type_version) self.entity: AbstractNativeEntity = \ NativeEntityClass.from_dict(entity_dict) if isinstance(entity, dict) else entity # noqa: E501 elif entity_dict['kind'] == \ shared_constants.ClusterEntityKind.TKG_S.value: self.entity = TKGEntity(**entity) if isinstance(entity, dict) else entity # noqa: E501 else: raise Exception("Invalid cluster kind") self.owner = Owner(**owner) if isinstance(owner, dict) else owner
def _get_sample_cluster_configuration_by_k8_runtime(k8_runtime, server_rde_in_use): # noqa: E501 NativeEntityClass = rde_factory.get_rde_model(server_rde_in_use) sample_native_entity = NativeEntityClass.sample_native_entity(k8_runtime.value) # noqa: E501 native_entity_dict = sample_native_entity.to_dict() # remove status part of the entity dict del native_entity_dict['status'] # Hiding the network spec section for Andromeda (CSE 3.1) # spec.settings.network is targeted for CSE 3.1.1 to accommodate CNI=Antrea # Below line can be deleted post Andromeda (CSE 3.1) del native_entity_dict['spec']['settings']['network'] # Hiding the cpu and memory properties from controlPlane and workers for # Andromeda (CSE 3.1). Below lines can be deleted once cpu and memory # support is added in CSE 3.1.1 del native_entity_dict['spec']['topology']['controlPlane']['cpu'] del native_entity_dict['spec']['topology']['controlPlane']['memory'] del native_entity_dict['spec']['topology']['workers']['cpu'] del native_entity_dict['spec']['topology']['workers']['memory'] sample_apply_spec = yaml.dump(native_entity_dict) CLIENT_LOGGER.info(sample_apply_spec) return sample_apply_spec
def cluster_create(data: dict, op_ctx: ctx.OperationContext): """Request handler for cluster create operation. :return: Defined entity of the native cluster :rtype: container_service_extension.def_.models.DefEntity """ input_entity: dict = data[RequestKey.INPUT_SPEC] # Validate the input rde_validator_factory.get_validator( rde_version=rde_constants.RDEVersion.RDE_2_0_0). \ validate(cloudapi_client=op_ctx.cloudapi_client, entity=input_entity) rde_in_use = server_utils.get_rde_version_in_use() svc = cluster_service_factory.ClusterServiceFactory(op_ctx). \ get_cluster_service(rde_in_use) # TODO Insert RDE converter if needed. NativeEntityClass: Type[AbstractNativeEntity] = rde_factory.get_rde_model( rde_in_use) # noqa: E501 cluster_entity_spec: AbstractNativeEntity = \ NativeEntityClass(**input_entity) return asdict(svc.create_cluster(cluster_entity_spec))
def create_cluster(behavior_ctx: BehaviorRequestContext): entity_id: str = behavior_ctx.entity_id entity: dict = behavior_ctx.entity cloudapi_client: CloudApiClient = behavior_ctx.op_ctx.cloudapi_client api_version: float = behavior_ctx.api_version # Validate the input rde_version_introduced_at_api_version = rde_utils.get_rde_version_introduced_at_api_version( api_version) # noqa: E501 rde_validator_factory.get_validator( rde_version=rde_version_introduced_at_api_version). \ validate(cloudapi_client=cloudapi_client, entity=entity) # TODO Based on the rde_in_use, convert the entity if necessary. # Call the backend to initiate the cluster creation. rde_in_use = server_utils.get_rde_version_in_use() svc = cluster_service_factory.ClusterServiceFactory( behavior_ctx).get_cluster_service(rde_in_use) # noqa: E501 NativeEntityClass: AbstractNativeEntity = rde_factory.get_rde_model( rde_in_use) # noqa: E501 input_entity: AbstractNativeEntity = NativeEntityClass(**entity) return svc.create_cluster(entity_id=entity_id, input_native_entity=input_entity) # noqa: E501
def _get_sample_cluster_configuration_by_k8_runtime( k8_runtime, server_rde_in_use): # noqa: E501 NativeEntityClass = rde_factory.get_rde_model(server_rde_in_use) return NativeEntityClass.get_sample_native_cluster_specification( k8_runtime) # noqa: E501