Skip to content

Commit

Permalink
fix:rename k8sNamespace
Browse files Browse the repository at this point in the history
  • Loading branch information
dyx1234 committed Oct 23, 2024
1 parent c3fc604 commit 79ae56a
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class K8sConfigMapConfigRepository extends AbstractConfigRepository
private final String namespace;
private String configMapName;
private String configMapKey;
private final String configMapNamespace;
private final String k8sNamespace;
private final ConfigUtil configUtil;
private final KubernetesManager kubernetesManager;
private volatile Properties configMapProperties;
Expand All @@ -66,8 +66,7 @@ public K8sConfigMapConfigRepository(String namespace) {
public K8sConfigMapConfigRepository(String namespace, KubernetesManager kubernetesManager) {
this.namespace = namespace;
configUtil = ApolloInjector.getInstance(ConfigUtil.class);
kubernetesManager = ApolloInjector.getInstance(KubernetesManager.class);
configMapNamespace = configUtil.getConfigMapNamespace();
k8sNamespace = configUtil.getK8sNamespace();
this.kubernetesManager = kubernetesManager;

this.setConfigMapKey(configUtil.getCluster(), namespace);
Expand All @@ -79,16 +78,15 @@ public K8sConfigMapConfigRepository(String namespace, ConfigRepository upstream)
this.namespace = namespace;
configUtil = ApolloInjector.getInstance(ConfigUtil.class);
kubernetesManager = ApolloInjector.getInstance(KubernetesManager.class);
configMapNamespace = configUtil.getConfigMapNamespace();
k8sNamespace = configUtil.getK8sNamespace();

this.setConfigMapKey(configUtil.getCluster(), namespace);
this.setConfigMapName(configUtil.getAppId(), false);
this.setUpstreamRepository(upstream);
}

void setConfigMapKey(String cluster, String namespace) {
// TODO 兜底key怎么设计不会冲突(cluster初始化时已经设置了层级)
// cluster: 用户定义>idc>default,所以已经不需要额外层级设置了
// cluster: 用户定义>idc>default
if (StringUtils.isBlank(cluster)) {
configMapKey = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR).join("default", namespace);
return;
Expand All @@ -105,6 +103,7 @@ public String getConfigMapName() {
}

void setConfigMapName(String appId, boolean syncImmediately) {
//configMapName = ConfigConsts.APOLLO_CONFIG_CACHE + appId;
configMapName = appId;
// 初始化configmap
this.checkConfigMapName(configMapName);
Expand All @@ -117,14 +116,14 @@ private void checkConfigMapName(String configMapName) {
if (StringUtils.isBlank(configMapName)) {
throw new IllegalArgumentException("ConfigMap name cannot be null");
}
if (kubernetesManager.checkConfigMapExist(configMapNamespace, configMapName)) {
if (kubernetesManager.checkConfigMapExist(k8sNamespace, configMapName)) {
return;
}
// Create an empty configmap, write the new value in the update event
Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "createK8sConfigMap");
transaction.addData("configMapName", configMapName);
try {
kubernetesManager.createConfigMap(configMapNamespace, configMapName, null);
kubernetesManager.createConfigMap(k8sNamespace, configMapName, null);
transaction.setStatus(Transaction.SUCCESS);
} catch (Throwable ex) {
Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(ex));
Expand All @@ -135,20 +134,14 @@ private void checkConfigMapName(String configMapName) {
}
}

/**
* TODO 测试点:
* 1. 从上游成功恢复(开启文件存储)
* 2. 从上游成功恢复(没开启文件存储,从remote)
* 3. 从k8s成功恢复
*/
@Override
public Properties getConfig() {
if (configMapProperties == null) {
sync();
}
Properties result = propertiesFactory.getPropertiesInstance();
result.putAll(configMapProperties);
logger.info("configmap值:{}", configMapProperties);
logger.info("configmap properties: {}", configMapProperties);
return result;
}

Expand Down Expand Up @@ -214,11 +207,11 @@ public Properties loadFromK8sConfigMap() {
Preconditions.checkNotNull(configMapName, "ConfigMap name cannot be null");

try {
String jsonConfig = kubernetesManager.getValueFromConfigMap(configMapNamespace, configUtil.getAppId(), configMapKey);
String jsonConfig = kubernetesManager.getValueFromConfigMap(k8sNamespace, configMapName, configMapKey);
if (jsonConfig == null) {
// TODO 重试访问idc,default
String fallbackKey = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR).join(configUtil, namespace);
jsonConfig = kubernetesManager.getValueFromConfigMap(configMapNamespace, configMapName, fallbackKey);
// TODO 这样重试访问idc,default是否正确
String retryKey = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR).join("default", namespace);
jsonConfig = kubernetesManager.getValueFromConfigMap(k8sNamespace, configMapName, retryKey);
}

// Convert jsonConfig to properties
Expand All @@ -237,7 +230,7 @@ public Properties loadFromK8sConfigMap() {
}
}

private boolean trySyncFromUpstream() {
public boolean trySyncFromUpstream() {
if (upstream == null) {
return false;
}
Expand Down Expand Up @@ -282,7 +275,7 @@ public void onRepositoryChange(String namespace, Properties newProperties) {
public void persistConfigMap(Properties properties) {
Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "persistK8sConfigMap");
transaction.addData("configMapName", configUtil.getAppId());
transaction.addData("configMapNamespace", configUtil.getConfigMapNamespace());
transaction.addData("k8sNamespace", configUtil.getK8sNamespace());
try {
// Convert properties to a JSON string using Gson
Gson gson = new Gson();
Expand All @@ -291,7 +284,7 @@ public void persistConfigMap(Properties properties) {
data.put(configMapKey, jsonConfig);

// update configmap
kubernetesManager.updateConfigMap(configUtil.getConfigMapNamespace(), configUtil.getAppId(), data);
kubernetesManager.updateConfigMap(configUtil.getK8sNamespace(), configUtil.getAppId(), data);
transaction.setStatus(Transaction.SUCCESS);
} catch (Exception ex) {
ApolloConfigException exception =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.ctrip.framework.apollo.kubernetes;

import com.ctrip.framework.apollo.core.utils.StringUtils;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.CoreV1Api;
Expand All @@ -35,7 +36,7 @@ public class KubernetesManager {
private ApiClient client;
private CoreV1Api coreV1Api;

private final Logger log = LoggerFactory.getLogger(this.getClass());
private final Logger logger = LoggerFactory.getLogger(this.getClass());

public KubernetesManager() {
try {
Expand All @@ -44,7 +45,7 @@ public KubernetesManager() {
coreV1Api = new CoreV1Api(client);
} catch (Exception e) {
String errorMessage = "Failed to initialize Kubernetes client: " + e.getMessage();
log.error(errorMessage, e);
logger.error(errorMessage, e);
throw new RuntimeException(errorMessage, e);
}
}
Expand All @@ -68,62 +69,65 @@ public V1ConfigMap buildConfigMap(String name, String namespace, Map<String, Str
/**
* Creates a Kubernetes ConfigMap.
*
* @param configMapNamespace the namespace of the ConfigMap
* @param k8sNamespace the namespace of the ConfigMap
* @param name the name of the ConfigMap
* @param data the data to be stored in the ConfigMap
* @return the name of the created ConfigMap
* @throws RuntimeException if an error occurs while creating the ConfigMap
*/
public String createConfigMap(String configMapNamespace, String name, Map<String, String> data) {
if (configMapNamespace == null || configMapNamespace.isEmpty() || name == null || name.isEmpty()) {
log.error("create configmap failed due to null or empty parameter: configMapNamespace={}, name={}", configMapNamespace, name);
public String createConfigMap(String k8sNamespace, String name, Map<String, String> data) {
if (StringUtils.isEmpty(k8sNamespace) || StringUtils.isEmpty(name)) {
logger.error("create configmap failed due to null or empty parameter: k8sNamespace={}, name={}", k8sNamespace, name);
return null;
}
V1ConfigMap configMap = buildConfigMap(name, configMapNamespace, data);
V1ConfigMap configMap = buildConfigMap(name, k8sNamespace, data);
try {
coreV1Api.createNamespacedConfigMap(configMapNamespace, configMap, null, null, null, null);
log.info("ConfigMap created successfully: name: {}, namespace: {}", name, configMapNamespace);
coreV1Api.createNamespacedConfigMap(k8sNamespace, configMap, null, null, null, null);
logger.info("ConfigMap created successfully: name: {}, namespace: {}", name, k8sNamespace);
return name;
} catch (Exception e) {
logger.error("Failed to create ConfigMap: {}", e.getMessage(), e);
throw new RuntimeException("Failed to create ConfigMap: " + e.getMessage(), e);
}
}

/**
* get value from config map
*
* @param configMapNamespace configMapNamespace
* @param k8sNamespace k8sNamespace
* @param name config map name (appId)
* @param key config map key (cluster+namespace)
* @return value(json string)
*/
public String getValueFromConfigMap(String configMapNamespace, String name, String key) {
if (configMapNamespace == null || configMapNamespace.isEmpty() || name == null || name.isEmpty() || key == null || key.isEmpty()) {
log.error("Parameters can not be null or empty: configMapNamespace={}, name={}", configMapNamespace, name);
public String getValueFromConfigMap(String k8sNamespace, String name, String key) {
if (StringUtils.isEmpty(k8sNamespace) || StringUtils.isEmpty(name) || StringUtils.isEmpty(key)) {
logger.error("Parameters can not be null or empty: k8sNamespace={}, name={}", k8sNamespace, name);
return null;
}
try {
V1ConfigMap configMap = coreV1Api.readNamespacedConfigMap(name, configMapNamespace, null);
V1ConfigMap configMap = coreV1Api.readNamespacedConfigMap(name, k8sNamespace, null);
if (!Objects.requireNonNull(configMap.getData()).containsKey(key)) {
throw new RuntimeException(String.format("Specified key not found in ConfigMap: %s, configMapNamespace: %s, name: %s", name, configMapNamespace, name));
logger.error("Specified key not found in ConfigMap: {}, k8sNamespace: {}, name: {}", name, k8sNamespace, name);
}
return configMap.getData().get(key);
} catch (Exception e) {
logger.error("Error occurred while getting value from ConfigMap: {}", e.getMessage(), e);
return null;
}
}

/**
* update config map
*
* @param configMapNamespace configmap namespace
* @param k8sNamespace configmap namespace
* @param name config map name (appId)
* @param data new data
* @return config map name
*/
// TODO 使用client自带的retry机制,设置重试次数,CAS
public boolean updateConfigMap(String configMapNamespace, String name, Map<String, String> data) {
if (configMapNamespace == null || configMapNamespace.isEmpty() || name == null || name.isEmpty() || data == null || data.isEmpty()) {
log.error("Parameters can not be null or empty: configMapNamespace={}, name={}", configMapNamespace, name);
public boolean updateConfigMap(String k8sNamespace, String name, Map<String, String> data) {
if (StringUtils.isEmpty(k8sNamespace) || StringUtils.isEmpty(name)) {
logger.error("Parameters can not be null or empty: k8sNamespace={}, name={}", k8sNamespace, name);
return false;
}

Expand All @@ -134,46 +138,47 @@ public boolean updateConfigMap(String configMapNamespace, String name, Map<Strin

while (retryCount < maxRetries) {
try {
V1ConfigMap configmap = coreV1Api.readNamespacedConfigMap(name, configMapNamespace, null);
V1ConfigMap configmap = coreV1Api.readNamespacedConfigMap(name, k8sNamespace, null);
configmap.setData(data);
coreV1Api.replaceNamespacedConfigMap(name, configMapNamespace, configmap, null, null, null, null);
coreV1Api.replaceNamespacedConfigMap(name, k8sNamespace, configmap, null, null, null, null);
return true;
} catch (ApiException e) {
if (e.getCode() == 409) {
retryCount++;
log.warn("Conflict occurred, retrying... (" + retryCount + ")");
logger.warn("Conflict occurred, retrying... ({})", retryCount);
try {
TimeUnit.MILLISECONDS.sleep(waitTime);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
waitTime = Math.min(waitTime * 2, 1000);
} else {
System.err.println("Error updating ConfigMap: " + e.getMessage());
logger.error("Error updating ConfigMap: {}", e.getMessage(), e);
}
}
}
return retryCount < maxRetries;
return false;
}

/**
* check config map exist
*
* @param configMapNamespace config map namespace
* @param k8sNamespace config map namespace
* @param configMapName config map name
* @return true if config map exist, false otherwise
*/
public boolean checkConfigMapExist(String configMapNamespace, String configMapName) {
if (configMapNamespace == null || configMapNamespace.isEmpty() || configMapName == null || configMapName.isEmpty()) {
log.error("Parameters can not be null or empty: configMapNamespace={}, configMapName={}", configMapNamespace, configMapName);
public boolean checkConfigMapExist(String k8sNamespace, String configMapName) {
if (StringUtils.isEmpty(k8sNamespace) || StringUtils.isEmpty(configMapName)) {
logger.error("Parameters can not be null or empty: k8sNamespace={}, configMapName={}", k8sNamespace, configMapName);
return false;
}
try {
log.info("Check whether ConfigMap exists, configMapName: {}", configMapName);
coreV1Api.readNamespacedConfigMap(configMapName, configMapNamespace, null);
logger.info("Check whether ConfigMap exists, configMapName: {}", configMapName);
coreV1Api.readNamespacedConfigMap(configMapName, k8sNamespace, null);
return true;
} catch (Exception e) {
// configmap not exist
logger.error("Error checking ConfigMap existence: {}", e.getMessage(), e);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class ConfigUtil {
private boolean propertyNamesCacheEnabled = false;
private boolean propertyFileCacheEnabled = true;
private boolean overrideSystemProperties = true;
private boolean PropertyKubernetesCacheEnabled = false;
private boolean propertyKubernetesCacheEnabled = false;
private boolean clientMonitorEnabled = false;
private boolean clientMonitorJmxEnabled = false;
private String monitorExternalType = "NONE";
Expand Down Expand Up @@ -378,32 +378,32 @@ private String getDeprecatedCustomizedCacheRoot() {
return cacheRoot;
}

public String getConfigMapNamespace() {
String configMapNamespace = getCustomizedConfigMapNamespace();
public String getK8sNamespace() {
String k8sNamespace = getCustomizedConfigMapNamespace();

if (!Strings.isNullOrEmpty(configMapNamespace)) {
return configMapNamespace;
if (!Strings.isNullOrEmpty(k8sNamespace)) {
return k8sNamespace;
}

return ConfigConsts.KUBERNETES_CACHE_CONFIG_MAP_NAMESPACE_DEFAULT;
}

private String getCustomizedConfigMapNamespace() {
// 1. Get from System Property
String configMapNamespace = System.getProperty(ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_CONFIGMAP_NAMESPACE);
if (Strings.isNullOrEmpty(configMapNamespace)) {
String k8sNamespace = System.getProperty(ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_NAMESPACE);
if (Strings.isNullOrEmpty(k8sNamespace)) {
// 2. Get from OS environment variable
configMapNamespace = System.getenv(ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_CONFIGMAP_NAMESPACE_ENVIRONMENT_VARIABLES);
k8sNamespace = System.getenv(ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_NAMESPACE_ENVIRONMENT_VARIABLES);
}
if (Strings.isNullOrEmpty(configMapNamespace)) {
if (Strings.isNullOrEmpty(k8sNamespace)) {
// 3. Get from server.properties
configMapNamespace = Foundation.server().getProperty(ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_CONFIGMAP_NAMESPACE, null);
k8sNamespace = Foundation.server().getProperty(ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_NAMESPACE, null);
}
if (Strings.isNullOrEmpty(configMapNamespace)) {
if (Strings.isNullOrEmpty(k8sNamespace)) {
// 4. Get from app.properties
configMapNamespace = Foundation.app().getProperty(ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_CONFIGMAP_NAMESPACE, null);
k8sNamespace = Foundation.app().getProperty(ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_NAMESPACE, null);
}
return configMapNamespace;
return k8sNamespace;
}

public boolean isInLocalMode() {
Expand Down Expand Up @@ -510,7 +510,7 @@ public boolean isPropertyFileCacheEnabled() {
}

public boolean isPropertyKubernetesCacheEnabled() {
return PropertyKubernetesCacheEnabled;
return propertyKubernetesCacheEnabled;
}

public boolean isOverrideSystemProperties() {
Expand All @@ -536,9 +536,9 @@ private void initOverrideSystemProperties() {
}

private void initPropertyKubernetesCacheEnabled() {
PropertyKubernetesCacheEnabled = getPropertyBoolean(ApolloClientSystemConsts.APOLLO_KUBERNETES_CACHE_ENABLE,
propertyKubernetesCacheEnabled = getPropertyBoolean(ApolloClientSystemConsts.APOLLO_KUBERNETES_CACHE_ENABLE,
ApolloClientSystemConsts.APOLLO_KUBERNETES_CACHE_ENABLE_ENVIRONMENT_VARIABLES,
PropertyKubernetesCacheEnabled);
propertyKubernetesCacheEnabled);
}

private void initClientMonitorExternalType() {
Expand Down
Loading

0 comments on commit 79ae56a

Please sign in to comment.