Skip to content

Commit

Permalink
feat: Refactor the configuration mechanism used by SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
CH3CHO committed Jan 20, 2024
1 parent ba52a7b commit 0d67d5e
Show file tree
Hide file tree
Showing 20 changed files with 550 additions and 255 deletions.
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
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);
}
}
}

This file was deleted.

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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
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;
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit 0d67d5e

Please sign in to comment.