|
|
@@ -29,7 +29,6 @@ import org.apache.commons.lang.StringUtils;
|
|
|
import org.eclipse.jgit.api.Git;
|
|
|
import org.eclipse.jgit.api.ResetCommand;
|
|
|
import org.eclipse.jgit.api.errors.GitAPIException;
|
|
|
-import org.eclipse.jgit.lib.ObjectId;
|
|
|
import org.eclipse.jgit.revwalk.RevCommit;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
@@ -279,6 +278,48 @@ public class EnvironmentServiceImpl implements EnvironmentService {
|
|
|
return EnvironmentConverter.convertToVO(environmentDAO.save(result));
|
|
|
}
|
|
|
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public EnvironmentVO hotUpdateConfig(Integer id) throws ServiceException {
|
|
|
+ Environment env = environmentDAO.findById(id).orElse(null);
|
|
|
+ if (env == null) {
|
|
|
+ throw ServiceException.BAD_REQUEST;
|
|
|
+ }
|
|
|
+ if (env.getBuildStatus() == BuildStatus.BUILDING || env.getDeployStatus() == DeployStatus.DEPLOYING || env.getDeployStatus() == DeployStatus.RESTARTING) {
|
|
|
+ throw new ServiceException("101", "存在正在进行的构建或部署!");
|
|
|
+ }
|
|
|
+
|
|
|
+ Config config = configDAO.findByConfigBelongsAndAndEntityId(ConfigBelongsToType.ENVIRONMENT, id);
|
|
|
+ if (config == null) {
|
|
|
+ throw ServiceException.INVALID_DATA;
|
|
|
+ }
|
|
|
+
|
|
|
+ ConfigVO.ConfigContent cfg = ConfigConverter.convertToVO(config).getConfig();
|
|
|
+ String labelKey = ResourceLabel.RESOURCE.getCode();
|
|
|
+ String labelValue = ResourceLabel.RESOURCE.getGenerator().gen("environment", String.valueOf(env.getAppId()), String.valueOf(env.getId()));
|
|
|
+ String ns = applicationProperties.getDeploymentNamespace();
|
|
|
+ Map<String, String> labelsMap = Collections.singletonMap(labelKey, labelValue);
|
|
|
+
|
|
|
+ // 同步 ConfigMap 数据(仅基于挂载配置内容)
|
|
|
+ ConfigMap configMap = ConfigMap.builder().data(cfg.getMountsConfigMaps()).build();
|
|
|
+ configMap.setName(labelValue);
|
|
|
+ configMap.setNamespace(ns);
|
|
|
+ configMap.setLabel(labelKey, labelValue);
|
|
|
+ List<ConfigMap> exists = configMapApi.getByCondition(K8sObjectRequest.builder().namespace(ns).labels(labelsMap).build());
|
|
|
+ if (CollectionUtils.isEmpty(exists) || exists.size() != 1) {
|
|
|
+ configMapApi.create(configMap);
|
|
|
+ } else {
|
|
|
+ configMapApi.update(configMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除相关 Pod,触发新 Pod 以挂载最新配置
|
|
|
+ deleteAllPods(id);
|
|
|
+
|
|
|
+ env.setDeployStatus(DeployStatus.RESTARTING);
|
|
|
+ env.setDeployStartTime(LocalDateTime.now());
|
|
|
+ return EnvironmentConverter.convertToVO(environmentDAO.save(env));
|
|
|
+ }
|
|
|
+
|
|
|
private void deleteAllPods(Integer environmentId) throws ServiceException {
|
|
|
// 查找环境
|
|
|
Environment environment = environmentDAO.findById(environmentId).orElse(null);
|