-
-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP]feat: reduce conflicts when update configmap in k8s #89 #93
base: main
Are you sure you want to change the base?
[WIP]feat: reduce conflicts when update configmap in k8s #89 #93
Conversation
WalkthroughThe changes in this pull request enhance the Changes
Assessment against linked issues
Possibly related PRs
Suggested reviewers
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
@nobodyiam @shoothzj Clould you review the idea of code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 6
🧹 Outside diff range and nitpick comments (2)
apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java (1)
48-48
: Consider makingpropertyKubernetesMaxWritePods
finalTo ensure immutability after initialization, consider declaring
propertyKubernetesMaxWritePods
asfinal
.Apply this diff:
-private int propertyKubernetesMaxWritePods; +private final int propertyKubernetesMaxWritePods;apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java (1)
417-433
: Variable naming ingetK8sConfigProperties
methodThe variable
k8sNamespace
is used to store generic property values, which may not always represent a namespace. To improve code readability and maintainability, consider renaming the variable to a more generic name likepropertyValue
.Apply this diff:
- String k8sNamespace = System.getProperty(key); - if (Strings.isNullOrEmpty(k8sNamespace)) { + String propertyValue = System.getProperty(key); + if (Strings.isNullOrEmpty(propertyValue)) { // ... - k8sNamespace = System.getenv(environmentKey); + propertyValue = System.getenv(environmentKey); // ... - return k8sNamespace; + return propertyValue;
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (4)
apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java
(4 hunks)apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
(4 hunks)apollo-client/src/test/java/com/ctrip/framework/apollo/kubernetes/KubernetesManagerTest.java
(2 hunks)apollo-core/src/main/java/com/ctrip/framework/apollo/core/ApolloClientSystemConsts.java
(1 hunks)
🔇 Additional comments (1)
apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java (1)
144-146
:
Verify the return value when skipping ConfigMap update
In the updateConfigMap
method, if the current pod is not a write pod, the method returns true
. Please verify that this behavior correctly indicates that the update is not necessary. Returning true
might imply that the ConfigMap was successfully updated, which could be misleading.
Run the following script to check how updateConfigMap
is used and how its return value is handled:
✅ Verification successful
Let me gather more context about the isWritePod
method and its usage to better understand the logic.
Let me check the complete implementation of the updateConfigMap
method to understand the full context.
Let me try a different pattern to get the method implementation.
Based on the implementation I can now generate the final response.
Return value in updateConfigMap
is correct for non-write pods
The return value of true
when the pod is not a write pod is correct and not misleading. Here's why:
- The method returns
true
to indicate a successful outcome, not specifically a successful update - For non-write pods, skipping the update is the expected behavior and represents a successful execution of the method
- This aligns with the method's contract where
true
indicates the operation completed as intended, whether that means updating (for write pods) or intentionally skipping (for non-write pods) - The actual update logic is only attempted for write pods, maintaining the proper separation of responsibilities in a multi-pod environment
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Find usages of `updateConfigMap` and inspect return value handling.
rg -A 3 'updateConfigMap\('
Length of output: 2898
Script:
#!/bin/bash
# Find the implementation and usage of isWritePod
ast-grep --pattern 'isWritePod($_) {
$$$
}'
# Also search for any documentation or comments about write pod behavior
rg -B 2 -A 2 'isWritePod'
Length of output: 1259
Script:
#!/bin/bash
# Get the complete implementation of updateConfigMap method
ast-grep --pattern 'updateConfigMap($_, $_, $_) {
$$$
}'
# Also get any related tests that verify this behavior
rg -B 2 -A 10 'void test.*updateConfigMap'
Length of output: 105
Script:
#!/bin/bash
# Get the complete implementation using a more specific context
rg -B 5 -A 20 'public boolean updateConfigMap\(' apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java
# Get test cases with different pattern
rg -B 5 -A 15 'updateConfigMap.*assert' apollo-client/src/test/java/com/ctrip/framework/apollo/kubernetes/KubernetesManagerTest.java
Length of output: 1323
apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java
Outdated
Show resolved
Hide resolved
apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java
Outdated
Show resolved
Hide resolved
apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java
Outdated
Show resolved
Hide resolved
apollo-core/src/main/java/com/ctrip/framework/apollo/core/ApolloClientSystemConsts.java
Outdated
Show resolved
Hide resolved
apollo-client/src/test/java/com/ctrip/framework/apollo/kubernetes/KubernetesManagerTest.java
Outdated
Show resolved
Hide resolved
apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
Show resolved
Hide resolved
45d2cce
to
b56e717
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (7)
apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java (2)
415-433
: Consider adding input validation for the default value.The
getK8sConfigProperties
method accepts a default value parameter but doesn't validate it. For properties that expect specific formats or ranges, consider adding validation to ensure the default value meets the requirements.private String getK8sConfigProperties(String key, String environmentKey, String defaultValue) { + if (defaultValue == null) { + throw new IllegalArgumentException("Default value cannot be null"); + } // 1. Get from System Property String k8sNamespace = System.getProperty(key); // ... rest of the method
401-433
: Consider adding documentation for the new configuration property.The new Kubernetes max write pods configuration would benefit from documentation explaining its purpose, acceptable values, and impact on the system. This helps users understand how to properly configure this value.
Add JavaDoc comments:
+ /** + * Initializes the maximum number of pods that can write to the Kubernetes ConfigMap. + * This helps reduce conflicts and pressure on the Kubernetes service when multiple + * pods attempt to update configurations simultaneously. + */ private void initPropertyKubernetesMaxWritePods() { // ... implementation } + /** + * Retrieves configuration properties from various sources in the following order: + * 1. System Property + * 2. Environment Variable + * 3. Server Properties + * 4. Application Properties + * 5. Default Value + * + * @param key The system property key + * @param environmentKey The environment variable key + * @param defaultValue The default value if not found in any source + * @return The configuration value + */ private String getK8sConfigProperties(String key, String environmentKey, String defaultValue) { // ... implementation }apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java (3)
49-57
: Add validation for propertyKubernetesMaxWritePodsThe max write pods value should be validated to ensure it's positive.
public KubernetesManager() { try { client = Config.defaultClient(); coreV1Api = new CoreV1Api(client); ConfigUtil configUtil = ApolloInjector.getInstance(ConfigUtil.class); propertyKubernetesMaxWritePods = configUtil.getPropertyKubernetesMaxWritePods(); + if (propertyKubernetesMaxWritePods <= 0) { + throw new IllegalArgumentException("Max write pods must be positive"); + } } catch (Exception e) {
149-151
: Add debug logging for write pod eligibility checkConsider adding debug logging to help troubleshoot pod selection behavior.
if (!isWritePod(k8sNamespace)) { + logger.debug("Pod {} is not eligible for writing ConfigMap", localPodName); return true; }
243-244
: Make app label key configurableThe "app" label key is hardcoded, which reduces flexibility. Consider making it configurable.
- String appName = localMetadata.getLabels().get("app"); - String labelSelector = "app=" + appName; + String appLabelKey = configUtil.getPropertyKubernetesAppLabelKey("app"); + String appName = localMetadata.getLabels().get(appLabelKey); + String labelSelector = appLabelKey + "=" + appName;apollo-client/src/test/java/com/ctrip/framework/apollo/kubernetes/KubernetesManagerTest.java (2)
155-161
: Add test cases for edge cases in pod selectionThe current test only covers the happy path. Consider adding test cases for:
- Multiple pods with different creation timestamps
- Pods without required labels
- Error scenarios in pod listing
+ @Test + public void testUpdateConfigMapWithMultiplePods() throws Exception { + // arrange + String namespace = "default"; + String name = "testConfigMap"; + + // Create multiple pods with different timestamps + V1Pod pod1 = createPodWithTimestamp("pod1", OffsetDateTime.now().minusHours(1)); + V1Pod pod2 = createPodWithTimestamp("pod2", OffsetDateTime.now()); + V1PodList v1PodList = new V1PodList().addItemsItem(pod1).addItemsItem(pod2); + + // Setup mocks and verify behavior + // ... implementation details ... + }
178-180
: Add assertions for ConfigMap data integrityThe test should verify that the ConfigMap data is updated correctly when the pod is eligible to write.
HashMap<String, String> updateData = new HashMap<>(existData); updateData.put("newKey","newValue"); boolean success = kubernetesManager.updateConfigMap(namespace, name, updateData); + + // Verify the ConfigMap was updated with the correct data + ArgumentCaptor<V1ConfigMap> configMapCaptor = ArgumentCaptor.forClass(V1ConfigMap.class); + verify(coreV1Api).replaceNamespacedConfigMap(eq(name), eq(namespace), configMapCaptor.capture(), + isNull(), isNull(), isNull(), isNull()); + + Map<String, String> updatedData = configMapCaptor.getValue().getData(); + assertEquals("newValue", updatedData.get("newKey")); + assertEquals("value", updatedData.get("key"));
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (4)
apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java
(4 hunks)apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
(4 hunks)apollo-client/src/test/java/com/ctrip/framework/apollo/kubernetes/KubernetesManagerTest.java
(5 hunks)apollo-core/src/main/java/com/ctrip/framework/apollo/core/ApolloClientSystemConsts.java
(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- apollo-core/src/main/java/com/ctrip/framework/apollo/core/ApolloClientSystemConsts.java
🔇 Additional comments (6)
apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java (5)
75-75
: LGTM: Field declaration with reasonable default value.
The default value of 3 for propertyKubernetesMaxWritePods
seems reasonable as it provides redundancy while limiting the number of pods that can write to the ConfigMap.
Line range hint 82-98
: LGTM: Constructor properly initializes the new field.
The constructor has been updated to include initPropertyKubernetesMaxWritePods()
in the initialization sequence, maintaining consistency with other property initializations.
396-399
: LGTM: Refactored method improves code reusability.
The getK8sNamespace()
method has been refactored to use the new getK8sConfigProperties()
helper method, which reduces code duplication and improves maintainability.
543-545
: LGTM: Getter method follows Java conventions.
The getter method is properly implemented following Java naming conventions and visibility modifiers.
401-411
:
Fix incorrect constant in error logging.
The error message references APOLLO_CACHE_KUBERNETES_NAMESPACE
instead of APOLLO_CACHE_KUBERNETES_MAX_WRITE_PODS
. This may lead to confusion when diagnosing configuration issues.
Apply this diff to correct the constant:
logger.error("Config for {} is invalid: {}",
- ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_NAMESPACE, propertyKubernetesMaxWritePodsStr);
+ ApolloClientSystemConsts.APOLLO_CACHE_KUBERNETES_MAX_WRITE_PODS, propertyKubernetesMaxWritePodsStr);
apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java (1)
260-263
:
Fix error handling in isWritePod method
Currently, the method returns true on errors, which could lead to multiple pods attempting to write simultaneously. This defeats the purpose of write pod selection.
} catch (Exception e) {
- logger.info("Error determining write pod eligibility:{}", e.getMessage(), e);
- return true;
+ logger.error("Error determining write pod eligibility: {}", e.getMessage(), e);
+ return false;
}
Likely invalid or redundant comment.
apollo-client/src/main/java/com/ctrip/framework/apollo/kubernetes/KubernetesManager.java
Show resolved
Hide resolved
I have read the CLA Document and I hereby sign the CLA |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
What's the purpose of this PR
fix #89
Which issue(s) this PR fixes:
fix #89
Brief changelog
reduce conflicts when update configmap in k8s
Follow this checklist to help us incorporate your contribution quickly and easily:
mvn clean test
to make sure this pull request doesn't break anything.CHANGES
log.Summary by CodeRabbit
New Features
Bug Fixes
Tests
Documentation