From e677d884627afde08cd6e5cc96c49f52f19e646a Mon Sep 17 00:00:00 2001 From: 24kpure <1033237526@qq.com> Date: Tue, 24 Dec 2024 18:00:45 +0800 Subject: [PATCH] changes for code review --- CHANGES.md | 1 + .../apollo/kubernetes/KubernetesManager.java | 14 ++++++++++++-- .../additional-spring-configuration-metadata.json | 7 +++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a8e204cf..4c398433 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ Apollo Java 2.4.0 * [Feature Support Kubernetes ConfigMap cache for Apollo java, golang client](https://github.com/apolloconfig/apollo-java/pull/79) * [Feature support pulling configuration information from multiple AppIds](https://github.com/apolloconfig/apollo-java/pull/70) * [Fix monitor arg cause npe](https://github.com/apolloconfig/apollo-java/pull/86) +* [Feature reduce conflicts when update configmap in k8](https://github.com/apolloconfig/apollo-java/pull/93) ------------------ All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/4?closed=1) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java index 76824e08..14f87bab 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java @@ -40,10 +40,20 @@ import java.util.Objects; import java.util.concurrent.TimeUnit; +/** + * Manages Kubernetes ConfigMap operations. + * Required Kubernetes permissions: + * - pods: [get, list] - For pod selection and write eligibility + * - configmaps: [get, create, update] - For ConfigMap operations + */ @Service public class KubernetesManager { private static final Logger logger = LoggerFactory.getLogger(KubernetesManager.class); + private static final String RUNNING_POD_FIELD_SELECTOR = "status.phase=Running"; + + private static final int MAX_SEARCH_NUM = 1000; + private ApiClient client; private CoreV1Api coreV1Api; private int propertyKubernetesMaxWritePods; @@ -244,8 +254,8 @@ private boolean isWritePod(String k8sNamespace) { String labelSelector = "app=" + appName; V1PodList v1PodList = coreV1Api.listNamespacedPod(k8sNamespace, null, null, - null, null, labelSelector, - null, null, null + null, RUNNING_POD_FIELD_SELECTOR, labelSelector, + MAX_SEARCH_NUM, null, null , null, null); return v1PodList.getItems().stream() diff --git a/apollo-client/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/apollo-client/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 1a268b52..5b425ee0 100644 --- a/apollo-client/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/apollo-client/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -126,6 +126,13 @@ "description": "kubernetes configmap namespace.", "defaultValue": "default" }, + { + "name": "apollo.cache.kubernetes.max-write-pods", + "type": "java.lang.String", + "sourceType": "com.ctrip.framework.apollo.util.ConfigUtil", + "description": "max number of pods that can write the configmap cache in Kubernetes.", + "defaultValue": "3" + }, { "name": "apollo.property.order.enable", "type": "java.lang.Boolean",