Skip to content

Commit a932a3d

Browse files
csviriCopilot
andauthored
improve: config providers for yaml and properties logs only warning on missing file (#3245)
* 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> * Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Attila Mészáros <a_meszaros@apple.com> --------- Signed-off-by: Attila Mészáros <a_meszaros@apple.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 2c16143 commit a932a3d

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import java.util.Optional;
2424
import java.util.Properties;
2525

26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
2629
import io.javaoperatorsdk.operator.config.loader.ConfigProvider;
2730

2831
/**
@@ -34,6 +37,8 @@
3437
*/
3538
public class PropertiesConfigProvider implements ConfigProvider {
3639

40+
private static final Logger log = LoggerFactory.getLogger(PropertiesConfigProvider.class);
41+
3742
private final Properties properties;
3843

3944
/** Returns a {@link PropertiesConfigProvider} backed by {@link System#getProperties()}. */
@@ -44,7 +49,18 @@ public static PropertiesConfigProvider systemProperties() {
4449
/**
4550
* Loads properties from the given file path.
4651
*
47-
* @throws UncheckedIOException if the file cannot be read
52+
* @throws UncheckedIOException if the file cannot be read. Does not throw an exception if the
53+
* file does not exist.
54+
*/
55+
public PropertiesConfigProvider(String path) {
56+
this(Path.of(path));
57+
}
58+
59+
/**
60+
* Loads properties from the given file path.
61+
*
62+
* @throws UncheckedIOException if the file cannot be read. Does not throw an exception if the
63+
* file does not exist.
4864
*/
4965
public PropertiesConfigProvider(Path path) {
5066
this.properties = load(path);
@@ -68,6 +84,11 @@ public <T> Optional<T> getValue(String key, Class<T> type) {
6884
}
6985

7086
private static Properties load(Path path) {
87+
if (!Files.exists(path)) {
88+
log.warn("{} does not exist", path);
89+
return new Properties();
90+
}
91+
7192
try (InputStream in = Files.newInputStream(path)) {
7293
Properties props = new Properties();
7394
props.load(in);

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import java.util.Map;
2424
import java.util.Optional;
2525

26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
2629
import io.javaoperatorsdk.operator.config.loader.ConfigProvider;
2730

2831
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -39,14 +42,27 @@
3942
*/
4043
public class YamlConfigProvider implements ConfigProvider {
4144

45+
private static final Logger log = LoggerFactory.getLogger(YamlConfigProvider.class);
46+
4247
private static final ObjectMapper MAPPER = new ObjectMapper(new YAMLFactory());
4348

4449
private final Map<String, Object> data;
4550

4651
/**
4752
* Loads YAML from the given file path.
4853
*
49-
* @throws UncheckedIOException if the file cannot be read
54+
* @throws UncheckedIOException if the file cannot be read. Does not throw an exception if the
55+
* file does not exist.
56+
*/
57+
public YamlConfigProvider(String path) {
58+
this(Path.of(path));
59+
}
60+
61+
/**
62+
* Loads YAML from the given file path.
63+
*
64+
* @throws UncheckedIOException if the file cannot be read. Does not throw an exception if the
65+
* file does not exist.
5066
*/
5167
public YamlConfigProvider(Path path) {
5268
this.data = load(path);
@@ -79,6 +95,11 @@ public <T> Optional<T> getValue(String key, Class<T> type) {
7995

8096
@SuppressWarnings("unchecked")
8197
private static Map<String, Object> load(Path path) {
98+
if (!Files.exists(path)) {
99+
log.warn("{} does not exist", path);
100+
return Map.of();
101+
}
102+
82103
try (InputStream in = Files.newInputStream(path)) {
83104
Map<String, Object> result = MAPPER.readValue(in, Map.class);
84105
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)