Fix namespace handling for ConfigMapConfigProperties. Remove duplicate code between ConfigMapPropertySource and SecretsPropertySource.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (C) 2016 to the original 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 io.fabric8.spring.cloud.kubernetes.config;
|
||||
|
||||
|
||||
public abstract class AbstractConfigProperties {
|
||||
|
||||
protected boolean enabled = true;
|
||||
protected String name;
|
||||
protected String namespace;
|
||||
|
||||
public abstract String getConfigurationTarget();
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
public void setNamespace(String namespace) {
|
||||
this.namespace = namespace;
|
||||
}
|
||||
}
|
||||
@@ -20,33 +20,12 @@ package io.fabric8.spring.cloud.kubernetes.config;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@ConfigurationProperties("spring.cloud.kubernetes.config")
|
||||
public class ConfigMapConfigProperties {
|
||||
public class ConfigMapConfigProperties extends AbstractConfigProperties {
|
||||
|
||||
private boolean enabled = true;
|
||||
private String name;
|
||||
private String namespace;
|
||||
private static final String TARGET = "Config Map";
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
public void setNamespace(String namespace) {
|
||||
this.namespace = namespace;
|
||||
@Override
|
||||
public String getConfigurationTarget() {
|
||||
return TARGET;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import static io.fabric8.spring.cloud.kubernetes.config.ConfigUtils.*;
|
||||
|
||||
@Order(0)
|
||||
public class ConfigMapPropertySourceLocator implements PropertySourceLocator {
|
||||
@@ -39,9 +39,8 @@ public class ConfigMapPropertySourceLocator implements PropertySourceLocator {
|
||||
public MapPropertySource locate(Environment environment) {
|
||||
if (environment instanceof ConfigurableEnvironment) {
|
||||
ConfigurableEnvironment env = (ConfigurableEnvironment) environment;
|
||||
String appName = env.getProperty(Constants.SPRING_APPLICATION_NAME, Constants.FALLBACK_APPLICATION_NAME);
|
||||
String name = properties.getName() == null || properties.getName().isEmpty() ? appName : properties.getName();
|
||||
String namespace = properties.getNamespace();
|
||||
String name = getApplicationName(environment, properties);
|
||||
String namespace = getApplicationNamespace(client, env, properties);
|
||||
return new ConfigMapPropertySource(client, name, namespace);
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.fabric8.spring.cloud.kubernetes.config;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import io.fabric8.kubernetes.client.KubernetesClient;
|
||||
|
||||
public class ConfigUtils {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SecretsPropertySource.class);
|
||||
|
||||
public static <C extends AbstractConfigProperties> String getApplicationName(Environment env, C config) {
|
||||
String name = config.getName();
|
||||
if (StringUtils.isEmpty(name)) {
|
||||
LOGGER.debug(config.getConfigurationTarget() + " name has not been set, taking it from property/env {} (default={})",
|
||||
Constants.SPRING_APPLICATION_NAME,
|
||||
Constants.FALLBACK_APPLICATION_NAME);
|
||||
|
||||
name = env.getProperty(
|
||||
Constants.SPRING_APPLICATION_NAME,
|
||||
Constants.FALLBACK_APPLICATION_NAME);
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
public static <C extends AbstractConfigProperties> String getApplicationNamespace(KubernetesClient client, Environment env, C config) {
|
||||
String namespace = config.getNamespace();
|
||||
if (StringUtils.isEmpty(namespace)) {
|
||||
LOGGER.debug(config.getConfigurationTarget() + " namespace has not been set, taking it from client (ns={})",
|
||||
client.getNamespace());
|
||||
|
||||
namespace = client.getNamespace();
|
||||
}
|
||||
|
||||
return namespace;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,25 +22,16 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
@ConfigurationProperties("spring.cloud.kubernetes.secrets")
|
||||
public class SecretsConfigProperties {
|
||||
public class SecretsConfigProperties extends AbstractConfigProperties {
|
||||
|
||||
private static final String TARGET = "Secret";
|
||||
|
||||
private boolean enabled = true;
|
||||
private boolean enableApi = false;
|
||||
private String name;
|
||||
private String namespace;
|
||||
private Map<String, String> labels = new HashMap<>();
|
||||
private List<String> paths = new LinkedList<>();
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public boolean isEnableApi() {
|
||||
return enableApi;
|
||||
@@ -50,26 +41,6 @@ public class SecretsConfigProperties {
|
||||
this.enableApi = enableApi;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getName(Environment env) {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
public void setNamespace(String namespace) {
|
||||
this.namespace = namespace;
|
||||
}
|
||||
|
||||
public void setLabels(Map<String, String> labels) {
|
||||
this.labels = labels;
|
||||
}
|
||||
@@ -85,4 +56,9 @@ public class SecretsConfigProperties {
|
||||
public List<String> getPaths() {
|
||||
return paths;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getConfigurationTarget() {
|
||||
return TARGET;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static io.fabric8.spring.cloud.kubernetes.config.ConfigUtils.*;
|
||||
|
||||
public class SecretsPropertySource extends MapPropertySource {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SecretsPropertySource.class);
|
||||
|
||||
@@ -116,34 +118,6 @@ public class SecretsPropertySource extends MapPropertySource {
|
||||
// *****************************
|
||||
// Helpers
|
||||
// *****************************
|
||||
|
||||
private static String getApplicationName(Environment env, SecretsConfigProperties config) {
|
||||
String name = config.getName();
|
||||
if (StringUtils.isEmpty(name)) {
|
||||
LOGGER.debug("Secret name has not been set, taking it from property/env {} (default={})",
|
||||
Constants.SPRING_APPLICATION_NAME,
|
||||
Constants.FALLBACK_APPLICATION_NAME);
|
||||
|
||||
name = env.getProperty(
|
||||
Constants.SPRING_APPLICATION_NAME,
|
||||
Constants.FALLBACK_APPLICATION_NAME);
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
private static String getApplicationNamespace(KubernetesClient client, Environment env, SecretsConfigProperties config) {
|
||||
String namespace = config.getNamespace();
|
||||
if (StringUtils.isEmpty(namespace)) {
|
||||
LOGGER.debug("Secret namespace has not been set, taking it from client (ns={})",
|
||||
client.getNamespace());
|
||||
|
||||
namespace = client.getNamespace();
|
||||
}
|
||||
|
||||
return namespace;
|
||||
}
|
||||
|
||||
private static void putAll(Secret secret, Map<String, Object> result) {
|
||||
if (secret != null && secret.getData() != null) {
|
||||
secret.getData().forEach((k, v) -> result.put(
|
||||
|
||||
Reference in New Issue
Block a user