Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(core): namespace a host native binary's child containers by port (#5800)

Hector Venturacb378afd410a
diff --git a/src/main/java/io/github/hectorvent/floci/config/HostDefaultsConfigSource.java b/src/main/java/io/github/hectorvent/floci/config/HostDefaultsConfigSource.java
index f20b43be3b..108303380b 100644
--- a/src/main/java/io/github/hectorvent/floci/config/HostDefaultsConfigSource.java
+++ b/src/main/java/io/github/hectorvent/floci/config/HostDefaultsConfigSource.java
@@ -6,8 +6,13 @@ import java.util.Map;
import java.util.Set;
/**
- * Supplies {@link HostDataDir}'s per-user data directory as the storage path of a native binary
- * running outside a container.
+ * Defaults for a native binary running outside a container: {@link HostDataDir}'s per-user data
+ * directory as the storage path, and a Docker resource namespace of {@code host-<port>}.
+ *
+ * <p>The namespace keeps the binary's child containers and volumes apart from those of an image,
+ * or of a second binary on another port, sharing the same Docker daemon. Without it, both would
+ * name an RDS instance {@code mydb} the same, and the one created second would remove the
+ * other's container before starting its own.
*
* <p>The ordinal sits just above the bundled {@code application.yml} (255) and below every source
* a user sets explicitly: {@code config/application.yml} in the working directory (265),
@@ -21,6 +26,8 @@ public class HostDefaultsConfigSource implements ConfigSource {
static final String PERSISTENT_PATH = "floci.storage.persistent-path";
static final String HOST_PERSISTENT_PATH = "floci.storage.host-persistent-path";
static final String FOLLOW_PERSISTENT_PATH = "${" + PERSISTENT_PATH + "}";
+ static final String RESOURCE_NAMESPACE = "floci.docker.resource-namespace";
+ static final String HOST_NAMESPACE = "host-${floci.port}";
private final Map<String, String> properties;
@@ -33,7 +40,8 @@ public class HostDefaultsConfigSource implements ConfigSource {
String dir = HostDataDir.persistentPathDefault();
// The binary and Docker share the host filesystem, so child containers bind the same path,
// including one the user sets in config/application.yml.
- this.properties = Map.of(PERSISTENT_PATH, dir, HOST_PERSISTENT_PATH, FOLLOW_PERSISTENT_PATH);
+ this.properties = Map.of(PERSISTENT_PATH, dir, HOST_PERSISTENT_PATH, FOLLOW_PERSISTENT_PATH,
+ RESOURCE_NAMESPACE, HOST_NAMESPACE);
} else {
this.properties = Map.of();
}
diff --git a/src/test/java/io/github/hectorvent/floci/config/HostDefaultsConfigSourceTest.java b/src/test/java/io/github/hectorvent/floci/config/HostDefaultsConfigSourceTest.java
index a63476a807..9887e2870c 100644
--- a/src/test/java/io/github/hectorvent/floci/config/HostDefaultsConfigSourceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/config/HostDefaultsConfigSourceTest.java
@@ -57,6 +57,16 @@ class HostDefaultsConfigSourceTest {
equalTo("/mnt/floci"));
}
+ @Test
+ void nativeHostRunNamespacesChildResourcesByPort() {
+ HostDefaultsConfigSource source = new HostDefaultsConfigSource(true);
+ assertThat(source.getValue(HostDefaultsConfigSource.RESOURCE_NAMESPACE), equalTo("host-${floci.port}"));
+
+ SmallRyeConfig config = configWith(source,
+ new PropertiesConfigSource(Map.of("floci.port", "4597"), "env", 300));
+ assertThat(config.getValue(HostDefaultsConfigSource.RESOURCE_NAMESPACE, String.class), equalTo("host-4597"));
+ }
+
@Test
void ordinalOverridesOnlyTheBundledYaml() {
// classpath application.yml is 255, config/application.yml in the working directory 265.