|
|
|
@ -57,16 +57,6 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
|
@Slf4j |
|
|
|
|
public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNamespaceService { |
|
|
|
|
|
|
|
|
|
private static String resourceYaml = "apiVersion: v1\n" |
|
|
|
|
+ "kind: ResourceQuota\n" |
|
|
|
|
+ "metadata:\n" |
|
|
|
|
+ " name: ${name}\n" |
|
|
|
|
+ " namespace: ${namespace}\n" |
|
|
|
|
+ "spec:\n" |
|
|
|
|
+ " hard:\n" |
|
|
|
|
+ " ${limitCpu}\n" |
|
|
|
|
+ " ${limitMemory}\n"; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private K8sNamespaceMapper k8sNamespaceMapper; |
|
|
|
|
|
|
|
|
@ -114,13 +104,10 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
|
|
|
|
|
* @param loginUser login user |
|
|
|
|
* @param namespace namespace |
|
|
|
|
* @param clusterCode k8s not null |
|
|
|
|
* @param limitsCpu limits cpu, can null means not limit |
|
|
|
|
* @param limitsMemory limits memory, can null means not limit |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public Map<String, Object> createK8sNamespace(User loginUser, String namespace, Long clusterCode, Double limitsCpu, |
|
|
|
|
Integer limitsMemory) { |
|
|
|
|
public Map<String, Object> createK8sNamespace(User loginUser, String namespace, Long clusterCode) { |
|
|
|
|
Map<String, Object> result = new HashMap<>(); |
|
|
|
|
if (isNotAdmin(loginUser, result)) { |
|
|
|
|
log.warn("Only admin can create K8s namespace, current login user name:{}.", loginUser.getUserName()); |
|
|
|
@ -139,18 +126,6 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (limitsCpu != null && limitsCpu < 0.0) { |
|
|
|
|
log.warn("Parameter limitsCpu is invalid."); |
|
|
|
|
putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, Constants.LIMITS_CPU); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (limitsMemory != null && limitsMemory < 0) { |
|
|
|
|
log.warn("Parameter limitsMemory is invalid."); |
|
|
|
|
putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, Constants.LIMITS_MEMORY); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (checkNamespaceExistInDb(namespace, clusterCode)) { |
|
|
|
|
log.warn("K8S namespace already exists."); |
|
|
|
|
putMsg(result, Status.K8S_NAMESPACE_EXIST, namespace, clusterCode); |
|
|
|
@ -183,18 +158,12 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
|
|
|
|
|
k8sNamespaceObj.setNamespace(namespace); |
|
|
|
|
k8sNamespaceObj.setClusterCode(clusterCode); |
|
|
|
|
k8sNamespaceObj.setUserId(loginUser.getId()); |
|
|
|
|
k8sNamespaceObj.setLimitsCpu(limitsCpu); |
|
|
|
|
k8sNamespaceObj.setLimitsMemory(limitsMemory); |
|
|
|
|
k8sNamespaceObj.setPodReplicas(0); |
|
|
|
|
k8sNamespaceObj.setPodRequestCpu(0.0); |
|
|
|
|
k8sNamespaceObj.setPodRequestMemory(0); |
|
|
|
|
k8sNamespaceObj.setCreateTime(now); |
|
|
|
|
k8sNamespaceObj.setUpdateTime(now); |
|
|
|
|
|
|
|
|
|
if (!Constants.K8S_LOCAL_TEST_CLUSTER_CODE.equals(k8sNamespaceObj.getClusterCode())) { |
|
|
|
|
try { |
|
|
|
|
String yamlStr = genDefaultResourceYaml(k8sNamespaceObj); |
|
|
|
|
k8sClientService.upsertNamespaceAndResourceToK8s(k8sNamespaceObj, yamlStr); |
|
|
|
|
k8sClientService.upsertNamespaceAndResourceToK8s(k8sNamespaceObj); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("Namespace create to k8s error", e); |
|
|
|
|
putMsg(result, Status.K8S_CLIENT_OPS_ERROR, e.getMessage()); |
|
|
|
@ -209,65 +178,6 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* update K8s Namespace tag and resource limit |
|
|
|
|
* |
|
|
|
|
* @param loginUser login user |
|
|
|
|
* @param userName owner |
|
|
|
|
* @param limitsCpu max cpu |
|
|
|
|
* @param limitsMemory max memory |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public Map<String, Object> updateK8sNamespace(User loginUser, int id, String userName, Double limitsCpu, |
|
|
|
|
Integer limitsMemory) { |
|
|
|
|
Map<String, Object> result = new HashMap<>(); |
|
|
|
|
if (isNotAdmin(loginUser, result)) { |
|
|
|
|
log.warn("Only admin can update K8s namespace, current login user name:{}.", loginUser.getUserName()); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (limitsCpu != null && limitsCpu < 0.0) { |
|
|
|
|
log.warn("Parameter limitsCpu is invalid."); |
|
|
|
|
putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, Constants.LIMITS_CPU); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (limitsMemory != null && limitsMemory < 0) { |
|
|
|
|
log.warn("Parameter limitsMemory is invalid."); |
|
|
|
|
putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, Constants.LIMITS_MEMORY); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
K8sNamespace k8sNamespaceObj = k8sNamespaceMapper.selectById(id); |
|
|
|
|
if (k8sNamespaceObj == null) { |
|
|
|
|
log.error("K8s namespace does not exist, namespaceId:{}.", id); |
|
|
|
|
putMsg(result, Status.K8S_NAMESPACE_NOT_EXIST, id); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Date now = new Date(); |
|
|
|
|
k8sNamespaceObj.setLimitsCpu(limitsCpu); |
|
|
|
|
k8sNamespaceObj.setLimitsMemory(limitsMemory); |
|
|
|
|
k8sNamespaceObj.setUpdateTime(now); |
|
|
|
|
|
|
|
|
|
if (!Constants.K8S_LOCAL_TEST_CLUSTER_CODE.equals(k8sNamespaceObj.getClusterCode())) { |
|
|
|
|
try { |
|
|
|
|
String yamlStr = genDefaultResourceYaml(k8sNamespaceObj); |
|
|
|
|
k8sClientService.upsertNamespaceAndResourceToK8s(k8sNamespaceObj, yamlStr); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("Namespace update to k8s error", e); |
|
|
|
|
putMsg(result, Status.K8S_CLIENT_OPS_ERROR, e.getMessage()); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// update to db
|
|
|
|
|
k8sNamespaceMapper.updateById(k8sNamespaceObj); |
|
|
|
|
log.info("K8s namespace update complete, namespace:{}.", k8sNamespaceObj.getNamespace()); |
|
|
|
|
putMsg(result, Status.SUCCESS); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* verify namespace and k8s |
|
|
|
|
* |
|
|
|
@ -338,42 +248,6 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
|
|
|
|
|
return k8sNamespaceMapper.existNamespace(namespace, clusterCode) == Boolean.TRUE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* use cpu memory create yaml |
|
|
|
|
* |
|
|
|
|
* @param k8sNamespace |
|
|
|
|
* @return yaml file |
|
|
|
|
*/ |
|
|
|
|
private String genDefaultResourceYaml(K8sNamespace k8sNamespace) { |
|
|
|
|
// resource use same name with namespace
|
|
|
|
|
String name = k8sNamespace.getNamespace(); |
|
|
|
|
String namespace = k8sNamespace.getNamespace(); |
|
|
|
|
String cpuStr = null; |
|
|
|
|
if (k8sNamespace.getLimitsCpu() != null) { |
|
|
|
|
cpuStr = k8sNamespace.getLimitsCpu() + ""; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String memoryStr = null; |
|
|
|
|
if (k8sNamespace.getLimitsMemory() != null) { |
|
|
|
|
memoryStr = k8sNamespace.getLimitsMemory() + "Gi"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String result = resourceYaml.replace("${name}", name) |
|
|
|
|
.replace("${namespace}", namespace); |
|
|
|
|
if (cpuStr == null) { |
|
|
|
|
result = result.replace("${limitCpu}", ""); |
|
|
|
|
} else { |
|
|
|
|
result = result.replace("${limitCpu}", "limits.cpu: '" + cpuStr + "'"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (memoryStr == null) { |
|
|
|
|
result = result.replace("${limitMemory}", ""); |
|
|
|
|
} else { |
|
|
|
|
result = result.replace("${limitMemory}", "limits.memory: " + memoryStr); |
|
|
|
|
} |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* query unauthorized namespace |
|
|
|
|
* |
|
|
|
|