This is an automated email from the ASF dual-hosted git repository.

hefengen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new fac19b2c8c refactor:enable option kubernetes (#5784)
fac19b2c8c is described below

commit fac19b2c8c31ca5fcd1d5ef516058b2d9b501aaf
Author: Misaya295 <45778734+misaya...@users.noreply.github.com>
AuthorDate: Wed Nov 20 22:44:40 2024 +0800

    refactor:enable option kubernetes (#5784)
    
    * refactor:enable option kubernetes
    
    * Update application.yml
    
    refactor:enable option for kubernetes
    
    * fix: fix cr
    
    * fix: fix cr
    
    * fix: fix cr
    
    ---------
    
    Co-authored-by: moremind <hefen...@apache.org>
    Co-authored-by: aias00 <liuhon...@apache.org>
---
 .../admin/config/KubernetesConfiguration.java      | 45 +++++++++++++---------
 .../shenyu/admin/scale/config/ScaleProperties.java | 20 ++++++++++
 .../admin/scale/scaler/KubernetesScaler.java       |  5 ++-
 shenyu-admin/src/main/resources/application.yml    | 33 ++++++++--------
 4 files changed, 66 insertions(+), 37 deletions(-)

diff --git 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/KubernetesConfiguration.java
 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/KubernetesConfiguration.java
index ff1156124e..a856c9f22b 100644
--- 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/KubernetesConfiguration.java
+++ 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/KubernetesConfiguration.java
@@ -21,52 +21,59 @@ import io.kubernetes.client.openapi.ApiClient;
 import io.kubernetes.client.openapi.apis.AppsV1Api;
 import io.kubernetes.client.util.Config;
 import org.apache.shenyu.admin.config.properties.DeploymentProperties;
+import org.apache.shenyu.admin.scale.scaler.KubernetesScaler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import 
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import 
org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.util.Optional;
 
 @Configuration
 @EnableConfigurationProperties(DeploymentProperties.class)
 public class KubernetesConfiguration {
 
+    private final Logger logger = 
LoggerFactory.getLogger(KubernetesConfiguration.class);
+
     /**
      * kubernetes apiClient.
      * @param deploymentProperties deploymentProperties
      * @return AppsV1Api
      */
     @Bean
+    @ConditionalOnProperty(value = "shenyu.k8s.scale.enabled", havingValue = 
"true")
     @ConditionalOnMissingBean(DeploymentProperties.class)
     public AppsV1Api apiClient(final DeploymentProperties 
deploymentProperties) {
         try {
-            if (isLocalEnvironment()) {
-
-                return new AppsV1Api(Config.defaultClient());
-
-            } else {
-                ApiClient client = Config.fromToken(
-                        deploymentProperties.getApiServer(),
-                        deploymentProperties.getToken(),
-                        false
-                );
-                client.setSslCaCert(new 
FileInputStream(deploymentProperties.getCaCertPath()));
-                return new AppsV1Api(client);
-            }
+            ApiClient client = Config.fromToken(
+                    deploymentProperties.getApiServer(),
+                    deploymentProperties.getToken(),
+                    false
+            );
+            client.setSslCaCert(new 
FileInputStream(deploymentProperties.getCaCertPath()));
+            return new AppsV1Api(client);
 
         } catch (IOException e) {
-            throw new RuntimeException(e);
+            logger.error("kubernetes apiClient create error", e);
         }
+        return null;
     }
 
     /**
-     * isLocalEnvironment.
-     *
-     * @return boolean
+     * kubernetes scaler.
+     * @param appsV1Api appsV1Api
+     * @param deploymentProperties deploymentProperties
+     * @return KubernetesScaler KubernetesScaler
      */
-    private boolean isLocalEnvironment() {
-        return System.getenv("ENV") == null || 
"local".equalsIgnoreCase(System.getenv("ENV"));
+    @Bean
+    @ConditionalOnMissingBean({AppsV1Api.class, DeploymentProperties.class})
+    public KubernetesScaler kubernetesScaler(final Optional<AppsV1Api> 
appsV1Api, final DeploymentProperties deploymentProperties) {
+        return new KubernetesScaler(appsV1Api, deploymentProperties);
     }
+
 }
diff --git 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/scale/config/ScaleProperties.java
 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/scale/config/ScaleProperties.java
index 7ff1116c2d..77692e7f01 100644
--- 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/scale/config/ScaleProperties.java
+++ 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/scale/config/ScaleProperties.java
@@ -24,10 +24,30 @@ import org.springframework.stereotype.Component;
 @ConfigurationProperties(prefix = "shenyu.k8s.scale")
 public class ScaleProperties {
 
+    private boolean enabled = true;
+
     private long monitorInterval;
 
     private int poolSize;
 
+    /**
+     * isEnabled.
+     *
+     * @return boolean
+     */
+    public boolean isEnabled() {
+        return enabled;
+    }
+
+    /**
+     * setEnabled.
+     *
+     * @param enabled enabled
+     */
+    public void setEnabled(final boolean enabled) {
+        this.enabled = enabled;
+    }
+
     /**
      * getMonitorInterval.
      *
diff --git 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/scale/scaler/KubernetesScaler.java
 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/scale/scaler/KubernetesScaler.java
index 36361e58ec..1d1a0d5d2e 100644
--- 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/scale/scaler/KubernetesScaler.java
+++ 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/scale/scaler/KubernetesScaler.java
@@ -28,6 +28,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
 
 import java.util.Objects;
+import java.util.Optional;
 
 @Component
 public class KubernetesScaler {
@@ -39,8 +40,8 @@ public class KubernetesScaler {
 
     private final DeploymentProperties deploymentProperties;
 
-    public KubernetesScaler(final AppsV1Api appsV1Api, final 
DeploymentProperties deploymentProperties) {
-        this.appsV1Api = appsV1Api;
+    public KubernetesScaler(final Optional<AppsV1Api> appsV1Api, final 
DeploymentProperties deploymentProperties) {
+        this.appsV1Api = appsV1Api.orElse(null);
         this.deploymentProperties = deploymentProperties;
     }
 
diff --git a/shenyu-admin/src/main/resources/application.yml 
b/shenyu-admin/src/main/resources/application.yml
index 49f986d23e..7a42448b22 100755
--- a/shenyu-admin/src/main/resources/application.yml
+++ b/shenyu-admin/src/main/resources/application.yml
@@ -208,22 +208,23 @@ shenyu:
         - system:resource:editMenu
         - system:resource:deleteButton
         - system:resource:deleteMenu
-#  k8s:
-#    scale:
-#      monitor-interval: 10000
-#      pool-size: 6
-#    prometheus:
-#      url: http://localhost:9090
-#      queries:
-#        cpu_usage: 
"sum(rate(container_cpu_usage_seconds_total{namespace='%s', pod=~'%s.*'}[5m]))"
-#        memory_usage: "sum(container_memory_usage_bytes{namespace='%s', 
pod=~'%s.*'})"
-#        request_count: "sum(rate(http_requests_total{namespace='%s', 
pod=~'%s.*'}[1m]))"
-#    deployment:
-#      name: "shenyu-bootstrap"
-#      namespace: "shenyu"
-#      apiServer: "https://127.0.0.1:6443";
-#      token: "token"
-#      caCertPath: "/etc/kubernetes/pki/ca.crt"
+  k8s:
+    scale:
+      enabled: false
+      monitor-interval: 10000
+      pool-size: 6
+    prometheus:
+      url: http://localhost:9090
+      queries:
+        cpu_usage: "sum(rate(container_cpu_usage_seconds_total{namespace='%s', 
pod=~'%s.*'}[5m]))"
+        memory_usage: "sum(container_memory_usage_bytes{namespace='%s', 
pod=~'%s.*'})"
+        request_count: "sum(rate(http_requests_total{namespace='%s', 
pod=~'%s.*'}[1m]))"
+    deployment:
+      name: "shenyu-bootstrap"
+      namespace: "shenyu"
+      apiServer: "https://127.0.0.1:6443";
+      token: "token"
+      caCertPath: "/etc/kubernetes/pki/ca.crt"
 
 springdoc:
   api-docs:

Reply via email to