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