guoshupei commented on code in PR #4784:
URL: https://github.com/apache/linkis/pull/4784#discussion_r1274329203


##########
linkis-engineconn-plugins/spark/src/main/scala/org/apache/linkis/engineplugin/spark/factory/SparkEngineConnResourceFactory.scala:
##########
@@ -20,19 +20,50 @@ package org.apache.linkis.engineplugin.spark.factory
 import org.apache.linkis.common.utils.{ByteTimeUtils, Logging}
 import org.apache.linkis.engineplugin.spark.config.SparkResourceConfiguration._
 import org.apache.linkis.manager.common.entity.resource.{
+  DriverAndKubernetesResource,
   DriverAndYarnResource,
+  KubernetesResource,
   LoadInstanceResource,
   Resource,
   YarnResource
 }
-import 
org.apache.linkis.manager.engineplugin.common.resource.AbstractEngineResourceFactory
+import org.apache.linkis.manager.engineplugin.common.resource.{
+  AbstractEngineResourceFactory,
+  EngineResourceRequest
+}
+import org.apache.linkis.manager.label.entity.cluster.ClusterLabel
+import org.apache.linkis.manager.label.utils.LabelUtil
 
 import org.apache.commons.lang3.StringUtils
 
 import java.util
 
+import io.fabric8.kubernetes.api.model.Quantity
+
 class SparkEngineConnResourceFactory extends AbstractEngineResourceFactory 
with Logging {
 
+  override protected def getMinRequestResource(
+      engineResourceRequest: EngineResourceRequest
+  ): Resource = {
+    val clusterLabel = 
LabelUtil.getLabelFromList[ClusterLabel](engineResourceRequest.labels)
+    if (clusterLabel != null && clusterLabel.getClusterType == "K8S") {

Review Comment:
   change '==' to `equals`, and use `toUpperCase` to be compatible with string 
matching



##########
linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/KubernetesResource.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.linkis.manager.common.entity.resource;
+
+import org.apache.linkis.common.utils.ByteTimeUtils;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class KubernetesResource extends Resource {
+  private final long memory;
+  private final long cores;
+  private final String namespace;
+
+  private KubernetesResource(Resource r) {
+    if (r instanceof KubernetesResource) {
+      KubernetesResource t = (KubernetesResource) r;
+      this.memory = t.memory;
+      this.cores = t.cores;
+      this.namespace = t.namespace;
+    } else if (r instanceof MemoryResource) {
+      MemoryResource m = (MemoryResource) r;
+      this.memory = m.getMemory();
+      this.cores = 0;
+      this.namespace = "default";
+    } else if (r instanceof CPUResource) {
+      CPUResource c = (CPUResource) r;
+      this.memory = 0;
+      this.cores = c.getCores();
+      this.namespace = "default";
+    } else {
+      this.memory = Long.MAX_VALUE;
+      this.cores = Long.MAX_VALUE;
+      this.namespace = "default";
+    }
+  }
+
+  public KubernetesResource() {
+    this(Long.MAX_VALUE, Long.MAX_VALUE);

Review Comment:
   add default value of namespace



##########
linkis-engineconn-plugins/spark/src/main/scala/org/apache/linkis/engineplugin/spark/factory/SparkEngineConnResourceFactory.scala:
##########
@@ -20,19 +20,50 @@ package org.apache.linkis.engineplugin.spark.factory
 import org.apache.linkis.common.utils.{ByteTimeUtils, Logging}
 import org.apache.linkis.engineplugin.spark.config.SparkResourceConfiguration._
 import org.apache.linkis.manager.common.entity.resource.{
+  DriverAndKubernetesResource,
   DriverAndYarnResource,
+  KubernetesResource,
   LoadInstanceResource,
   Resource,
   YarnResource
 }
-import 
org.apache.linkis.manager.engineplugin.common.resource.AbstractEngineResourceFactory
+import org.apache.linkis.manager.engineplugin.common.resource.{
+  AbstractEngineResourceFactory,
+  EngineResourceRequest
+}
+import org.apache.linkis.manager.label.entity.cluster.ClusterLabel
+import org.apache.linkis.manager.label.utils.LabelUtil
 
 import org.apache.commons.lang3.StringUtils
 
 import java.util
 
+import io.fabric8.kubernetes.api.model.Quantity
+
 class SparkEngineConnResourceFactory extends AbstractEngineResourceFactory 
with Logging {
 
+  override protected def getMinRequestResource(
+      engineResourceRequest: EngineResourceRequest
+  ): Resource = {
+    val clusterLabel = 
LabelUtil.getLabelFromList[ClusterLabel](engineResourceRequest.labels)
+    if (clusterLabel != null && clusterLabel.getClusterType == "K8S") {
+      getRequestKubernetesResource(engineResourceRequest.properties)
+    } else {
+      getRequestResource(engineResourceRequest.properties)
+    }
+  }
+
+  override protected def getMaxRequestResource(
+      engineResourceRequest: EngineResourceRequest
+  ): Resource = {
+    val clusterLabel = 
LabelUtil.getLabelFromList[ClusterLabel](engineResourceRequest.labels)
+    if (clusterLabel != null && clusterLabel.getStringValue.startsWith("K8S")) 
{

Review Comment:
   use `toUpperCase` to be compatible with string matching



##########
linkis-engineconn-plugins/spark/src/main/scala/org/apache/linkis/engineplugin/spark/factory/SparkEngineConnResourceFactory.scala:
##########
@@ -20,19 +20,50 @@ package org.apache.linkis.engineplugin.spark.factory
 import org.apache.linkis.common.utils.{ByteTimeUtils, Logging}
 import org.apache.linkis.engineplugin.spark.config.SparkResourceConfiguration._
 import org.apache.linkis.manager.common.entity.resource.{
+  DriverAndKubernetesResource,
   DriverAndYarnResource,
+  KubernetesResource,
   LoadInstanceResource,
   Resource,
   YarnResource
 }
-import 
org.apache.linkis.manager.engineplugin.common.resource.AbstractEngineResourceFactory
+import org.apache.linkis.manager.engineplugin.common.resource.{
+  AbstractEngineResourceFactory,
+  EngineResourceRequest
+}
+import org.apache.linkis.manager.label.entity.cluster.ClusterLabel
+import org.apache.linkis.manager.label.utils.LabelUtil
 
 import org.apache.commons.lang3.StringUtils
 
 import java.util
 
+import io.fabric8.kubernetes.api.model.Quantity
+
 class SparkEngineConnResourceFactory extends AbstractEngineResourceFactory 
with Logging {
 
+  override protected def getMinRequestResource(
+      engineResourceRequest: EngineResourceRequest
+  ): Resource = {
+    val clusterLabel = 
LabelUtil.getLabelFromList[ClusterLabel](engineResourceRequest.labels)
+    if (clusterLabel != null && clusterLabel.getClusterType == "K8S") {

Review Comment:
   change '==' to `equals`, and use `toUpperCase` to be compatible with string 
matching



##########
linkis-computation-governance/linkis-manager/linkis-application-manager/src/main/java/org/apache/linkis/manager/rm/external/kubernetes/KubernetesResourceRequester.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.linkis.manager.rm.external.kubernetes;
+
+import org.apache.linkis.manager.common.entity.resource.*;
+import org.apache.linkis.manager.rm.external.domain.ExternalAppInfo;
+import org.apache.linkis.manager.rm.external.domain.ExternalResourceIdentifier;
+import org.apache.linkis.manager.rm.external.domain.ExternalResourceProvider;
+import org.apache.linkis.manager.rm.external.request.ExternalResourceRequester;
+
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
+
+import java.util.*;
+
+import io.fabric8.kubernetes.api.model.Node;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceQuota;
+import io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics;
+import io.fabric8.kubernetes.client.ConfigBuilder;
+import io.fabric8.kubernetes.client.DefaultKubernetesClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KubernetesResourceRequester implements ExternalResourceRequester {
+  private static final Logger logger = 
LoggerFactory.getLogger(KubernetesResourceRequester.class);
+  private ExternalResourceProvider provider = null;
+  private DefaultKubernetesClient client = null;
+  private String k8sMasterUrl;
+  private String k8sClientCertData;
+  private String k8sClientKeyData;
+  private String k8sCaCertData;
+
+  @Override
+  public NodeResource requestResourceInfo(
+      ExternalResourceIdentifier identifier, ExternalResourceProvider 
provider) {
+    this.provider = provider;
+    if (this.client == null
+        || !StringUtils.equals(
+            k8sMasterUrl, (String) 
provider.getConfigMap().get("k8sMasterUrl"))) {
+      reloadExternalResourceAddress(provider);
+    }

Review Comment:
   KubernetesResourceRequester is singleton, there are problems with member 
variables of client and provider 



##########
linkis-computation-governance/linkis-manager/linkis-application-manager/src/main/java/org/apache/linkis/manager/rm/service/RequestResourceService.java:
##########
@@ -404,7 +403,61 @@ public Pair<Integer, String> generateNotEnoughMessage(
         return generateNotEnoughMessage(
             dy.getYarnResource(), dyAvailable.getYarnResource(), 
dyMax.getYarnResource());
       }
-
+    } else if (requestResource instanceof KubernetesResource) {
+      KubernetesResource kubernetesResource = (KubernetesResource) 
requestResource;
+      KubernetesResource kubernetesResourceAvailable = (KubernetesResource) 
availableResource;
+      KubernetesResource kubernetesResourceMax = (KubernetesResource) 
maxResource;
+      if (kubernetesResource.getCores() > kubernetesResource.getCores()) {

Review Comment:
   The right thing to do is  kubernetesResource.getCores() > 
kubernetesResourceAvailable.getCores()



##########
linkis-engineconn-plugins/spark/src/main/scala/org/apache/linkis/engineplugin/spark/factory/SparkEngineConnResourceFactory.scala:
##########
@@ -63,4 +94,44 @@ class SparkEngineConnResourceFactory extends 
AbstractEngineResourceFactory with
     )
   }
 
+  def getRequestKubernetesResource(properties: util.Map[String, String]): 
Resource = {
+    val executorNum = LINKIS_SPARK_EXECUTOR_INSTANCES.getValue(properties)
+    val executorCores = if 
(properties.containsKey(LINKIS_SPARK_KUBERNETES_EXECUTOR_CORES)) {
+      val executorCoresQuantity =
+        
Quantity.parse(LINKIS_SPARK_KUBERNETES_EXECUTOR_CORES.getValue(properties))
+      (Quantity.getAmountInBytes(executorCoresQuantity).doubleValue() * 
1000).toLong
+    } else {
+      LINKIS_SPARK_EXECUTOR_CORES.getValue(properties) * 1000L
+    }
+    val executorMemory = LINKIS_SPARK_EXECUTOR_MEMORY.getValue(properties)
+    val executorMemoryWithUnit = if (StringUtils.isNumeric(executorMemory)) {
+      executorMemory + "g"
+    } else {
+      executorMemory
+    }
+    val driverCores = if 
(properties.containsKey(LINKIS_SPARK_KUBERNETES_DRIVER_CORES)) {
+      val executorCoresQuantity =
+        
Quantity.parse(LINKIS_SPARK_KUBERNETES_DRIVER_CORES.getValue(properties))
+      (Quantity.getAmountInBytes(executorCoresQuantity).doubleValue() * 
1000).toLong
+    } else {
+      LINKIS_SPARK_DRIVER_CORES.getValue(properties) * 1000L
+    }
+    val driverMemory = LINKIS_SPARK_DRIVER_MEMORY.getValue(properties)
+    val driverMemoryWithUnit = if (StringUtils.isNumeric(driverMemory)) {
+      driverMemory + "g"
+    } else {
+      driverMemory
+    }
+    val totalExecutorMemory = ByteTimeUtils.byteStringAsBytes(
+      executorMemoryWithUnit
+    ) * executorNum + ByteTimeUtils.byteStringAsBytes(driverMemoryWithUnit)
+    val totalExecutorCores = executorCores * executorNum + driverCores
+    // logger.info(s"总需求内存:$totalExecutorMemory, 总需求核数$totalExecutorCores")

Review Comment:
   Use English



-- 
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: notifications-unsubscr...@linkis.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@linkis.apache.org
For additional commands, e-mail: notifications-h...@linkis.apache.org

Reply via email to