Skip to content

Commit

Permalink
support sofa registry kubernetes
Browse files Browse the repository at this point in the history
  • Loading branch information
呈铭 committed Feb 4, 2024
1 parent 038e2b7 commit f6a8609
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,36 @@ public String getParameter(String key) {
return parameters == null ? null : parameters.get(key);
}

/**
* Gets parameter or default.
*
* @param key the key
* @return the value
*/
public String getParameter(String key, String defaultValue) {
return parameters == null ? defaultValue : parameters.get(key);
}

/**
* Gets parameter or default.
*
* @param key the key
* @return the value
*/
public int getParameter(String key, int defaultValue) {
return parameters == null ? defaultValue : Integer.parseInt(parameters.get(key));
}

/**
* Gets parameter or default.
*
* @param key the key
* @return the value
*/
public boolean getParameter(String key, boolean defaultValue) {
return parameters == null ? defaultValue : Boolean.parseBoolean(parameters.get(key));
}

@Override
public String toString() {
return "RegistryConfig{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void register(ProviderConfig config) {
Pod curPod = podResource.edit(pod -> new PodBuilder(pod).editOrNewMetadata()
.removeFromAnnotations(ANNOTATION_KEY)
.removeFromLabels(LABEL_KEY)
// sava provider info to k8s
// sava provider config to k8s pod
.addToAnnotations(ANNOTATION_KEY, JSON.toJSONString(config))
// add labels on service provider and have the service consumer subscribe based on the labels for selection
.addToLabels(LABEL_KEY, config.getInterfaceId()).endMetadata().build());
Expand Down Expand Up @@ -169,7 +169,7 @@ public List<ProviderGroup> subscribe(ConsumerConfig config) {
FilterWatchListDeletable<Pod, PodList, PodResource> podPodListPodResourceFilterWatchListDeletable =
kubernetesClient.pods()
.inNamespace(namespace)
.withLabelIn(LABEL_KEY);
.withLabel(LABEL_KEY);

SharedIndexInformer<Pod> inform = podPodListPodResourceFilterWatchListDeletable.inform(new ResourceEventHandler<Pod>() {
@Override
Expand Down Expand Up @@ -251,7 +251,7 @@ public void init() {
private List<Pod> getPods() {
return kubernetesClient.pods()
.inNamespace(namespace)
.withLabelIn(LABEL_KEY)
.withLabel(LABEL_KEY)
.list()
.getItems();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static List<ProviderInfo> convertPodsToProviders(List<Pod> pods) {
}

providerConfig.getServer().forEach(serverConfig -> {
String url = convertInstanceToUrl((ServerConfig) serverConfig, providerConfig);
String url = convertInstanceToUrl(pod, (ServerConfig) serverConfig, providerConfig);
ProviderInfo providerInfo = ProviderHelper.toProviderInfo(url);
providerInfos.add(providerInfo);
});
Expand All @@ -57,13 +57,13 @@ public static List<ProviderInfo> convertPodsToProviders(List<Pod> pods) {
return providerInfos;
}

private static String convertInstanceToUrl(ServerConfig serverConfig, ProviderConfig providerConfig) {
private static String convertInstanceToUrl(Pod pod, ServerConfig serverConfig, ProviderConfig providerConfig) {
String uri = "";
String protocol = serverConfig.getProtocol();
if (StringUtils.isNotEmpty(protocol)) {
uri = protocol + "://";
}
uri += serverConfig.getHost() + ":" + serverConfig.getPort();
uri += pod.getStatus().getPodIP() + ":" + serverConfig.getPort();

Map<String, String> metaData = RegistryUtils.convertProviderToMap(providerConfig, serverConfig);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,62 @@

public class KubernetesClientConstants {

public static final String DEFAULT_MASTER_URL = "https://kubernetes.default.svc";
public static final String DEFAULT_MASTER_URL = "https://kubernetes.default.svc";

public static final String LABEL_KEY = "io.sofa.rpc/label";
public static final String LABEL_KEY = "io.sofa.rpc/label";

public static final String ANNOTATION_KEY = "io.sofa.rpc/annotation";
public static final String ANNOTATION_KEY = "io.sofa.rpc/annotation";

public static final String TRUST_CERTS = "trustCerts";

public static final String USE_HTTPS = "useHttps";

public static final String HTTP2_DISABLE = "http2Disable";

public static final String NAMESPACE = "namespace";

public static final String API_VERSION = "apiVersion";

public static final String CA_CERT_FILE = "caCertFile";

public static final String CA_CERT_DATA = "caCertData";

public static final String CLIENT_CERT_FILE = "clientCertFile";

public static final String CLIENT_CERT_DATA = "clientCertData";

public static final String CLIENT_KEY_FILE = "clientKeyFile";

public static final String CLIENT_KEY_DATA = "clientKeyData";

public static final String CLIENT_KEY_ALGO = "clientKeyAlgo";

public static final String CLIENT_KEY_PASSPHRASE = "clientKeyPassphrase";

public static final String OAUTH_TOKEN = "oauthToken";

public static final String USERNAME = "username";

public static final String PASSWORD = "password";

public static final String WATCH_RECONNECT_INTERVAL = "watchReconnectInterval";

public static final String WATCH_RECONNECT_LIMIT = "watchReconnectLimit";

public static final String CONNECTION_TIMEOUT = "connectionTimeout";

public static final String REQUEST_TIMEOUT = "requestTimeout";

public static final String ROLLING_TIMEOUT = "rollingTimeout";

public static final String LOGGING_INTERVAL = "loggingInterval";

public static final String HTTP_PROXY = "httpProxy";

public static final String HTTPS_PROXY = "httpsProxy";

public static final String PROXY_USERNAME = "proxyUsername";

public static final String PROXY_PASSWORD = "proxyPassword";

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,32 @@

import java.util.Base64;

import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.API_VERSION;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.CA_CERT_DATA;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.CA_CERT_FILE;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.CLIENT_CERT_DATA;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.CLIENT_CERT_FILE;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.CLIENT_KEY_ALGO;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.CLIENT_KEY_DATA;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.CLIENT_KEY_FILE;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.CLIENT_KEY_PASSPHRASE;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.CONNECTION_TIMEOUT;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.DEFAULT_MASTER_URL;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.HTTP2_DISABLE;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.HTTPS_PROXY;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.HTTP_PROXY;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.LOGGING_INTERVAL;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.NAMESPACE;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.OAUTH_TOKEN;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.PASSWORD;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.PROXY_PASSWORD;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.PROXY_USERNAME;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.REQUEST_TIMEOUT;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.TRUST_CERTS;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.USERNAME;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.USE_HTTPS;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.WATCH_RECONNECT_INTERVAL;
import static com.alipay.sofa.rpc.registry.kubernetes.constant.KubernetesClientConstants.WATCH_RECONNECT_LIMIT;

public class KubernetesConfigUtils {

Expand All @@ -32,42 +57,41 @@ public static Config buildKubernetesConfig(RegistryConfig registryConfig) {
// Init default config
Config base = Config.autoConfigure(null);

// TODO should be set by user config
return new ConfigBuilder(base)
.withMasterUrl(buildMasterUrl(registryConfig))
.withApiVersion(base.getApiVersion())
.withNamespace(base.getNamespace())
.withUsername(base.getUsername())
.withPassword(base.getPassword())
.withOauthToken(base.getOauthToken())
.withCaCertFile(base.getCaCertFile())
.withCaCertData(decodeBase64(base.getCaCertData()))
.withClientKeyFile(base.getClientKeyFile())
.withClientKeyData(decodeBase64(base.getClientKeyData()))
.withClientCertFile(base.getClientCertFile())
.withClientCertData(decodeBase64(base.getClientCertData()))
.withClientKeyAlgo(base.getClientKeyAlgo())
.withClientKeyPassphrase(base.getClientKeyPassphrase())
.withConnectionTimeout(base.getConnectionTimeout())
.withRequestTimeout(base.getRequestTimeout())
.withWatchReconnectInterval(base.getWatchReconnectInterval())
.withWatchReconnectLimit(base.getWatchReconnectLimit())
.withLoggingInterval(base.getLoggingInterval())
.withTrustCerts(base.isTrustCerts())
.withHttp2Disable(base.isHttp2Disable())
.withHttpProxy(base.getHttpProxy())
.withHttpsProxy(base.getHttpsProxy())
.withProxyUsername(base.getProxyUsername())
.withProxyPassword(base.getProxyPassword())
.withNoProxy(base.getNoProxy())
.withApiVersion(registryConfig.getParameter(API_VERSION, base.getApiVersion()))
.withNamespace(registryConfig.getParameter(NAMESPACE, base.getNamespace()))
.withUsername(registryConfig.getParameter(USERNAME, base.getUsername()))
.withPassword(registryConfig.getParameter(PASSWORD, base.getPassword()))
.withOauthToken(registryConfig.getParameter(OAUTH_TOKEN, base.getOauthToken()))
.withCaCertFile(registryConfig.getParameter(CA_CERT_FILE, base.getCaCertFile()))
.withCaCertData(registryConfig.getParameter(CA_CERT_DATA, decodeBase64(base.getCaCertData())))
.withClientKeyFile(registryConfig.getParameter(CLIENT_KEY_FILE, base.getClientKeyFile()))
.withClientKeyData(registryConfig.getParameter(CLIENT_KEY_DATA, decodeBase64(base.getClientKeyData())))
.withClientCertFile(registryConfig.getParameter(CLIENT_CERT_FILE, base.getClientCertFile()))
.withClientCertData(registryConfig.getParameter(CLIENT_CERT_DATA, decodeBase64(base.getClientCertData())))
.withClientKeyAlgo(registryConfig.getParameter(CLIENT_KEY_ALGO, base.getClientKeyAlgo()))
.withClientKeyPassphrase(registryConfig.getParameter(CLIENT_KEY_PASSPHRASE, base.getClientKeyPassphrase()))
.withConnectionTimeout(registryConfig.getParameter(CONNECTION_TIMEOUT, base.getConnectionTimeout()))
.withRequestTimeout(registryConfig.getParameter(REQUEST_TIMEOUT, base.getRequestTimeout()))
.withWatchReconnectInterval(
registryConfig.getParameter(WATCH_RECONNECT_INTERVAL, base.getWatchReconnectInterval()))
.withWatchReconnectLimit(registryConfig.getParameter(WATCH_RECONNECT_LIMIT, base.getWatchReconnectLimit()))
.withLoggingInterval(registryConfig.getParameter(LOGGING_INTERVAL, base.getLoggingInterval()))
.withTrustCerts(registryConfig.getParameter(TRUST_CERTS, base.isTrustCerts()))
.withHttp2Disable(registryConfig.getParameter(HTTP2_DISABLE, base.isHttp2Disable()))
.withHttpProxy(registryConfig.getParameter(HTTP_PROXY, base.getHttpProxy()))
.withHttpsProxy(registryConfig.getParameter(HTTPS_PROXY, base.getHttpsProxy()))
.withProxyUsername(registryConfig.getParameter(PROXY_USERNAME, base.getProxyUsername()))
.withProxyPassword(registryConfig.getParameter(PROXY_PASSWORD, base.getProxyPassword()))
.build();
}

private static String buildMasterUrl(RegistryConfig registryConfig) {
String address = registryConfig.getAddress();
// TODO maybe use Config property
if (StringUtils.isNotBlank(address)) {
return "http://" + address;
return (Boolean.parseBoolean(registryConfig.getParameter(USE_HTTPS, "false")) ? "https://" : "http://") +
address;
}
return DEFAULT_MASTER_URL;
}
Expand Down

0 comments on commit f6a8609

Please sign in to comment.