Skip to content
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

feat: Support starting up without the ConfigMap with system configs #282

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1ConfigMap;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand Down Expand Up @@ -118,19 +119,7 @@ public long getLong(String key, long defaultValue) {

@Override
public void setConfig(String key, String value) {
V1ConfigMap configMap = getConfigMap();
if (configMap == null) {
throw new IllegalStateException("No ConfigMap is available. Please reinstall Higress Console.");
}
if (configMap.getData() == null) {
configMap.setData(new HashMap<>());
}
configMap.getData().put(key, value);
try {
kubernetesClientService.replaceConfigMap(configMap);
} catch (ApiException e) {
throw new BusinessException("Error occurs when updating ConfigMap.", e);
}
setConfigs(Collections.singletonMap(key, value));
}

@Override
Expand Down Expand Up @@ -159,8 +148,30 @@ public void setConfig(String key, Object value) {

@Override
public void setConfigs(Map<String, Object> configs) {
if (MapUtils.isNotEmpty(configs)) {
configs.forEach(this::setConfig);
if (MapUtils.isEmpty(configs)) {
return;
}

V1ConfigMap configMap = getConfigMap();
if (configMap == null) {
configMap = initConfigMap();
}
Map<String, String> data = configMap.getData();
if (data == null) {
data = new HashMap<>();
configMap.setData(data);
}
for (Map.Entry<String, Object> config : configs.entrySet()) {
if (config.getValue() == null) {
data.remove(config.getKey());
} else {
data.put(config.getKey(), config.getValue().toString());
}
}
try {
kubernetesClientService.replaceConfigMap(configMap);
} catch (ApiException e) {
throw new BusinessException("Error occurs when updating ConfigMap.", e);
}
}

Expand Down Expand Up @@ -194,8 +205,20 @@ private V1ConfigMap getConfigMap() {
try {
return kubernetesClientService.readConfigMap(configMapName);
} catch (ApiException e) {
log.error("Error occurs when reading ConfigMap " + configMapName, e);
return null;
throw new BusinessException("Error occurs when reading ConfigMap " + configMapName, e);
}
}

private V1ConfigMap initConfigMap() {
V1ConfigMap configMap = new V1ConfigMap();
V1ObjectMeta metadata = new V1ObjectMeta();
configMap.metadata(metadata);
metadata.setName(configMapName);
configMap.setData(Collections.emptyMap());
try {
return kubernetesClientService.createConfigMap(configMap);
} catch (ApiException e) {
throw new BusinessException("Error occurs when reading ConfigMap " + configMapName, e);
}
}
}
Loading