mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(docker): give internal Docker labels io.floci.* names and keep the old keys as aliases (#4871)
* feat(docker): give internal Docker labels io.floci.* names and keep the old keys as aliases Owner, ECS run, credentials-proxy, security-group and VPC network labels move to io.floci.owner, io.floci.ecs.run, io.floci.ecs.credentials-proxy, io.floci.security-group.helper/workload, io.floci.component and the standard resource-identity keys. The previous keys (floci_owner_port, floci.ecs-run, floci.ecs-task-role-credentials-proxy, floci.security-group-*, floci.component, floci_component, floci_vpc_id, floci_vpc_region, floci_vpc_owner_port) are still written as legacy aliases with the same value. Readers take the new key first and fall back to the legacy one, label filters query both key sets, and owner-scoped cleanup leaves an object alone when the two disagree. All alias keys are reserved from extra-labels. VPC networks are now owned by ownerIdentity (namespace and port) instead of the bare API port, so two namespaced instances on the same port no longer reconcile each other's networks. * fix(ec2): keep pre-upgrade VPC networks of a namespaced deployment its own Greptile and pgermosen on #4871: a VPC network created before the owner label took the resource namespace carries only floci_vpc_owner_port with the bare API port, so with a namespace set the deployment read its own surviving network as a CIDR collision and the startup sweep left it behind. Such a legacy-only network is now this deployment's when it holds this port and its floci_namespace label names this deployment's namespace, the identity the new io.floci.owner key encodes. Matching the bare port alone would also claim, and let the sweep delete, the pre-upgrade networks of another namespace on the same port. Also imports List in ContainerStorageHelperTest instead of qualifying it inline.
This commit is contained in:
1 parent
bcb50b765f
commit
24c655050f
21 files changed
+1129
-114
No files matched your search
+40
@@ -103,10 +103,39 @@ class ContainerLifecycleManagerLabelsTest {
|
||||
capturedLabels(createCmd));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWritesTheLegacyAliasOfEachNewKeyWithTheSameValue() {
|
||||
CreateContainerCmd createCmd = stubCreateContainer();
|
||||
ContainerSpec spec = specWithLabels(Map.of(
|
||||
ContainerStorageHelper.OWNER_LABEL, "alpha/4566",
|
||||
ContainerStorageHelper.ECS_RUN_LABEL, "run-1"));
|
||||
|
||||
manager().create(spec);
|
||||
|
||||
assertEquals(
|
||||
Map.of("floci", "true", "floci_emulator", "floci-aws",
|
||||
"io.floci.owner", "alpha/4566", "floci_owner_port", "alpha/4566",
|
||||
"io.floci.ecs.run", "run-1", "floci.ecs-run", "run-1"),
|
||||
capturedLabels(createCmd));
|
||||
}
|
||||
|
||||
@Test
|
||||
void protectedWorkloadCannotAdministerOrSpoofItsNetwork() {
|
||||
CreateContainerCmd createCmd = stubCreateContainer();
|
||||
|
||||
manager().create(specWithLabels(Map.of(ContainerStorageHelper.SECURITY_GROUP_WORKLOAD_LABEL, "true")));
|
||||
|
||||
ArgumentCaptor<HostConfig> hostConfig = ArgumentCaptor.forClass(HostConfig.class);
|
||||
verify(createCmd).withHostConfig(hostConfig.capture());
|
||||
List<Capability> dropped = List.of(hostConfig.getValue().getCapDrop());
|
||||
assertTrue(dropped.contains(Capability.NET_ADMIN));
|
||||
assertTrue(dropped.contains(Capability.NET_RAW));
|
||||
}
|
||||
|
||||
@Test
|
||||
void protectedWorkloadSpecWithOnlyTheLegacyLabelStillDropsNetworkAdministration() {
|
||||
CreateContainerCmd createCmd = stubCreateContainer();
|
||||
|
||||
manager().create(specWithLabels(Map.of("floci.security-group-workload", "true")));
|
||||
|
||||
ArgumentCaptor<HostConfig> hostConfig = ArgumentCaptor.forClass(HostConfig.class);
|
||||
@@ -128,6 +157,17 @@ class ContainerLifecycleManagerLabelsTest {
|
||||
assertNotEquals(Boolean.TRUE, hostConfig.getValue().getPrivileged());
|
||||
}
|
||||
|
||||
@Test
|
||||
void firewallHelperSpecWithTheNewLabelGetsNetAdmin() {
|
||||
CreateContainerCmd createCmd = stubCreateContainer();
|
||||
|
||||
manager().create(specWithLabels(Map.of(ContainerStorageHelper.SECURITY_GROUP_HELPER_LABEL, "true")));
|
||||
|
||||
ArgumentCaptor<HostConfig> hostConfig = ArgumentCaptor.forClass(HostConfig.class);
|
||||
verify(createCmd).withHostConfig(hostConfig.capture());
|
||||
assertEquals(List.of(Capability.NET_ADMIN), List.of(hostConfig.getValue().getCapAdd()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void specLabelWinsOverDefaultOnKeyConflict() {
|
||||
CreateContainerCmd createCmd = stubCreateContainer();
|
||||
|
||||
+172
-4
@@ -4,10 +4,14 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -106,7 +110,7 @@ class ContainerStorageHelperTest {
|
||||
void extraLabelsAreMergedIntoDefaultLabels() {
|
||||
// A dotted key — exactly the shape that motivates list-of-entries config over a Map,
|
||||
// whose env-var naming convention cannot express such keys.
|
||||
EmulatorConfig config = config("", java.util.List.of(
|
||||
EmulatorConfig config = config("", List.of(
|
||||
label("com.example.project", "my-project"),
|
||||
label("environment", "dev")));
|
||||
|
||||
@@ -120,7 +124,7 @@ class ContainerStorageHelperTest {
|
||||
void extraLabelsCannotOverrideReservedKeys() {
|
||||
// The reserved labels drive container/volume discovery and pruning; a user label must
|
||||
// never be able to break `docker volume prune --filter label=floci=true` cleanup.
|
||||
EmulatorConfig config = config(" run/one ", java.util.List.of(
|
||||
EmulatorConfig config = config(" run/one ", List.of(
|
||||
label("floci", "false"),
|
||||
label("floci_emulator", "spoofed"),
|
||||
label("floci_namespace", "spoofed"),
|
||||
@@ -134,7 +138,7 @@ class ContainerStorageHelperTest {
|
||||
|
||||
@Test
|
||||
void blankExtraLabelKeysAreIgnored() {
|
||||
EmulatorConfig config = config("", java.util.List.of(
|
||||
EmulatorConfig config = config("", List.of(
|
||||
label(" ", "dropped"),
|
||||
label(null, "dropped"),
|
||||
label("kept", null)));
|
||||
@@ -164,6 +168,170 @@ class ContainerStorageHelperTest {
|
||||
ContainerStorageHelper.resourceIdentityLabels("ecr", null, "", " "));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extraLabelsCannotSetAnAliasedKeyNewOrLegacy() {
|
||||
// A cleanup path trusts the owner and identity labels; a user label must never fake one.
|
||||
EmulatorConfig config = config("", List.of(
|
||||
label("io.floci.owner", "spoofed"),
|
||||
label("floci_owner_port", "spoofed"),
|
||||
label("io.floci.ecs.run", "spoofed"),
|
||||
label("floci.ecs-run", "spoofed"),
|
||||
label("floci.security-group-helper", "true"),
|
||||
label("io.floci.component", "spoofed"),
|
||||
label("floci_vpc_owner_port", "spoofed"),
|
||||
label("floci_vpc_id", "spoofed"),
|
||||
label("io.floci.resource-id", "spoofed"),
|
||||
label("floci_component", "ec2-vpc"),
|
||||
label("kept", "yes")));
|
||||
|
||||
assertEquals(
|
||||
Map.of("floci", "true", "floci_emulator", "floci-aws", "kept", "yes"),
|
||||
ContainerStorageHelper.defaultLabels(config));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ownerIdentityPrefixesTheResourceNamespaceToThePort() {
|
||||
EmulatorConfig plain = config("");
|
||||
when(plain.port()).thenReturn(4566);
|
||||
EmulatorConfig namespaced = config("alpha");
|
||||
when(namespaced.port()).thenReturn(4566);
|
||||
|
||||
assertEquals("4566", ContainerStorageHelper.ownerIdentity(plain));
|
||||
assertEquals("alpha/4566", ContainerStorageHelper.ownerIdentity(namespaced));
|
||||
}
|
||||
|
||||
@Test
|
||||
void containerAliasesWriteEachLegacyKeyWithTheNewKeysValue() {
|
||||
Map<String, String> labels = ContainerStorageHelper.CONTAINER_LABEL_ALIASES.withLegacyAliases(Map.of(
|
||||
"io.floci.owner", "alpha/4566",
|
||||
"io.floci.ecs.run", "run-1",
|
||||
"io.floci.ecs.credentials-proxy", "true",
|
||||
"io.floci.security-group.helper", "true",
|
||||
"io.floci.security-group.workload", "true",
|
||||
"io.floci.component", "pipes-kafka-rest-bridge",
|
||||
"io.floci.service", "ecs"));
|
||||
|
||||
assertEquals("alpha/4566", labels.get("floci_owner_port"));
|
||||
assertEquals("run-1", labels.get("floci.ecs-run"));
|
||||
assertEquals("true", labels.get("floci.ecs-task-role-credentials-proxy"));
|
||||
assertEquals("true", labels.get("floci.security-group-helper"));
|
||||
assertEquals("true", labels.get("floci.security-group-workload"));
|
||||
assertEquals("pipes-kafka-rest-bridge", labels.get("floci.component"));
|
||||
assertEquals("alpha/4566", labels.get("io.floci.owner"), "the new key stays");
|
||||
assertEquals("ecs", labels.get("io.floci.service"));
|
||||
assertEquals(13, labels.size(), "a key without an alias gains nothing");
|
||||
}
|
||||
|
||||
@Test
|
||||
void networkAliasesUseTheirOwnLegacyKeysAndTheComposite() {
|
||||
Map<String, String> labels = ContainerStorageHelper.NETWORK_LABEL_ALIASES.withLegacyAliases(Map.of(
|
||||
"io.floci.owner", "alpha/4566",
|
||||
"io.floci.resource-id", "vpc-1",
|
||||
"io.floci.region", "us-east-1",
|
||||
"io.floci.service", "ec2",
|
||||
"io.floci.component", "vpc-network"));
|
||||
|
||||
assertEquals("alpha/4566", labels.get("floci_vpc_owner_port"));
|
||||
assertFalse(labels.containsKey("floci_owner_port"), "the container alias of the owner is not a network's");
|
||||
assertEquals("vpc-1", labels.get("floci_vpc_id"));
|
||||
assertEquals("us-east-1", labels.get("floci_vpc_region"));
|
||||
assertEquals("ec2-vpc", labels.get("floci_component"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theCompositeAliasNeedsEveryOneOfItsNewLabels() {
|
||||
Map<String, String> labels = ContainerStorageHelper.NETWORK_LABEL_ALIASES.withLegacyAliases(Map.of(
|
||||
"io.floci.service", "ec2", "io.floci.component", "something-else"));
|
||||
|
||||
assertFalse(labels.containsKey("floci_component"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void labelValuePrefersTheNewKeyAndFallsBackToTheLegacyOne() {
|
||||
ContainerStorageHelper.LabelAliases aliases = ContainerStorageHelper.CONTAINER_LABEL_ALIASES;
|
||||
|
||||
assertEquals("4566", aliases.labelValue(Map.of("io.floci.owner", "4566"), "io.floci.owner"));
|
||||
assertEquals("4566", aliases.labelValue(Map.of("floci_owner_port", "4566"), "io.floci.owner"));
|
||||
assertEquals("4566", aliases.labelValue(
|
||||
Map.of("io.floci.owner", "4566", "floci_owner_port", "4566"), "io.floci.owner"));
|
||||
assertEquals("4566", aliases.labelValue(
|
||||
Map.of("io.floci.owner", "4566", "floci_owner_port", "4567"), "io.floci.owner"));
|
||||
assertNull(aliases.labelValue(Map.of(), "io.floci.owner"));
|
||||
assertNull(aliases.labelValue(null, "io.floci.owner"));
|
||||
assertEquals("run-1", ContainerStorageHelper.labelValue(Map.of("floci.ecs-run", "run-1"), "io.floci.ecs.run"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void labelValueReadsTheCompositeBackIntoEachOfItsNewKeys() {
|
||||
ContainerStorageHelper.LabelAliases aliases = ContainerStorageHelper.NETWORK_LABEL_ALIASES;
|
||||
Map<String, String> legacy = Map.of("floci_component", "ec2-vpc", "floci_vpc_id", "vpc-1");
|
||||
|
||||
assertEquals("ec2", aliases.labelValue(legacy, "io.floci.service"));
|
||||
assertEquals("vpc-network", aliases.labelValue(legacy, "io.floci.component"));
|
||||
assertEquals("vpc-1", aliases.labelValue(legacy, "io.floci.resource-id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesRequiresEveryOwnerKeyPresentToAgree() {
|
||||
ContainerStorageHelper.LabelAliases aliases = ContainerStorageHelper.CONTAINER_LABEL_ALIASES;
|
||||
|
||||
assertTrue(aliases.matches(Map.of("io.floci.owner", "4566"), "io.floci.owner", "4566"));
|
||||
assertTrue(aliases.matches(Map.of("floci_owner_port", "4566"), "io.floci.owner", "4566"));
|
||||
assertTrue(aliases.matches(
|
||||
Map.of("io.floci.owner", "4566", "floci_owner_port", "4566"), "io.floci.owner", "4566"));
|
||||
assertFalse(aliases.matches(
|
||||
Map.of("io.floci.owner", "4566", "floci_owner_port", "4567"), "io.floci.owner", "4566"));
|
||||
assertFalse(aliases.matches(
|
||||
Map.of("io.floci.owner", "4567", "floci_owner_port", "4566"), "io.floci.owner", "4566"));
|
||||
assertFalse(aliases.matches(Map.of(), "io.floci.owner", "4566"), "an unowned object matches nobody");
|
||||
}
|
||||
|
||||
@Test
|
||||
void consistentRejectsAnObjectWhoseNewAndLegacyLabelsDisagree() {
|
||||
ContainerStorageHelper.LabelAliases containers = ContainerStorageHelper.CONTAINER_LABEL_ALIASES;
|
||||
ContainerStorageHelper.LabelAliases networks = ContainerStorageHelper.NETWORK_LABEL_ALIASES;
|
||||
|
||||
assertTrue(containers.consistent("c", Map.of("io.floci.owner", "4566")));
|
||||
assertTrue(containers.consistent("c", Map.of("floci_owner_port", "4566")));
|
||||
assertTrue(containers.consistent("c", Map.of("io.floci.owner", "4566", "floci_owner_port", "4566")));
|
||||
assertFalse(containers.consistent("c", Map.of("io.floci.owner", "4566", "floci_owner_port", "4567")));
|
||||
assertFalse(containers.consistent("c", Map.of("io.floci.ecs.run", "a", "floci.ecs-run", "b")));
|
||||
assertTrue(networks.consistent("n", Map.of(
|
||||
"io.floci.service", "ec2", "io.floci.component", "vpc-network", "floci_component", "ec2-vpc")));
|
||||
assertFalse(networks.consistent("n", Map.of("io.floci.component", "other", "floci_component", "ec2-vpc")));
|
||||
assertFalse(networks.consistent("n", Map.of("io.floci.resource-id", "vpc-1", "floci_vpc_id", "vpc-2")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void labelFiltersSpellTheSameFilterWithTheLegacyKeys() {
|
||||
assertEquals(
|
||||
List.of(Map.of("io.floci.service", "ecs", "io.floci.owner", "4566"),
|
||||
Map.of("io.floci.service", "ecs", "floci_owner_port", "4566")),
|
||||
ContainerStorageHelper.CONTAINER_LABEL_ALIASES.labelFilters(
|
||||
Map.of("io.floci.service", "ecs", "io.floci.owner", "4566")));
|
||||
assertEquals(
|
||||
List.of(Map.of("io.floci.service", "ec2", "io.floci.component", "vpc-network"),
|
||||
Map.of("floci_component", "ec2-vpc")),
|
||||
ContainerStorageHelper.NETWORK_LABEL_ALIASES.labelFilters(
|
||||
Map.of("io.floci.service", "ec2", "io.floci.component", "vpc-network")));
|
||||
assertEquals(
|
||||
List.of(Map.of("io.floci.service", "ec2")),
|
||||
ContainerStorageHelper.CONTAINER_LABEL_ALIASES.labelFilters(Map.of("io.floci.service", "ec2")),
|
||||
"a filter with no aliased key is queried once");
|
||||
}
|
||||
|
||||
@Test
|
||||
void listByLabelsQueriesEachKeySetAndListsEveryObjectOnce() {
|
||||
Map<Map<String, String>, List<String>> daemon = Map.of(
|
||||
Map.of("io.floci.owner", "4566"), List.of("new-only", "both"),
|
||||
Map.of("floci_owner_port", "4566"), List.of("legacy-only", "both"));
|
||||
|
||||
List<String> found = ContainerStorageHelper.CONTAINER_LABEL_ALIASES.listByLabels(
|
||||
Map.of("io.floci.owner", "4566"), daemon::get, id -> id);
|
||||
|
||||
assertEquals(List.of("new-only", "both", "legacy-only"), found);
|
||||
}
|
||||
|
||||
private static EmulatorConfig.DockerConfig.LabelEntry label(String key, String value) {
|
||||
return new EmulatorConfig.DockerConfig.LabelEntry() {
|
||||
@Override
|
||||
@@ -179,7 +347,7 @@ class ContainerStorageHelperTest {
|
||||
}
|
||||
|
||||
private static EmulatorConfig config(String namespace) {
|
||||
return config(namespace, java.util.List.of());
|
||||
return config(namespace, List.of());
|
||||
}
|
||||
|
||||
private static EmulatorConfig config(
|
||||
|
||||
+102
-13
@@ -45,6 +45,7 @@ import java.io.Closeable;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -342,6 +343,63 @@ class Ec2ContainerManagerTest {
|
||||
|
||||
@Test
|
||||
void startOfAProtectedInstanceReusesTheHelperTransportAddress() throws Exception {
|
||||
Ec2MetadataServer metadataServer = mock(Ec2MetadataServer.class);
|
||||
Instance instance = startProtectedInstance(metadataServer, Map.of(
|
||||
"floci.security-group-helper", "true",
|
||||
"io.floci.service", "ec2",
|
||||
"io.floci.resource-id", "i-protected",
|
||||
"floci_owner_port", "4566"));
|
||||
|
||||
awaitUntil(() -> "running".equals(instance.getState().getName()), Duration.ofSeconds(5));
|
||||
verify(metadataServer, timeout(2000)).registerContainer("172.17.0.7", "i-protected", instance);
|
||||
assertEquals("172.17.0.7", instance.getContainerBridgeIp(),
|
||||
"StartInstances must keep addressing the instance through its protected namespace");
|
||||
}
|
||||
|
||||
@Test
|
||||
void startOfAProtectedInstanceAcceptsAHelperLabelledWithTheNewKeys() throws Exception {
|
||||
Ec2MetadataServer metadataServer = mock(Ec2MetadataServer.class);
|
||||
Instance instance = startProtectedInstance(metadataServer, Map.of(
|
||||
"io.floci.security-group.helper", "true",
|
||||
"io.floci.service", "ec2",
|
||||
"io.floci.resource-id", "i-protected",
|
||||
"io.floci.owner", "4566"));
|
||||
|
||||
awaitUntil(() -> "running".equals(instance.getState().getName()), Duration.ofSeconds(5));
|
||||
verify(metadataServer, timeout(2000)).registerContainer("172.17.0.7", "i-protected", instance);
|
||||
}
|
||||
|
||||
@Test
|
||||
void startOfAProtectedInstanceAcceptsAHelperWhoseNewAndLegacyKeysAgree() throws Exception {
|
||||
Ec2MetadataServer metadataServer = mock(Ec2MetadataServer.class);
|
||||
Instance instance = startProtectedInstance(metadataServer, Map.of(
|
||||
"io.floci.security-group.helper", "true",
|
||||
"floci.security-group-helper", "true",
|
||||
"io.floci.service", "ec2",
|
||||
"io.floci.resource-id", "i-protected",
|
||||
"io.floci.owner", "4566",
|
||||
"floci_owner_port", "4566"));
|
||||
|
||||
awaitUntil(() -> "running".equals(instance.getState().getName()), Duration.ofSeconds(5));
|
||||
verify(metadataServer, timeout(2000)).registerContainer("172.17.0.7", "i-protected", instance);
|
||||
}
|
||||
|
||||
@Test
|
||||
void startOfAProtectedInstanceRefusesAHelperWhoseNewAndLegacyOwnersDisagree() throws Exception {
|
||||
Ec2MetadataServer metadataServer = mock(Ec2MetadataServer.class);
|
||||
Instance instance = startProtectedInstance(metadataServer, Map.of(
|
||||
"io.floci.security-group.helper", "true",
|
||||
"floci.security-group-helper", "true",
|
||||
"io.floci.service", "ec2",
|
||||
"io.floci.resource-id", "i-protected",
|
||||
"io.floci.owner", "4566",
|
||||
"floci_owner_port", "4567"));
|
||||
|
||||
awaitUntil(() -> "stopped".equals(instance.getState().getName()), Duration.ofSeconds(5));
|
||||
verify(metadataServer, never()).registerContainer(anyString(), anyString(), any());
|
||||
}
|
||||
|
||||
private Instance startProtectedInstance(Ec2MetadataServer metadataServer, Map<String, String> helperLabels) {
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
when(lifecycleManager.isContainerRunning(TEST_CONTAINER_ID)).thenReturn(true);
|
||||
|
||||
@@ -360,11 +418,7 @@ class Ec2ContainerManagerTest {
|
||||
|
||||
InspectContainerResponse helper = mock(InspectContainerResponse.class);
|
||||
ContainerConfig helperConfig = mock(ContainerConfig.class);
|
||||
when(helperConfig.getLabels()).thenReturn(Map.of(
|
||||
"floci.security-group-helper", "true",
|
||||
"io.floci.service", "ec2",
|
||||
"io.floci.resource-id", "i-protected",
|
||||
"floci_owner_port", "4566"));
|
||||
when(helperConfig.getLabels()).thenReturn(helperLabels);
|
||||
when(helper.getConfig()).thenReturn(helperConfig);
|
||||
NetworkSettings helperNetworks = mock(NetworkSettings.class);
|
||||
when(helperNetworks.getNetworks())
|
||||
@@ -378,7 +432,6 @@ class Ec2ContainerManagerTest {
|
||||
when(config.port()).thenReturn(4566);
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.empty());
|
||||
|
||||
Ec2MetadataServer metadataServer = mock(Ec2MetadataServer.class);
|
||||
Ec2ContainerManager manager = new Ec2ContainerManager(
|
||||
mock(ContainerBuilder.class),
|
||||
lifecycleManager,
|
||||
@@ -401,11 +454,7 @@ class Ec2ContainerManagerTest {
|
||||
instance.setDockerContainerId(TEST_CONTAINER_ID);
|
||||
|
||||
manager.start(instance);
|
||||
awaitUntil(() -> "running".equals(instance.getState().getName()), Duration.ofSeconds(5));
|
||||
|
||||
verify(metadataServer, timeout(2000)).registerContainer("172.17.0.7", "i-protected", instance);
|
||||
assertEquals("172.17.0.7", instance.getContainerBridgeIp(),
|
||||
"StartInstances must keep addressing the instance through its protected namespace");
|
||||
return instance;
|
||||
}
|
||||
|
||||
private static Ec2ContainerManager managerWith(ContainerLifecycleManager lifecycleManager,
|
||||
@@ -1618,7 +1667,7 @@ class Ec2ContainerManagerTest {
|
||||
awaitUntil(() -> "running".equals(instance.getState().getName()), Duration.ofSeconds(2));
|
||||
// Without this label the reconciler cannot tell one emulator's containers from another's
|
||||
// on a shared Docker daemon, so it could only ever be unsafe or a no-op.
|
||||
verify(harness.builder).withLabels(Map.of(Ec2ContainerManager.LABEL_OWNER_PORT, "4680"));
|
||||
verify(harness.builder).withLabels(Map.of(Ec2ContainerManager.LABEL_OWNER, "4680"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1687,6 +1736,46 @@ class Ec2ContainerManagerTest {
|
||||
verify(harness.lifecycleManager, never()).removeIfExists("c-sibling");
|
||||
}
|
||||
|
||||
@Test
|
||||
void reconcileOrphanedContainersFindsOwnersUnderTheLegacyKeyOrBoth() {
|
||||
LaunchHarness harness = reconcileHarness(true, 4680);
|
||||
stubContainerListing(harness.dockerClient,
|
||||
withOwners("c-legacy", Map.of("floci_owner_port", "4680"), "i-legacy"),
|
||||
withOwners("c-both", Map.of("io.floci.owner", "4680", "floci_owner_port", "4680"), "i-both"),
|
||||
withOwners("c-legacy-sibling", Map.of("floci_owner_port", "4620"), "i-sibling"));
|
||||
|
||||
int removed = harness.manager.reconcileOrphanedContainers((region, instanceId) -> false);
|
||||
|
||||
assertEquals(2, removed);
|
||||
verify(harness.lifecycleManager).removeIfExists("c-legacy");
|
||||
verify(harness.lifecycleManager).removeIfExists("c-both");
|
||||
verify(harness.lifecycleManager, never()).removeIfExists("c-legacy-sibling");
|
||||
}
|
||||
|
||||
@Test
|
||||
void reconcileOrphanedContainersLeavesAContainerWhoseOwnersDisagreeAlone() {
|
||||
LaunchHarness harness = reconcileHarness(true, 4680);
|
||||
stubContainerListing(harness.dockerClient,
|
||||
withOwners("c-new-ours", Map.of("io.floci.owner", "4680", "floci_owner_port", "4620"), "i-1"),
|
||||
withOwners("c-legacy-ours", Map.of("io.floci.owner", "4620", "floci_owner_port", "4680"), "i-2"));
|
||||
|
||||
int removed = harness.manager.reconcileOrphanedContainers((region, instanceId) -> false);
|
||||
|
||||
assertEquals(0, removed);
|
||||
verify(harness.lifecycleManager, never()).removeIfExists(anyString());
|
||||
}
|
||||
|
||||
private static Container withOwners(String containerId, Map<String, String> owners, String instanceId) {
|
||||
Map<String, String> labels = new HashMap<>(owners);
|
||||
labels.put(Ec2ContainerManager.LABEL_SERVICE, Ec2ContainerManager.SERVICE_VALUE);
|
||||
labels.put(Ec2ContainerManager.LABEL_REGION, "us-east-1");
|
||||
labels.put(Ec2ContainerManager.LABEL_RESOURCE_ID, instanceId);
|
||||
Container container = mock(Container.class);
|
||||
when(container.getId()).thenReturn(containerId);
|
||||
when(container.getLabels()).thenReturn(labels);
|
||||
return container;
|
||||
}
|
||||
|
||||
@Test
|
||||
void reconcileOrphanedContainersDoesNothingWhenDisabled() {
|
||||
LaunchHarness harness = reconcileHarness(false, 4680);
|
||||
@@ -1701,7 +1790,7 @@ class Ec2ContainerManagerTest {
|
||||
when(container.getId()).thenReturn(containerId);
|
||||
when(container.getLabels()).thenReturn(Map.of(
|
||||
Ec2ContainerManager.LABEL_SERVICE, Ec2ContainerManager.SERVICE_VALUE,
|
||||
Ec2ContainerManager.LABEL_OWNER_PORT, ownerPort,
|
||||
Ec2ContainerManager.LABEL_OWNER, ownerPort,
|
||||
Ec2ContainerManager.LABEL_REGION, region,
|
||||
Ec2ContainerManager.LABEL_RESOURCE_ID, instanceId));
|
||||
return container;
|
||||
|
||||
+52
@@ -1,6 +1,8 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.ListContainersCmd;
|
||||
import com.github.dockerjava.api.model.Container;
|
||||
import com.github.dockerjava.api.model.Info;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
|
||||
@@ -11,6 +13,7 @@ import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -21,7 +24,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.RETURNS_SELF;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -73,4 +78,51 @@ class SecurityGroupFirewallManagerTest {
|
||||
when(embeddedDnsServer.getServerIp()).thenReturn(Optional.empty());
|
||||
assertEquals(List.of(), manager.vpcResolvers());
|
||||
}
|
||||
@Test
|
||||
void survivingEcsHelpersAreFoundUnderTheNewAndLegacyLabelsAndRemovedOnlyWhenTheirOwnersAgree() {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.network().securityGroupEnforcement().enabled()).thenReturn(true);
|
||||
when(config.port()).thenReturn(4566);
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.empty());
|
||||
DockerClient dockerClient = mock(DockerClient.class);
|
||||
ListContainersCmd listCmd = mock(ListContainersCmd.class, RETURNS_SELF);
|
||||
when(dockerClient.listContainersCmd()).thenReturn(listCmd);
|
||||
when(listCmd.exec()).thenReturn(List.of());
|
||||
ListContainersCmd byNewKeys = mock(ListContainersCmd.class);
|
||||
ListContainersCmd byLegacyKeys = mock(ListContainersCmd.class);
|
||||
when(listCmd.withLabelFilter(Map.of("io.floci.security-group.helper", "true"))).thenReturn(byNewKeys);
|
||||
when(listCmd.withLabelFilter(Map.of("floci.security-group-helper", "true"))).thenReturn(byLegacyKeys);
|
||||
Container newOnly = exitedEcsHelper("new-only",
|
||||
Map.of("io.floci.security-group.helper", "true", "io.floci.owner", "4566"));
|
||||
Container legacyOnly = exitedEcsHelper("legacy-only",
|
||||
Map.of("floci.security-group-helper", "true", "floci_owner_port", "4566"));
|
||||
Container both = exitedEcsHelper("both", Map.of(
|
||||
"io.floci.security-group.helper", "true", "io.floci.owner", "4566",
|
||||
"floci.security-group-helper", "true", "floci_owner_port", "4566"));
|
||||
Container disagreeing = exitedEcsHelper("disagreeing", Map.of(
|
||||
"io.floci.security-group.helper", "true", "io.floci.owner", "4566",
|
||||
"floci.security-group-helper", "true", "floci_owner_port", "4567"));
|
||||
when(byNewKeys.exec()).thenReturn(List.of(newOnly, both, disagreeing));
|
||||
when(byLegacyKeys.exec()).thenReturn(List.of(legacyOnly, both, disagreeing));
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
SecurityGroupFirewallManager manager = new SecurityGroupFirewallManager(dockerClient,
|
||||
mock(ContainerBuilder.class), lifecycleManager, config, mock(EmbeddedDnsServer.class));
|
||||
|
||||
manager.quarantineSurvivingNamespaces();
|
||||
|
||||
verify(lifecycleManager).removeIfExists("new-only");
|
||||
verify(lifecycleManager).removeIfExists("legacy-only");
|
||||
verify(lifecycleManager).removeIfExists("both");
|
||||
verify(lifecycleManager, never()).removeIfExists("disagreeing");
|
||||
}
|
||||
|
||||
private static Container exitedEcsHelper(String id, Map<String, String> ownerLabels) {
|
||||
Map<String, String> labels = new HashMap<>(ownerLabels);
|
||||
labels.put("io.floci.service", "ecs");
|
||||
Container container = mock(Container.class);
|
||||
when(container.getId()).thenReturn(id);
|
||||
when(container.getState()).thenReturn("exited");
|
||||
when(container.getLabels()).thenReturn(labels);
|
||||
return container;
|
||||
}
|
||||
}
|
||||
+179
-4
@@ -21,6 +21,7 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@@ -28,6 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.RETURNS_SELF;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -519,12 +521,185 @@ class VpcNetworkManagerTest {
|
||||
assertEquals("10.0.0.0/16", manager.effectiveVpcCidr(REGION, "vpc-1").orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createdNetworkCarriesTheIdentityLabelsAndTheirLegacyAliases() {
|
||||
manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");
|
||||
manager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");
|
||||
String address = manager.allocatePrivateIp(REGION, "subnet-a").orElseThrow();
|
||||
|
||||
manager.attach(REGION, "vpc-1", "subnet-a", "container-1", address);
|
||||
|
||||
Map<String, String> expected = new LinkedHashMap<>();
|
||||
expected.put("floci", "true");
|
||||
expected.put("floci_emulator", "floci-aws");
|
||||
expected.put("io.floci", "aws");
|
||||
expected.put("io.floci.service", "ec2");
|
||||
expected.put("io.floci.resource-id", "vpc-1");
|
||||
expected.put("io.floci.region", REGION);
|
||||
expected.put("io.floci.component", "vpc-network");
|
||||
expected.put("io.floci.owner", "4650");
|
||||
expected.put("floci_component", "ec2-vpc");
|
||||
expected.put("floci_vpc_id", "vpc-1");
|
||||
expected.put("floci_vpc_region", REGION);
|
||||
expected.put("floci_vpc_owner_port", "4650");
|
||||
assertEquals(expected, createdNetworkLabels());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNamespacedEmulatorOwnsItsNetworksByNamespaceAndPort() {
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.of("alpha"));
|
||||
manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");
|
||||
manager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");
|
||||
String address = manager.allocatePrivateIp(REGION, "subnet-a").orElseThrow();
|
||||
|
||||
manager.attach(REGION, "vpc-1", "subnet-a", "container-1", address);
|
||||
|
||||
Map<String, String> labels = createdNetworkLabels();
|
||||
assertEquals("alpha/4650", labels.get("io.floci.owner"));
|
||||
assertEquals("alpha/4650", labels.get("floci_vpc_owner_port"));
|
||||
}
|
||||
|
||||
/** The labels a namespaced or plain Floci wrote on a VPC network before the io.floci.owner key. */
|
||||
private static Map<String, String> preUpgradeLabels(String vpcId, String namespace) {
|
||||
Map<String, String> labels = new LinkedHashMap<>();
|
||||
labels.put("floci", "true");
|
||||
if (namespace != null) {
|
||||
labels.put("floci_namespace", namespace);
|
||||
}
|
||||
labels.put("floci_component", "ec2-vpc");
|
||||
labels.put("floci_vpc_id", vpcId);
|
||||
labels.put("floci_vpc_region", REGION);
|
||||
labels.put("floci_vpc_owner_port", "4650");
|
||||
return labels;
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPreUpgradeNetworkOfThisNamespacedEmulatorIsNotReadAsACollision() {
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.of("alpha"));
|
||||
existingNetwork(manager.networkName(REGION, "vpc-1"), "10.0.0.0/16", preUpgradeLabels("vpc-1", "alpha"));
|
||||
|
||||
manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");
|
||||
|
||||
assertFalse(manager.isSubstituted(REGION, "vpc-1"),
|
||||
"a network this namespace created before the owner label took the namespace is still its own");
|
||||
assertEquals("10.0.0.0/16", manager.effectiveVpcCidr(REGION, "vpc-1").orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void reconcileRemovesAPreUpgradeOrphanOnlyWhenItsNamespaceIsThisEmulators() {
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.of("alpha"));
|
||||
existingNetwork("floci-aws-alpha-vpc-4650-us-east-1-vpc-dead", "10.7.0.0/16",
|
||||
preUpgradeLabels("vpc-dead", "alpha"));
|
||||
existingNetwork("floci-aws-beta-vpc-4650-us-east-1-vpc-beta", "10.8.0.0/16",
|
||||
preUpgradeLabels("vpc-beta", "beta"));
|
||||
existingNetwork("floci-aws-vpc-4650-us-east-1-vpc-plain", "10.9.0.0/16",
|
||||
preUpgradeLabels("vpc-plain", null));
|
||||
|
||||
manager.reconcileOrphans((region, vpcId) -> false);
|
||||
|
||||
assertEquals(List.of("floci-aws-alpha-vpc-4650-us-east-1-vpc-dead"), removedNetworks,
|
||||
"the bare port in the legacy owner key is shared by every deployment on it, so only the "
|
||||
+ "namespace label decides which pre-upgrade network is this emulator's");
|
||||
}
|
||||
|
||||
@Test
|
||||
void reconcileQueriesTheNewAndLegacyKeySetsAndRemovesOrphansUnderEither() {
|
||||
existingNetwork("vpc-new", "10.7.0.0/16", newLabels("vpc-new", "4650"));
|
||||
existingNetwork("vpc-legacy", "10.8.0.0/16", ourLabels("vpc-legacy", "4650"));
|
||||
existingNetwork("vpc-both", "10.9.0.0/16", bothLabels("vpc-both", "4650"));
|
||||
|
||||
manager.reconcileOrphans((region, vpcId) -> false);
|
||||
|
||||
assertEquals(List.of("vpc-new", "vpc-legacy", "vpc-both"), removedNetworks,
|
||||
"each network is removed once, however many of the queries list it");
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<String>> filters = ArgumentCaptor.forClass(List.class);
|
||||
verify(docker.listNetworksCmd(), times(2)).withFilter(eq("label"),
|
||||
filters.capture());
|
||||
assertEquals(List.of(Set.of("io.floci.service=ec2", "io.floci.component=vpc-network"),
|
||||
Set.of("floci_component=ec2-vpc")),
|
||||
filters.getAllValues().stream().map(Set::copyOf).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void reconcileLeavesANetworkWhoseNewAndLegacyLabelsDisagreeAlone() {
|
||||
Map<String, String> ownerDisagrees = bothLabels("vpc-1", "4650");
|
||||
ownerDisagrees.put("floci_vpc_owner_port", "4620");
|
||||
existingNetwork("owner-disagrees", "10.7.0.0/16", ownerDisagrees);
|
||||
Map<String, String> vpcDisagrees = bothLabels("vpc-dead", "4650");
|
||||
vpcDisagrees.put("floci_vpc_id", "vpc-live");
|
||||
existingNetwork("vpc-disagrees", "10.8.0.0/16", vpcDisagrees);
|
||||
|
||||
manager.reconcileOrphans((region, vpcId) -> "vpc-live".equals(vpcId));
|
||||
|
||||
assertTrue(removedNetworks.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoNamespacedEmulatorsOnTheSamePortNeverReconcileEachOthersNetworks() {
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.of("alpha"));
|
||||
existingNetwork("alpha-dead", "10.7.0.0/16", bothLabels("vpc-a", "alpha/4650"));
|
||||
existingNetwork("beta-live", "10.8.0.0/16", bothLabels("vpc-b", "beta/4650"));
|
||||
// Written by a namespaced emulator before owners carried the namespace: a bare port no
|
||||
// longer names this deployment, so it is left alone rather than deleted.
|
||||
existingNetwork("bare-port", "10.9.0.0/16", ourLabels("vpc-c", "4650"));
|
||||
|
||||
manager.reconcileOrphans((region, vpcId) -> false);
|
||||
|
||||
assertEquals(List.of("alpha-dead"), removedNetworks);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anotherNamespacesNetworkForTheSameVpcIdIsStillACollision() {
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.of("alpha"));
|
||||
existingNetwork("beta-vpc-1", "10.0.0.0/16", bothLabels("vpc-1", "beta/4650"));
|
||||
|
||||
manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");
|
||||
|
||||
assertTrue(manager.isSubstituted(REGION, "vpc-1"),
|
||||
"a sibling deployment's live network holds that range; it is not this VPC's own");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSurvivingNamespacedNetworkForTheSameVpcIsNotReadAsACollision() {
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.of("alpha"));
|
||||
existingNetwork("alpha-vpc-1", "10.0.0.0/16", newLabels("vpc-1", "alpha/4650"));
|
||||
|
||||
manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");
|
||||
|
||||
assertFalse(manager.isSubstituted(REGION, "vpc-1"));
|
||||
}
|
||||
|
||||
private Map<String, String> createdNetworkLabels() {
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Map<String, String>> labels = ArgumentCaptor.forClass(Map.class);
|
||||
verify(docker.createNetworkCmd()).withLabels(labels.capture());
|
||||
return labels.getValue();
|
||||
}
|
||||
|
||||
/** What a network created before the {@code io.floci.*} keys carries. */
|
||||
private static Map<String, String> ourLabels(String vpcId, String ownerPort) {
|
||||
Map<String, String> labels = new LinkedHashMap<>();
|
||||
labels.put(VpcNetworkManager.LABEL_COMPONENT, VpcNetworkManager.COMPONENT_VALUE);
|
||||
labels.put(VpcNetworkManager.LABEL_VPC_ID, vpcId);
|
||||
labels.put(VpcNetworkManager.LABEL_VPC_REGION, REGION);
|
||||
labels.put(VpcNetworkManager.LABEL_OWNER_PORT, ownerPort);
|
||||
labels.put("floci_component", "ec2-vpc");
|
||||
labels.put("floci_vpc_id", vpcId);
|
||||
labels.put("floci_vpc_region", REGION);
|
||||
labels.put("floci_vpc_owner_port", ownerPort);
|
||||
return labels;
|
||||
}
|
||||
|
||||
private static Map<String, String> newLabels(String vpcId, String owner) {
|
||||
Map<String, String> labels = new LinkedHashMap<>();
|
||||
labels.put("io.floci.service", "ec2");
|
||||
labels.put("io.floci.component", "vpc-network");
|
||||
labels.put("io.floci.resource-id", vpcId);
|
||||
labels.put("io.floci.region", REGION);
|
||||
labels.put("io.floci.owner", owner);
|
||||
return labels;
|
||||
}
|
||||
|
||||
private static Map<String, String> bothLabels(String vpcId, String owner) {
|
||||
Map<String, String> labels = newLabels(vpcId, owner);
|
||||
labels.putAll(ourLabels(vpcId, owner));
|
||||
return labels;
|
||||
}
|
||||
|
||||
|
||||
+90
-1
@@ -1,8 +1,10 @@
|
||||
package io.github.hectorvent.floci.services.ecs.container;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.ListContainersCmd;
|
||||
import com.github.dockerjava.api.command.RemoveContainerCmd;
|
||||
import com.github.dockerjava.api.command.StopContainerCmd;
|
||||
import com.github.dockerjava.api.model.Container;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
@@ -18,13 +20,18 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.RETURNS_SELF;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
@@ -204,10 +211,92 @@ class EcsContainerManagerLifecycleTest {
|
||||
assertFalse(handle.getLogStreamsByContainerId().containsKey("stopped-id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeLeftoverContainersFindsOwnLeftoversUnderTheNewAndLegacyLabels() {
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
Map<String, Container> newKeys = Map.of(
|
||||
"new-only", leftover("new-only", Map.of(
|
||||
"io.floci.service", "ecs", "io.floci.owner", "alpha/4566", "io.floci.ecs.run", "old-run")),
|
||||
"both", leftover("both", Map.of(
|
||||
"io.floci.service", "ecs", "io.floci.owner", "alpha/4566", "io.floci.ecs.run", "old-run",
|
||||
"floci_owner_port", "alpha/4566", "floci.ecs-run", "old-run")),
|
||||
"current-run", leftover("current-run", Map.of(
|
||||
"io.floci.service", "ecs", "io.floci.owner", "alpha/4566", "io.floci.ecs.run", "current")));
|
||||
Map<String, Container> legacyKeys = Map.of(
|
||||
"legacy-only", leftover("legacy-only", Map.of(
|
||||
"io.floci.service", "ecs", "floci_owner_port", "alpha/4566", "floci.ecs-run", "old-run")),
|
||||
"both", newKeys.get("both"),
|
||||
"legacy-current-run", leftover("legacy-current-run", Map.of(
|
||||
"io.floci.service", "ecs", "floci_owner_port", "alpha/4566", "floci.ecs-run", "current")));
|
||||
stubLeftoverListing(lifecycleManager, newKeys, legacyKeys);
|
||||
|
||||
assertTrue(manager(lifecycleManager, namespacedConfig()).removeLeftoverContainers("current"));
|
||||
|
||||
verify(lifecycleManager).removeIfExistsStrict("new-only");
|
||||
verify(lifecycleManager).removeIfExistsStrict("legacy-only");
|
||||
verify(lifecycleManager).removeIfExistsStrict("both");
|
||||
verify(lifecycleManager, never()).removeIfExistsStrict("current-run");
|
||||
verify(lifecycleManager, never()).removeIfExistsStrict("legacy-current-run");
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeLeftoverContainersLeavesAContainerWhoseNewAndLegacyLabelsDisagreeAlone() {
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
// Relabelled outside Floci: one owner key names a sibling deployment, or the run keys
|
||||
// disagree on whether the container is the current run's. Neither is removed.
|
||||
Map<String, Container> newKeys = Map.of(
|
||||
"owner-disagrees", leftover("owner-disagrees", Map.of(
|
||||
"io.floci.service", "ecs", "io.floci.owner", "alpha/4566", "io.floci.ecs.run", "old-run",
|
||||
"floci_owner_port", "beta/4566", "floci.ecs-run", "old-run")),
|
||||
"run-disagrees", leftover("run-disagrees", Map.of(
|
||||
"io.floci.service", "ecs", "io.floci.owner", "alpha/4566", "io.floci.ecs.run", "current",
|
||||
"floci_owner_port", "alpha/4566", "floci.ecs-run", "old-run")));
|
||||
Map<String, Container> legacyKeys = Map.of("run-disagrees", newKeys.get("run-disagrees"));
|
||||
stubLeftoverListing(lifecycleManager, newKeys, legacyKeys);
|
||||
|
||||
assertTrue(manager(lifecycleManager, namespacedConfig()).removeLeftoverContainers("current"));
|
||||
|
||||
verify(lifecycleManager, never()).removeIfExistsStrict(anyString());
|
||||
}
|
||||
|
||||
private static void stubLeftoverListing(ContainerLifecycleManager lifecycleManager,
|
||||
Map<String, Container> newKeys, Map<String, Container> legacyKeys) {
|
||||
DockerClient dockerClient = mock(DockerClient.class);
|
||||
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
|
||||
ListContainersCmd listCmd = mock(ListContainersCmd.class, RETURNS_SELF);
|
||||
when(dockerClient.listContainersCmd()).thenReturn(listCmd);
|
||||
ListContainersCmd byNewKeys = mock(ListContainersCmd.class);
|
||||
ListContainersCmd byLegacyKeys = mock(ListContainersCmd.class);
|
||||
when(listCmd.withLabelFilter(Map.of("io.floci.service", "ecs", "io.floci.owner", "alpha/4566")))
|
||||
.thenReturn(byNewKeys);
|
||||
when(listCmd.withLabelFilter(Map.of("io.floci.service", "ecs", "floci_owner_port", "alpha/4566")))
|
||||
.thenReturn(byLegacyKeys);
|
||||
when(byNewKeys.exec()).thenReturn(List.copyOf(newKeys.values()));
|
||||
when(byLegacyKeys.exec()).thenReturn(List.copyOf(legacyKeys.values()));
|
||||
}
|
||||
|
||||
private static Container leftover(String id, Map<String, String> labels) {
|
||||
Container container = mock(Container.class);
|
||||
when(container.getId()).thenReturn(id);
|
||||
when(container.getLabels()).thenReturn(labels);
|
||||
return container;
|
||||
}
|
||||
|
||||
private static EmulatorConfig namespacedConfig() {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.port()).thenReturn(4566);
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.of("alpha"));
|
||||
return config;
|
||||
}
|
||||
|
||||
private static EcsContainerManager manager(ContainerLifecycleManager lifecycleManager) {
|
||||
return manager(lifecycleManager, mock(EmulatorConfig.class));
|
||||
}
|
||||
|
||||
private static EcsContainerManager manager(ContainerLifecycleManager lifecycleManager, EmulatorConfig config) {
|
||||
return new EcsContainerManager(
|
||||
mock(ContainerBuilder.class), lifecycleManager, mock(ContainerLogStreamer.class),
|
||||
mock(ContainerDetector.class), mock(EmulatorConfig.class), mock(RegionResolver.class),
|
||||
mock(ContainerDetector.class), config, mock(RegionResolver.class),
|
||||
mock(LaunchedContainerAwsEnv.class), mock(SsmService.class), mock(SecretsManagerService.class), mock(S3Service.class),
|
||||
mock(EcrRegistryManager.class), mock(HostVolumePolicy.class));
|
||||
}
|
||||
|
||||
+3
-3
@@ -122,7 +122,7 @@ class EcsContainerManagerSecurityGroupTest {
|
||||
List.of(), "us-east-1");
|
||||
|
||||
verify(builder).withNetworkMode("container:helper-id");
|
||||
verify(builder).withLabels(Map.of("floci.security-group-workload", "true"));
|
||||
verify(builder).withLabels(Map.of("io.floci.security-group.workload", "true"));
|
||||
verify(builder, never()).withPortBinding(anyInt(), anyInt());
|
||||
verify(builder, never()).withDynamicPort(anyInt());
|
||||
verify(builder, never()).withExposedPort(anyInt());
|
||||
@@ -144,7 +144,7 @@ class EcsContainerManagerSecurityGroupTest {
|
||||
List.of(), "us-east-1");
|
||||
|
||||
verify(builder, never()).withNetworkMode(anyString());
|
||||
verify(builder, never()).withLabels(Map.of("floci.security-group-workload", "true"));
|
||||
verify(builder, never()).withLabels(Map.of("io.floci.security-group.workload", "true"));
|
||||
verify(ec2Service, never()).createNetworkInterface(any(), any(), any(), any(), any(), any(), any());
|
||||
verify(builder).withDynamicPort(80);
|
||||
}
|
||||
@@ -235,7 +235,7 @@ class EcsContainerManagerSecurityGroupTest {
|
||||
manager.startTask(awsvpcTask(), taskDef, List.of(), "us-east-1");
|
||||
|
||||
verify(builder, times(2)).withNetworkMode("container:helper-id");
|
||||
verify(builder, times(2)).withLabels(Map.of("floci.security-group-workload", "true"));
|
||||
verify(builder, times(2)).withLabels(Map.of("io.floci.security-group.workload", "true"));
|
||||
|
||||
verify(firewallManager).createNamespace(eq("ecs"), eq("abc123"), any(), any(), any(), any(), any());
|
||||
verify(lifecycleManager).create(any());
|
||||
|
||||
+3
-3
@@ -144,7 +144,7 @@ class EcsCredentialsProxyDockerIntegrationTest {
|
||||
dockerClient, containerBuilder, lifecycleManager, dockerHostResolver, config);
|
||||
proxy.ensureProxyOn(network);
|
||||
proxyContainerId = dockerClient.listContainersCmd()
|
||||
.withLabelFilter(Map.of("floci.ecs-task-role-credentials-proxy", "true"))
|
||||
.withLabelFilter(Map.of(ContainerStorageHelper.ECS_CREDENTIALS_PROXY_LABEL, "true"))
|
||||
.withNetworkFilter(List.of(networkId))
|
||||
.exec().get(0).getId();
|
||||
|
||||
@@ -213,7 +213,7 @@ class EcsCredentialsProxyDockerIntegrationTest {
|
||||
ContainerSpec staleSpec = containerBuilder.newContainer("floci/network-helper:local")
|
||||
.withName(name)
|
||||
.withDockerNetwork(Optional.of(network))
|
||||
.withLabels(Map.of("floci.ecs-task-role-credentials-proxy", "true"))
|
||||
.withLabels(Map.of(ContainerStorageHelper.ECS_CREDENTIALS_PROXY_LABEL, "true"))
|
||||
.build();
|
||||
String staleId = lifecycleManager.createAndStart(staleSpec).containerId();
|
||||
dockerClient.stopContainerCmd(staleId).withTimeout(1).exec();
|
||||
@@ -228,7 +228,7 @@ class EcsCredentialsProxyDockerIntegrationTest {
|
||||
try {
|
||||
proxy.ensureProxyOn(network);
|
||||
proxyContainerId = dockerClient.listContainersCmd()
|
||||
.withLabelFilter(Map.of("floci.ecs-task-role-credentials-proxy", "true"))
|
||||
.withLabelFilter(Map.of(ContainerStorageHelper.ECS_CREDENTIALS_PROXY_LABEL, "true"))
|
||||
.withNetworkFilter(List.of(networkId))
|
||||
.exec().get(0).getId();
|
||||
assertEquals("running", dockerClient.inspectContainerCmd(proxyContainerId)
|
||||
|
||||
+82
@@ -182,6 +182,47 @@ class EcsCredentialsProxyTest {
|
||||
verify(lifecycleManager, never()).removeIfExists("other-instance-proxy");
|
||||
}
|
||||
|
||||
@Test
|
||||
void removesAStaleContainerThisSameInstanceOwnsUnderTheNewLabels() {
|
||||
stubSuccessfulLaunch("proxy-1");
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.empty());
|
||||
when(config.port()).thenReturn(4566);
|
||||
stubStaleContainerByName("stale-own", Map.of(
|
||||
"io.floci.ecs.credentials-proxy", "true", "io.floci.owner", "4566"));
|
||||
|
||||
proxy.ensureProxyOn("floci-net");
|
||||
|
||||
verify(lifecycleManager).removeIfExists("stale-own");
|
||||
}
|
||||
|
||||
@Test
|
||||
void removesAStaleContainerWhoseNewAndLegacyLabelsAgree() {
|
||||
stubSuccessfulLaunch("proxy-1");
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.empty());
|
||||
when(config.port()).thenReturn(4566);
|
||||
stubStaleContainerByName("stale-own", Map.of(
|
||||
"io.floci.ecs.credentials-proxy", "true", "io.floci.owner", "4566",
|
||||
"floci.ecs-task-role-credentials-proxy", "true", "floci_owner_port", "4566"));
|
||||
|
||||
proxy.ensureProxyOn("floci-net");
|
||||
|
||||
verify(lifecycleManager).removeIfExists("stale-own");
|
||||
}
|
||||
|
||||
@Test
|
||||
void leavesAStaleContainerWhoseNewAndLegacyOwnersDisagreeAlone() {
|
||||
stubSuccessfulLaunch("proxy-1");
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.empty());
|
||||
when(config.port()).thenReturn(4566);
|
||||
stubStaleContainerByName("relabelled", Map.of(
|
||||
"io.floci.ecs.credentials-proxy", "true", "io.floci.owner", "4566",
|
||||
"floci.ecs-task-role-credentials-proxy", "true", "floci_owner_port", "4567"));
|
||||
|
||||
proxy.ensureProxyOn("floci-net");
|
||||
|
||||
verify(lifecycleManager, never()).removeIfExists("relabelled");
|
||||
}
|
||||
|
||||
@Test
|
||||
void leavesAnUnrelatedSameNamedContainerAlone() {
|
||||
stubSuccessfulLaunch("proxy-1");
|
||||
@@ -245,6 +286,47 @@ class EcsCredentialsProxyTest {
|
||||
verify(lifecycleManager, never()).removeIfExists("other-instance-proxy");
|
||||
}
|
||||
|
||||
@Test
|
||||
void reapSurvivingProxiesQueriesTheNewAndLegacyLabelsAndRemovesOnlyAgreeingOwnLeftovers() {
|
||||
when(config.services().ecs().reconcileContainersOnStartup()).thenReturn(true);
|
||||
when(config.docker().resourceNamespace()).thenReturn(Optional.empty());
|
||||
when(config.port()).thenReturn(4566);
|
||||
ListContainersCmd listCmd = mock(ListContainersCmd.class, RETURNS_SELF);
|
||||
ListContainersCmd newKeys = mock(ListContainersCmd.class);
|
||||
ListContainersCmd legacyKeys = mock(ListContainersCmd.class);
|
||||
when(dockerClient.listContainersCmd()).thenReturn(listCmd);
|
||||
when(listCmd.withLabelFilter(Map.of("io.floci.ecs.credentials-proxy", "true"))).thenReturn(newKeys);
|
||||
when(listCmd.withLabelFilter(Map.of("floci.ecs-task-role-credentials-proxy", "true")))
|
||||
.thenReturn(legacyKeys);
|
||||
Container newOnly = proxyContainer("new-only",
|
||||
Map.of("io.floci.ecs.credentials-proxy", "true", "io.floci.owner", "4566"));
|
||||
Container legacyOnly = proxyContainer("legacy-only",
|
||||
Map.of("floci.ecs-task-role-credentials-proxy", "true", "floci_owner_port", "4566"));
|
||||
Container both = proxyContainer("both", Map.of(
|
||||
"io.floci.ecs.credentials-proxy", "true", "io.floci.owner", "4566",
|
||||
"floci.ecs-task-role-credentials-proxy", "true", "floci_owner_port", "4566"));
|
||||
// Relabelled outside Floci: the legacy owner names another instance, so it is left alone.
|
||||
Container disagreeing = proxyContainer("disagreeing", Map.of(
|
||||
"io.floci.ecs.credentials-proxy", "true", "io.floci.owner", "4566",
|
||||
"floci.ecs-task-role-credentials-proxy", "true", "floci_owner_port", "4567"));
|
||||
when(newKeys.exec()).thenReturn(List.of(newOnly, both, disagreeing));
|
||||
when(legacyKeys.exec()).thenReturn(List.of(legacyOnly, both, disagreeing));
|
||||
|
||||
proxy.reapSurvivingProxies();
|
||||
|
||||
verify(lifecycleManager).removeIfExists("new-only");
|
||||
verify(lifecycleManager).removeIfExists("legacy-only");
|
||||
verify(lifecycleManager).removeIfExists("both");
|
||||
verify(lifecycleManager, never()).removeIfExists("disagreeing");
|
||||
}
|
||||
|
||||
private static Container proxyContainer(String id, Map<String, String> labels) {
|
||||
Container container = mock(Container.class);
|
||||
when(container.getId()).thenReturn(id);
|
||||
when(container.getLabels()).thenReturn(labels);
|
||||
return container;
|
||||
}
|
||||
|
||||
@Test
|
||||
void reapSurvivingProxiesLeavesProxiesAloneWhenStartupReconciliationIsOff() {
|
||||
when(config.services().ecs().reconcileContainersOnStartup()).thenReturn(false);
|
||||
|
||||
@@ -118,6 +118,25 @@ class KarapaceManagerTest {
|
||||
assertEquals(Integer.valueOf(hostPort), spec.portBindings().get(REST_PORT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void bridgeContainerCarriesThePipesIdentityAndItsComponent() throws Exception {
|
||||
int hostPort = startFakeRestServer();
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(false);
|
||||
when(portAllocator.allocate(9500, 9599)).thenReturn(hostPort);
|
||||
when(lifecycleManager.createAndStart(any())).thenReturn(new ContainerInfo("container-1",
|
||||
Map.of(REST_PORT, new EndpointInfo("localhost", hostPort))));
|
||||
|
||||
manager.ensureStarted("broker-1:9092");
|
||||
|
||||
ArgumentCaptor<ContainerSpec> specCaptor = ArgumentCaptor.forClass(ContainerSpec.class);
|
||||
verify(lifecycleManager).createAndStart(specCaptor.capture());
|
||||
// The legacy floci.component alias is added where every container is created, not here.
|
||||
assertEquals(
|
||||
Map.of("io.floci", "aws", "io.floci.service", "pipes",
|
||||
"io.floci.component", "pipes-kafka-rest-bridge"),
|
||||
specCaptor.getValue().labels());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ensureStartedReusesTheSameContainerForTheSameTarget() throws Exception {
|
||||
int hostPort = startFakeRestServer();
|
||||
|
||||
Reference in new issue
Block a user