-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactor the configuration mechanism used by SDK
- Loading branch information
Showing
20 changed files
with
550 additions
and
255 deletions.
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
backend/console/src/main/java/com/alibaba/higress/console/config/SdkConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* 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.Builder builder = KubernetesClientService.isInCluster() | ||
? HigressServiceConfig.fromInClusterConfig() : HigressServiceConfig.fromKubeConfig(kubeConfig); | ||
builder.withIngressClassName(controllerIngressClassName).withControllerNamespace(controllerNamespace) | ||
.withControllerServiceName(controllerServiceName).withControllerServiceHost(controllerServiceHost) | ||
.withControllerServicePort(controllerServicePort).withControllerJwtPolicy(controllerJwtPolicy) | ||
.withControllerAccessToken(controllerAccessToken); | ||
serviceProvider = HigressServiceProvider.create(builder.build()); | ||
} | ||
|
||
@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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
backend/sdk/src/main/java/com/alibaba/higress/sdk/config/HigressServiceConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* 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 fromInClusterConfig() { | ||
return new Builder(); | ||
} | ||
|
||
public static HigressServiceConfig.Builder fromKubeConfig(String filePath) { | ||
Builder builder = new Builder(); | ||
if (StringUtils.isNotEmpty(filePath)) { | ||
builder.withKubeConfigPath(filePath); | ||
} | ||
return 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() {} | ||
|
||
private 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); | ||
} | ||
} | ||
} |
37 changes: 0 additions & 37 deletions
37
backend/sdk/src/main/java/com/alibaba/higress/sdk/constant/ConfigKey.java
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
backend/sdk/src/main/java/com/alibaba/higress/sdk/constant/HigressConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.