From a8580e312a3dae481d20f369997461fa6ed1a3cb Mon Sep 17 00:00:00 2001 From: po-168 Date: Mon, 30 Dec 2024 16:41:03 +0800 Subject: [PATCH] [type:refactor] Optimize code for shenyu-kubernetes-controller --- .../shenyu/k8s/parser/ContextPathParser.java | 7 ++-- .../k8s/parser/DivideIngressParser.java | 28 +++++++++---- .../shenyu/k8s/parser/DubboIngressParser.java | 41 ++++++++++++------- .../apache/shenyu/k8s/parser/GrpcParser.java | 23 +++++++---- .../shenyu/k8s/parser/IngressParser.java | 3 +- .../shenyu/k8s/parser/MotanIngressParser.java | 17 ++++---- .../apache/shenyu/k8s/parser/SofaParser.java | 21 +++------- .../shenyu/k8s/parser/SpringCloudParser.java | 15 ++++--- .../shenyu/k8s/parser/WebSocketParser.java | 36 ++++++++++------ .../k8s/reconciler/EndpointsReconciler.java | 27 +++++++----- .../k8s/reconciler/IngressReconciler.java | 21 +++++----- 11 files changed, 143 insertions(+), 96 deletions(-) diff --git a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/ContextPathParser.java b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/ContextPathParser.java index c35432e3a9ca..e6a15cfad223 100644 --- a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/ContextPathParser.java +++ b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/ContextPathParser.java @@ -100,17 +100,18 @@ private List parseIngressRule(final V1IngressRule ingressR List 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 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 ruleDataList = new ArrayList<>(); List ruleConditionList = getRuleConditionList(annotations); diff --git a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/DivideIngressParser.java b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/DivideIngressParser.java index d6c702e71409..15b03b4451f9 100644 --- a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/DivideIngressParser.java +++ b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/DivideIngressParser.java @@ -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; @@ -200,7 +201,8 @@ private List parseIngressRule(final V1IngressRule ingressR List 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; } @@ -219,7 +221,7 @@ private List 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 conditionList = new ArrayList<>(2); if (Objects.nonNull(hostCondition)) { conditionList.add(hostCondition); @@ -229,7 +231,7 @@ private List 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) @@ -251,7 +253,7 @@ private List 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) @@ -267,11 +269,19 @@ private List 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; diff --git a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/DubboIngressParser.java b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/DubboIngressParser.java index bb1d4b5f7a7d..7c76d9c9397a 100644 --- a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/DubboIngressParser.java +++ b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/DubboIngressParser.java @@ -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; @@ -128,13 +129,16 @@ public ShenyuMemoryConfig parse(final V1Ingress ingress, final CoreV1Api coreV1A if (Objects.nonNull(tlsList) && CollectionUtils.isNotEmpty(tlsList)) { List sslList = new ArrayList<>(); for (V1IngressTLS tls : tlsList) { - if (Objects.nonNull(tls.getSecretName()) && Objects.nonNull(tls.getHosts()) && CollectionUtils.isNotEmpty(tls.getHosts())) { + List 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 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)) ); } @@ -186,11 +190,19 @@ private List 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; @@ -207,11 +219,12 @@ private List parseIngressRule(final V1IngressRule ingressR List 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 conditionList = new ArrayList<>(2); if (Objects.nonNull(hostCondition)) { conditionList.add(hostCondition); @@ -222,7 +235,7 @@ private List parseIngressRule(final V1IngressRule ingressR if (upstreamList.isEmpty()) { upstreamList = dubboUpstreamList; } - SelectorData selectorData = createSelectorData(path.getPath(), conditionList, upstreamList); + SelectorData selectorData = createSelectorData(pathPath, conditionList, upstreamList); List ruleDataList = new ArrayList<>(); List metaDataList = new ArrayList<>(); for (String label : labels.keySet()) { diff --git a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/GrpcParser.java b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/GrpcParser.java index 2de074aad5b1..34093f420bde 100644 --- a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/GrpcParser.java +++ b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/GrpcParser.java @@ -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; @@ -210,11 +211,16 @@ private List 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; @@ -230,11 +236,12 @@ private List parseIngressRule(final V1IngressRule ingressR List 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 conditionList = new ArrayList<>(2); if (Objects.nonNull(hostCondition)) { conditionList.add(hostCondition); @@ -242,7 +249,7 @@ private List parseIngressRule(final V1IngressRule ingressR conditionList.add(pathCondition); List grpcUpstreamList = parseUpstream(path.getBackend(), namespace); - SelectorData selectorData = createSelectorData(path.getPath(), conditionList, grpcUpstreamList); + SelectorData selectorData = createSelectorData(pathPath, conditionList, grpcUpstreamList); List ruleDataList = new ArrayList<>(); List metaDataList = new ArrayList<>(); for (String label : labels.keySet()) { diff --git a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/IngressParser.java b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/IngressParser.java index ec86b6591ccb..09c442609898 100644 --- a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/IngressParser.java +++ b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/IngressParser.java @@ -32,6 +32,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * Parser of Ingress. @@ -103,7 +104,7 @@ public List 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 shenyuMemoryConfigList, final CoreV1Api coreV1Api) { diff --git a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/MotanIngressParser.java b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/MotanIngressParser.java index 58475c272d64..5479d7f444a5 100644 --- a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/MotanIngressParser.java +++ b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/MotanIngressParser.java @@ -115,13 +115,15 @@ public ShenyuMemoryConfig parse(final V1Ingress ingress, final CoreV1Api coreV1A if (Objects.nonNull(tlsList) && CollectionUtils.isNotEmpty(tlsList)) { List sslList = new ArrayList<>(); for (V1IngressTLS tls : tlsList) { - if (tls.getSecretName() != null && tls.getHosts() != null && CollectionUtils.isNotEmpty(tls.getHosts())) { + String secretName = tls.getSecretName(); + List 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)) ); } @@ -153,7 +155,8 @@ private List parseIngressRule(final V1IngressRule ingressR List 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; } @@ -172,7 +175,7 @@ private List 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 conditionList = new ArrayList<>(2); if (Objects.nonNull(hostCondition)) { conditionList.add(hostCondition); @@ -181,14 +184,14 @@ private List 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 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) diff --git a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/SofaParser.java b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/SofaParser.java index 10d9384704b2..2b7ba3f0e528 100644 --- a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/SofaParser.java +++ b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/SofaParser.java @@ -25,12 +25,11 @@ import io.kubernetes.client.openapi.models.V1Ingress; import io.kubernetes.client.openapi.models.V1IngressBackend; import io.kubernetes.client.openapi.models.V1IngressRule; -import io.kubernetes.client.openapi.models.V1IngressServiceBackend; import io.kubernetes.client.openapi.models.V1IngressTLS; import io.kubernetes.client.openapi.models.V1Secret; import io.kubernetes.client.openapi.models.V1Service; import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; +import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.tuple.Pair; import org.apache.shenyu.common.config.ssl.SslCrtAndKeyStream; import org.apache.shenyu.common.dto.ConditionData; @@ -127,9 +126,10 @@ public ShenyuMemoryConfig parse(final V1Ingress ingress, final CoreV1Api coreV1A if (Objects.nonNull(tls.getSecretName()) && Objects.nonNull(tls.getHosts()) && CollectionUtils.isNotEmpty(tls.getHosts())) { 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")); + Map secretData = secret.getData(); + if (MapUtils.isNotEmpty(secretData)) { + InputStream keyCertChainInputStream = new ByteArrayInputStream(secretData.get("tls.crt")); + InputStream keyInputStream = new ByteArrayInputStream(secretData.get("tls.key")); tls.getHosts().forEach(host -> sslList.add(new SslCrtAndKeyStream(host, keyCertChainInputStream, keyInputStream)) ); @@ -145,17 +145,6 @@ public ShenyuMemoryConfig parse(final V1Ingress ingress, final CoreV1Api coreV1A return res; } - 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(); - } - } - return null; - } - private List parseIngressRule(final V1IngressRule ingressRule, final String namespace, final Map annotations, diff --git a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/SpringCloudParser.java b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/SpringCloudParser.java index a1f05c12b1f2..9f5dedbf8074 100644 --- a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/SpringCloudParser.java +++ b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/SpringCloudParser.java @@ -32,6 +32,7 @@ import io.kubernetes.client.openapi.models.V1Secret; import io.kubernetes.client.openapi.models.V1Service; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; import org.apache.shenyu.common.config.ssl.SslCrtAndKeyStream; @@ -127,9 +128,10 @@ public ShenyuMemoryConfig parse(final V1Ingress ingress, final CoreV1Api coreV1A if (Objects.nonNull(tls.getSecretName()) && Objects.nonNull(tls.getHosts()) && CollectionUtils.isNotEmpty(tls.getHosts())) { 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")); + Map secretData = secret.getData(); + if (MapUtils.isNotEmpty(secretData)) { + InputStream keyCertChainInputStream = new ByteArrayInputStream(secretData.get("tls.crt")); + InputStream keyInputStream = new ByteArrayInputStream(secretData.get("tls.key")); tls.getHosts().forEach(host -> sslList.add(new SslCrtAndKeyStream(host, keyCertChainInputStream, keyInputStream)) ); @@ -152,11 +154,12 @@ private List parseIngressRule(final V1IngressRule ingressR List 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 conditionList = new ArrayList<>(2); if (Objects.nonNull(hostCondition)) { conditionList.add(hostCondition); @@ -164,7 +167,7 @@ private List parseIngressRule(final V1IngressRule ingressR conditionList.add(pathCondition); SpringCloudSelectorHandle springCloudSelectorHandle = createSpringCloudSelectorHandle(annotations, path, namespace); - SelectorData selectorData = createSelectorData(path.getPath(), conditionList, springCloudSelectorHandle); + SelectorData selectorData = createSelectorData(pathPath, conditionList, springCloudSelectorHandle); SpringCloudRuleHandle ruleHandle = createSpringCloudRuleHandle(annotations); List ruleDataList = new ArrayList<>(); List metaDataList = new ArrayList<>(); diff --git a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/WebSocketParser.java b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/WebSocketParser.java index 0e3f38ba2a51..178b8f0bf076 100644 --- a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/WebSocketParser.java +++ b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/WebSocketParser.java @@ -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; @@ -123,13 +124,15 @@ public ShenyuMemoryConfig parse(final V1Ingress ingress, final CoreV1Api coreV1A if (Objects.nonNull(tlsList) && CollectionUtils.isNotEmpty(tlsList)) { List sslList = new ArrayList<>(); for (V1IngressTLS tls : tlsList) { - if (tls.getSecretName() != null && tls.getHosts() != null && CollectionUtils.isNotEmpty(tls.getHosts())) { + String secretName = tls.getSecretName(); + List 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)) ); } @@ -200,7 +203,8 @@ private List parseIngressRule(final V1IngressRule ingressR List 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; } @@ -219,7 +223,7 @@ private List 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 conditionList = new ArrayList<>(2); if (Objects.nonNull(hostCondition)) { conditionList.add(hostCondition); @@ -229,7 +233,7 @@ private List parseIngressRule(final V1IngressRule ingressR SelectorData selectorData = SelectorData.builder() .pluginId(String.valueOf(PluginEnum.WEB_SOCKET.getCode())) .pluginName(PluginEnum.WEB_SOCKET.getName()) - .name(path.getPath()) + .name(pathPath) .matchMode(MatchModeEnum.AND.getCode()) .type(SelectorTypeEnum.CUSTOM_FLOW.getCode()) .enabled(true) @@ -249,7 +253,7 @@ private List parseIngressRule(final V1IngressRule ingressR webSocketRuleHandle.setTimeout(Long.parseLong(annotations.getOrDefault(IngressConstants.TIMEOUT_ANNOTATION_KEY, "3000"))); } RuleData ruleData = RuleData.builder() - .name(path.getPath()) + .name(pathPath) .pluginName(PluginEnum.WEB_SOCKET.getName()) .matchMode(MatchModeEnum.AND.getCode()) .conditionDataList(conditionList) @@ -265,11 +269,19 @@ private List 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; diff --git a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/reconciler/EndpointsReconciler.java b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/reconciler/EndpointsReconciler.java index bcffb07925be..b7303363141b 100644 --- a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/reconciler/EndpointsReconciler.java +++ b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/reconciler/EndpointsReconciler.java @@ -24,10 +24,11 @@ import io.kubernetes.client.informer.cache.Lister; import io.kubernetes.client.openapi.ApiClient; import io.kubernetes.client.openapi.models.CoreV1EndpointPort; -import io.kubernetes.client.openapi.models.V1EndpointSubset; import io.kubernetes.client.openapi.models.V1EndpointAddress; +import io.kubernetes.client.openapi.models.V1EndpointSubset; import io.kubernetes.client.openapi.models.V1Endpoints; import io.kubernetes.client.openapi.models.V1Ingress; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.tuple.Pair; import org.apache.shenyu.common.dto.SelectorData; import org.apache.shenyu.common.dto.convert.selector.DivideUpstream; @@ -43,6 +44,7 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Objects; import java.util.Set; /** @@ -87,12 +89,12 @@ public EndpointsReconciler(final SharedIndexInformer ingressInformer, @Override public Result reconcile(final Request request) { List> ingressList = ServiceIngressCache.getInstance().getIngressName(request.getNamespace(), request.getName()); - if (ingressList == null || ingressList.isEmpty()) { + if (CollectionUtils.isEmpty(ingressList)) { return new Result(false); } V1Endpoints v1Endpoints = endpointsLister.namespace(request.getNamespace()).get(request.getName()); - if (v1Endpoints == null) { + if (Objects.isNull(v1Endpoints)) { // The deletion event is not processed, because deleting all upstreams in the Selector has // the same effect as not deleting them, and they cannot be accessed LOG.info("Cannot find endpoints {}", request); @@ -135,10 +137,12 @@ public Result reconcile(final Request request) { private List getUpstreamFromEndpoints(final V1Endpoints v1Endpoints) { List res = new ArrayList<>(); - if (v1Endpoints.getSubsets() != null) { - for (V1EndpointSubset subset : v1Endpoints.getSubsets()) { + List subsets = v1Endpoints.getSubsets(); + if (CollectionUtils.isNotEmpty(subsets)) { + for (V1EndpointSubset subset : subsets) { List ports = subset.getPorts(); - if (ports == null || ports.isEmpty() || subset.getAddresses() == null || subset.getAddresses().isEmpty()) { + List addresses = subset.getAddresses(); + if (CollectionUtils.isEmpty(ports) || CollectionUtils.isEmpty(addresses)) { continue; } CoreV1EndpointPort endpointPort = ports.stream() @@ -148,12 +152,15 @@ private List getUpstreamFromEndpoints(final V1Endpoints v1Endpoi String port = null; if (endpointPort.getPort() > 0) { port = String.valueOf(endpointPort.getPort()); - } else if (endpointPort.getName() != null) { - port = endpointPort.getName(); + } else { + String endpointPortName = endpointPort.getName(); + if (Objects.nonNull(endpointPortName)) { + port = endpointPortName; + } } - for (V1EndpointAddress address : subset.getAddresses()) { + for (V1EndpointAddress address : addresses) { String ip = address.getIp(); - if (ip != null) { + if (Objects.nonNull(ip)) { DivideUpstream upstream = new DivideUpstream(); upstream.setUpstreamUrl(ip + ":" + port); upstream.setWeight(100); diff --git a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/reconciler/IngressReconciler.java b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/reconciler/IngressReconciler.java index 300f675f2cde..e80bdf63a57c 100644 --- a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/reconciler/IngressReconciler.java +++ b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/reconciler/IngressReconciler.java @@ -447,20 +447,20 @@ private void addNewIngressConfigToShenyu(final V1Ingress v1Ingress, final CoreV1 private void processShenyuMemoryConfig(final ShenyuMemoryConfig shenyuMemoryConfig, final V1Ingress v1Ingress, final String pluginName) throws IOException { List routeConfigList = shenyuMemoryConfig.getRouteConfigList(); - if (routeConfigList == null) { + if (CollectionUtils.isEmpty(routeConfigList)) { return; } for (IngressConfiguration routeConfig : routeConfigList) { SelectorData selectorData = routeConfig.getSelectorData(); - if (selectorData != null) { + if (Objects.nonNull(selectorData)) { processSelectorData(routeConfig, selectorData, v1Ingress, pluginName); } List metaDataList = routeConfig.getMetaDataList(); - if (metaDataList != null) { + if (Objects.nonNull(metaDataList)) { processMetaDataList(metaDataList); } } @@ -479,7 +479,7 @@ private void processSelectorData(final IngressConfiguration routeConfig, final S List ruleDataList = routeConfig.getRuleDataList(); - if (ruleDataList != null) { + if (CollectionUtils.isNotEmpty(ruleDataList)) { processRuleDataList(ruleDataList, selectorData); } @@ -493,7 +493,7 @@ private void processSelectorData(final IngressConfiguration routeConfig, final S private void processRuleDataList(final List ruleDataList, final SelectorData selectorData) { for (RuleData ruleData : ruleDataList) { - if (ruleData != null) { + if (Objects.nonNull(ruleData)) { ruleData.setId(IngressSelectorCache.getInstance().generateRuleId()); ruleData.setSelectorId(selectorData.getId()); ruleData.setSort(100); @@ -504,7 +504,7 @@ private void processRuleDataList(final List ruleDataList, final Select private void processMetaDataList(final List metaDataList) { for (MetaData metaData : metaDataList) { - if (metaData != null) { + if (Objects.nonNull(metaData)) { metaData.setId(IngressSelectorCache.getInstance().generateMetaDataId()); shenyuCacheRepository.saveOrUpdateMetaData(metaData); } @@ -512,16 +512,17 @@ private void processMetaDataList(final List metaDataList) { } private void processGlobalDefaultBackend(final ShenyuMemoryConfig shenyuMemoryConfig, final V1Ingress v1Ingress, final String pluginName) { - if (shenyuMemoryConfig.getGlobalDefaultBackend() != null) { + Pair, IngressConfiguration> configurationPair = shenyuMemoryConfig.getGlobalDefaultBackend(); + if (Objects.nonNull(configurationPair)) { synchronized (IngressReconciler.class) { if (Objects.isNull(globalDefaultBackend)) { // Add a default backend - IngressConfiguration ingressConfiguration = shenyuMemoryConfig.getGlobalDefaultBackend().getRight(); + IngressConfiguration ingressConfiguration = configurationPair.getRight(); SelectorData selectorData = ingressConfiguration.getSelectorData(); shenyuCacheRepository.saveOrUpdateSelectorData(selectorData); ingressConfiguration.getRuleDataList().forEach(shenyuCacheRepository::saveOrUpdateRuleData); ingressConfiguration.getMetaDataList().forEach(shenyuCacheRepository::saveOrUpdateMetaData); - globalDefaultBackend = shenyuMemoryConfig.getGlobalDefaultBackend(); + globalDefaultBackend = configurationPair; IngressSelectorCache.getInstance().put( Objects.requireNonNull(v1Ingress.getMetadata()).getNamespace(), v1Ingress.getMetadata().getName(), @@ -536,7 +537,7 @@ private void processGlobalDefaultBackend(final ShenyuMemoryConfig shenyuMemoryCo private void processTlsConfigurations(final ShenyuMemoryConfig shenyuMemoryConfig, final V1Ingress v1Ingress) throws IOException { List tlsConfigList = shenyuMemoryConfig.getTlsConfigList(); - if (tlsConfigList != null) { + if (CollectionUtils.isNotEmpty(tlsConfigList)) { final String namespace = Objects.requireNonNull(v1Ingress.getMetadata()).getNamespace(); final String ingressName = v1Ingress.getMetadata().getName(); Set oldDomainSet = Optional.ofNullable(IngressSecretCache.getInstance().removeDomainByIngress(namespace, ingressName)).orElse(new HashSet<>());