Skip to content
Closed
Show file tree
Hide file tree
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 @@ -22,11 +22,12 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -123,12 +124,16 @@ protected abstract SecretsPropertySource getPropertySource(ConfigurableEnvironme

protected void putPathConfig(CompositePropertySource composite) {

if (!properties.paths().isEmpty()) {
Set<String> uniquePaths = new LinkedHashSet<>(properties.paths());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do this for configmaps, but not for secrets, so I decided to add it here


if (!uniquePaths.isEmpty()) {
LOG.warn(
"path support is deprecated and will be removed in a future release. Please use spring.config.import");
}

this.properties.paths().stream().map(Paths::get).filter(Files::exists).flatMap(x -> {
LOG.debug("paths property sources : " + uniquePaths);

uniquePaths.stream().map(Paths::get).filter(Files::exists).flatMap(x -> {
try {
return Files.walk(x);
}
Expand Down Expand Up @@ -189,7 +194,7 @@ private SecretsPropertySource property(Path filePath) {
try {
String content = new String(Files.readAllBytes(filePath)).trim();
String sourceName = fileName.toLowerCase(Locale.ROOT);
SourceData sourceData = new SourceData(sourceName, Collections.singletonMap(fileName, content));
SourceData sourceData = new SourceData(sourceName, Map.of(fileName, content));
return new SecretsPropertySource(sourceData);
}
catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.cloud.bootstrap.config.BootstrapPropertySource;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.cloud.kubernetes.commons.config.MountConfigMapPropertySource;
import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySource;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
Expand Down Expand Up @@ -120,6 +121,10 @@ else if (propertySource instanceof MountConfigMapPropertySource mountConfigMapPr
// we know that the type is correct here
managedSources.add((S) mountConfigMapPropertySource);
}
else if (propertySource instanceof SecretsPropertySource secretsPropertySource) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fixing a bug here with this one.

In case of configmaps, when we create a property source from paths, we create an instance of MountConfigMapPropertySource. Then, in ConfigReloadUtil, there is such a check:

if (source instanceof MountConfigMapPropertySource mountConfigMapPropertySource) { ... }

which means that reloads will take it into consideration.


While refactoring one integration test, I saw that it is failing, and after some debugging, realized that we have no code like the above for secrets. As such, I added that simple condition, because when we create a property source from paths in case of secrets, we create an instance of SecretsPropertySource

// we know that the type is correct here
managedSources.add((S) secretsPropertySource);
}
}
}

Expand Down Expand Up @@ -179,6 +184,8 @@ static boolean changed(List<? extends MapPropertySource> k8sSources, List<? exte
for (int i = 0; i < k8sSources.size(); i++) {
MapPropertySource k8sSource = k8sSources.get(i);
MapPropertySource appSource = appSources.get(i);
System.out.println("k8sSource (abc): " + k8sSource.getSource());
System.out.println("appSource (abc): " + appSource.getSource());
if (changed(k8sSource, appSource)) {
LOG.debug(() -> "found change in : " + k8sSource);
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2023 the original author or authors.
* Copyright 2013-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,7 +44,7 @@
/**
* @author wind57
*/
class Fabric8ConfigMapMountMountPollingBootstrapIT {
class Fabric8ConfigMapMountPollingBootstrapIT {

private static final String IMAGE_NAME = "spring-cloud-kubernetes-fabric8-client-reload";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
@SpringBootApplication
@EnableConfigurationProperties({ LeftProperties.class, RightProperties.class, RightWithLabelsProperties.class,
ConfigMapProperties.class, SecretsProperties.class })
ConfigMapProperties.class, SecretProperties.class })
public class App {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* @author wind57
*/
@ConfigurationProperties("from.properties")
@ConfigurationProperties("from.properties.configmap")
public class ConfigMapProperties {

private String key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,23 @@
@RestController
public class Controller {

private final LeftProperties leftProperties;

private final RightProperties rightProperties;

private final RightWithLabelsProperties rightWithLabelsProperties;

private final ConfigMapProperties configMapProperties;

public Controller(LeftProperties leftProperties, RightProperties rightProperties,
RightWithLabelsProperties rightWithLabelsProperties, ConfigMapProperties configMapProperties) {
this.leftProperties = leftProperties;
this.rightProperties = rightProperties;
this.rightWithLabelsProperties = rightWithLabelsProperties;
this.configMapProperties = configMapProperties;
}

@GetMapping("/left")
public String left() {
return leftProperties.getValue();
}

@GetMapping("/right")
public String right() {
return rightProperties.getValue();
}
private final SecretProperties secretsProperties;

@GetMapping("/with-label")
public String witLabel() {
return rightWithLabelsProperties.getValue();
public Controller(ConfigMapProperties configMapProperties, SecretProperties secretsProperties) {
this.configMapProperties = configMapProperties;
this.secretsProperties = secretsProperties;
}

@GetMapping("/mount")
@GetMapping("/configmap")
public String key() {
return configMapProperties.getKey();
}

@GetMapping("/secret")
public String secret() {
return secretsProperties.getKey();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
/**
* @author wind57
*/
@ConfigurationProperties("from.properties")
public class SecretsProperties {
@ConfigurationProperties("from.properties.secret")
public class SecretProperties {

private String key;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spring:
application:
name: poll-reload-mount
name: poll-reload
cloud:
kubernetes:
reload:
Expand All @@ -12,6 +12,7 @@ spring:
config:
paths:
- /tmp/application.properties

config:
import: "kubernetes:"

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
logging:
level:
root: DEBUG

spring:
application:
name: event-reload
cloud:
kubernetes:
reload:
enabled: true
strategy: shutdown
strategy: refresh
mode: event
namespaces:
- right
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
logging:
level:
root: DEBUG

spring:
application:
name: event-reload
cloud:
kubernetes:
reload:
enabled: true
strategy: shutdown
strategy: refresh
mode: event
namespaces:
- right
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ spring:
kubernetes:
reload:
enabled: true
monitoring-config-maps: true
strategy: shutdown
strategy: refresh
mode: polling
period: 5000

monitoring-secrets: true

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
logging:
level:
root: DEBUG

spring:
cloud:
kubernetes:
config:
sources:
- namespace: left
name: left-configmap
- namespace: right
name: right-configmap
- namespace: right
name: right-configmap-with-label

# otherwise on context refresh we lose this property
# and test fails, since beans are not wired.
main:
cloud-platform: kubernetes
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
logging:
level:
root: DEBUG

spring:
cloud:
kubernetes:
config:
sources:
- namespace: left
name: left-configmap
- namespace: right
name: right-configmap

# otherwise on context refresh we lose this property
# and test fails, since beans are not wired.
main:
cloud-platform: kubernetes
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
spring:
cloud:
kubernetes:
config:
secrets:
paths:
- /tmp/application.properties
# at the moment, we do not support reading properties/yaml/yml
# files when mounting via 'paths'
- /tmp/from.properties.secret.key
enabled: true

config:
enabled: false
Loading
Loading