@ -22,13 +22,16 @@ import org.apache.dolphinscheduler.api.service.K8sNamespaceService;
import org.apache.dolphinscheduler.api.utils.PageInfo ;
import org.apache.dolphinscheduler.api.utils.PageInfo ;
import org.apache.dolphinscheduler.api.utils.Result ;
import org.apache.dolphinscheduler.api.utils.Result ;
import org.apache.dolphinscheduler.common.Constants ;
import org.apache.dolphinscheduler.common.Constants ;
import org.apache.dolphinscheduler.common.enums.AuthorizationType ;
import org.apache.dolphinscheduler.common.utils.CodeGenerateUtils ;
import org.apache.dolphinscheduler.dao.entity.Cluster ;
import org.apache.dolphinscheduler.dao.entity.K8sNamespace ;
import org.apache.dolphinscheduler.dao.entity.K8sNamespace ;
import org.apache.dolphinscheduler.dao.entity.User ;
import org.apache.dolphinscheduler.dao.entity.User ;
import org.apache.dolphinscheduler.dao.mapper.ClusterMapper ;
import org.apache.dolphinscheduler.dao.mapper.K8sNamespaceMapper ;
import org.apache.dolphinscheduler.dao.mapper.K8sNamespaceMapper ;
import org.apache.dolphinscheduler.remote.exceptions.RemotingException ;
import org.apache.dolphinscheduler.api.k8s.K8sClientService ;
import org.apache.dolphinscheduler.api.k8s.K8sClientService ;
import org.apache.commons.lang3 .StringUtils ;
import org.apache.commons.lang.StringUtils ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.Date ;
import java.util.Date ;
@ -63,11 +66,16 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
+ " hard:\n"
+ " hard:\n"
+ " ${limitCpu}\n"
+ " ${limitCpu}\n"
+ " ${limitMemory}\n" ;
+ " ${limitMemory}\n" ;
@Autowired
@Autowired
private K8sNamespaceMapper k8sNamespaceMapper ;
private K8sNamespaceMapper k8sNamespaceMapper ;
@Autowired
@Autowired
private K8sClientService k8sClientService ;
private K8sClientService k8sClientService ;
@Autowired
private ClusterMapper clusterMapper ;
/ * *
/ * *
* query namespace list paging
* query namespace list paging
*
*
@ -80,6 +88,11 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
@Override
@Override
public Result queryListPaging ( User loginUser , String searchVal , Integer pageNo , Integer pageSize ) {
public Result queryListPaging ( User loginUser , String searchVal , Integer pageNo , Integer pageSize ) {
Result result = new Result ( ) ;
Result result = new Result ( ) ;
if ( ! isAdmin ( loginUser ) ) {
putMsg ( result , Status . USER_NO_OPERATION_PERM ) ;
return result ;
}
Page < K8sNamespace > page = new Page < > ( pageNo , pageSize ) ;
Page < K8sNamespace > page = new Page < > ( pageNo , pageSize ) ;
IPage < K8sNamespace > k8sNamespaceList = k8sNamespaceMapper . queryK8sNamespacePaging ( page , searchVal ) ;
IPage < K8sNamespace > k8sNamespaceList = k8sNamespaceMapper . queryK8sNamespacePaging ( page , searchVal ) ;
@ -99,16 +112,15 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
*
*
* @param loginUser login user
* @param loginUser login user
* @param namespace namespace
* @param namespace namespace
* @param k8s k8s not null
* @param clusterCode k8s not null
* @param limitsCpu limits cpu , can null means not limit
* @param limitsCpu limits cpu , can null means not limit
* @param limitsMemory limits memory , can null means not limit
* @param limitsMemory limits memory , can null means not limit
* @return
* @return
* /
* /
@Override
@Override
public Map < String , Object > createK8sNamespace ( User loginUser , String namespace , String k8s , Double limitsCpu , Integer limitsMemory ) {
public Map < String , Object > createK8sNamespace ( User loginUser , String namespace , Long clusterCode , Double limitsCpu , Integer limitsMemory ) {
Map < String , Object > result = new HashMap < > ( ) ;
Map < String , Object > result = new HashMap < > ( ) ;
if ( ! canOperatorPermissions ( loginUser , null , AuthorizationType . K8S_NAMESPACE , null ) ) {
if ( isNotAdmin ( loginUser , result ) ) {
putMsg ( result , Status . USER_NO_OPERATION_PERM ) ;
return result ;
return result ;
}
}
@ -117,8 +129,8 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
return result ;
return result ;
}
}
if ( StringUtils . isEmpty ( k8s ) ) {
if ( clusterCode = = null ) {
putMsg ( result , Status . REQUEST_PARAMS_NOT_VALID_ERROR , Constants . K8S ) ;
putMsg ( result , Status . REQUEST_PARAMS_NOT_VALID_ERROR , Constants . CLUSTER ) ;
return result ;
return result ;
}
}
@ -132,27 +144,45 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
return result ;
return result ;
}
}
if ( checkNamespaceExistInDb ( namespace , k8s ) ) {
if ( checkNamespaceExistInDb ( namespace , clusterCode ) ) {
putMsg ( result , Status . K8S_NAMESPACE_EXIST , namespace , k8s ) ;
putMsg ( result , Status . K8S_NAMESPACE_EXIST , namespace , clusterCode ) ;
return result ;
}
Cluster cluster = clusterMapper . queryByClusterCode ( clusterCode ) ;
if ( cluster = = null ) {
putMsg ( result , Status . CLUSTER_NOT_EXISTS , namespace , clusterCode ) ;
return result ;
}
long code = 0L ;
try {
code = CodeGenerateUtils . getInstance ( ) . genCode ( ) ;
cluster . setCode ( code ) ;
} catch ( CodeGenerateUtils . CodeGenerateException e ) {
logger . error ( "Cluster code get error, " , e ) ;
}
if ( code = = 0L ) {
putMsg ( result , Status . INTERNAL_SERVER_ERROR_ARGS , "Error generating cluster code" ) ;
return result ;
return result ;
}
}
K8sNamespace k8sNamespaceObj = new K8sNamespace ( ) ;
K8sNamespace k8sNamespaceObj = new K8sNamespace ( ) ;
Date now = new Date ( ) ;
Date now = new Date ( ) ;
k8sNamespaceObj . setCode ( code ) ;
k8sNamespaceObj . setNamespace ( namespace ) ;
k8sNamespaceObj . setNamespace ( namespace ) ;
k8sNamespaceObj . setK8s ( k8s ) ;
k8sNamespaceObj . setClusterCode ( clusterCode ) ;
k8sNamespaceObj . setUserId ( loginUser . getId ( ) ) ;
k8sNamespaceObj . setUserId ( loginUser . getId ( ) ) ;
k8sNamespaceObj . setLimitsCpu ( limitsCpu ) ;
k8sNamespaceObj . setLimitsCpu ( limitsCpu ) ;
k8sNamespaceObj . setLimitsMemory ( limitsMemory ) ;
k8sNamespaceObj . setLimitsMemory ( limitsMemory ) ;
k8sNamespaceObj . setOnlineJobNum ( 0 ) ;
k8sNamespaceObj . setPodReplicas ( 0 ) ;
k8sNamespaceObj . setPodReplicas ( 0 ) ;
k8sNamespaceObj . setPodRequestCpu ( 0 . 0 ) ;
k8sNamespaceObj . setPodRequestCpu ( 0 . 0 ) ;
k8sNamespaceObj . setPodRequestMemory ( 0 ) ;
k8sNamespaceObj . setPodRequestMemory ( 0 ) ;
k8sNamespaceObj . setCreateTime ( now ) ;
k8sNamespaceObj . setCreateTime ( now ) ;
k8sNamespaceObj . setUpdateTime ( now ) ;
k8sNamespaceObj . setUpdateTime ( now ) ;
if ( ! Constants . K8S_LOCAL_TEST_CLUSTER . equals ( k8sNamespaceObj . getK8s ( ) ) ) {
if ( ! Constants . K8S_LOCAL_TEST_CLUSTER_CODE . equals ( k8sNamespaceObj . getClusterCode ( ) ) ) {
try {
try {
String yamlStr = genDefaultResourceYaml ( k8sNamespaceObj ) ;
String yamlStr = genDefaultResourceYaml ( k8sNamespaceObj ) ;
k8sClientService . upsertNamespaceAndResourceToK8s ( k8sNamespaceObj , yamlStr ) ;
k8sClientService . upsertNamespaceAndResourceToK8s ( k8sNamespaceObj , yamlStr ) ;
@ -181,8 +211,7 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
@Override
@Override
public Map < String , Object > updateK8sNamespace ( User loginUser , int id , String userName , Double limitsCpu , Integer limitsMemory ) {
public Map < String , Object > updateK8sNamespace ( User loginUser , int id , String userName , Double limitsCpu , Integer limitsMemory ) {
Map < String , Object > result = new HashMap < > ( ) ;
Map < String , Object > result = new HashMap < > ( ) ;
if ( ! canOperatorPermissions ( loginUser , null , AuthorizationType . K8S_NAMESPACE , null ) ) {
if ( isNotAdmin ( loginUser , result ) ) {
putMsg ( result , Status . USER_NO_OPERATION_PERM ) ;
return result ;
return result ;
}
}
@ -207,7 +236,7 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
k8sNamespaceObj . setLimitsMemory ( limitsMemory ) ;
k8sNamespaceObj . setLimitsMemory ( limitsMemory ) ;
k8sNamespaceObj . setUpdateTime ( now ) ;
k8sNamespaceObj . setUpdateTime ( now ) ;
if ( ! Constants . K8S_LOCAL_TEST_CLUSTER . equals ( k8sNamespaceObj . getK8s ( ) ) ) {
if ( ! Constants . K8S_LOCAL_TEST_CLUSTER_CODE . equals ( k8sNamespaceObj . getClusterCode ( ) ) ) {
try {
try {
String yamlStr = genDefaultResourceYaml ( k8sNamespaceObj ) ;
String yamlStr = genDefaultResourceYaml ( k8sNamespaceObj ) ;
k8sClientService . upsertNamespaceAndResourceToK8s ( k8sNamespaceObj , yamlStr ) ;
k8sClientService . upsertNamespaceAndResourceToK8s ( k8sNamespaceObj , yamlStr ) ;
@ -228,24 +257,24 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
* verify namespace and k8s
* verify namespace and k8s
*
*
* @param namespace namespace
* @param namespace namespace
* @param k8s k8s
* @param clusterCode cluster code
* @return true if the k8s and namespace not exists , otherwise return false
* @return true if the k8s and namespace not exists , otherwise return false
* /
* /
@Override
@Override
public Result < Object > verifyNamespaceK8s ( String namespace , String k8s ) {
public Result < Object > verifyNamespaceK8s ( String namespace , Long clusterCode ) {
Result < Object > result = new Result < > ( ) ;
Result < Object > result = new Result < > ( ) ;
if ( StringUtils . isEmpty ( namespace ) ) {
if ( StringUtils . isEmpty ( namespace ) ) {
putMsg ( result , Status . REQUEST_PARAMS_NOT_VALID_ERROR , Constants . NAMESPACE ) ;
putMsg ( result , Status . REQUEST_PARAMS_NOT_VALID_ERROR , Constants . NAMESPACE ) ;
return result ;
return result ;
}
}
if ( StringUtils . isEmpty ( k8s ) ) {
if ( clusterCode = = null ) {
putMsg ( result , Status . REQUEST_PARAMS_NOT_VALID_ERROR , Constants . K8S ) ;
putMsg ( result , Status . REQUEST_PARAMS_NOT_VALID_ERROR , Constants . CLUSTER ) ;
return result ;
return result ;
}
}
if ( checkNamespaceExistInDb ( namespace , k8s ) ) {
if ( checkNamespaceExistInDb ( namespace , clusterCode ) ) {
putMsg ( result , Status . K8S_NAMESPACE_EXIST , namespace , k8s ) ;
putMsg ( result , Status . K8S_NAMESPACE_EXIST , namespace , clusterCode ) ;
return result ;
return result ;
}
}
@ -263,8 +292,7 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
@Override
@Override
public Map < String , Object > deleteNamespaceById ( User loginUser , int id ) {
public Map < String , Object > deleteNamespaceById ( User loginUser , int id ) {
Map < String , Object > result = new HashMap < > ( ) ;
Map < String , Object > result = new HashMap < > ( ) ;
if ( ! canOperatorPermissions ( loginUser , null , AuthorizationType . K8S_NAMESPACE , null ) ) {
if ( isNotAdmin ( loginUser , result ) ) {
putMsg ( result , Status . USER_NO_OPERATION_PERM ) ;
return result ;
return result ;
}
}
@ -273,8 +301,13 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
putMsg ( result , Status . K8S_NAMESPACE_NOT_EXIST , id ) ;
putMsg ( result , Status . K8S_NAMESPACE_NOT_EXIST , id ) ;
return result ;
return result ;
}
}
if ( ! Constants . K8S_LOCAL_TEST_CLUSTER . equals ( k8sNamespaceObj . getK8s ( ) ) ) {
if ( ! Constants . K8S_LOCAL_TEST_CLUSTER_CODE . equals ( k8sNamespaceObj . getClusterCode ( ) ) ) {
k8sClientService . deleteNamespaceToK8s ( k8sNamespaceObj . getNamespace ( ) , k8sNamespaceObj . getK8s ( ) ) ;
try {
k8sClientService . deleteNamespaceToK8s ( k8sNamespaceObj . getNamespace ( ) , k8sNamespaceObj . getClusterCode ( ) ) ;
} catch ( RemotingException e ) {
putMsg ( result , Status . K8S_CLIENT_OPS_ERROR , id ) ;
return result ;
}
}
}
k8sNamespaceMapper . deleteById ( id ) ;
k8sNamespaceMapper . deleteById ( id ) ;
putMsg ( result , Status . SUCCESS ) ;
putMsg ( result , Status . SUCCESS ) ;
@ -287,8 +320,8 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
* @param namespace namespace
* @param namespace namespace
* @return true if the k8s and namespace not exists , otherwise return false
* @return true if the k8s and namespace not exists , otherwise return false
* /
* /
private boolean checkNamespaceExistInDb ( String namespace , String k8s ) {
private boolean checkNamespaceExistInDb ( String namespace , Long clusterCode ) {
return k8sNamespaceMapper . existNamespace ( namespace , k8s ) = = Boolean . TRUE ;
return k8sNamespaceMapper . existNamespace ( namespace , clusterCode ) = = Boolean . TRUE ;
}
}
/ * *
/ * *
@ -338,8 +371,7 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
@Override
@Override
public Map < String , Object > queryUnauthorizedNamespace ( User loginUser , Integer userId ) {
public Map < String , Object > queryUnauthorizedNamespace ( User loginUser , Integer userId ) {
Map < String , Object > result = new HashMap < > ( ) ;
Map < String , Object > result = new HashMap < > ( ) ;
if ( loginUser . getId ( ) ! = userId & & ! canOperatorPermissions ( loginUser , null , AuthorizationType . K8S_NAMESPACE , null ) ) {
if ( loginUser . getId ( ) ! = userId & & isNotAdmin ( loginUser , result ) ) {
putMsg ( result , Status . USER_NO_OPERATION_PERM ) ;
return result ;
return result ;
}
}
// query all namespace list,this auth does not like project
// query all namespace list,this auth does not like project
@ -367,8 +399,7 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
public Map < String , Object > queryAuthorizedNamespace ( User loginUser , Integer userId ) {
public Map < String , Object > queryAuthorizedNamespace ( User loginUser , Integer userId ) {
Map < String , Object > result = new HashMap < > ( ) ;
Map < String , Object > result = new HashMap < > ( ) ;
if ( loginUser . getId ( ) ! = userId & & ! canOperatorPermissions ( loginUser , null , AuthorizationType . K8S_NAMESPACE , null ) ) {
if ( loginUser . getId ( ) ! = userId & & isNotAdmin ( loginUser , result ) ) {
putMsg ( result , Status . USER_NO_OPERATION_PERM ) ;
return result ;
return result ;
}
}
@ -387,7 +418,7 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
* /
* /
@Override
@Override
public List < K8sNamespace > queryNamespaceAvailable ( User loginUser ) {
public List < K8sNamespace > queryNamespaceAvailable ( User loginUser ) {
if ( canOperatorPermissions ( loginUser , null , AuthorizationType . K8S_NAMESPACE , null ) ) {
if ( isAdmin ( loginUser ) ) {
return k8sNamespaceMapper . selectList ( null ) ;
return k8sNamespaceMapper . selectList ( null ) ;
} else {
} else {
return k8sNamespaceMapper . queryNamespaceAvailable ( loginUser . getId ( ) ) ;
return k8sNamespaceMapper . queryNamespaceAvailable ( loginUser . getId ( ) ) ;
@ -419,5 +450,4 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames
}
}
return resultList ;
return resultList ;
}
}
}
}