caishunfeng commented on code in PR #10650:
URL: https://github.com/apache/dolphinscheduler/pull/10650#discussion_r911725448
##########
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/k8s/K8sManager.java:
##########
@@ -48,31 +44,96 @@ public class K8sManager {
/**
* cache k8s client
*/
- private static Map<String, KubernetesClient> clientMap = new Hashtable<>();
+ private static Map<Long, KubernetesClient> clientMap = new Hashtable<>();
@Autowired
- private K8sMapper k8sMapper;
+ private ClusterMapper clusterMapper;
+
+ /**
+ * get k8s client for api use
+ *
+ * @param clusterCode
+ * @return
+ */
+ public synchronized KubernetesClient getK8sClient(Long clusterCode) throws
RemotingException {
+ if (null == clusterCode) {
+ return null;
+ }
- public KubernetesClient getK8sClient(String k8sName) {
- if (null == k8sName) {
+ return getAndUpdateK8sClient(clusterCode, false);
+ }
+
+ /**
+ * @param clusterCode
+ * @return new client if need updated
+ */
+ public synchronized KubernetesClient getAndUpdateK8sClient(Long
clusterCode, boolean update) throws RemotingException {
+ if (null == clusterCode) {
return null;
}
- return clientMap.get(k8sName);
+
+ if (update) {
+ deleteK8sClientInner(clusterCode);
+ }
+
+ if (clientMap.containsKey(clusterCode)) {
+ return clientMap.get(clusterCode);
+ } else {
+ createK8sClientInner(clusterCode);
+ }
+ return clientMap.get(clusterCode);
+ }
+
+
+ private void deleteK8sClientInner(Long clusterCode) {
+ if (clusterCode == null) {
+ return;
+ }
+ Cluster cluster = clusterMapper.queryByClusterCode(clusterCode);
+ if (cluster == null) {
+ return;
+ }
+ KubernetesClient client = clientMap.get(clusterCode);
+ if (client != null) {
+ client.close();
+ }
}
- @EventListener
- public void buildApiClientAll(ApplicationReadyEvent readyEvent) throws
RemotingException {
- QueryWrapper<K8s> nodeWrapper = new QueryWrapper<>();
- List<K8s> k8sList = k8sMapper.selectList(nodeWrapper);
+ private void createK8sClientInner(Long clusterCode) throws
RemotingException {
+ Cluster cluster = clusterMapper.queryByClusterCode(clusterCode);
+ if (cluster == null) {
+ return;
+ }
- if (k8sList != null) {
- for (K8s k8s : k8sList) {
- DefaultKubernetesClient client = getClient(k8s.getK8sConfig());
- clientMap.put(k8s.getK8sName(), client);
+ String k8sConfig = ClusterConfUtils.getK8sConfig(cluster.getConfig());
+ if (k8sConfig != null) {
+ DefaultKubernetesClient client = null;
+ try {
+ client = getClient(k8sConfig);
+ clientMap.put(clusterCode, client);
+ } catch (RemotingException e) {
+ logger.error("cluster code ={},fail to get k8s ApiClient:
{}", clusterCode, e.getMessage());
+ throw new RemotingException("fail to get k8s ApiClient:" +
e.getMessage());
}
}
}
+
+// @EventListener
+// public void buildApiClientAll(ApplicationReadyEvent readyEvent) throws
RemotingException {
Review Comment:
remove if no use.
##########
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java:
##########
@@ -420,4 +451,10 @@ private List<K8sNamespace>
getUnauthorizedNamespaces(Set<K8sNamespace> namespace
return resultList;
}
-}
+
+ private void setClusterName()
Review Comment:
remove
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]