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

[type:refactor] Optimize code for shenyu-kubernetes-controller #5877

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a8580e3
[type:refactor] Optimize code for shenyu-kubernetes-controller
po-168 Dec 30, 2024
3b38a26
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Dec 31, 2024
5ae0de9
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Dec 31, 2024
b4be3da
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 2, 2025
34e4ee3
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 2, 2025
124da8d
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 3, 2025
ab2558d
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 6, 2025
eb6f0d0
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 9, 2025
67b95b3
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 13, 2025
25c493e
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 14, 2025
64a74ec
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 15, 2025
f48742b
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 16, 2025
8e7f6ca
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 17, 2025
57f2c61
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 18, 2025
9b8ab61
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 18, 2025
98573ee
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 20, 2025
627e8aa
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 21, 2025
738a26c
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 21, 2025
5e1a7f6
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 22, 2025
7b0fc82
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 22, 2025
91a6646
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 23, 2025
b6ef141
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 24, 2025
0355291
Merge branch 'master' into dev-shenyu-kubernetes-controller
Aias00 Jan 24, 2025
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 @@ -100,17 +100,18 @@ private List<IngressConfiguration> parseIngressRule(final V1IngressRule ingressR
List<V1HTTPIngressPath> paths = ingressRule.getHttp().getPaths();
if (Objects.nonNull(paths)) {
for (V1HTTPIngressPath path : paths) {
if (path.getPath() == null) {
String pathPath = path.getPath();
if (Objects.isNull(pathPath)) {
continue;
}
OperatorEnum operator = getOperator(path.getPathType());
ConditionData pathCondition = createPathCondition(path.getPath(), operator);
ConditionData pathCondition = createPathCondition(pathPath, operator);
List<ConditionData> conditionList = new ArrayList<>(2);
if (Objects.nonNull(hostCondition)) {
conditionList.add(hostCondition);
}
conditionList.add(pathCondition);
SelectorData selectorData = createSelectorData(path.getPath(), conditionList);
SelectorData selectorData = createSelectorData(pathPath, conditionList);
ContextMappingRuleHandle contextMappingRuleHandle = createContextMappingRuleHandle(annotations);
List<RuleData> ruleDataList = new ArrayList<>();
List<ConditionData> ruleConditionList = getRuleConditionList(annotations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.kubernetes.client.openapi.models.V1IngressTLS;
import io.kubernetes.client.openapi.models.V1Secret;
import io.kubernetes.client.openapi.models.V1Service;
import io.kubernetes.client.openapi.models.V1ServiceBackendPort;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
Expand Down Expand Up @@ -200,7 +201,8 @@ private List<IngressConfiguration> parseIngressRule(final V1IngressRule ingressR
List<V1HTTPIngressPath> paths = ingressRule.getHttp().getPaths();
if (Objects.nonNull(paths)) {
for (V1HTTPIngressPath path : paths) {
if (path.getPath() == null) {
String pathPath = path.getPath();
if (Objects.isNull(pathPath)) {
continue;
}

Expand All @@ -219,7 +221,7 @@ private List<IngressConfiguration> parseIngressRule(final V1IngressRule ingressR
ConditionData pathCondition = new ConditionData();
pathCondition.setOperator(operator.getAlias());
pathCondition.setParamType(ParamTypeEnum.URI.getName());
pathCondition.setParamValue(path.getPath());
pathCondition.setParamValue(pathPath);
List<ConditionData> conditionList = new ArrayList<>(2);
if (Objects.nonNull(hostCondition)) {
conditionList.add(hostCondition);
Expand All @@ -229,7 +231,7 @@ private List<IngressConfiguration> parseIngressRule(final V1IngressRule ingressR
SelectorData selectorData = SelectorData.builder()
.pluginId(String.valueOf(PluginEnum.DIVIDE.getCode()))
.pluginName(PluginEnum.DIVIDE.getName())
.name(path.getPath())
.name(pathPath)
.matchMode(MatchModeEnum.AND.getCode())
.type(SelectorTypeEnum.CUSTOM_FLOW.getCode())
.enabled(true)
Expand All @@ -251,7 +253,7 @@ private List<IngressConfiguration> parseIngressRule(final V1IngressRule ingressR
divideRuleHandle.setRequestMaxSize(Long.parseLong(annotations.getOrDefault(IngressConstants.REQUEST_MAX_SIZE_ANNOTATION_KEY, "102400")));
}
RuleData ruleData = RuleData.builder()
.name(path.getPath())
.name(pathPath)
.pluginName(PluginEnum.DIVIDE.getName())
.matchMode(MatchModeEnum.AND.getCode())
.conditionDataList(conditionList)
Expand All @@ -267,11 +269,19 @@ private List<IngressConfiguration> parseIngressRule(final V1IngressRule ingressR
}

private String parsePort(final V1IngressServiceBackend service) {
if (Objects.nonNull(service.getPort())) {
if (service.getPort().getNumber() != null && service.getPort().getNumber() > 0) {
return String.valueOf(service.getPort().getNumber());
} else if (service.getPort().getName() != null && StringUtils.isNoneBlank(service.getPort().getName().trim())) {
return service.getPort().getName().trim();
V1ServiceBackendPort servicePort = service.getPort();
if (Objects.nonNull(servicePort)) {
Integer portNumber = servicePort.getNumber();
if (Objects.nonNull(portNumber) && portNumber > 0) {
return String.valueOf(portNumber);
} else {
String servicePortName = servicePort.getName();
if (Objects.nonNull(servicePortName)) {
String trim = servicePortName.trim();
if (StringUtils.isNoneBlank(trim)) {
return trim;
}
}
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.kubernetes.client.openapi.models.V1IngressTLS;
import io.kubernetes.client.openapi.models.V1Secret;
import io.kubernetes.client.openapi.models.V1Service;
import io.kubernetes.client.openapi.models.V1ServiceBackendPort;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
Expand Down Expand Up @@ -128,13 +129,16 @@ public ShenyuMemoryConfig parse(final V1Ingress ingress, final CoreV1Api coreV1A
if (Objects.nonNull(tlsList) && CollectionUtils.isNotEmpty(tlsList)) {
List<SslCrtAndKeyStream> sslList = new ArrayList<>();
for (V1IngressTLS tls : tlsList) {
if (Objects.nonNull(tls.getSecretName()) && Objects.nonNull(tls.getHosts()) && CollectionUtils.isNotEmpty(tls.getHosts())) {
List<String> hosts = tls.getHosts();
String secretName = tls.getSecretName();
if (Objects.nonNull(secretName) && CollectionUtils.isNotEmpty(hosts)) {
try {
V1Secret secret = coreV1Api.readNamespacedSecret(tls.getSecretName(), namespace, "ture");
if (secret.getData() != null) {
InputStream keyCertChainInputStream = new ByteArrayInputStream(secret.getData().get("tls.crt"));
InputStream keyInputStream = new ByteArrayInputStream(secret.getData().get("tls.key"));
tls.getHosts().forEach(host ->
V1Secret secret = coreV1Api.readNamespacedSecret(secretName, namespace, "ture");
Map<String, byte[]> secretData = secret.getData();
if (Objects.nonNull(secretData)) {
InputStream keyCertChainInputStream = new ByteArrayInputStream(secretData.get("tls.crt"));
InputStream keyInputStream = new ByteArrayInputStream(secretData.get("tls.key"));
hosts.forEach(host ->
sslList.add(new SslCrtAndKeyStream(host, keyCertChainInputStream, keyInputStream))
);
}
Expand Down Expand Up @@ -186,11 +190,19 @@ private List<DubboUpstream> getDefaultDubboRouteConfig(final V1IngressBackend de
}

private String parsePort(final V1IngressServiceBackend service) {
if (Objects.nonNull(service.getPort())) {
if (service.getPort().getNumber() != null && service.getPort().getNumber() > 0) {
return String.valueOf(service.getPort().getNumber());
} else if (service.getPort().getName() != null && StringUtils.isNoneBlank(service.getPort().getName().trim())) {
return service.getPort().getName().trim();
V1ServiceBackendPort servicePort = service.getPort();
if (Objects.nonNull(servicePort)) {
Integer portNumber = servicePort.getNumber();
if (Objects.nonNull(portNumber) && portNumber > 0) {
return String.valueOf(portNumber);
} else {
String servicePortName = servicePort.getName();
if (Objects.nonNull(servicePortName)) {
String trim = servicePortName.trim();
if (StringUtils.isNoneBlank(trim)) {
return trim;
}
}
}
}
return null;
Expand All @@ -207,11 +219,12 @@ private List<IngressConfiguration> parseIngressRule(final V1IngressRule ingressR
List<V1HTTPIngressPath> paths = ingressRule.getHttp().getPaths();
if (Objects.nonNull(paths)) {
for (V1HTTPIngressPath path : paths) {
if (path.getPath() == null) {
String pathPath = path.getPath();
if (Objects.isNull(pathPath)) {
continue;
}
OperatorEnum operator = getOperator(path.getPathType());
ConditionData pathCondition = createPathCondition(path.getPath(), operator);
ConditionData pathCondition = createPathCondition(pathPath, operator);
List<ConditionData> conditionList = new ArrayList<>(2);
if (Objects.nonNull(hostCondition)) {
conditionList.add(hostCondition);
Expand All @@ -222,7 +235,7 @@ private List<IngressConfiguration> parseIngressRule(final V1IngressRule ingressR
if (upstreamList.isEmpty()) {
upstreamList = dubboUpstreamList;
}
SelectorData selectorData = createSelectorData(path.getPath(), conditionList, upstreamList);
SelectorData selectorData = createSelectorData(pathPath, conditionList, upstreamList);
List<RuleData> ruleDataList = new ArrayList<>();
List<MetaData> metaDataList = new ArrayList<>();
for (String label : labels.keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.kubernetes.client.openapi.models.V1IngressTLS;
import io.kubernetes.client.openapi.models.V1Secret;
import io.kubernetes.client.openapi.models.V1Service;
import io.kubernetes.client.openapi.models.V1ServiceBackendPort;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
Expand Down Expand Up @@ -210,11 +211,16 @@ private List<GrpcUpstream> parseUpstream(final V1IngressBackend backend, final S
}

private String parsePort(final V1IngressServiceBackend service) {
if (Objects.nonNull(service.getPort())) {
if (service.getPort().getNumber() != null && service.getPort().getNumber() > 0) {
return String.valueOf(service.getPort().getNumber());
} else if (service.getPort().getName() != null && StringUtils.isNoneBlank(service.getPort().getName().trim())) {
return service.getPort().getName().trim();
V1ServiceBackendPort servicePort = service.getPort();
if (Objects.nonNull(servicePort)) {
Integer portNumber = servicePort.getNumber();
if (Objects.nonNull(portNumber) && portNumber > 0) {
return String.valueOf(portNumber);
} else {
String servicePortName = servicePort.getName();
if (Objects.nonNull(servicePortName) && StringUtils.isNoneBlank(servicePortName.trim())) {
return servicePortName.trim();
}
}
}
return null;
Expand All @@ -230,19 +236,20 @@ private List<IngressConfiguration> parseIngressRule(final V1IngressRule ingressR
List<V1HTTPIngressPath> paths = ingressRule.getHttp().getPaths();
if (Objects.nonNull(paths)) {
for (V1HTTPIngressPath path : paths) {
if (path.getPath() == null) {
String pathPath = path.getPath();
if (Objects.isNull(pathPath)) {
continue;
}
OperatorEnum operator = getOperator(path.getPathType());
ConditionData pathCondition = createPathCondition(path.getPath(), operator);
ConditionData pathCondition = createPathCondition(pathPath, operator);
List<ConditionData> conditionList = new ArrayList<>(2);
if (Objects.nonNull(hostCondition)) {
conditionList.add(hostCondition);
}
conditionList.add(pathCondition);
List<GrpcUpstream> grpcUpstreamList = parseUpstream(path.getBackend(), namespace);

SelectorData selectorData = createSelectorData(path.getPath(), conditionList, grpcUpstreamList);
SelectorData selectorData = createSelectorData(pathPath, conditionList, grpcUpstreamList);
List<RuleData> ruleDataList = new ArrayList<>();
List<MetaData> metaDataList = new ArrayList<>();
for (String label : labels.keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Parser of Ingress.
Expand Down Expand Up @@ -103,7 +104,7 @@ public List<ShenyuMemoryConfig> parse(final V1Ingress ingress, final CoreV1Api c

private boolean getBooleanAnnotation(final V1Ingress ingress, final String annotationKey) {
String annotationValue = ingress.getMetadata().getAnnotations().get(annotationKey);
return annotationValue != null && Boolean.parseBoolean(annotationValue);
return Objects.nonNull(annotationValue) && Boolean.parseBoolean(annotationValue);
}

private void contextPathParse(final V1Ingress ingress, final List<ShenyuMemoryConfig> shenyuMemoryConfigList, final CoreV1Api coreV1Api) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,15 @@ public ShenyuMemoryConfig parse(final V1Ingress ingress, final CoreV1Api coreV1A
if (Objects.nonNull(tlsList) && CollectionUtils.isNotEmpty(tlsList)) {
List<SslCrtAndKeyStream> sslList = new ArrayList<>();
for (V1IngressTLS tls : tlsList) {
if (tls.getSecretName() != null && tls.getHosts() != null && CollectionUtils.isNotEmpty(tls.getHosts())) {
String secretName = tls.getSecretName();
List<String> hosts = tls.getHosts();
if (Objects.nonNull(secretName) && CollectionUtils.isNotEmpty(hosts)) {
try {
V1Secret secret = coreV1Api.readNamespacedSecret(tls.getSecretName(), namespace, "ture");
V1Secret secret = coreV1Api.readNamespacedSecret(secretName, namespace, "ture");
if (Objects.nonNull(secret.getData())) {
InputStream keyCertChainInputStream = new ByteArrayInputStream(secret.getData().get("tls.crt"));
InputStream keyInputStream = new ByteArrayInputStream(secret.getData().get("tls.key"));
tls.getHosts().forEach(host ->
hosts.forEach(host ->
sslList.add(new SslCrtAndKeyStream(host, keyCertChainInputStream, keyInputStream))
);
}
Expand Down Expand Up @@ -153,7 +155,8 @@ private List<IngressConfiguration> parseIngressRule(final V1IngressRule ingressR
List<V1HTTPIngressPath> paths = ingressRule.getHttp().getPaths();
if (Objects.nonNull(paths)) {
for (V1HTTPIngressPath path : paths) {
if (path.getPath() == null) {
String pathPath = path.getPath();
if (Objects.isNull(pathPath)) {
continue;
}

Expand All @@ -172,7 +175,7 @@ private List<IngressConfiguration> parseIngressRule(final V1IngressRule ingressR
ConditionData pathCondition = new ConditionData();
pathCondition.setOperator(operator.getAlias());
pathCondition.setParamType(ParamTypeEnum.URI.getName());
pathCondition.setParamValue(path.getPath());
pathCondition.setParamValue(pathPath);
List<ConditionData> conditionList = new ArrayList<>(2);
if (Objects.nonNull(hostCondition)) {
conditionList.add(hostCondition);
Expand All @@ -181,14 +184,14 @@ private List<IngressConfiguration> parseIngressRule(final V1IngressRule ingressR
ConditionData ruleConditionData = new ConditionData();
ruleConditionData.setParamType(ParamTypeEnum.URI.getName());
ruleConditionData.setOperator(OperatorEnum.EQ.getAlias());
ruleConditionData.setParamName(annotations.getOrDefault(IngressConstants.PLUGIN_MOTAN_PATH, path.getPath()));
ruleConditionData.setParamName(annotations.getOrDefault(IngressConstants.PLUGIN_MOTAN_PATH, pathPath));
List<ConditionData> ruleConditionDataList = new ArrayList<>();
ruleConditionDataList.add(ruleConditionData);

SelectorData selectorData = SelectorData.builder()
.pluginId(String.valueOf(PluginEnum.MOTAN.getCode()))
.pluginName(PluginEnum.MOTAN.getName())
.name(path.getPath())
.name(pathPath)
.matchMode(MatchModeEnum.AND.getCode())
.type(SelectorTypeEnum.CUSTOM_FLOW.getCode())
.enabled(true)
Expand Down
Loading
Loading