-
-
Notifications
You must be signed in to change notification settings - Fork 76
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
[OSPP]Support Kubernetes ConfigMap for Apollo java, golang client #79
Changes from 13 commits
82f7356
e14fd00
562b325
8705ba1
d8c51dd
435df9d
e2c7cd7
73581e9
c52337c
3a22a38
80f6c59
65ce65a
88b1d75
c3fc604
79ae56a
9252a93
82d61d7
2f5ddf6
f95ca86
7438b07
0c66c59
f7ff6d9
84d10fd
41672e6
03aa3ad
74a6b45
b8392ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,180 @@ | ||||||
/* | ||||||
* Copyright 2022 Apollo Authors | ||||||
* | ||||||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
* you may not use this file except in compliance with the License. | ||||||
* You may obtain a copy of the License at | ||||||
* | ||||||
* http://www.apache.org/licenses/LICENSE-2.0 | ||||||
* | ||||||
* Unless required by applicable law or agreed to in writing, software | ||||||
* distributed under the License is distributed on an "AS IS" BASIS, | ||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
* See the License for the specific language governing permissions and | ||||||
* limitations under the License. | ||||||
* | ||||||
*/ | ||||||
package com.ctrip.framework.apollo.Kubernetes; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use lowercase package naming convention. Java package names should be in lowercase to avoid conflicts with class names and to follow standard conventions. Please rename the package Apply this change: -package com.ctrip.framework.apollo.Kubernetes;
+package com.ctrip.framework.apollo.kubernetes; 📝 Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. package name should use lowercase There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shoothzj Thank you for acknowledging the package naming convention. ((\ /)/ |
||||||
|
||||||
import io.kubernetes.client.openapi.ApiClient; | ||||||
import io.kubernetes.client.openapi.ApiException; | ||||||
import io.kubernetes.client.openapi.apis.CoreV1Api; | ||||||
import io.kubernetes.client.openapi.Configuration; | ||||||
import io.kubernetes.client.openapi.models.*; | ||||||
import io.kubernetes.client.util.Config; | ||||||
import org.slf4j.Logger; | ||||||
import org.slf4j.LoggerFactory; | ||||||
import org.springframework.stereotype.Service; | ||||||
|
||||||
import java.util.Map; | ||||||
import java.util.Objects; | ||||||
import java.util.concurrent.TimeUnit; | ||||||
|
||||||
@Service | ||||||
public class KubernetesManager { | ||||||
private ApiClient client; | ||||||
private CoreV1Api coreV1Api; | ||||||
|
||||||
private final Logger log = LoggerFactory.getLogger(this.getClass()); | ||||||
|
||||||
public KubernetesManager() { | ||||||
try { | ||||||
client = Config.defaultClient(); | ||||||
Configuration.setDefaultApiClient(client); | ||||||
coreV1Api = new CoreV1Api(client); | ||||||
} catch (Exception e) { | ||||||
String errorMessage = "Failed to initialize Kubernetes client: " + e.getMessage(); | ||||||
log.error(errorMessage, e); | ||||||
throw new RuntimeException(errorMessage, e); | ||||||
} | ||||||
} | ||||||
|
||||||
public KubernetesManager(CoreV1Api coreV1Api) { | ||||||
this.coreV1Api = coreV1Api; | ||||||
} | ||||||
|
||||||
public V1ConfigMap buildConfigMap(String name, String namespace, Map<String, String> data) { | ||||||
V1ObjectMeta metadata = new V1ObjectMeta() | ||||||
.name(name) | ||||||
.namespace(namespace); | ||||||
|
||||||
return new V1ConfigMap() | ||||||
.apiVersion("v1") | ||||||
.kind("ConfigMap") | ||||||
.metadata(metadata) | ||||||
.data(data); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Creates a Kubernetes ConfigMap. | ||||||
* | ||||||
* @param configMapNamespace 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); | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure input validation halts execution upon failure The input validation checks for null or empty parameters log an error but do not halt execution. This can lead to exceptions or unintended behavior later in the code. Consider throwing an exception to prevent the methods from proceeding with invalid input. Apply the following changes to the methods:
Also applies to: 99-101, 129-131, 154-156 |
||||||
V1ConfigMap configMap = buildConfigMap(name, configMapNamespace, data); | ||||||
try { | ||||||
coreV1Api.createNamespacedConfigMap(configMapNamespace, configMap, null, null, null, null); | ||||||
log.info("ConfigMap created successfully: name: {}, namespace: {}", name, configMapNamespace); | ||||||
return name; | ||||||
} catch (Exception e) { | ||||||
throw new RuntimeException("Failed to create ConfigMap: " + e.getMessage(), e); | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* get value from config map | ||||||
* | ||||||
* @param configMapNamespace configMapNamespace | ||||||
* @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); | ||||||
return null; | ||||||
} | ||||||
try { | ||||||
V1ConfigMap configMap = coreV1Api.readNamespacedConfigMap(name, configMapNamespace, 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)); | ||||||
} | ||||||
return configMap.getData().get(key); | ||||||
} catch (Exception e) { | ||||||
return null; | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid suppressing exceptions and returning null. In Consider rethrowing the exception or handling it based on the specific case: -} catch (Exception e) {
- return null;
+} catch (ApiException e) {
+ log.error("Failed to get value from ConfigMap", e);
+ throw new RuntimeException("Failed to get value from ConfigMap: " + e.getResponseBody(), e); This ensures that calling methods are aware that an error has occurred.
|
||||||
} | ||||||
|
||||||
/** | ||||||
* update config map | ||||||
* | ||||||
* @param configMapNamespace configmap namespace | ||||||
* @param name config map name (appId) | ||||||
* @param data new data | ||||||
* @return config map name | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update method documentation to reflect correct return type The Javadoc comment states that the method returns the config map name, but the method actually returns a boolean indicating success. Update the - * @return config map name
+ * @return true if the update is successful, false otherwise 📝 Committable suggestion
Suggested change
|
||||||
*/ | ||||||
// TODO 使用client自带的retry机制,设置重试次数,CAS | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Address the TODO: Use client's built-in retry mechanism The TODO comment indicates that the client's built-in retry mechanism should be used instead of custom retry logic. Implementing this can simplify the code and adhere to best practices. Would you like assistance in implementing the client's built-in retry mechanism for updating the ConfigMap? This can improve reliability and reduce code complexity. |
||||||
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); | ||||||
return false; | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Ensure consistent error handling. Returning Modify the method signature and return type: -public String updateConfigMap(String configMapNamespace, String name, Map<String, String> data) {
+public boolean updateConfigMap(String configMapNamespace, String name, Map<String, String> data) { Update the return statements accordingly: -return name;
+return true; In the catch block: -return null;
+throw new RuntimeException("Failed to update ConfigMap: " + e.getMessage(), e); This approach provides clearer feedback to the caller about the operation's success.
|
||||||
|
||||||
// retry | ||||||
int maxRetries = 5; | ||||||
int retryCount = 0; | ||||||
long waitTime = 100; | ||||||
|
||||||
while (retryCount < maxRetries) { | ||||||
try { | ||||||
V1ConfigMap configmap = coreV1Api.readNamespacedConfigMap(name, configMapNamespace, null); | ||||||
configmap.setData(data); | ||||||
coreV1Api.replaceNamespacedConfigMap(name, configMapNamespace, configmap, null, null, null, null); | ||||||
return true; | ||||||
} catch (ApiException e) { | ||||||
if (e.getCode() == 409) { | ||||||
retryCount++; | ||||||
log.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()); | ||||||
} | ||||||
} | ||||||
} | ||||||
return retryCount < maxRetries; | ||||||
} | ||||||
|
||||||
/** | ||||||
* check config map exist | ||||||
* | ||||||
* @param configMapNamespace 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); | ||||||
return false; | ||||||
} | ||||||
try { | ||||||
log.info("Check whether ConfigMap exists, configMapName: {}", configMapName); | ||||||
coreV1Api.readNamespacedConfigMap(configMapName, configMapNamespace, null); | ||||||
return true; | ||||||
} catch (Exception e) { | ||||||
// configmap not exist | ||||||
return false; | ||||||
} | ||||||
} | ||||||
} |
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.
Add it to the last