diff --git a/backend/console/pom.xml b/backend/console/pom.xml index bde28dd9..46246efe 100644 --- a/backend/console/pom.xml +++ b/backend/console/pom.xml @@ -4,7 +4,7 @@ 4.0.0 - com.alibaba.higress + io.higress.api higress-admin-parent 0.0.1-SNAPSHOT @@ -57,6 +57,7 @@ org.projectlombok lombok + provided diff --git a/backend/console/src/main/java/com/alibaba/higress/console/config/SdkConfig.java b/backend/console/src/main/java/com/alibaba/higress/console/config/SdkConfig.java new file mode 100644 index 00000000..108b1013 --- /dev/null +++ b/backend/console/src/main/java/com/alibaba/higress/console/config/SdkConfig.java @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2022-2024 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.higress.console.config; + +import java.io.IOException; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.alibaba.higress.console.constant.SystemConfigKey; +import com.alibaba.higress.sdk.config.HigressServiceConfig; +import com.alibaba.higress.sdk.constant.HigressConstants; +import com.alibaba.higress.sdk.service.DomainService; +import com.alibaba.higress.sdk.service.HigressServiceProvider; +import com.alibaba.higress.sdk.service.RouteService; +import com.alibaba.higress.sdk.service.ServiceService; +import com.alibaba.higress.sdk.service.ServiceSourceService; +import com.alibaba.higress.sdk.service.TlsCertificateService; +import com.alibaba.higress.sdk.service.WasmPluginInstanceService; +import com.alibaba.higress.sdk.service.WasmPluginService; +import com.alibaba.higress.sdk.service.kubernetes.KubernetesClientService; +import com.alibaba.higress.sdk.service.kubernetes.KubernetesModelConverter; + +@Configuration +public class SdkConfig { + + @Value("${" + SystemConfigKey.KUBE_CONFIG_KEY + ":}") + private String kubeConfig; + + @Value("${" + SystemConfigKey.CONTROLLER_SERVICE_NAME_KEY + ":" + HigressConstants.CONTROLLER_SERVICE_NAME_DEFAULT + + "}") + private String controllerServiceName = HigressConstants.CONTROLLER_SERVICE_NAME_DEFAULT; + + @Value("${" + SystemConfigKey.NS_KEY + ":" + HigressConstants.NS_DEFAULT + "}") + private String controllerNamespace = HigressConstants.NS_DEFAULT; + + @Value("${" + SystemConfigKey.CONTROLLER_INGRESS_CLASS_NAME_KEY + ":" + + HigressConstants.CONTROLLER_INGRESS_CLASS_NAME_DEFAULT + "}") + private String controllerIngressClassName = HigressConstants.CONTROLLER_INGRESS_CLASS_NAME_DEFAULT; + + @Value("${" + SystemConfigKey.CONTROLLER_SERVICE_HOST_KEY + ":" + HigressConstants.CONTROLLER_SERVICE_HOST_DEFAULT + + "}") + private String controllerServiceHost = HigressConstants.CONTROLLER_SERVICE_HOST_DEFAULT; + + @Value("${" + SystemConfigKey.CONTROLLER_SERVICE_PORT_KEY + ":" + HigressConstants.CONTROLLER_SERVICE_PORT_DEFAULT + + "}") + private int controllerServicePort = HigressConstants.CONTROLLER_SERVICE_PORT_DEFAULT; + + @Value("${" + SystemConfigKey.CONTROLLER_JWT_POLICY_KEY + ":" + HigressConstants.CONTROLLER_JWT_POLICY_DEFAULT + + "}") + private String controllerJwtPolicy = HigressConstants.CONTROLLER_JWT_POLICY_DEFAULT; + + @Value("${" + SystemConfigKey.CONTROLLER_ACCESS_TOKEN_KEY + ":}") + private String controllerAccessToken; + + private HigressServiceProvider serviceProvider; + + @PostConstruct + public void initialize() throws IOException { + HigressServiceConfig config = HigressServiceConfig.builder().withIngressClassName(controllerIngressClassName) + .withControllerNamespace(controllerNamespace).withControllerServiceName(controllerServiceName) + .withControllerServiceHost(controllerServiceHost).withControllerServicePort(controllerServicePort) + .withControllerJwtPolicy(controllerJwtPolicy).withControllerAccessToken(controllerAccessToken).build(); + serviceProvider = HigressServiceProvider.create(config); + } + + @Bean + public KubernetesClientService kubernetesClientService() { + return serviceProvider.kubernetesClientService(); + } + + @Bean + public KubernetesModelConverter kubernetesModelConverter() { + return serviceProvider.kubernetesModelConverter(); + } + + @Bean + public DomainService domainService() { + return serviceProvider.domainService(); + } + + @Bean + public RouteService routeService() { + return serviceProvider.routeService(); + } + + @Bean + public ServiceService serviceService() { + return serviceProvider.serviceService(); + } + + @Bean + public ServiceSourceService serviceSourceService() { + return serviceProvider.serviceSourceService(); + } + + @Bean + public TlsCertificateService tlsCertificateService() { + return serviceProvider.tlsCertificateService(); + } + + @Bean + public WasmPluginService wasmPluginService() { + return serviceProvider.wasmPluginService(); + } + + @Bean + public WasmPluginInstanceService wasmPluginInstanceService() { + return serviceProvider.wasmPluginInstanceService(); + } +} diff --git a/backend/console/src/main/java/com/alibaba/higress/console/constant/SystemConfigKey.java b/backend/console/src/main/java/com/alibaba/higress/console/constant/SystemConfigKey.java index 30864f8c..9f5a370a 100644 --- a/backend/console/src/main/java/com/alibaba/higress/console/constant/SystemConfigKey.java +++ b/backend/console/src/main/java/com/alibaba/higress/console/constant/SystemConfigKey.java @@ -22,6 +22,22 @@ public class SystemConfigKey { public static final boolean DEV_BUILD_DEFAULT = true; + public static final String NS_KEY = CONFIG_KEY_PREFIX + "ns"; + + public static final String KUBE_CONFIG_KEY = CONFIG_KEY_PREFIX + "kube-config"; + + public static final String CONTROLLER_ACCESS_TOKEN_KEY = CONFIG_KEY_PREFIX + "controller.access-token"; + + public static final String CONTROLLER_JWT_POLICY_KEY = CONFIG_KEY_PREFIX + "controller.jwt-policy"; + + public static final String CONTROLLER_SERVICE_PORT_KEY = CONFIG_KEY_PREFIX + "controller.service.port"; + + public static final String CONTROLLER_SERVICE_HOST_KEY = CONFIG_KEY_PREFIX + "controller.service.host"; + + public static final String CONTROLLER_INGRESS_CLASS_NAME_KEY = CONFIG_KEY_PREFIX + "controller.ingress-class-name"; + + public static final String CONTROLLER_SERVICE_NAME_KEY = CONFIG_KEY_PREFIX + "controller.service.name"; + public static final String CONFIG_MAP_NAME_KEY = CONFIG_KEY_PREFIX + "config-map.name"; public static final String CONFIG_MAP_NAME_KEY_DEFAULT = "higress-console"; diff --git a/backend/console/src/main/java/com/alibaba/higress/console/service/SystemServiceImpl.java b/backend/console/src/main/java/com/alibaba/higress/console/service/SystemServiceImpl.java index 527c8e68..7c84baf2 100644 --- a/backend/console/src/main/java/com/alibaba/higress/console/service/SystemServiceImpl.java +++ b/backend/console/src/main/java/com/alibaba/higress/console/service/SystemServiceImpl.java @@ -80,7 +80,7 @@ public void initialize() { } List capabilities = new ArrayList<>(); - if (kubernetesClientService.checkIngressV1Supported()) { + if (kubernetesClientService.isIngressV1Supported()) { capabilities.add(CapabilityKey.CONFIG_INGRESS_V1); } this.capabilities = capabilities; diff --git a/backend/console/src/test/java/com/alibaba/higress/console/HigressConsoleApplicationTests.java b/backend/console/src/test/java/com/alibaba/higress/console/HigressConsoleApplicationTests.java index 14c44731..46b767c5 100644 --- a/backend/console/src/test/java/com/alibaba/higress/console/HigressConsoleApplicationTests.java +++ b/backend/console/src/test/java/com/alibaba/higress/console/HigressConsoleApplicationTests.java @@ -12,14 +12,9 @@ */ package com.alibaba.higress.console; -import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class HigressConsoleApplicationTests { - @Test - void contextLoads() { - } - } diff --git a/backend/pom.xml b/backend/pom.xml index 4d55ec70..adb61307 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -10,11 +10,12 @@ - com.alibaba.higress + io.higress.api higress-admin-parent 0.0.1-SNAPSHOT higress-admin-parent Admin Project for Higress + http://higress.io pom @@ -23,6 +24,31 @@ console + + + The Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + git@github.com:alibaba/higress.git + scm:git@github.com:alibaba/higress.git + scm:git@github.com:alibaba/higress.git + + + + + johnlanni + zty98751@alibaba-inc.com + + + CH3CHO + ch3cho@qq.com + + + 17 @@ -42,12 +68,16 @@ 6.0.0 4.1 3.2.1 + 3.1.0 + 0.3.0 ${project.build.sourceEncoding} ${project.build.sourceEncoding} 1.2.1 true + + ${project.basedir}/target/delombok-sources @@ -123,9 +153,74 @@ git-commit-id-maven-plugin ${git-commit-id-plugin.version} + + org.apache.maven.plugins + maven-gpg-plugin + ${maven-gpg-plugin.version} + + + org.sonatype.central + central-publishing-maven-plugin + ${central-publishing-maven-plugin.version} + true + + + maven-compiler-plugin + + ${maven.compiler.source} + ${maven.compiler.target} + ${maven.compiler.source} + true + true + + + + org.projectlombok + lombok-maven-plugin + ${lombok.version}.0 + + ${project.basedir}/src/main/java + ${delombok.output} + false + + + + generate-sources + + delombok + + + + + + maven-javadoc-plugin + + UTF-8 + ${delombok.output} + + + + attach-javadocs + + jar + + + + + + maven-source-plugin + + + attach-sources + + jar + + + + org.apache.maven.plugins maven-pmd-plugin @@ -168,6 +263,31 @@ + + org.apache.maven.plugins + maven-gpg-plugin + + + sign-artifacts + verify + + sign + + + + + + org.sonatype.central + central-publishing-maven-plugin + true + + central + true + + higress-console + + + diff --git a/backend/sdk/pom.xml b/backend/sdk/pom.xml index dd23e655..0aec82cc 100644 --- a/backend/sdk/pom.xml +++ b/backend/sdk/pom.xml @@ -4,7 +4,7 @@ 4.0.0 - com.alibaba.higress + io.higress.api higress-admin-parent 0.0.1-SNAPSHOT @@ -14,17 +14,6 @@ Admin SDK Project for Higress - - org.springframework - spring-context - true - - - org.springframework - spring-web - true - - com.google.guava guava @@ -50,11 +39,22 @@ org.projectlombok lombok + provided - org.springframework.boot - spring-boot-starter-test + org.junit.jupiter + junit-jupiter + test + + + org.mockito + mockito-core + test + + + org.mockito + mockito-junit-jupiter test diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/config/HigressServiceConfig.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/config/HigressServiceConfig.java new file mode 100644 index 00000000..9b1aceb9 --- /dev/null +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/config/HigressServiceConfig.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2022-2024 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.higress.sdk.config; + +import java.util.Optional; + +import org.apache.commons.lang3.StringUtils; + +import com.alibaba.higress.sdk.constant.HigressConstants; + +import lombok.Data; + +/** + * @author CH3CHO + */ +@Data +public class HigressServiceConfig { + + private final String kubeConfigPath; + private final String ingressClassName; + private final String controllerNamespace; + private final String controllerServiceName; + private final String controllerServiceHost; + private final Integer controllerServicePort; + private final String controllerJwtPolicy; + private final String controllerAccessToken; + + public static HigressServiceConfig.Builder builder() { + return new Builder(); + } + + public static final class Builder { + private String kubeConfigPath; + private String ingressClassName = HigressConstants.CONTROLLER_INGRESS_CLASS_NAME_DEFAULT; + private String controllerNamespace = HigressConstants.NS_DEFAULT; + private String controllerServiceName = HigressConstants.CONTROLLER_SERVICE_NAME_DEFAULT; + private String controllerServiceHost = HigressConstants.CONTROLLER_SERVICE_HOST_DEFAULT; + private Integer controllerServicePort = HigressConstants.CONTROLLER_SERVICE_PORT_DEFAULT; + private String controllerJwtPolicy = HigressConstants.CONTROLLER_JWT_POLICY_DEFAULT; + private String controllerAccessToken; + + private Builder() {} + + public Builder withKubeConfigPath(String kubeConfigPath) { + this.kubeConfigPath = kubeConfigPath; + return this; + } + + public Builder withIngressClassName(String ingressClassName) { + this.ingressClassName = ingressClassName; + return this; + } + + public Builder withControllerNamespace(String controllerNamespace) { + this.controllerNamespace = controllerNamespace; + return this; + } + + public Builder withControllerServiceName(String controllerServiceName) { + this.controllerServiceName = controllerServiceName; + return this; + } + + public Builder withControllerServiceHost(String controllerServiceHost) { + this.controllerServiceHost = controllerServiceHost; + return this; + } + + public Builder withControllerServicePort(Integer controllerServicePort) { + this.controllerServicePort = controllerServicePort; + return this; + } + + public Builder withControllerJwtPolicy(String controllerJwtPolicy) { + this.controllerJwtPolicy = controllerJwtPolicy; + return this; + } + + public Builder withControllerAccessToken(String controllerAccessToken) { + this.controllerAccessToken = controllerAccessToken; + return this; + } + + public HigressServiceConfig build() { + return new HigressServiceConfig(kubeConfigPath, + StringUtils.firstNonEmpty(ingressClassName, HigressConstants.CONTROLLER_INGRESS_CLASS_NAME_DEFAULT), + StringUtils.firstNonEmpty(controllerNamespace, HigressConstants.NS_DEFAULT), + StringUtils.firstNonEmpty(controllerServiceName, HigressConstants.CONTROLLER_SERVICE_NAME_DEFAULT), + StringUtils.firstNonEmpty(controllerServiceHost, HigressConstants.CONTROLLER_SERVICE_HOST_DEFAULT), + Optional.ofNullable(controllerServicePort).orElse(HigressConstants.CONTROLLER_SERVICE_PORT_DEFAULT), + StringUtils.firstNonEmpty(controllerJwtPolicy, HigressConstants.CONTROLLER_JWT_POLICY_DEFAULT), + controllerAccessToken); + } + } +} diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/constant/ConfigKey.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/constant/ConfigKey.java deleted file mode 100644 index 54e13e89..00000000 --- a/backend/sdk/src/main/java/com/alibaba/higress/sdk/constant/ConfigKey.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2022-2023 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.higress.sdk.constant; - -import java.nio.file.Paths; - -public class ConfigKey { - public static final String KUBE_CONFIG_DEFAULT_PATH = - Paths.get(System.getProperty("user.home"), "/.kube/config").toString(); - public static final String NS_DEFAULT = "higress-system"; - public static final String PROTECTED_NSES = "kube-system" + Separators.LIST_CONFIG_SEPARATOR + NS_DEFAULT; - public static final String CONTROLLER_SERVICE_NAME_DEFAULT = "higress-controller"; - public static final String CONTROLLER_INGRESS_CLASS_NAME_DEFAULT = "higress"; - public static final String CONTROLLER_SERVICE_HOST_DEFAULT = "localhost"; - public static final int CONTROLLER_SERVICE_PORT_DEFAULT = 15014; - public static final String CONTROLLER_JWT_POLICY_DEFAULT = KubernetesConstants.JwtPolicy.THIRD_PARTY_JWT; - private static final String CONFIG_KEY_PREFIX = "higress-console."; - public static final String CONTROLLER_ACCESS_TOKEN_KEY = CONFIG_KEY_PREFIX + "controller.access-token"; - public static final String CONTROLLER_JWT_POLICY_KEY = CONFIG_KEY_PREFIX + "controller.jwt-policy"; - public static final String CONTROLLER_SERVICE_PORT_KEY = CONFIG_KEY_PREFIX + "controller.service.port"; - public static final String CONTROLLER_SERVICE_HOST_KEY = CONFIG_KEY_PREFIX + "controller.service.host"; - public static final String CONTROLLER_INGRESS_CLASS_NAME_KEY = CONFIG_KEY_PREFIX + "controller.ingress-class-name"; - public static final String CONTROLLER_SERVICE_NAME_KEY = CONFIG_KEY_PREFIX + "controller.service.name"; - public static final String PROTECTED_NSES_KEY = CONFIG_KEY_PREFIX + "protected-ns-list"; - public static final String NS_KEY = CONFIG_KEY_PREFIX + "ns"; - public static final String KUBE_CONFIG_KEY = CONFIG_KEY_PREFIX + "kube-config"; -} diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/constant/HigressConstants.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/constant/HigressConstants.java new file mode 100644 index 00000000..892441ed --- /dev/null +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/constant/HigressConstants.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2022-2024 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.higress.sdk.constant; + +public class HigressConstants { + public static final String NS_DEFAULT = "higress-system"; + public static final String CONTROLLER_SERVICE_NAME_DEFAULT = "higress-controller"; + public static final String CONTROLLER_INGRESS_CLASS_NAME_DEFAULT = "higress"; + public static final String CONTROLLER_SERVICE_HOST_DEFAULT = "localhost"; + public static final int CONTROLLER_SERVICE_PORT_DEFAULT = 15014; + public static final String CONTROLLER_JWT_POLICY_DEFAULT = KubernetesConstants.JwtPolicy.THIRD_PARTY_JWT; +} diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/constant/KubernetesConstants.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/constant/KubernetesConstants.java index c74e87a7..7c36e2d9 100644 --- a/backend/sdk/src/main/java/com/alibaba/higress/sdk/constant/KubernetesConstants.java +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/constant/KubernetesConstants.java @@ -14,6 +14,8 @@ public class KubernetesConstants { + public static final String KUBE_SYSTEM_NS = "kube-system"; + public static final String K8S_CERT = "cert"; public static final String K8S_ENABLE_HTTPS = "enableHttps"; diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/http/HttpStatus.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/http/HttpStatus.java new file mode 100644 index 00000000..21278b42 --- /dev/null +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/http/HttpStatus.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2022-2023 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.higress.sdk.http; + +public final class HttpStatus { + + private HttpStatus() {} + + public static final int CONTINUE = 100; + public static final int SWITCHING_PROTOCOLS = 101; + public static final int PROCESSING = 102; + public static final int OK = 200; + public static final int CREATED = 201; + public static final int ACCEPTED = 202; + public static final int NON_AUTHORITATIVE_INFORMATION = 203; + public static final int NO_CONTENT = 204; + public static final int RESET_CONTENT = 205; + public static final int PARTIAL_CONTENT = 206; + public static final int MULTI_STATUS = 207; + public static final int MULTIPLE_CHOICES = 300; + public static final int MOVED_PERMANENTLY = 301; + public static final int MOVED_TEMPORARILY = 302; + public static final int SEE_OTHER = 303; + public static final int NOT_MODIFIED = 304; + public static final int USE_PROXY = 305; + public static final int TEMPORARY_REDIRECT = 307; + public static final int BAD_REQUEST = 400; + public static final int UNAUTHORIZED = 401; + public static final int PAYMENT_REQUIRED = 402; + public static final int FORBIDDEN = 403; + public static final int NOT_FOUND = 404; + public static final int METHOD_NOT_ALLOWED = 405; + public static final int NOT_ACCEPTABLE = 406; + public static final int PROXY_AUTHENTICATION_REQUIRED = 407; + public static final int REQUEST_TIMEOUT = 408; + public static final int CONFLICT = 409; + public static final int GONE = 410; + public static final int LENGTH_REQUIRED = 411; + public static final int PRECONDITION_FAILED = 412; + public static final int REQUEST_TOO_LONG = 413; + public static final int REQUEST_URI_TOO_LONG = 414; + public static final int UNSUPPORTED_MEDIA_TYPE = 415; + public static final int REQUESTED_RANGE_NOT_SATISFIABLE = 416; + public static final int EXPECTATION_FAILED = 417; + public static final int INSUFFICIENT_SPACE_ON_RESOURCE = 419; + public static final int METHOD_FAILURE = 420; + public static final int UNPROCESSABLE_ENTITY = 422; + public static final int LOCKED = 423; + public static final int FAILED_DEPENDENCY = 424; + public static final int INTERNAL_SERVER_ERROR = 500; + public static final int NOT_IMPLEMENTED = 501; + public static final int BAD_GATEWAY = 502; + public static final int SERVICE_UNAVAILABLE = 503; + public static final int GATEWAY_TIMEOUT = 504; + public static final int HTTP_VERSION_NOT_SUPPORTED = 505; + public static final int INSUFFICIENT_STORAGE = 507; +} diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/DomainServiceImpl.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/DomainServiceImpl.java index 00010a89..76d62d38 100644 --- a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/DomainServiceImpl.java +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/DomainServiceImpl.java @@ -15,15 +15,13 @@ import java.util.List; import java.util.Optional; -import javax.annotation.Resource; - import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import org.springframework.http.HttpStatus; import com.alibaba.higress.sdk.constant.CommonKey; import com.alibaba.higress.sdk.exception.BusinessException; import com.alibaba.higress.sdk.exception.ResourceConflictException; +import com.alibaba.higress.sdk.http.HttpStatus; import com.alibaba.higress.sdk.model.CommonPageQuery; import com.alibaba.higress.sdk.model.Domain; import com.alibaba.higress.sdk.model.PaginatedResult; @@ -39,20 +37,21 @@ import lombok.extern.slf4j.Slf4j; @Slf4j -@org.springframework.stereotype.Service -public class DomainServiceImpl implements DomainService { - - @Resource - private KubernetesClientService kubernetesClientService; - - @Resource - private KubernetesModelConverter kubernetesModelConverter; - - @Resource - private RouteService routeService; - - @Resource - private WasmPluginInstanceService wasmPluginInstanceService; +class DomainServiceImpl implements DomainService { + + private final KubernetesClientService kubernetesClientService; + private final KubernetesModelConverter kubernetesModelConverter; + private final RouteService routeService; + private final WasmPluginInstanceService wasmPluginInstanceService; + + public DomainServiceImpl(KubernetesClientService kubernetesClientService, + KubernetesModelConverter kubernetesModelConverter, RouteService routeService, + WasmPluginInstanceService wasmPluginInstanceService) { + this.kubernetesClientService = kubernetesClientService; + this.kubernetesModelConverter = kubernetesModelConverter; + this.routeService = routeService; + this.wasmPluginInstanceService = wasmPluginInstanceService; + } @Override public Domain add(Domain domain) { @@ -61,7 +60,7 @@ public Domain add(Domain domain) { try { newDomainConfigMap = kubernetesClientService.createConfigMap(domainConfigMap); } catch (ApiException e) { - if (e.getCode() == HttpStatus.CONFLICT.value()) { + if (e.getCode() == HttpStatus.CONFLICT) { throw new ResourceConflictException(); } throw new BusinessException("Error occurs when adding a new domain.", e); @@ -119,7 +118,7 @@ public Domain put(Domain domain) { try { updatedConfigMap = kubernetesClientService.replaceConfigMap(domainConfigMap); } catch (ApiException e) { - if (e.getCode() == HttpStatus.CONFLICT.value()) { + if (e.getCode() == HttpStatus.CONFLICT) { throw new ResourceConflictException(); } throw new BusinessException( diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/HigressServiceProvider.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/HigressServiceProvider.java new file mode 100644 index 00000000..41a26367 --- /dev/null +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/HigressServiceProvider.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2022-2024 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.higress.sdk.service; + +import java.io.IOException; + +import com.alibaba.higress.sdk.config.HigressServiceConfig; +import com.alibaba.higress.sdk.service.kubernetes.KubernetesClientService; +import com.alibaba.higress.sdk.service.kubernetes.KubernetesModelConverter; + +/** + * @author CH3CHO + */ +public interface HigressServiceProvider { + + static HigressServiceProvider create(HigressServiceConfig config) throws IOException { + return new HigressServiceProviderImpl(config); + } + + KubernetesClientService kubernetesClientService(); + + KubernetesModelConverter kubernetesModelConverter(); + + DomainService domainService(); + + RouteService routeService(); + + ServiceService serviceService(); + + ServiceSourceService serviceSourceService(); + + TlsCertificateService tlsCertificateService(); + + WasmPluginService wasmPluginService(); + + WasmPluginInstanceService wasmPluginInstanceService(); +} diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/HigressServiceProviderImpl.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/HigressServiceProviderImpl.java new file mode 100644 index 00000000..0c8aa6e1 --- /dev/null +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/HigressServiceProviderImpl.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2022-2024 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.higress.sdk.service; + +import com.alibaba.higress.sdk.config.HigressServiceConfig; +import com.alibaba.higress.sdk.service.kubernetes.KubernetesClientService; +import com.alibaba.higress.sdk.service.kubernetes.KubernetesModelConverter; + +import java.io.IOException; + +/** + * @author CH3CHO + */ +class HigressServiceProviderImpl implements HigressServiceProvider { + + private final KubernetesClientService kubernetesClientService; + private final KubernetesModelConverter kubernetesModelConverter; + private final DomainService domainService; + private final RouteService routeService; + private final ServiceService serviceService; + private final ServiceSourceService serviceSourceService; + private final TlsCertificateService tlsCertificateService; + private final WasmPluginService wasmPluginService; + private final WasmPluginInstanceService wasmPluginInstanceService; + + HigressServiceProviderImpl(HigressServiceConfig config) throws IOException { + kubernetesClientService = new KubernetesClientService(config); + kubernetesModelConverter = new KubernetesModelConverter(kubernetesClientService); + serviceService = new ServiceServiceImpl(kubernetesClientService); + serviceSourceService = new ServiceSourceServiceImpl(kubernetesClientService, kubernetesModelConverter); + tlsCertificateService = new TlsCertificateServiceImpl(kubernetesClientService, kubernetesModelConverter); + wasmPluginService = new WasmPluginServiceImpl(kubernetesClientService, kubernetesModelConverter); + wasmPluginInstanceService = + new WasmPluginInstanceServiceImpl(wasmPluginService, kubernetesClientService, kubernetesModelConverter); + routeService = + new RouteServiceImpl(kubernetesClientService, kubernetesModelConverter, wasmPluginInstanceService); + domainService = new DomainServiceImpl(kubernetesClientService, kubernetesModelConverter, routeService, + wasmPluginInstanceService); + } + + @Override + public KubernetesClientService kubernetesClientService() { + return kubernetesClientService; + } + + @Override + public KubernetesModelConverter kubernetesModelConverter() { + return kubernetesModelConverter; + } + + @Override + public DomainService domainService() { + return domainService; + } + + @Override + public RouteService routeService() { + return routeService; + } + + @Override + public ServiceService serviceService() { + return serviceService; + } + + @Override + public ServiceSourceService serviceSourceService() { + return serviceSourceService; + } + + @Override + public TlsCertificateService tlsCertificateService() { + return tlsCertificateService; + } + + @Override + public WasmPluginService wasmPluginService() { + return wasmPluginService; + } + + @Override + public WasmPluginInstanceService wasmPluginInstanceService() { + return wasmPluginInstanceService; + } +} diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/RouteServiceImpl.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/RouteServiceImpl.java index fc0bf4a3..c8e8f7f3 100644 --- a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/RouteServiceImpl.java +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/RouteServiceImpl.java @@ -15,15 +15,12 @@ import java.util.Collections; import java.util.List; -import javax.annotation.Resource; - import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Service; import com.alibaba.higress.sdk.exception.BusinessException; import com.alibaba.higress.sdk.exception.ResourceConflictException; +import com.alibaba.higress.sdk.http.HttpStatus; import com.alibaba.higress.sdk.model.PaginatedResult; import com.alibaba.higress.sdk.model.Route; import com.alibaba.higress.sdk.model.RoutePageQuery; @@ -33,26 +30,19 @@ import io.kubernetes.client.openapi.ApiException; import io.kubernetes.client.openapi.models.V1Ingress; +import lombok.extern.slf4j.Slf4j; -@Service -public class RouteServiceImpl implements RouteService { +@Slf4j +class RouteServiceImpl implements RouteService { - private KubernetesClientService kubernetesClientService; - private KubernetesModelConverter kubernetesModelConverter; - private WasmPluginInstanceService wasmPluginInstanceService; + private final KubernetesClientService kubernetesClientService; + private final KubernetesModelConverter kubernetesModelConverter; + private final WasmPluginInstanceService wasmPluginInstanceService; - @Resource - public void setKubernetesClientService(KubernetesClientService kubernetesClientService) { + public RouteServiceImpl(KubernetesClientService kubernetesClientService, + KubernetesModelConverter kubernetesModelConverter, WasmPluginInstanceService wasmPluginInstanceService) { this.kubernetesClientService = kubernetesClientService; - } - - @Resource - public void setKubernetesModelConverter(KubernetesModelConverter kubernetesModelConverter) { this.kubernetesModelConverter = kubernetesModelConverter; - } - - @Resource - public void setWasmPluginInstanceService(WasmPluginInstanceService wasmPluginInstanceService) { this.wasmPluginInstanceService = wasmPluginInstanceService; } @@ -90,7 +80,7 @@ public Route add(Route route) { try { newIngress = kubernetesClientService.createIngress(ingress); } catch (ApiException e) { - if (e.getCode() == HttpStatus.CONFLICT.value()) { + if (e.getCode() == HttpStatus.CONFLICT) { throw new ResourceConflictException(); } throw new BusinessException( @@ -107,7 +97,7 @@ public Route update(Route route) { try { updatedIngress = kubernetesClientService.replaceIngress(ingress); } catch (ApiException e) { - if (e.getCode() == HttpStatus.CONFLICT.value()) { + if (e.getCode() == HttpStatus.CONFLICT) { throw new ResourceConflictException(); } throw new BusinessException( diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/ServiceServiceImpl.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/ServiceServiceImpl.java index 80bcded4..62442d7b 100644 --- a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/ServiceServiceImpl.java +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/ServiceServiceImpl.java @@ -20,8 +20,6 @@ import java.util.Map; import java.util.Set; -import javax.annotation.Resource; - import org.apache.commons.collections4.CollectionUtils; import com.alibaba.higress.sdk.constant.CommonKey; @@ -38,11 +36,13 @@ import lombok.extern.slf4j.Slf4j; @Slf4j -@org.springframework.stereotype.Service -public class ServiceServiceImpl implements ServiceService { +class ServiceServiceImpl implements ServiceService { + + private final KubernetesClientService kubernetesClientService; - @Resource - private KubernetesClientService kubernetesClientService; + public ServiceServiceImpl(KubernetesClientService kubernetesClientService) { + this.kubernetesClientService = kubernetesClientService; + } @Override public PaginatedResult list(CommonPageQuery query) { @@ -89,7 +89,8 @@ public PaginatedResult list(CommonPageQuery query) { } } - services.sort(Comparator.comparing(Service::getNamespace).thenComparing(Service::getName).thenComparing(Service::getPort)); + services.sort(Comparator.comparing(Service::getNamespace).thenComparing(Service::getName) + .thenComparing(Service::getPort)); return PaginatedResult.createFromFullList(services, query); } catch (Exception e) { diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/ServiceSourceServiceImpl.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/ServiceSourceServiceImpl.java index 72e908b0..8256cec0 100644 --- a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/ServiceSourceServiceImpl.java +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/ServiceSourceServiceImpl.java @@ -21,17 +21,14 @@ import java.util.Optional; import java.util.stream.Collectors; -import javax.annotation.Resource; - import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Service; import com.alibaba.higress.sdk.exception.BusinessException; import com.alibaba.higress.sdk.exception.ResourceConflictException; import com.alibaba.higress.sdk.exception.ValidationException; +import com.alibaba.higress.sdk.http.HttpStatus; import com.alibaba.higress.sdk.model.CommonPageQuery; import com.alibaba.higress.sdk.model.PaginatedResult; import com.alibaba.higress.sdk.model.ServiceSource; @@ -45,22 +42,19 @@ import io.kubernetes.client.openapi.ApiException; import io.kubernetes.client.openapi.models.V1ObjectMeta; import io.kubernetes.client.openapi.models.V1Secret; +import lombok.extern.slf4j.Slf4j; -@Service -public class ServiceSourceServiceImpl implements ServiceSourceService { +@Slf4j +class ServiceSourceServiceImpl implements ServiceSourceService { private static final int SECRET_NAME_ATTEMPTS = 5; - private KubernetesClientService kubernetesClientService; - private KubernetesModelConverter kubernetesModelConverter; + private final KubernetesClientService kubernetesClientService; + private final KubernetesModelConverter kubernetesModelConverter; - @Resource - public void setKubernetesClientService(KubernetesClientService kubernetesClientService) { + public ServiceSourceServiceImpl(KubernetesClientService kubernetesClientService, + KubernetesModelConverter kubernetesModelConverter) { this.kubernetesClientService = kubernetesClientService; - } - - @Resource - public void setKubernetesModelConverter(KubernetesModelConverter kubernetesModelConverter) { this.kubernetesModelConverter = kubernetesModelConverter; } @@ -110,7 +104,7 @@ public ServiceSource addOrUpdate(ServiceSource serviceSource) { kubernetesClientService.replaceMcpBridge(mcpBridge); } } catch (ApiException e) { - if (e.getCode() == HttpStatus.CONFLICT.value()) { + if (e.getCode() == HttpStatus.CONFLICT) { throw new ResourceConflictException(); } throw new BusinessException( @@ -197,7 +191,7 @@ public ServiceSource add(ServiceSource serviceSource) { kubernetesClientService.replaceMcpBridge(mcpBridge); } } catch (ApiException e) { - if (e.getCode() == HttpStatus.CONFLICT.value()) { + if (e.getCode() == HttpStatus.CONFLICT) { throw new ResourceConflictException(); } throw new BusinessException( @@ -288,7 +282,7 @@ private void syncAuthSecret(ServiceSource serviceSource, V1RegistryConfig regist registry.setAuthSecretName(secretName); done = true; } catch (ApiException e) { - if (e.getCode() == HttpStatus.CONFLICT.value()) { + if (e.getCode() == HttpStatus.CONFLICT) { continue; } String message = "Error occurs when creating the secret associated with ServiceSource named " diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/TlsCertificateServiceImpl.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/TlsCertificateServiceImpl.java index 4e342337..8183f20c 100644 --- a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/TlsCertificateServiceImpl.java +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/TlsCertificateServiceImpl.java @@ -15,15 +15,12 @@ import java.util.Collections; import java.util.List; -import javax.annotation.Resource; - import org.apache.commons.collections4.CollectionUtils; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Service; import com.alibaba.higress.sdk.constant.KubernetesConstants; import com.alibaba.higress.sdk.exception.BusinessException; import com.alibaba.higress.sdk.exception.ResourceConflictException; +import com.alibaba.higress.sdk.http.HttpStatus; import com.alibaba.higress.sdk.model.CommonPageQuery; import com.alibaba.higress.sdk.model.PaginatedResult; import com.alibaba.higress.sdk.model.TlsCertificate; @@ -32,20 +29,17 @@ import io.kubernetes.client.openapi.ApiException; import io.kubernetes.client.openapi.models.V1Secret; +import lombok.extern.slf4j.Slf4j; -@Service -public class TlsCertificateServiceImpl implements TlsCertificateService { +@Slf4j +class TlsCertificateServiceImpl implements TlsCertificateService { - private KubernetesClientService kubernetesClientService; - private KubernetesModelConverter kubernetesModelConverter; + private final KubernetesClientService kubernetesClientService; + private final KubernetesModelConverter kubernetesModelConverter; - @Resource - public void setKubernetesClientService(KubernetesClientService kubernetesClientService) { + public TlsCertificateServiceImpl(KubernetesClientService kubernetesClientService, + KubernetesModelConverter kubernetesModelConverter) { this.kubernetesClientService = kubernetesClientService; - } - - @Resource - public void setKubernetesModelConverter(KubernetesModelConverter kubernetesModelConverter) { this.kubernetesModelConverter = kubernetesModelConverter; } @@ -87,7 +81,7 @@ public TlsCertificate add(TlsCertificate certificate) { try { newSecret = kubernetesClientService.createSecret(secret); } catch (ApiException e) { - if (e.getCode() == HttpStatus.CONFLICT.value()) { + if (e.getCode() == HttpStatus.CONFLICT) { throw new ResourceConflictException(); } throw new BusinessException("Error occurs when updating the secret generated by tls certificate with name: " @@ -103,7 +97,7 @@ public TlsCertificate update(TlsCertificate tlsCertificate) { try { newSecret = kubernetesClientService.replaceSecret(secret); } catch (ApiException e) { - if (e.getCode() == HttpStatus.CONFLICT.value()) { + if (e.getCode() == HttpStatus.CONFLICT) { throw new ResourceConflictException(); } throw new BusinessException("Error occurs when updating the secret generated by tls certificate with name: " diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/WasmPluginInstanceServiceImpl.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/WasmPluginInstanceServiceImpl.java index 4620e50e..cc10eb16 100644 --- a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/WasmPluginInstanceServiceImpl.java +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/WasmPluginInstanceServiceImpl.java @@ -19,17 +19,14 @@ import java.util.Map; import java.util.Objects; -import javax.annotation.Resource; - import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.openapi4j.core.util.TreeUtil; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Service; import com.alibaba.higress.sdk.exception.BusinessException; import com.alibaba.higress.sdk.exception.ResourceConflictException; import com.alibaba.higress.sdk.exception.ValidationException; +import com.alibaba.higress.sdk.http.HttpStatus; import com.alibaba.higress.sdk.model.WasmPlugin; import com.alibaba.higress.sdk.model.WasmPluginConfig; import com.alibaba.higress.sdk.model.WasmPluginInstance; @@ -39,26 +36,19 @@ import com.alibaba.higress.sdk.service.kubernetes.crd.wasm.V1alpha1WasmPlugin; import io.kubernetes.client.openapi.ApiException; +import lombok.extern.slf4j.Slf4j; -@Service -public class WasmPluginInstanceServiceImpl implements WasmPluginInstanceService { +@Slf4j +class WasmPluginInstanceServiceImpl implements WasmPluginInstanceService { - private WasmPluginService wasmPluginService; - private KubernetesClientService kubernetesClientService; - private KubernetesModelConverter kubernetesModelConverter; + private final WasmPluginService wasmPluginService; + private final KubernetesClientService kubernetesClientService; + private final KubernetesModelConverter kubernetesModelConverter; - @Resource - public void setWasmPluginService(WasmPluginService wasmPluginService) { + public WasmPluginInstanceServiceImpl(WasmPluginService wasmPluginService, + KubernetesClientService kubernetesClientService, KubernetesModelConverter kubernetesModelConverter) { this.wasmPluginService = wasmPluginService; - } - - @Resource - public void setKubernetesClientService(KubernetesClientService kubernetesClientService) { this.kubernetesClientService = kubernetesClientService; - } - - @Resource - public void setKubernetesModelConverter(KubernetesModelConverter kubernetesModelConverter) { this.kubernetesModelConverter = kubernetesModelConverter; } @@ -165,7 +155,7 @@ public WasmPluginInstance addOrUpdate(WasmPluginInstance instance) { result = kubernetesClientService.replaceWasmPlugin(result); } } catch (ApiException e) { - if (e.getCode() == HttpStatus.CONFLICT.value()) { + if (e.getCode() == HttpStatus.CONFLICT) { throw new ResourceConflictException(); } throw new BusinessException( diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/WasmPluginServiceImpl.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/WasmPluginServiceImpl.java index e5cb5b00..55899596 100644 --- a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/WasmPluginServiceImpl.java +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/WasmPluginServiceImpl.java @@ -33,7 +33,6 @@ import java.util.regex.Pattern; import javax.annotation.PostConstruct; -import javax.annotation.Resource; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; @@ -41,13 +40,12 @@ import org.apache.commons.lang3.StringUtils; import org.openapi4j.core.util.TreeUtil; import org.openapi4j.parser.model.v3.Schema; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Service; import com.alibaba.higress.sdk.constant.Separators; import com.alibaba.higress.sdk.exception.BusinessException; import com.alibaba.higress.sdk.exception.NotFoundException; import com.alibaba.higress.sdk.exception.ResourceConflictException; +import com.alibaba.higress.sdk.http.HttpStatus; import com.alibaba.higress.sdk.model.PaginatedResult; import com.alibaba.higress.sdk.model.WasmPlugin; import com.alibaba.higress.sdk.model.WasmPluginConfig; @@ -72,8 +70,7 @@ * @author CH3CHO */ @Slf4j -@Service -public class WasmPluginServiceImpl implements WasmPluginService { +class WasmPluginServiceImpl implements WasmPluginService { private static final String PLUGINS_RESOURCE_FOLDER = "plugins/"; private static final String PLUGINS_PROPERTIES_FILE = PLUGINS_RESOURCE_FOLDER + "plugins.properties"; @@ -95,17 +92,13 @@ public class WasmPluginServiceImpl implements WasmPluginService { private volatile List builtInPlugins = Collections.emptyList(); - private KubernetesClientService kubernetesClientService; + private final KubernetesClientService kubernetesClientService; - private KubernetesModelConverter kubernetesModelConverter; + private final KubernetesModelConverter kubernetesModelConverter; - @Resource - public void setKubernetesClientService(KubernetesClientService kubernetesClientService) { + public WasmPluginServiceImpl(KubernetesClientService kubernetesClientService, + KubernetesModelConverter kubernetesModelConverter) { this.kubernetesClientService = kubernetesClientService; - } - - @Resource - public void setKubernetesModelConverter(KubernetesModelConverter kubernetesModelConverter) { this.kubernetesModelConverter = kubernetesModelConverter; } @@ -394,7 +387,7 @@ public WasmPlugin addCustom(WasmPlugin plugin) { try { addedCr = kubernetesClientService.createWasmPlugin(cr); } catch (ApiException e) { - if (e.getCode() == HttpStatus.CONFLICT.value()) { + if (e.getCode() == HttpStatus.CONFLICT) { throw new ResourceConflictException(); } throw new BusinessException("Error occurs when adding a new Wasm plugin.", e); @@ -448,7 +441,7 @@ public WasmPlugin updateCustom(WasmPlugin plugin) { try { updatedCr = kubernetesClientService.replaceWasmPlugin(cr); } catch (ApiException e) { - if (e.getCode() == HttpStatus.CONFLICT.value()) { + if (e.getCode() == HttpStatus.CONFLICT) { throw new ResourceConflictException(); } throw new BusinessException("Error occurs when updating the Wasm plugin wth name " + crName, e); @@ -465,7 +458,7 @@ public WasmPlugin updateCustom(WasmPlugin plugin) { try { updatedCr = kubernetesClientService.createWasmPlugin(cr); } catch (ApiException e) { - if (e.getCode() == HttpStatus.CONFLICT.value()) { + if (e.getCode() == HttpStatus.CONFLICT) { throw new ResourceConflictException(); } throw new BusinessException("Error occurs when adding the Wasm plugin CR wth name " + crName, e); diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesClientService.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesClientService.java index 7dd194b5..6cd2b012 100644 --- a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesClientService.java +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesClientService.java @@ -20,38 +20,33 @@ import java.io.FileReader; import java.io.IOException; import java.nio.charset.Charset; +import java.nio.file.Paths; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Comparator; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; -import java.util.Set; - -import javax.annotation.PostConstruct; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; -import com.alibaba.higress.sdk.constant.ConfigKey; +import com.alibaba.higress.sdk.config.HigressServiceConfig; import com.alibaba.higress.sdk.constant.KubernetesConstants; import com.alibaba.higress.sdk.constant.KubernetesConstants.Label; import com.alibaba.higress.sdk.constant.Separators; +import com.alibaba.higress.sdk.http.HttpStatus; import com.alibaba.higress.sdk.service.kubernetes.crd.mcp.V1McpBridge; import com.alibaba.higress.sdk.service.kubernetes.crd.mcp.V1McpBridgeList; import com.alibaba.higress.sdk.service.kubernetes.crd.wasm.V1alpha1WasmPlugin; import com.alibaba.higress.sdk.service.kubernetes.crd.wasm.V1alpha1WasmPluginList; import com.alibaba.higress.sdk.service.kubernetes.model.IstioEndpointShard; import com.alibaba.higress.sdk.service.kubernetes.model.RegistryzService; +import com.google.common.net.HttpHeaders; import io.kubernetes.client.common.KubernetesObject; import io.kubernetes.client.openapi.ApiClient; @@ -64,8 +59,6 @@ import io.kubernetes.client.openapi.models.V1ConfigMapList; import io.kubernetes.client.openapi.models.V1Ingress; import io.kubernetes.client.openapi.models.V1IngressList; -import io.kubernetes.client.openapi.models.V1Namespace; -import io.kubernetes.client.openapi.models.V1NamespaceList; import io.kubernetes.client.openapi.models.V1ObjectMeta; import io.kubernetes.client.openapi.models.V1Secret; import io.kubernetes.client.openapi.models.V1SecretList; @@ -79,10 +72,12 @@ import okhttp3.Response; @Slf4j -@org.springframework.stereotype.Service public class KubernetesClientService { - private static final String POD_SERVICE_ACCOUNT_TOKEN_FILE_PATH = "/var/run/secrets/kubernetes.io/serviceaccount/token"; + private static final String KUBE_CONFIG_DEFAULT_PATH = + Paths.get(System.getProperty("user.home"), "/.kube/config").toString(); + private static final String POD_SERVICE_ACCOUNT_TOKEN_FILE_PATH = + "/var/run/secrets/kubernetes.io/serviceaccount/token"; private static final String CONTROLLER_ACCESS_TOKEN_FILE_PATH = "/var/run/secrets/access-token/token"; private static final String DEFAULT_LABEL_SELECTORS = buildLabelSelector(KubernetesConstants.Label.RESOURCE_DEFINER_KEY, Label.RESOURCE_DEFINER_VALUE); @@ -93,46 +88,45 @@ public class KubernetesClientService { private Boolean inCluster; - @Value("${" + ConfigKey.KUBE_CONFIG_KEY + ":}") - private String kubeConfig; - - @Value("${" + ConfigKey.CONTROLLER_SERVICE_NAME_KEY + ":" + ConfigKey.CONTROLLER_SERVICE_NAME_DEFAULT + "}") - private String controllerServiceName = ConfigKey.CONTROLLER_SERVICE_NAME_DEFAULT; + private final String kubeConfig; - @Value("${" + ConfigKey.NS_KEY + ":" + ConfigKey.NS_DEFAULT + "}") - private String controllerNamespace = ConfigKey.NS_DEFAULT; + private final String controllerServiceName; - @Value("#{'${" + ConfigKey.PROTECTED_NSES_KEY + ":" + ConfigKey.PROTECTED_NSES + "}'.split('" - + Separators.LIST_CONFIG_SEPARATOR + "')}") - private Set protectedNses = - new HashSet<>(Arrays.asList(ConfigKey.PROTECTED_NSES.split(Separators.LIST_CONFIG_SEPARATOR))); + private final String controllerNamespace; - @Value("${" + ConfigKey.CONTROLLER_INGRESS_CLASS_NAME_KEY + ":" + ConfigKey.CONTROLLER_INGRESS_CLASS_NAME_DEFAULT - + "}") - private String controllerIngressClassName = ConfigKey.CONTROLLER_INGRESS_CLASS_NAME_DEFAULT; + private final String controllerIngressClassName; - @Value("${" + ConfigKey.CONTROLLER_SERVICE_HOST_KEY + ":" + ConfigKey.CONTROLLER_SERVICE_HOST_DEFAULT + "}") - private String controllerServiceHost = ConfigKey.CONTROLLER_SERVICE_HOST_DEFAULT; + private final String controllerServiceHost; - @Value("${" + ConfigKey.CONTROLLER_SERVICE_PORT_KEY + ":" + ConfigKey.CONTROLLER_SERVICE_PORT_DEFAULT + "}") - private int controllerServicePort = ConfigKey.CONTROLLER_SERVICE_PORT_DEFAULT; + private final int controllerServicePort; - @Value("${" + ConfigKey.CONTROLLER_JWT_POLICY_KEY + ":" + ConfigKey.CONTROLLER_JWT_POLICY_DEFAULT + "}") - private String controllerJwtPolicy = ConfigKey.CONTROLLER_JWT_POLICY_DEFAULT; + private final String controllerJwtPolicy; - @Value("${" + ConfigKey.CONTROLLER_ACCESS_TOKEN_KEY + ":}") private String controllerAccessToken; private boolean ingressV1Supported; - @PostConstruct - public void init() throws IOException { - if (checkInCluster()) { + public KubernetesClientService(HigressServiceConfig config) throws IOException { + validateConfig(config); + + this.kubeConfig = config.getKubeConfigPath(); + this.controllerNamespace = config.getControllerNamespace(); + this.controllerServiceName = config.getControllerServiceName(); + this.controllerServiceHost = config.getControllerServiceHost(); + this.controllerServicePort = config.getControllerServicePort(); + this.controllerIngressClassName = config.getIngressClassName(); + this.controllerJwtPolicy = config.getControllerJwtPolicy(); + this.controllerAccessToken = config.getControllerAccessToken(); + this.inCluster = isInCluster(); + + if (inCluster) { client = ClientBuilder.cluster().build(); + if (StringUtils.isEmpty(controllerAccessToken)) { + controllerAccessToken = readTokenFromFile(); + } log.info("init KubernetesClientService InCluster"); } else { - String kubeConfigPath = - !Strings.isNullOrEmpty(kubeConfig) ? kubeConfig : ConfigKey.KUBE_CONFIG_DEFAULT_PATH; + String kubeConfigPath = !Strings.isNullOrEmpty(kubeConfig) ? kubeConfig : KUBE_CONFIG_DEFAULT_PATH; try (FileReader reader = new FileReader(kubeConfigPath)) { client = ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(reader)).build(); } @@ -154,42 +148,12 @@ private void initializeK8sCapabilities() { } } - public boolean checkIngressV1Supported() { + public boolean isIngressV1Supported() { return ingressV1Supported; } - public boolean checkHigress() throws ApiException { - CoreV1Api api = new CoreV1Api(client); - V1NamespaceList list = api.listNamespace(null, null, null, null, null, null, null, null, null, null); - for (V1Namespace item : list.getItems()) { - if (item.getMetadata() != null && ConfigKey.NS_DEFAULT.equals(item.getMetadata().getName())) { - return true; - } - } - return false; - } - - public String checkControllerService() { - // try { - // Configuration.setDefaultApiClient(client); - // CoreV1Api api = new CoreV1Api(); - // V1ServiceList list = api.listServiceForAllNamespaces(null, null, null, null, null, null, null, null, null, - // null); - // for (V1Service item : list.getItems()) { - // if (controllerServiceName.equals(item.getMetadata().getName())) { - // log.info("Get Higress Controller name {}, namespace {}", item.getMetadata().getName(), - // item.getMetadata().getNamespace()); - // return item.getMetadata().getName() + "." + item.getMetadata().getNamespace(); - // } - // } - // } catch (Exception e) { - // log.error("checkControllerService fail use default ", e); - // } - return inCluster ? controllerServiceName + "." + controllerNamespace : controllerServiceHost; - } - public boolean isNamespaceProtected(String namespace) { - return controllerNamespace.equals(namespace) || protectedNses.contains(namespace); + return KubernetesConstants.KUBE_SYSTEM_NS.equals(namespace) || controllerNamespace.equals(namespace); } public List gatewayServiceList() throws IOException { @@ -220,11 +184,8 @@ public Map> gatewayServiceEndpoint() thr return null; } - public boolean checkInCluster() { - if (inCluster == null) { - inCluster = new File(POD_SERVICE_ACCOUNT_TOKEN_FILE_PATH).exists(); - } - return inCluster; + private static boolean isInCluster() { + return new File(POD_SERVICE_ACCOUNT_TOKEN_FILE_PATH).exists(); } public List listIngress() { @@ -265,7 +226,7 @@ public V1Ingress readIngress(String name) throws ApiException { try { return apiInstance.readNamespacedIngress(name, controllerNamespace, null); } catch (ApiException e) { - if (e.getCode() == HttpStatus.NOT_FOUND.value()) { + if (e.getCode() == HttpStatus.NOT_FOUND) { return null; } throw e; @@ -297,7 +258,7 @@ public void deleteIngress(String name) throws ApiException { try { status = apiInstance.deleteNamespacedIngress(name, controllerNamespace, null, null, null, null, null, null); } catch (ApiException ae) { - if (ae.getCode() == HttpStatus.NOT_FOUND.value()) { + if (ae.getCode() == HttpStatus.NOT_FOUND) { // The Ingress to be deleted is already gone or never existed. return; } @@ -324,7 +285,7 @@ public V1ConfigMap readConfigMap(String name) throws ApiException { try { return coreV1Api.readNamespacedConfigMap(name, controllerNamespace, null); } catch (ApiException e) { - if (e.getCode() == HttpStatus.NOT_FOUND.value()) { + if (e.getCode() == HttpStatus.NOT_FOUND) { return null; } throw e; @@ -337,7 +298,7 @@ public void deleteConfigMap(String name) throws ApiException { try { status = coreV1Api.deleteNamespacedConfigMap(name, controllerNamespace, null, null, null, null, null, null); } catch (ApiException ae) { - if (ae.getCode() == HttpStatus.NOT_FOUND.value()) { + if (ae.getCode() == HttpStatus.NOT_FOUND) { // The ConfigMap to be deleted is already gone or never existed. return; } @@ -373,7 +334,7 @@ public V1Secret readSecret(String name) throws ApiException { try { return coreV1Api.readNamespacedSecret(name, controllerNamespace, null); } catch (ApiException e) { - if (e.getCode() == HttpStatus.NOT_FOUND.value()) { + if (e.getCode() == HttpStatus.NOT_FOUND) { return null; } throw e; @@ -403,7 +364,7 @@ public void deleteSecret(String name) throws ApiException { try { status = coreV1Api.deleteNamespacedSecret(name, controllerNamespace, null, null, null, null, null, null); } catch (ApiException ae) { - if (ae.getCode() == HttpStatus.NOT_FOUND.value()) { + if (ae.getCode() == HttpStatus.NOT_FOUND) { // The Secret to be deleted is already gone or never existed. return; } @@ -459,7 +420,7 @@ public V1McpBridge readMcpBridge(String name) throws ApiException { controllerNamespace, V1McpBridge.PLURAL, name); return client.getJSON().deserialize(client.getJSON().serialize(response), V1McpBridge.class); } catch (ApiException e) { - if (e.getCode() == HttpStatus.NOT_FOUND.value()) { + if (e.getCode() == HttpStatus.NOT_FOUND) { return null; } throw e; @@ -535,7 +496,7 @@ public V1alpha1WasmPlugin readWasmPlugin(String name) throws ApiException { V1alpha1WasmPlugin.VERSION, controllerNamespace, V1alpha1WasmPlugin.PLURAL, name); return client.getJSON().deserialize(client.getJSON().serialize(response), V1alpha1WasmPlugin.class); } catch (ApiException e) { - if (e.getCode() == HttpStatus.NOT_FOUND.value()) { + if (e.getCode() == HttpStatus.NOT_FOUND) { return null; } throw e; @@ -546,11 +507,11 @@ private void checkResponseStatus(V1Status status) { // TODO: Throw exception accordingly. } - private Request buildControllerRequest(String path) throws IOException { - String serviceUrl = checkControllerService(); - String url = "http://" + serviceUrl + ":" + controllerServicePort + path; + private Request buildControllerRequest(String path) { + String serviceHost = inCluster ? controllerServiceName + "." + controllerNamespace : controllerServiceHost; + String url = "http://" + serviceHost + ":" + controllerServicePort + path; Request.Builder builder = new Request.Builder().url(url); - String token = checkInCluster() ? readTokenFromFile() : getTokenFromConfiguration(); + String token = controllerAccessToken; if (!Strings.isNullOrEmpty(token)) { builder.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token); } @@ -565,10 +526,6 @@ private String readTokenFromFile() throws IOException { return FileUtils.readFileToString(new File(fileName), Charset.defaultCharset()); } - private String getTokenFromConfiguration() { - return controllerAccessToken; - } - private void renderDefaultLabels(KubernetesObject object) { KubernetesUtil.setLabel(object, Label.RESOURCE_DEFINER_KEY, Label.RESOURCE_DEFINER_VALUE); } @@ -579,4 +536,27 @@ private static List sortKubernetesObjects(List 65535) { + throw new IllegalArgumentException("controllerServicePort is invalid"); + } + if (StringUtils.isEmpty(config.getControllerJwtPolicy())) { + throw new IllegalArgumentException("controllerJwtPolicy is required"); + } + } } diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesModelConverter.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesModelConverter.java index e7877115..c7e86cf1 100644 --- a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesModelConverter.java +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesModelConverter.java @@ -33,7 +33,6 @@ import java.util.Set; import java.util.regex.Pattern; -import javax.annotation.Resource; import javax.naming.InvalidNameException; import javax.naming.ldap.LdapName; import javax.security.auth.x500.X500Principal; @@ -96,7 +95,6 @@ * @author CH3CHO */ @Slf4j -@org.springframework.stereotype.Service public class KubernetesModelConverter { private static final String PSEUDO_HEADER_PREFIX = ":"; @@ -106,7 +104,7 @@ public class KubernetesModelConverter { private static final Set SUPPORTED_ANNOTATIONS; private static final Integer DEFAULT_WEIGHT = 100; - private KubernetesClientService kubernetesClientService; + private final KubernetesClientService kubernetesClientService; static { V1TypedLocalObjectReference mcpBridgeReference = new V1TypedLocalObjectReference(); @@ -135,8 +133,7 @@ public class KubernetesModelConverter { SUPPORTED_ANNOTATIONS = Collections.unmodifiableSet(supportedAnnotations); } - @Resource - public void setKubernetesClientService(KubernetesClientService kubernetesClientService) { + public KubernetesModelConverter(KubernetesClientService kubernetesClientService) { this.kubernetesClientService = kubernetesClientService; } diff --git a/backend/console/src/main/resources/plugins/basic-auth/README.md b/backend/sdk/src/main/resources/plugins/basic-auth/README.md similarity index 100% rename from backend/console/src/main/resources/plugins/basic-auth/README.md rename to backend/sdk/src/main/resources/plugins/basic-auth/README.md diff --git a/backend/console/src/main/resources/plugins/basic-auth/spec.yaml b/backend/sdk/src/main/resources/plugins/basic-auth/spec.yaml similarity index 100% rename from backend/console/src/main/resources/plugins/basic-auth/spec.yaml rename to backend/sdk/src/main/resources/plugins/basic-auth/spec.yaml diff --git a/backend/console/src/main/resources/plugins/bot-detect/README.md b/backend/sdk/src/main/resources/plugins/bot-detect/README.md similarity index 100% rename from backend/console/src/main/resources/plugins/bot-detect/README.md rename to backend/sdk/src/main/resources/plugins/bot-detect/README.md diff --git a/backend/console/src/main/resources/plugins/bot-detect/README_EN.md b/backend/sdk/src/main/resources/plugins/bot-detect/README_EN.md similarity index 100% rename from backend/console/src/main/resources/plugins/bot-detect/README_EN.md rename to backend/sdk/src/main/resources/plugins/bot-detect/README_EN.md diff --git a/backend/console/src/main/resources/plugins/bot-detect/spec.yaml b/backend/sdk/src/main/resources/plugins/bot-detect/spec.yaml similarity index 100% rename from backend/console/src/main/resources/plugins/bot-detect/spec.yaml rename to backend/sdk/src/main/resources/plugins/bot-detect/spec.yaml diff --git a/backend/console/src/main/resources/plugins/custom-response/README.md b/backend/sdk/src/main/resources/plugins/custom-response/README.md similarity index 100% rename from backend/console/src/main/resources/plugins/custom-response/README.md rename to backend/sdk/src/main/resources/plugins/custom-response/README.md diff --git a/backend/console/src/main/resources/plugins/custom-response/README_EN.md b/backend/sdk/src/main/resources/plugins/custom-response/README_EN.md similarity index 100% rename from backend/console/src/main/resources/plugins/custom-response/README_EN.md rename to backend/sdk/src/main/resources/plugins/custom-response/README_EN.md diff --git a/backend/console/src/main/resources/plugins/custom-response/spec.yaml b/backend/sdk/src/main/resources/plugins/custom-response/spec.yaml similarity index 100% rename from backend/console/src/main/resources/plugins/custom-response/spec.yaml rename to backend/sdk/src/main/resources/plugins/custom-response/spec.yaml diff --git a/backend/console/src/main/resources/plugins/hmac-auth/README.md b/backend/sdk/src/main/resources/plugins/hmac-auth/README.md similarity index 100% rename from backend/console/src/main/resources/plugins/hmac-auth/README.md rename to backend/sdk/src/main/resources/plugins/hmac-auth/README.md diff --git a/backend/console/src/main/resources/plugins/hmac-auth/spec.yaml b/backend/sdk/src/main/resources/plugins/hmac-auth/spec.yaml similarity index 100% rename from backend/console/src/main/resources/plugins/hmac-auth/spec.yaml rename to backend/sdk/src/main/resources/plugins/hmac-auth/spec.yaml diff --git a/backend/console/src/main/resources/plugins/jwt-auth/README.md b/backend/sdk/src/main/resources/plugins/jwt-auth/README.md similarity index 100% rename from backend/console/src/main/resources/plugins/jwt-auth/README.md rename to backend/sdk/src/main/resources/plugins/jwt-auth/README.md diff --git a/backend/console/src/main/resources/plugins/jwt-auth/README_EN.md b/backend/sdk/src/main/resources/plugins/jwt-auth/README_EN.md similarity index 100% rename from backend/console/src/main/resources/plugins/jwt-auth/README_EN.md rename to backend/sdk/src/main/resources/plugins/jwt-auth/README_EN.md diff --git a/backend/console/src/main/resources/plugins/jwt-auth/spec.yaml b/backend/sdk/src/main/resources/plugins/jwt-auth/spec.yaml similarity index 100% rename from backend/console/src/main/resources/plugins/jwt-auth/spec.yaml rename to backend/sdk/src/main/resources/plugins/jwt-auth/spec.yaml diff --git a/backend/console/src/main/resources/plugins/key-auth/README.md b/backend/sdk/src/main/resources/plugins/key-auth/README.md similarity index 100% rename from backend/console/src/main/resources/plugins/key-auth/README.md rename to backend/sdk/src/main/resources/plugins/key-auth/README.md diff --git a/backend/console/src/main/resources/plugins/key-auth/spec.yaml b/backend/sdk/src/main/resources/plugins/key-auth/spec.yaml similarity index 100% rename from backend/console/src/main/resources/plugins/key-auth/spec.yaml rename to backend/sdk/src/main/resources/plugins/key-auth/spec.yaml diff --git a/backend/console/src/main/resources/plugins/key-rate-limit/README.md b/backend/sdk/src/main/resources/plugins/key-rate-limit/README.md similarity index 100% rename from backend/console/src/main/resources/plugins/key-rate-limit/README.md rename to backend/sdk/src/main/resources/plugins/key-rate-limit/README.md diff --git a/backend/console/src/main/resources/plugins/key-rate-limit/README_EN.md b/backend/sdk/src/main/resources/plugins/key-rate-limit/README_EN.md similarity index 100% rename from backend/console/src/main/resources/plugins/key-rate-limit/README_EN.md rename to backend/sdk/src/main/resources/plugins/key-rate-limit/README_EN.md diff --git a/backend/console/src/main/resources/plugins/key-rate-limit/spec.yaml b/backend/sdk/src/main/resources/plugins/key-rate-limit/spec.yaml similarity index 100% rename from backend/console/src/main/resources/plugins/key-rate-limit/spec.yaml rename to backend/sdk/src/main/resources/plugins/key-rate-limit/spec.yaml diff --git a/backend/console/src/main/resources/plugins/plugins.properties b/backend/sdk/src/main/resources/plugins/plugins.properties similarity index 100% rename from backend/console/src/main/resources/plugins/plugins.properties rename to backend/sdk/src/main/resources/plugins/plugins.properties diff --git a/backend/console/src/main/resources/plugins/request-block/README.md b/backend/sdk/src/main/resources/plugins/request-block/README.md similarity index 100% rename from backend/console/src/main/resources/plugins/request-block/README.md rename to backend/sdk/src/main/resources/plugins/request-block/README.md diff --git a/backend/console/src/main/resources/plugins/request-block/README_EN.md b/backend/sdk/src/main/resources/plugins/request-block/README_EN.md similarity index 100% rename from backend/console/src/main/resources/plugins/request-block/README_EN.md rename to backend/sdk/src/main/resources/plugins/request-block/README_EN.md diff --git a/backend/console/src/main/resources/plugins/request-block/spec.yaml b/backend/sdk/src/main/resources/plugins/request-block/spec.yaml similarity index 100% rename from backend/console/src/main/resources/plugins/request-block/spec.yaml rename to backend/sdk/src/main/resources/plugins/request-block/spec.yaml diff --git a/backend/console/src/main/resources/plugins/sni-misdirect/README.md b/backend/sdk/src/main/resources/plugins/sni-misdirect/README.md similarity index 100% rename from backend/console/src/main/resources/plugins/sni-misdirect/README.md rename to backend/sdk/src/main/resources/plugins/sni-misdirect/README.md diff --git a/backend/console/src/main/resources/plugins/sni-misdirect/spec.yaml b/backend/sdk/src/main/resources/plugins/sni-misdirect/spec.yaml similarity index 100% rename from backend/console/src/main/resources/plugins/sni-misdirect/spec.yaml rename to backend/sdk/src/main/resources/plugins/sni-misdirect/spec.yaml diff --git a/backend/console/src/main/resources/plugins/waf/README.md b/backend/sdk/src/main/resources/plugins/waf/README.md similarity index 100% rename from backend/console/src/main/resources/plugins/waf/README.md rename to backend/sdk/src/main/resources/plugins/waf/README.md diff --git a/backend/console/src/main/resources/plugins/waf/spec.yaml b/backend/sdk/src/main/resources/plugins/waf/spec.yaml similarity index 100% rename from backend/console/src/main/resources/plugins/waf/spec.yaml rename to backend/sdk/src/main/resources/plugins/waf/spec.yaml diff --git a/backend/sdk/src/test/java/com/alibaba/higress/sdk/demo/HelloHigressSdk.java b/backend/sdk/src/test/java/com/alibaba/higress/sdk/demo/HelloHigressSdk.java new file mode 100644 index 00000000..71c8a50e --- /dev/null +++ b/backend/sdk/src/test/java/com/alibaba/higress/sdk/demo/HelloHigressSdk.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022-2023 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.higress.sdk.demo; + +import java.io.IOException; +import java.util.List; + +import com.alibaba.higress.sdk.config.HigressServiceConfig; +import com.alibaba.higress.sdk.model.Route; +import com.alibaba.higress.sdk.model.RoutePageQuery; +import com.alibaba.higress.sdk.service.HigressServiceProvider; + +public class HelloHigressSdk { + + public static void main(String[] args) { + HigressServiceProvider provider; + try { + HigressServiceConfig.Builder builder = HigressServiceConfig.builder(); + provider = HigressServiceProvider.create(builder.build()); + } catch (IOException e) { + throw new RuntimeException("Unable to create HigressServiceProvider", e); + } + + List routes = provider.routeService().list(new RoutePageQuery()).getData(); + System.out.println(routes.size()); + for (Route route : routes) { + System.out.println(route.getName()); + } + + System.exit(0); + } +} diff --git a/backend/sdk/src/test/java/com/alibaba/higress/sdk/service/WasmPluginServiceTest.java b/backend/sdk/src/test/java/com/alibaba/higress/sdk/service/WasmPluginServiceTest.java index b1c216ff..7a940099 100644 --- a/backend/sdk/src/test/java/com/alibaba/higress/sdk/service/WasmPluginServiceTest.java +++ b/backend/sdk/src/test/java/com/alibaba/higress/sdk/service/WasmPluginServiceTest.java @@ -17,6 +17,7 @@ import java.util.Collections; +import com.alibaba.higress.sdk.service.kubernetes.KubernetesModelConverter; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -32,10 +33,10 @@ public class WasmPluginServiceTest { @BeforeEach public void setUp() throws Exception { - service = new WasmPluginServiceImpl(); KubernetesClientService kubernetesClientService = mock(KubernetesClientService.class); when(kubernetesClientService.listWasmPlugin()).thenReturn(Collections.emptyList()); - service.setKubernetesClientService(kubernetesClientService); + KubernetesModelConverter kubernetesModelConverter = mock(KubernetesModelConverter.class); + service = new WasmPluginServiceImpl(kubernetesClientService, kubernetesModelConverter); service.initialize(); } diff --git a/backend/sdk/src/test/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesClientServiceTest.java b/backend/sdk/src/test/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesClientServiceTest.java deleted file mode 100644 index 2ca8857d..00000000 --- a/backend/sdk/src/test/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesClientServiceTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2022-2023 Alibaba Group Holding Ltd. - * - * Licensed 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 com.alibaba.higress.sdk.service.kubernetes; - -import java.io.IOException; -import java.util.List; - -import org.junit.jupiter.api.Test; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.serializer.SerializerFeature; - -import io.kubernetes.client.openapi.models.V1Ingress; - -public class KubernetesClientServiceTest { - - @Test - public void checkControllerService() throws IOException { - KubernetesClientService kubernetesClientService = new KubernetesClientService(); - kubernetesClientService.init(); - System.out.println(kubernetesClientService.checkControllerService()); - } - - @Test - public void ingressTest() throws IOException { - KubernetesClientService kubernetesClientService = new KubernetesClientService(); - kubernetesClientService.init(); - List result = kubernetesClientService.listIngress(); - - System.out.println(JSON.toJSONString(result, SerializerFeature.PrettyFormat)); - } - -} diff --git a/backend/sdk/src/test/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesModelConverterTest.java b/backend/sdk/src/test/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesModelConverterTest.java index e17e40f4..16953a1f 100644 --- a/backend/sdk/src/test/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesModelConverterTest.java +++ b/backend/sdk/src/test/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesModelConverterTest.java @@ -15,6 +15,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.List; import org.junit.jupiter.api.AfterEach; @@ -40,13 +41,16 @@ import io.kubernetes.client.openapi.models.V1ObjectMeta; import io.kubernetes.client.openapi.models.V1TypedLocalObjectReference; +import static org.mockito.Mockito.mock; + public class KubernetesModelConverterTest { private KubernetesModelConverter converter; @BeforeEach public void setUp() { - converter = new KubernetesModelConverter(); + KubernetesClientService service = mock(KubernetesClientService.class); + converter = new KubernetesModelConverter(service); } @AfterEach @@ -577,6 +581,7 @@ private static Route buildBasicRoute() { route.setDomains(new ArrayList<>()); route.setPath(new RoutePredicate()); route.setCors(new CorsConfig()); + route.setCustomConfigs(new HashMap<>()); return route; } } diff --git a/backend/style/higress_checkstyle.xml b/backend/style/higress_checkstyle.xml index 97aafc5d..23c3223f 100644 --- a/backend/style/higress_checkstyle.xml +++ b/backend/style/higress_checkstyle.xml @@ -24,17 +24,17 @@ - + - + - +