diff --git a/operator-framework/src/main/java/io/javaoperatorsdk/operator/config/loader/provider/PropertiesConfigProvider.java b/operator-framework/src/main/java/io/javaoperatorsdk/operator/config/loader/provider/PropertiesConfigProvider.java index 35dd38f406..8670b88f06 100644 --- a/operator-framework/src/main/java/io/javaoperatorsdk/operator/config/loader/provider/PropertiesConfigProvider.java +++ b/operator-framework/src/main/java/io/javaoperatorsdk/operator/config/loader/provider/PropertiesConfigProvider.java @@ -23,6 +23,9 @@ import java.util.Optional; import java.util.Properties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import io.javaoperatorsdk.operator.config.loader.ConfigProvider; /** @@ -34,6 +37,8 @@ */ public class PropertiesConfigProvider implements ConfigProvider { + private static final Logger log = LoggerFactory.getLogger(PropertiesConfigProvider.class); + private final Properties properties; /** Returns a {@link PropertiesConfigProvider} backed by {@link System#getProperties()}. */ @@ -44,7 +49,18 @@ public static PropertiesConfigProvider systemProperties() { /** * Loads properties from the given file path. * - * @throws UncheckedIOException if the file cannot be read + * @throws UncheckedIOException if the file cannot be read. Does not throw an exception if the + * file does not exist. + */ + public PropertiesConfigProvider(String path) { + this(Path.of(path)); + } + + /** + * Loads properties from the given file path. + * + * @throws UncheckedIOException if the file cannot be read. Does not throw an exception if the + * file does not exist. */ public PropertiesConfigProvider(Path path) { this.properties = load(path); @@ -68,6 +84,11 @@ public Optional getValue(String key, Class type) { } private static Properties load(Path path) { + if (!Files.exists(path)) { + log.warn("{} does not exist", path); + return new Properties(); + } + try (InputStream in = Files.newInputStream(path)) { Properties props = new Properties(); props.load(in); diff --git a/operator-framework/src/main/java/io/javaoperatorsdk/operator/config/loader/provider/YamlConfigProvider.java b/operator-framework/src/main/java/io/javaoperatorsdk/operator/config/loader/provider/YamlConfigProvider.java index 52b07b011d..d631496d59 100644 --- a/operator-framework/src/main/java/io/javaoperatorsdk/operator/config/loader/provider/YamlConfigProvider.java +++ b/operator-framework/src/main/java/io/javaoperatorsdk/operator/config/loader/provider/YamlConfigProvider.java @@ -23,6 +23,9 @@ import java.util.Map; import java.util.Optional; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import io.javaoperatorsdk.operator.config.loader.ConfigProvider; import com.fasterxml.jackson.databind.ObjectMapper; @@ -39,6 +42,8 @@ */ public class YamlConfigProvider implements ConfigProvider { + private static final Logger log = LoggerFactory.getLogger(YamlConfigProvider.class); + private static final ObjectMapper MAPPER = new ObjectMapper(new YAMLFactory()); private final Map data; @@ -46,7 +51,18 @@ public class YamlConfigProvider implements ConfigProvider { /** * Loads YAML from the given file path. * - * @throws UncheckedIOException if the file cannot be read + * @throws UncheckedIOException if the file cannot be read. Does not throw an exception if the + * file does not exist. + */ + public YamlConfigProvider(String path) { + this(Path.of(path)); + } + + /** + * Loads YAML from the given file path. + * + * @throws UncheckedIOException if the file cannot be read. Does not throw an exception if the + * file does not exist. */ public YamlConfigProvider(Path path) { this.data = load(path); @@ -79,6 +95,11 @@ public Optional getValue(String key, Class type) { @SuppressWarnings("unchecked") private static Map load(Path path) { + if (!Files.exists(path)) { + log.warn("{} does not exist", path); + return Map.of(); + } + try (InputStream in = Files.newInputStream(path)) { Map result = MAPPER.readValue(in, Map.class); return result != null ? result : Map.of(); diff --git a/operator-framework/src/test/java/io/javaoperatorsdk/operator/config/loader/provider/PropertiesConfigProviderTest.java b/operator-framework/src/test/java/io/javaoperatorsdk/operator/config/loader/provider/PropertiesConfigProviderTest.java index c44534eb3a..dd9fd7b599 100644 --- a/operator-framework/src/test/java/io/javaoperatorsdk/operator/config/loader/provider/PropertiesConfigProviderTest.java +++ b/operator-framework/src/test/java/io/javaoperatorsdk/operator/config/loader/provider/PropertiesConfigProviderTest.java @@ -16,7 +16,6 @@ package io.javaoperatorsdk.operator.config.loader.provider; import java.io.IOException; -import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; @@ -27,7 +26,6 @@ import org.junit.jupiter.api.io.TempDir; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; class PropertiesConfigProviderTest { @@ -120,10 +118,9 @@ void loadsFromFile(@TempDir Path dir) throws IOException { } @Test - void throwsUncheckedIOExceptionForMissingFile(@TempDir Path dir) { + void returnsEmptyForNonExistingFile(@TempDir Path dir) { Path missing = dir.resolve("does-not-exist.properties"); - assertThatExceptionOfType(UncheckedIOException.class) - .isThrownBy(() -> new PropertiesConfigProvider(missing)) - .withMessageContaining("does-not-exist.properties"); + var provider = new PropertiesConfigProvider(missing); + assertThat(provider.getValue("any.key", String.class)).isEmpty(); } } diff --git a/operator-framework/src/test/java/io/javaoperatorsdk/operator/config/loader/provider/YamlConfigProviderTest.java b/operator-framework/src/test/java/io/javaoperatorsdk/operator/config/loader/provider/YamlConfigProviderTest.java index 4f8c53ac38..2362b85ea4 100644 --- a/operator-framework/src/test/java/io/javaoperatorsdk/operator/config/loader/provider/YamlConfigProviderTest.java +++ b/operator-framework/src/test/java/io/javaoperatorsdk/operator/config/loader/provider/YamlConfigProviderTest.java @@ -16,7 +16,6 @@ package io.javaoperatorsdk.operator.config.loader.provider; import java.io.IOException; -import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; @@ -27,7 +26,6 @@ import org.junit.jupiter.api.io.TempDir; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; class YamlConfigProviderTest { @@ -135,10 +133,9 @@ void loadsFromFile(@TempDir Path dir) throws IOException { } @Test - void throwsUncheckedIOExceptionForMissingFile(@TempDir Path dir) { + void returnsEmptyForNonExistingFile(@TempDir Path dir) { Path missing = dir.resolve("does-not-exist.yaml"); - assertThatExceptionOfType(UncheckedIOException.class) - .isThrownBy(() -> new YamlConfigProvider(missing)) - .withMessageContaining("does-not-exist.yaml"); + var provider = new YamlConfigProvider(missing); + assertThat(provider.getValue("any.key", String.class)).isEmpty(); } }