Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor:enable option kubernetes #5784

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,60 @@
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")
misaya295 marked this conversation as resolved.
Show resolved Hide resolved
@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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,30 @@
@ConfigurationProperties(prefix = "shenyu.k8s.scale")
public class ScaleProperties {

private boolean enabled;
misaya295 marked this conversation as resolved.
Show resolved Hide resolved

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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.Optional;

@Component
public class KubernetesScaler {
Expand All @@ -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;
}

Expand Down
33 changes: 17 additions & 16 deletions shenyu-admin/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading