Skip to content

Commit

Permalink
fix(sdk): fix mapping template upgrading problems (#114)
Browse files Browse the repository at this point in the history
* fix :
stage data plane in_process should show completed time;
add check during the upgrading
* remove feature related to mapping template upgrade v2
  • Loading branch information
kuangxiang20240501 authored Nov 12, 2024
1 parent aab5eeb commit 12cdedc
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 518 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,20 @@

import static com.consoleconnect.kraken.operator.core.service.UnifiedAssetService.getSearchPageRequest;

import com.consoleconnect.kraken.operator.auth.model.AuthDataProperty;
import com.consoleconnect.kraken.operator.auth.security.UserContext;
import com.consoleconnect.kraken.operator.controller.aspect.TemplateUpgradeBlockChecker;
import com.consoleconnect.kraken.operator.controller.dto.*;
import com.consoleconnect.kraken.operator.controller.event.TemplateUpgradeEvent;
import com.consoleconnect.kraken.operator.controller.service.TemplateIngestService;
import com.consoleconnect.kraken.operator.controller.service.TemplateUpgradeService;
import com.consoleconnect.kraken.operator.core.dto.Tuple2;
import com.consoleconnect.kraken.operator.core.dto.UnifiedAssetDto;
import com.consoleconnect.kraken.operator.core.enums.AssetKindEnum;
import com.consoleconnect.kraken.operator.core.enums.DeployStatusEnum;
import com.consoleconnect.kraken.operator.core.enums.EnvNameEnum;
import com.consoleconnect.kraken.operator.core.model.HttpResponse;
import com.consoleconnect.kraken.operator.core.service.UnifiedAssetService;
import com.consoleconnect.kraken.operator.core.toolkit.AssetsConstants;
import com.consoleconnect.kraken.operator.core.toolkit.Paging;
import com.consoleconnect.kraken.operator.core.toolkit.PagingHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

@AllArgsConstructor
@RestController()
Expand All @@ -40,75 +25,7 @@
@Tag(name = "Template Upgrade", description = "Template Upgrade")
@Slf4j
public class TemplateUpgradeController {
private final UnifiedAssetService unifiedAssetService;
private final TemplateIngestService templateIngestService;
private final TemplateUpgradeService templateUpgradeService;
private final AuthDataProperty.ResourceServer resourceServer;

@Operation(summary = "list template upgrade releases change log")
@GetMapping("/releases")
public HttpResponse<Paging<TemplateUpgradeReleaseVO>> listTemplateChangeLog(
@PathVariable("productId") String productId,
@RequestParam(value = "orderBy", required = false, defaultValue = "createdAt") String orderBy,
@RequestParam(value = "direction", required = false, defaultValue = "DESC")
Sort.Direction direction,
@RequestParam(value = "page", required = false, defaultValue = PagingHelper.DEFAULT_PAGE_STR)
int page,
@RequestParam(value = "size", required = false, defaultValue = PagingHelper.DEFAULT_SIZE_STR)
int size) {
List<Tuple2> tuple3s = List.of();
Paging<UnifiedAssetDto> assetDtoPaging =
unifiedAssetService.findBySpecification(
Tuple2.ofList(
AssetsConstants.FIELD_KIND, AssetKindEnum.PRODUCT_TEMPLATE_UPGRADE.getKind()),
tuple3s,
null,
getSearchPageRequest(page, size, direction, orderBy),
null);
List<TemplateUpgradeReleaseVO> list =
assetDtoPaging.getData().stream()
.map(templateUpgradeService::generateTemplateUpgradeReleaseVO)
.toList();
// the latest can upgrade
list.stream()
.findFirst()
.ifPresent(
vo -> {
vo.setShowStageUpgradeButton(true);
vo.setShowProductionUpgradeButton(true);
});
allowProductionUpgrade(list);
return HttpResponse.ok(
PagingHelper.toPageNoSubList(
list, assetDtoPaging.getPage(), assetDtoPaging.getSize(), assetDtoPaging.getTotal()));
}

private static void allowProductionUpgrade(List<TemplateUpgradeReleaseVO> list) {
list.stream()
.filter(vo -> !vo.isShowStageUpgradeButton())
.filter(vo -> CollectionUtils.isNotEmpty(vo.getDeployments()))
.findFirst()
.ifPresent(
vo -> {
vo.setShowStageUpgradeButton(true);
TemplateUpgradeDeploymentVO latestProdDeployment =
list.get(0).getDeployments().stream()
.filter(t -> t.getEnvName().equalsIgnoreCase(EnvNameEnum.PRODUCTION.name()))
.findFirst()
.orElse(null);
if (latestProdDeployment != null) {
return;
}
vo.getDeployments().stream()
.filter(
deployment ->
deployment.getEnvName().equalsIgnoreCase(EnvNameEnum.PRODUCTION.name()))
.findFirst()
.ifPresentOrElse(
d -> vo.setShowProductionUpgradeButton(false),
() -> vo.setShowProductionUpgradeButton(true));
});
}

@Operation(summary = "list template upgrade deployments")
@GetMapping("/template-deployments")
Expand Down Expand Up @@ -142,65 +59,4 @@ public HttpResponse<List<TemplateUpgradeDeploymentVO>> currentUpgradeVersion(
@PathVariable("productId") String productId) {
return HttpResponse.ok(templateUpgradeService.currentUpgradeVersion());
}

@Operation(summary = "stage environment upgrade")
@PostMapping("/stage")
@TemplateUpgradeBlockChecker
public Mono<HttpResponse<String>> stageUpgrade(
@PathVariable("productId") String productId,
@RequestBody CreateUpgradeRequest createUpgradeRequest) {
return UserContext.getUserId()
.publishOn(Schedulers.boundedElastic())
.map(
userId -> {
templateUpgradeService.checkCondition2StageUpgrade(
createUpgradeRequest.getTemplateUpgradeId(),
createUpgradeRequest.getStageEnvId());
TemplateUpgradeEvent templateUpgradeEvent = new TemplateUpgradeEvent();
templateUpgradeEvent.setTemplateUpgradeId(
createUpgradeRequest.getTemplateUpgradeId());
templateUpgradeEvent.setEnvId(createUpgradeRequest.getStageEnvId());
templateUpgradeEvent.setUserId(userId);
return templateUpgradeService.stageUpgrade(templateUpgradeEvent);
})
.map(HttpResponse::ok);
}

@Operation(summary = "product environment upgrade")
@PostMapping("/production")
@TemplateUpgradeBlockChecker
public Mono<HttpResponse<String>> productUpgrade(
@PathVariable("productId") String productId,
@RequestBody CreateProductionUpgradeRequest createProductionUpgradeRequest) {
return UserContext.getUserId()
.publishOn(Schedulers.boundedElastic())
.map(
userId -> {
templateUpgradeService.checkCondition2ProductionUpgrade(
createProductionUpgradeRequest);
return templateUpgradeService.deployProductionV2(
createProductionUpgradeRequest.getTemplateUpgradeId(),
createProductionUpgradeRequest.getStageEnvId(),
createProductionUpgradeRequest.getProductEnvId(),
userId);
})
.map(HttpResponse::ok);
}

@Operation(summary = "whether update is allowed ")
@GetMapping("/allow-update-operations")
public HttpResponse<Boolean> allowUpdateOperations(@PathVariable("productId") String productId) {
Paging<UnifiedAssetDto> assetDtoPaging =
unifiedAssetService.findBySpecification(
Tuple2.ofList(
AssetsConstants.FIELD_KIND,
AssetKindEnum.PRODUCT_TEMPLATE_DEPLOYMENT.getKind(),
AssetsConstants.FIELD_STATUS,
DeployStatusEnum.IN_PROCESS.name()),
null,
null,
null,
null);
return HttpResponse.ok(CollectionUtils.isEmpty(assetDtoPaging.getData()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static com.consoleconnect.kraken.operator.core.toolkit.LabelConstants.CONDITION_NULL;

import com.consoleconnect.kraken.operator.auth.security.UserContext;
import com.consoleconnect.kraken.operator.controller.aspect.TemplateUpgradeBlockChecker;
import com.consoleconnect.kraken.operator.controller.dto.*;
import com.consoleconnect.kraken.operator.controller.event.TemplateUpgradeEvent;
import com.consoleconnect.kraken.operator.controller.service.TemplateUpgradeService;
Expand Down Expand Up @@ -43,7 +42,6 @@ public class TemplateUpgradeV3Controller {

@Operation(summary = "control plane upgrade")
@PostMapping("/control-plane")
@TemplateUpgradeBlockChecker
public Mono<HttpResponse<String>> controlPlaneUpgrade(
@PathVariable("productId") String productId,
@RequestBody CreateControlPlaneUpgradeRequest request) {
Expand All @@ -58,7 +56,6 @@ public Mono<HttpResponse<String>> controlPlaneUpgrade(

@Operation(summary = "stage environment upgrade")
@PostMapping("/stage")
@TemplateUpgradeBlockChecker
public Mono<HttpResponse<String>> stageUpgrade(
@PathVariable("productId") String productId,
@RequestBody CreateUpgradeRequest createUpgradeRequest) {
Expand All @@ -78,7 +75,6 @@ public Mono<HttpResponse<String>> stageUpgrade(

@Operation(summary = "product environment upgrade")
@PostMapping("/production")
@TemplateUpgradeBlockChecker
public Mono<HttpResponse<String>> productUpgrade(
@PathVariable("productId") String productId,
@RequestBody CreateProductionUpgradeRequest createProductionUpgradeRequest) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.consoleconnect.kraken.operator.controller.aspect;

import static com.consoleconnect.kraken.operator.controller.enums.SystemStateEnum.CAN_UPGRADE_STATES;

import com.consoleconnect.kraken.operator.controller.enums.SystemStateEnum;
import com.consoleconnect.kraken.operator.controller.model.SystemInfo;
import com.consoleconnect.kraken.operator.controller.service.SystemInfoService;
import com.consoleconnect.kraken.operator.core.exception.KrakenException;
Expand All @@ -21,7 +20,7 @@ public class TemplateUpgradeAspect {
" @annotation(com.consoleconnect.kraken.operator.controller.aspect.TemplateUpgradeBlockChecker)")
public Object validateAspect(ProceedingJoinPoint pjp) throws Throwable {
SystemInfo systemInfo = systemInfoService.find();
if (!CAN_UPGRADE_STATES.contains(systemInfo.getStatus())) {
if (!SystemStateEnum.RUNNING.name().equalsIgnoreCase(systemInfo.getStatus())) {
throw KrakenException.forbidden("System is upgrading, current operation is not allowed");
}
return pjp.proceed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ public enum SystemStateEnum {
STAGE_UPGRADE_DONE,
PRODUCTION_UPGRADING,
RUNNING;
public static final List<String> CAN_UPGRADE_STATES =
public static final List<String> CAN_CONTROL_UPGRADE_STATES =
List.of(
RUNNING.name(),
SystemStateEnum.CONTROL_PLANE_UPGRADE_DONE.name(),
SystemStateEnum.STAGE_UPGRADE_DONE.name());
public static final List<String> CAN_STAGE_UPGRADE_STATES =
List.of(
SystemStateEnum.CONTROL_PLANE_UPGRADE_DONE.name(),
SystemStateEnum.STAGE_UPGRADE_DONE.name());
}
Original file line number Diff line number Diff line change
Expand Up @@ -424,25 +424,18 @@ private void handleTemplateDeploymentCallback(String appTemplateDeploymentId) {
});
}

private void updateSystemStatus(UnifiedAssetEntity templateDeployment) {
public void updateSystemStatus(UnifiedAssetEntity templateDeployment) {
String templateUpgradeId =
templateDeployment.getLabels().getOrDefault(LABEL_APP_TEMPLATE_UPGRADE_ID, "");
UnifiedAssetDto templateUpgrade = unifiedAssetService.findOne(templateUpgradeId);
String envName = templateDeployment.getLabels().get(LABEL_ENV_NAME);
String originVersion = templateUpgrade.getMetadata().getLabels().get(LABEL_PRODUCT_VERSION);
String version = Constants.formatVersionUsingV(originVersion);
if (EnvNameEnum.STAGE.name().equalsIgnoreCase(envName)) {
systemInfoService.updateProductVersion(
SystemStateEnum.STAGE_UPGRADE_DONE,
null,
Constants.formatVersion(
templateUpgrade.getMetadata().getLabels().get(LABEL_PRODUCT_VERSION)),
null);
SystemStateEnum.STAGE_UPGRADE_DONE, null, version, null);
} else {
systemInfoService.updateProductVersion(
SystemStateEnum.RUNNING,
null,
null,
Constants.formatVersion(
templateUpgrade.getMetadata().getLabels().get(LABEL_PRODUCT_VERSION)));
systemInfoService.updateProductVersion(SystemStateEnum.RUNNING, null, null, version);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.BeanUtils;
Expand Down Expand Up @@ -303,7 +302,11 @@ public Paging<TemplateUpgradeDeploymentVO> listTemplateDeployment(
templateUpgradeDeploymentVO.setStatus(dto.getMetadata().getStatus());
templateUpgradeDeploymentVO.setDeploymentId(dto.getId());
templateUpgradeDeploymentVO.setCreatedAt(dto.getCreatedAt());
templateUpgradeDeploymentVO.setUpdatedAt(dto.getUpdatedAt());
if (DeployStatusEnum.SUCCESS
.name()
.equalsIgnoreCase(templateUpgradeDeploymentVO.getStatus())) {
templateUpgradeDeploymentVO.setUpdatedAt(dto.getUpdatedAt());
}
return templateUpgradeDeploymentVO;
})
.toList();
Expand Down Expand Up @@ -573,12 +576,13 @@ public String getSystemUpgradeUser() {

@Transactional(rollbackFor = Exception.class)
public String controlPlaneUpgradeV3(String templateUpgradeId, String userId) {
checkOnlyOneControlPlaneUpgrade(templateUpgradeId);
return this.controlPlaneUpgrade(templateUpgradeId, userId);
}

public String controlPlaneUpgrade(String templateUpgradeId, String userId) {
SystemInfo systemInfo = systemInfoService.find();
if (!SystemStateEnum.CAN_UPGRADE_STATES.contains(systemInfo.getStatus())) {
if (!SystemStateEnum.CAN_CONTROL_UPGRADE_STATES.contains(systemInfo.getStatus())) {
throw KrakenException.badRequest("Current system state is not allowed to upgrade");
}
UnifiedAssetDto productAsset = this.getProductAsset();
Expand Down Expand Up @@ -650,6 +654,20 @@ public String controlPlaneUpgrade(String templateUpgradeId, String userId) {
return ingestionDataResult.getData().getId().toString();
}

private void checkOnlyOneControlPlaneUpgrade(String templateUpgradeId) {
Paging<UnifiedAssetDto> assetDtoPaging =
unifiedAssetService.findBySpecification(
Tuple2.ofList(
AssetsConstants.FIELD_KIND, PRODUCT_TEMPLATE_CONTROL_DEPLOYMENT.getKind()),
Tuple2.ofList(LabelConstants.LABEL_APP_TEMPLATE_UPGRADE_ID, templateUpgradeId),
null,
PageRequest.of(0, 1),
null);
if (CollectionUtils.isNotEmpty(assetDtoPaging.getData())) {
throw KrakenException.internalError("Control plane upgrade already exists");
}
}

@Transactional
public String stageUpgrade(TemplateUpgradeEvent event) {
UpgradeSourceService upgradeSourceService =
Expand Down Expand Up @@ -843,11 +861,6 @@ private void deployMapperDeployment(
List<UnifiedAssetDto> byAllKeysIn =
unifiedAssetService.findByAllKeysIn(new ArrayList<>(changedMappers), false);
byAllKeysIn.stream()
.filter(
ent ->
MapUtils.isNotEmpty(ent.getMetadata().getLabels())
&& LabelConstants.VALUE_DEPLOYED_STATUS_DEPLOYED.equalsIgnoreCase(
ent.getMetadata().getLabels().get(LabelConstants.LABEL_DEPLOYED_STATUS)))
.map(UnifiedAssetDto::getMetadata)
.map(Metadata::getKey)
.forEach(
Expand All @@ -868,26 +881,11 @@ private void deployMapperDeployment(
if (CollectionUtils.isNotEmpty(deploymentIds)) {
envDeployment.setMapperDeployment(deploymentIds);
}
List<String> draftMappers =
byAllKeysIn.stream()
.filter(
ent ->
!(MapUtils.isNotEmpty(ent.getMetadata().getLabels())
&& LabelConstants.VALUE_DEPLOYED_STATUS_DEPLOYED.equalsIgnoreCase(
ent.getMetadata()
.getLabels()
.get(LabelConstants.LABEL_DEPLOYED_STATUS))))
.map(UnifiedAssetDto::getMetadata)
.map(Metadata::getKey)
.toList();
if (CollectionUtils.isNotEmpty(draftMappers)) {
envDeployment.setMapperDraft(draftMappers);
}
}

public String stageUpgradeV3(TemplateUpgradeEvent event) {
SystemInfo systemInfo = systemInfoService.find();
if (!SystemStateEnum.CAN_UPGRADE_STATES.contains(systemInfo.getStatus())) {
if (!SystemStateEnum.CAN_STAGE_UPGRADE_STATES.contains(systemInfo.getStatus())) {
throw KrakenException.badRequest("The current system status does not support upgrade");
}
checkIsLatestUpgrade(event.getTemplateUpgradeId(), true);
Expand Down Expand Up @@ -917,6 +915,7 @@ public String stageUpgradeV3(TemplateUpgradeEvent event) {
templateSynCompletedEvent.setTemplateUpgradeRecords(upgradeRecords);
templateSynCompletedEvent.setEnvId(event.getEnvId());
templateSynCompletedEvent.setTemplateUpgradeId(event.getTemplateUpgradeId());
templateSynCompletedEvent.setUserId(event.getUserId());
try {
IngestionDataResult ingestionDataResult = this.deployStageV3(templateSynCompletedEvent);
String deploymentId = ingestionDataResult.getData().getId().toString();
Expand Down
Loading

0 comments on commit 12cdedc

Please sign in to comment.