Skip to content

Commit 1e52c77

Browse files
committed
improve: config providers for yaml and properties logs only warning on missing file
This it is easier to handle dynamic configurations coming from a ConfigMap Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent 1a07a04 commit 1e52c77

File tree

4 files changed

+52
-14
lines changed

4 files changed

+52
-14
lines changed

operator-framework/src/main/java/io/javaoperatorsdk/operator/config/loader/provider/PropertiesConfigProvider.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.javaoperatorsdk.operator.config.loader.provider;
1717

18+
import java.io.File;
1819
import java.io.IOException;
1920
import java.io.InputStream;
2021
import java.io.UncheckedIOException;
@@ -23,6 +24,9 @@
2324
import java.util.Optional;
2425
import java.util.Properties;
2526

27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
2630
import io.javaoperatorsdk.operator.config.loader.ConfigProvider;
2731

2832
/**
@@ -34,6 +38,8 @@
3438
*/
3539
public class PropertiesConfigProvider implements ConfigProvider {
3640

41+
private static final Logger log = LoggerFactory.getLogger(PropertiesConfigProvider.class);
42+
3743
private final Properties properties;
3844

3945
/** Returns a {@link PropertiesConfigProvider} backed by {@link System#getProperties()}. */
@@ -44,7 +50,18 @@ public static PropertiesConfigProvider systemProperties() {
4450
/**
4551
* Loads properties from the given file path.
4652
*
47-
* @throws UncheckedIOException if the file cannot be read
53+
* @throws UncheckedIOException if the file cannot be read. Does not throw exception if the file
54+
* not exists.
55+
*/
56+
public PropertiesConfigProvider(String path) {
57+
this(Path.of(path));
58+
}
59+
60+
/**
61+
* Loads properties from the given file path.
62+
*
63+
* @throws UncheckedIOException if the file cannot be read. Does not throw exception if the file
64+
* not exists.
4865
*/
4966
public PropertiesConfigProvider(Path path) {
5067
this.properties = load(path);
@@ -68,6 +85,11 @@ public <T> Optional<T> getValue(String key, Class<T> type) {
6885
}
6986

7087
private static Properties load(Path path) {
88+
if (!new File(path.toString()).exists()) {
89+
log.warn("{} does not exist", path);
90+
return new Properties();
91+
}
92+
7193
try (InputStream in = Files.newInputStream(path)) {
7294
Properties props = new Properties();
7395
props.load(in);

operator-framework/src/main/java/io/javaoperatorsdk/operator/config/loader/provider/YamlConfigProvider.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.javaoperatorsdk.operator.config.loader.provider;
1717

18+
import java.io.File;
1819
import java.io.IOException;
1920
import java.io.InputStream;
2021
import java.io.UncheckedIOException;
@@ -23,6 +24,9 @@
2324
import java.util.Map;
2425
import java.util.Optional;
2526

27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
2630
import io.javaoperatorsdk.operator.config.loader.ConfigProvider;
2731

2832
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -39,14 +43,27 @@
3943
*/
4044
public class YamlConfigProvider implements ConfigProvider {
4145

46+
private static final Logger log = LoggerFactory.getLogger(YamlConfigProvider.class);
47+
4248
private static final ObjectMapper MAPPER = new ObjectMapper(new YAMLFactory());
4349

4450
private final Map<String, Object> data;
4551

4652
/**
4753
* Loads YAML from the given file path.
4854
*
49-
* @throws UncheckedIOException if the file cannot be read
55+
* @throws UncheckedIOException if the file cannot be read. Does not throw exception if the file
56+
* not exists.
57+
*/
58+
public YamlConfigProvider(String path) {
59+
this(Path.of(path));
60+
}
61+
62+
/**
63+
* Loads YAML from the given file path.
64+
*
65+
* @throws UncheckedIOException if the file cannot be read. Does not throw exception if the file
66+
* not exists.
5067
*/
5168
public YamlConfigProvider(Path path) {
5269
this.data = load(path);
@@ -79,6 +96,11 @@ public <T> Optional<T> getValue(String key, Class<T> type) {
7996

8097
@SuppressWarnings("unchecked")
8198
private static Map<String, Object> load(Path path) {
99+
if (!new File(path.toString()).exists()) {
100+
log.warn("{} does not exist", path);
101+
return Map.of();
102+
}
103+
82104
try (InputStream in = Files.newInputStream(path)) {
83105
Map<String, Object> result = MAPPER.readValue(in, Map.class);
84106
return result != null ? result : Map.of();

operator-framework/src/test/java/io/javaoperatorsdk/operator/config/loader/provider/PropertiesConfigProviderTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package io.javaoperatorsdk.operator.config.loader.provider;
1717

1818
import java.io.IOException;
19-
import java.io.UncheckedIOException;
2019
import java.nio.file.Files;
2120
import java.nio.file.Path;
2221
import java.time.Duration;
@@ -27,7 +26,6 @@
2726
import org.junit.jupiter.api.io.TempDir;
2827

2928
import static org.assertj.core.api.Assertions.assertThat;
30-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3129
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3230

3331
class PropertiesConfigProviderTest {
@@ -120,10 +118,9 @@ void loadsFromFile(@TempDir Path dir) throws IOException {
120118
}
121119

122120
@Test
123-
void throwsUncheckedIOExceptionForMissingFile(@TempDir Path dir) {
121+
void returnsEmptyForNonExistingFile(@TempDir Path dir) {
124122
Path missing = dir.resolve("does-not-exist.properties");
125-
assertThatExceptionOfType(UncheckedIOException.class)
126-
.isThrownBy(() -> new PropertiesConfigProvider(missing))
127-
.withMessageContaining("does-not-exist.properties");
123+
var provider = new PropertiesConfigProvider(missing);
124+
assertThat(provider.getValue("any.key", String.class)).isEmpty();
128125
}
129126
}

operator-framework/src/test/java/io/javaoperatorsdk/operator/config/loader/provider/YamlConfigProviderTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package io.javaoperatorsdk.operator.config.loader.provider;
1717

1818
import java.io.IOException;
19-
import java.io.UncheckedIOException;
2019
import java.nio.file.Files;
2120
import java.nio.file.Path;
2221
import java.time.Duration;
@@ -27,7 +26,6 @@
2726
import org.junit.jupiter.api.io.TempDir;
2827

2928
import static org.assertj.core.api.Assertions.assertThat;
30-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3129
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3230

3331
class YamlConfigProviderTest {
@@ -135,10 +133,9 @@ void loadsFromFile(@TempDir Path dir) throws IOException {
135133
}
136134

137135
@Test
138-
void throwsUncheckedIOExceptionForMissingFile(@TempDir Path dir) {
136+
void returnsEmptyForNonExistingFile(@TempDir Path dir) {
139137
Path missing = dir.resolve("does-not-exist.yaml");
140-
assertThatExceptionOfType(UncheckedIOException.class)
141-
.isThrownBy(() -> new YamlConfigProvider(missing))
142-
.withMessageContaining("does-not-exist.yaml");
138+
var provider = new YamlConfigProvider(missing);
139+
assertThat(provider.getValue("any.key", String.class)).isEmpty();
143140
}
144141
}

0 commit comments

Comments
 (0)