Skip to content

Commit 0ce4201

Browse files
committed
changes for code review
1 parent 1cc173e commit 0ce4201

File tree

5 files changed

+28
-51
lines changed

5 files changed

+28
-51
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Apollo Java 2.4.0
1414
* [Fix monitor arg cause npe](https://github.com/apolloconfig/apollo-java/pull/86)
1515
* [Fix the concurrent issue in SpringValueRegistry.scanAndClean](https://github.com/apolloconfig/apollo-java/pull/95)
1616
* [Feature support incremental configuration synchronization client](https://github.com/apolloconfig/apollo-java/pull/90)
17+
* [Feature reduce conflicts when update configmap in k8](https://github.com/apolloconfig/apollo-java/pull/93)
1718

1819
------------------
1920
All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/4?closed=1)

apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
*/
1717
package com.ctrip.framework.apollo.kubernetes;
1818

19-
import com.ctrip.framework.apollo.build.ApolloInjector;
2019
import com.ctrip.framework.apollo.core.utils.StringUtils;
2120
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
22-
import com.ctrip.framework.apollo.util.ConfigUtil;
2321
import com.google.common.annotations.VisibleForTesting;
2422
import com.google.common.base.Strings;
2523
import io.kubernetes.client.openapi.ApiClient;
@@ -40,21 +38,29 @@
4038
import java.util.Objects;
4139
import java.util.concurrent.TimeUnit;
4240

41+
/**
42+
* Manages Kubernetes ConfigMap operations.
43+
* Required Kubernetes permissions:
44+
* - pods: [get, list] - For pod selection and write eligibility
45+
* - configmaps: [get, create, update] - For ConfigMap operations
46+
*/
4347
@Service
4448
public class KubernetesManager {
4549
private static final Logger logger = LoggerFactory.getLogger(KubernetesManager.class);
4650

51+
private static final String RUNNING_POD_FIELD_SELECTOR = "status.phase=Running";
52+
53+
private static final int MAX_SEARCH_NUM = 100;
54+
4755
private ApiClient client;
4856
private CoreV1Api coreV1Api;
49-
private int propertyKubernetesMaxWritePods;
57+
private int propertyKubernetesMaxWritePods = 3;
5058
private String localPodName = System.getenv("HOSTNAME");
5159

5260
public KubernetesManager() {
5361
try {
5462
client = Config.defaultClient();
5563
coreV1Api = new CoreV1Api(client);
56-
ConfigUtil configUtil = ApolloInjector.getInstance(ConfigUtil.class);
57-
propertyKubernetesMaxWritePods = configUtil.getPropertyKubernetesMaxWritePods();
5864
} catch (Exception e) {
5965
String errorMessage = "Failed to initialize Kubernetes client: " + e.getMessage();
6066
logger.error(errorMessage, e);
@@ -244,8 +250,8 @@ private boolean isWritePod(String k8sNamespace) {
244250
String labelSelector = "app=" + appName;
245251

246252
V1PodList v1PodList = coreV1Api.listNamespacedPod(k8sNamespace, null, null,
247-
null, null, labelSelector,
248-
null, null, null
253+
null, RUNNING_POD_FIELD_SELECTOR, labelSelector,
254+
MAX_SEARCH_NUM, null, null
249255
, null, null);
250256

251257
return v1PodList.getItems().stream()

apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,12 @@ public class ConfigUtil {
7272
private boolean propertyFileCacheEnabled = true;
7373
private boolean overrideSystemProperties = true;
7474
private boolean propertyKubernetesCacheEnabled = false;
75-
private int propertyKubernetesMaxWritePods = 3;
7675
private boolean clientMonitorEnabled = false;
7776
private boolean clientMonitorJmxEnabled = false;
7877
private String monitorExternalType = "";
7978
private long monitorExternalExportPeriod = 10;
8079
private int monitorExceptionQueueSize = 25;
8180

82-
8381
public ConfigUtil() {
8482
warnLogRateLimiter = RateLimiter.create(0.017); // 1 warning log output per minute
8583
initRefreshInterval();
@@ -95,7 +93,6 @@ public ConfigUtil() {
9593
initPropertyFileCacheEnabled();
9694
initOverrideSystemProperties();
9795
initPropertyKubernetesCacheEnabled();
98-
initPropertyKubernetesMaxWritePods();
9996
initClientMonitorEnabled();
10097
initClientMonitorJmxEnabled();
10198
initClientMonitorExternalType();
@@ -393,44 +390,31 @@ private String getDeprecatedCustomizedCacheRoot() {
393390
}
394391

395392
public String getK8sNamespace() {
396-
return getK8sConfigProperties(ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_NAMESPACE,
397-
ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_NAMESPACE_ENVIRONMENT_VARIABLES,
398-
ConfigConsts.KUBERNETES_CACHE_CONFIG_MAP_NAMESPACE_DEFAULT);
399-
}
393+
String k8sNamespace = getCacheKubernetesNamespace();
400394

401-
private void initPropertyKubernetesMaxWritePods() {
402-
String propertyKubernetesMaxWritePodsStr = getK8sConfigProperties(ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_MAX_WRITE_PODS,
403-
ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_MAX_WRITE_PODS_ENVIRONMENT_VARIABLES,
404-
String.valueOf(propertyKubernetesMaxWritePods));
405-
if (!Strings.isNullOrEmpty(propertyKubernetesMaxWritePodsStr)) {
406-
try {
407-
propertyKubernetesMaxWritePods = Integer.parseInt(propertyKubernetesMaxWritePodsStr);
408-
} catch (Throwable ex) {
409-
logger.error("Config for {} is invalid: {}",
410-
ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_NAMESPACE, propertyKubernetesMaxWritePodsStr);
411-
}
395+
if (!Strings.isNullOrEmpty(k8sNamespace)) {
396+
return k8sNamespace;
412397
}
398+
399+
return ConfigConsts.KUBERNETES_CACHE_CONFIG_MAP_NAMESPACE_DEFAULT;
413400
}
414401

415-
private String getK8sConfigProperties(String key, String environmentKey, String defaultValue) {
402+
private String getCacheKubernetesNamespace() {
416403
// 1. Get from System Property
417-
String k8sNamespace = System.getProperty(key);
404+
String k8sNamespace = System.getProperty(ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_NAMESPACE);
418405
if (Strings.isNullOrEmpty(k8sNamespace)) {
419406
// 2. Get from OS environment variable
420-
k8sNamespace = System.getenv(environmentKey);
407+
k8sNamespace = System.getenv(ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_NAMESPACE_ENVIRONMENT_VARIABLES);
421408
}
422409
if (Strings.isNullOrEmpty(k8sNamespace)) {
423410
// 3. Get from server.properties
424-
k8sNamespace = Foundation.server().getProperty(key, null);
411+
k8sNamespace = Foundation.server().getProperty(ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_NAMESPACE, null);
425412
}
426413
if (Strings.isNullOrEmpty(k8sNamespace)) {
427414
// 4. Get from app.properties
428-
k8sNamespace = Foundation.app().getProperty(key, null);
429-
}
430-
if (!Strings.isNullOrEmpty(k8sNamespace)) {
431-
return k8sNamespace;
415+
k8sNamespace = Foundation.app().getProperty(ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_NAMESPACE, null);
432416
}
433-
return defaultValue;
417+
return k8sNamespace;
434418
}
435419

436420
public boolean isInLocalMode() {
@@ -540,10 +524,6 @@ public boolean isPropertyKubernetesCacheEnabled() {
540524
return propertyKubernetesCacheEnabled;
541525
}
542526

543-
public int getPropertyKubernetesMaxWritePods() {
544-
return propertyKubernetesMaxWritePods;
545-
}
546-
547527
public boolean isOverrideSystemProperties() {
548528
return overrideSystemProperties;
549529
}
@@ -637,7 +617,7 @@ private void initClientMonitorExceptionQueueSize() {
637617
public int getMonitorExceptionQueueSize() {
638618
return monitorExceptionQueueSize;
639619
}
640-
620+
641621
private boolean getPropertyBoolean(String propertyName, String envName, boolean defaultVal) {
642622
String enablePropertyNamesCache = System.getProperty(propertyName);
643623
if (Strings.isNullOrEmpty(enablePropertyNamesCache)) {

apollo-client/src/test/java/com/ctrip/framework/apollo/kubernetes/KubernetesManagerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ public void testUpdateConfigMapSuccess() throws Exception {
182182
// assert
183183
assertTrue(success);
184184
Mockito.verify(coreV1Api, Mockito.times(1)).listNamespacedPod(namespace, null, null,
185-
null, null, "app=app",
186-
null, null, null
185+
null, "status.phase=Running", "app=app",
186+
100, null, null
187187
, null, null);
188188
}
189189

apollo-core/src/main/java/com/ctrip/framework/apollo/core/ApolloClientSystemConsts.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,6 @@ public class ApolloClientSystemConsts {
8383
*/
8484
public static final String APOLLO_CACHE_KUBERNETES_NAMESPACE_ENVIRONMENT_VARIABLES = "APOLLO_CACHE_KUBERNETES_NAMESPACE";
8585

86-
/**
87-
* max number of pods that can write the configmap cache in Kubernetes
88-
*/
89-
public static final String APOLLO_CACHE_KUBERNETES_MAX_WRITE_PODS = "apollo.cache.kubernetes.max-write-pods";
90-
91-
/**
92-
* max number of pods that can write the configmap cache in Kubernetes environment variables
93-
*/
94-
public static final String APOLLO_CACHE_KUBERNETES_MAX_WRITE_PODS_ENVIRONMENT_VARIABLES = "APOLLO_CACHE_KUBERNETES_MAX_WRITE_PODS";
95-
9686
/**
9787
* apollo client access key
9888
*/

0 commit comments

Comments
 (0)