diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java index 4cfcb14b2c..97d4a67f2b 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java @@ -32,6 +32,7 @@ import org.apache.dolphinscheduler.dao.mapper.K8sNamespaceMapper; import org.apache.dolphinscheduler.remote.exceptions.RemotingException; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.collections.CollectionUtils; import java.util.ArrayList; import java.util.Date; @@ -40,6 +41,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -439,10 +441,31 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames */ @Override public List queryNamespaceAvailable(User loginUser) { + List k8sNamespaces; if (isAdmin(loginUser)) { - return k8sNamespaceMapper.selectList(null); + k8sNamespaces = k8sNamespaceMapper.selectList(null); } else { - return k8sNamespaceMapper.queryNamespaceAvailable(loginUser.getId()); + k8sNamespaces = k8sNamespaceMapper.queryNamespaceAvailable(loginUser.getId()); + } + setClusterName(k8sNamespaces); + return k8sNamespaces; + } + + /** + * set cluster_name + * @param k8sNamespaces source data + */ + private void setClusterName(List k8sNamespaces) { + if (CollectionUtils.isNotEmpty(k8sNamespaces)) { + List clusters = clusterMapper.queryAllClusterList(); + if (CollectionUtils.isNotEmpty(clusters)) { + Map codeNameMap = clusters.stream() + .collect(Collectors.toMap(Cluster::getCode, Cluster::getName, (a, b) -> a)); + for (K8sNamespace k8sNamespace : k8sNamespaces) { + String clusterName = codeNameMap.get(k8sNamespace.getClusterCode()); + k8sNamespace.setClusterName(clusterName); + } + } } } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/K8SNamespaceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/K8SNamespaceServiceTest.java index ed684ea914..390153aa3d 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/K8SNamespaceServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/K8SNamespaceServiceTest.java @@ -221,6 +221,25 @@ public class K8SNamespaceServiceTest { Assert.assertTrue(CollectionUtils.isEmpty(namespaces)); } + @Test + public void testQueryNamespaceAvailable() { + List k8sNamespaces = new ArrayList<>(); + K8sNamespace k8sNamespace = new K8sNamespace(); + k8sNamespace.setClusterCode(1L); + k8sNamespaces.add(k8sNamespace); + + List clusters = new ArrayList<>(); + Cluster cluster = new Cluster(); + cluster.setCode(1L); + cluster.setName("test"); + clusters.add(cluster); + + Mockito.when(k8sNamespaceMapper.selectList(Mockito.any())).thenReturn(k8sNamespaces); + Mockito.when(clusterMapper.queryAllClusterList()).thenReturn(clusters); + List result = k8sNamespaceService.queryNamespaceAvailable(getLoginUser()); + Assert.assertEquals(result.get(0).getClusterName(), cluster.getName()); + } + private User getLoginUser() { User loginUser = new User();