mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(eks): advertise a configurable endpoint host (#6331)
endpoint-mode=host always returned https://localhost:<port>, so a cluster was only usable from the machine running Floci. The new floci.services.eks.endpoint-host (FLOCI_SERVICES_EKS_ENDPOINT_HOST) is advertised instead, and added to the k3s certificate as a SAN so the endpoint still verifies against the cluster CA. It follows rds.endpoint-host. The value must be a DNS name, a dotted IPv4 address or an IPv6 address; an IPv6 literal is bracketed in the endpoint URL. A scheme, port, path or list stops Floci at startup, before persisted clusters are restored, so a typo cannot leave every restored cluster FAILED. Network mode inside a container advertises the container name, so it ignores the setting. The SAN is fixed when the container is created, so the container carries the host it was made for as a label, and an adopted container advertises that host rather than the configured one. When the two differ on restore, the surviving container is replaced (data volume and port kept), as a node-capacity change already does; if the replacement fails, the old container stays and keeps advertising the host its certificate carries. Unset, nothing changes: no label is added and existing containers are adopted as before. Co-authored-by: Robin Baeckman <27041454+RobinBaeckman@users.noreply.github.com>
This commit is contained in:
1 parent
39f37c21ff
commit
1476b0e219
7 files changed
+424
-21
No files matched your search
@@ -157,27 +157,71 @@ class EksClusterManagerTest {
|
||||
@Test
|
||||
void hostModeAlwaysReturnsHostReachableEndpoint() {
|
||||
assertEquals("https://localhost:6500",
|
||||
EksClusterManager.resolvePublicEndpoint(true, "host", "floci-eks-demo", 6500));
|
||||
EksClusterManager.resolvePublicEndpoint(true, "host", "floci-eks-demo", "localhost", 6500));
|
||||
assertEquals("https://localhost:6500",
|
||||
EksClusterManager.resolvePublicEndpoint(false, "host", "floci-eks-demo", 6500));
|
||||
EksClusterManager.resolvePublicEndpoint(false, "host", "floci-eks-demo", "localhost", 6500));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hostModeAdvertisesAnIpv4EndpointHostAsIs() {
|
||||
assertEquals("https://192.168.8.24:6500",
|
||||
EksClusterManager.resolvePublicEndpoint(false, "host", "floci-eks-demo", "192.168.8.24", 6500));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hostModeBracketsAnIpv6EndpointHost() {
|
||||
assertEquals("https://[fd00::24]:6500",
|
||||
EksClusterManager.resolvePublicEndpoint(false, "host", "floci-eks-demo", "fd00::24", 6500));
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointHostAcceptsHostnamesAndIpAddresses() {
|
||||
assertEquals("localhost", EksClusterManager.endpointHostFrom(Optional.empty()));
|
||||
assertEquals("localhost", EksClusterManager.endpointHostFrom(Optional.of(" ")));
|
||||
assertEquals("aws.example.test", EksClusterManager.endpointHostFrom(Optional.of(" aws.example.test ")));
|
||||
assertEquals("192.168.8.24", EksClusterManager.endpointHostFrom(Optional.of("192.168.8.24")));
|
||||
assertEquals("fd00::24", EksClusterManager.endpointHostFrom(Optional.of("fd00::24")));
|
||||
assertEquals("fd00::24", EksClusterManager.endpointHostFrom(Optional.of("[fd00::24]")));
|
||||
assertEquals("localhost", EksClusterManager.endpointHostFrom(Optional.of("LOCALHOST")));
|
||||
assertEquals("a".repeat(63) + ".test", EksClusterManager.endpointHostFrom(Optional.of("a".repeat(63) + ".test")));
|
||||
assertEquals("aws-1.example.test", EksClusterManager.endpointHostFrom(Optional.of("AWS-1.Example.Test")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointHostRejectsAUrlOrAPort() {
|
||||
for (String value : List.of("https://aws.example.test", "aws.example.test:443", "aws.example.test/eks",
|
||||
"aws example.test", "[fd00::24]:6443", "a.example.test,b.example.test", "[fd00::24", "fd00::24%eth0",
|
||||
"user@aws.example.test", "aws.example.test?x", "aws.example.test#x", "-aws.example.test", "[[::1]]",
|
||||
"999.1.1.1", "1.2.3", "my_host", "a".repeat(64) + ".example.test",
|
||||
("a".repeat(60) + ".").repeat(5) + "test")) {
|
||||
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
|
||||
() -> EksClusterManager.endpointHostFrom(Optional.of(value)), value);
|
||||
assertTrue(e.getMessage().contains("floci.services.eks.endpoint-host"), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void hostModeAdvertisesTheConfiguredEndpointHost() {
|
||||
assertEquals("https://aws.example.test:6500",
|
||||
EksClusterManager.resolvePublicEndpoint(false, "host", "floci-eks-demo", "aws.example.test", 6500));
|
||||
}
|
||||
|
||||
@Test
|
||||
void networkModeReturnsContainerDnsOnlyInContainer() {
|
||||
assertEquals("https://floci-eks-demo:6443",
|
||||
EksClusterManager.resolvePublicEndpoint(true, "network", "floci-eks-demo", 6500));
|
||||
EksClusterManager.resolvePublicEndpoint(true, "network", "floci-eks-demo", "localhost", 6500));
|
||||
// Native mode has no usable container DNS name — falls back to the host endpoint.
|
||||
assertEquals("https://localhost:6500",
|
||||
EksClusterManager.resolvePublicEndpoint(false, "network", "floci-eks-demo", 6500));
|
||||
EksClusterManager.resolvePublicEndpoint(false, "network", "floci-eks-demo", "localhost", 6500));
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointModeIsCaseInsensitiveAndDefaultsToHost() {
|
||||
assertEquals("https://floci-eks-demo:6443",
|
||||
EksClusterManager.resolvePublicEndpoint(true, "NETWORK", "floci-eks-demo", 6500));
|
||||
EksClusterManager.resolvePublicEndpoint(true, "NETWORK", "floci-eks-demo", "localhost", 6500));
|
||||
// Unknown / unset modes behave as host.
|
||||
assertEquals("https://localhost:6500",
|
||||
EksClusterManager.resolvePublicEndpoint(true, "bogus", "floci-eks-demo", 6500));
|
||||
EksClusterManager.resolvePublicEndpoint(true, "bogus", "floci-eks-demo", "localhost", 6500));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -702,6 +746,108 @@ class EksClusterManagerTest {
|
||||
.thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void recreatesASurvivorWhoseCertificateLacksTheEndpointHost() {
|
||||
when(config.services().eks().endpointHost()).thenReturn(Optional.of("aws.example.test"));
|
||||
when(lifecycleManager.findByName("floci-eks-demo"))
|
||||
.thenReturn(Optional.of(survivingContainer("cid-old")));
|
||||
when(lifecycleManager.adopt(eq("cid-old"), eq(List.of(6443)), any()))
|
||||
.thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440)));
|
||||
stubFreshStart("cid-new", 6440);
|
||||
|
||||
Cluster cluster = cluster();
|
||||
manager.restoreCluster(cluster);
|
||||
|
||||
assertEquals("cid-new", cluster.getContainerId());
|
||||
assertEquals("https://aws.example.test:6440", cluster.getEndpoint());
|
||||
verify(dockerClient).stopContainerCmd("cid-old");
|
||||
}
|
||||
|
||||
@Test
|
||||
void adoptsASurvivorCreatedForTheConfiguredEndpointHost() {
|
||||
when(config.services().eks().endpointHost()).thenReturn(Optional.of("aws.example.test"));
|
||||
when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.of(containerFromJson(
|
||||
"{\"Id\":\"cid-1\",\"Labels\":{\"io.floci.eks.node-capacity\":\"m5.large:unbounded\","
|
||||
+ "\"io.floci.eks.endpoint-host\":\"aws.example.test\"}}")));
|
||||
when(lifecycleManager.adopt(eq("cid-1"), eq(List.of(6443)), any()))
|
||||
.thenReturn(new ContainerInfo("cid-1", Map.of(), Map.of(6443, 6512)));
|
||||
|
||||
Cluster cluster = cluster();
|
||||
manager.restoreCluster(cluster);
|
||||
|
||||
assertEquals("cid-1", cluster.getContainerId());
|
||||
assertEquals("https://aws.example.test:6512", cluster.getEndpoint());
|
||||
verify(lifecycleManager, never()).create(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void recreatesASurvivorCreatedForAnotherEndpointHostWhenTheHostIsUnset() {
|
||||
when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.of(containerFromJson(
|
||||
"{\"Id\":\"cid-old\",\"Labels\":{\"io.floci.eks.node-capacity\":\"m5.large:unbounded\","
|
||||
+ "\"io.floci.eks.endpoint-host\":\"aws.example.test\"}}")));
|
||||
when(lifecycleManager.adopt(eq("cid-old"), eq(List.of(6443)), any()))
|
||||
.thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440)));
|
||||
stubFreshStart("cid-new", 6440);
|
||||
|
||||
Cluster cluster = cluster();
|
||||
manager.restoreCluster(cluster);
|
||||
|
||||
assertEquals("cid-new", cluster.getContainerId());
|
||||
assertEquals("https://localhost:6440", cluster.getEndpoint());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFailedReplacementKeepsAdvertisingTheHostTheOldCertificateCarries() {
|
||||
when(config.services().eks().endpointHost()).thenReturn(Optional.of("aws.example.test"));
|
||||
when(lifecycleManager.findByName("floci-eks-demo"))
|
||||
.thenReturn(Optional.of(survivingContainer("cid-old")));
|
||||
when(lifecycleManager.adopt(eq("cid-old"), eq(List.of(6443)), any()))
|
||||
.thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440)));
|
||||
when(lifecycleManager.create(any())).thenReturn("cid-new");
|
||||
when(lifecycleManager.startCreated(any(), any()))
|
||||
.thenThrow(new RuntimeException("replacement failed"));
|
||||
|
||||
Cluster cluster = cluster();
|
||||
manager.restoreCluster(cluster);
|
||||
|
||||
assertEquals("cid-old", cluster.getContainerId());
|
||||
assertEquals("https://localhost:6440", cluster.getEndpoint());
|
||||
}
|
||||
|
||||
@Test
|
||||
void replacesASurvivorMadeForTheEndpointHostWhenItsCapacityChanged() {
|
||||
when(config.services().eks().endpointHost()).thenReturn(Optional.of("aws.example.test"));
|
||||
when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.of(containerFromJson(
|
||||
"{\"Id\":\"cid-old\",\"Labels\":{\"io.floci.eks.node-capacity\":\"t3.small:unbounded\","
|
||||
+ "\"io.floci.eks.endpoint-host\":\"aws.example.test\"}}")));
|
||||
when(lifecycleManager.adopt(eq("cid-old"), eq(List.of(6443)), any()))
|
||||
.thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440)));
|
||||
stubFreshStart("cid-new", 6440);
|
||||
|
||||
Cluster cluster = cluster();
|
||||
manager.restoreCluster(cluster);
|
||||
|
||||
assertEquals("cid-new", cluster.getContainerId());
|
||||
assertEquals("https://aws.example.test:6440", cluster.getEndpoint());
|
||||
}
|
||||
|
||||
@Test
|
||||
void networkModeIgnoresTheEndpointHost() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
when(config.services().eks().endpointMode()).thenReturn("network");
|
||||
when(config.services().eks().endpointHost()).thenReturn(Optional.of("aws.example.test"));
|
||||
when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.of(containerFromJson(
|
||||
"{\"Id\":\"cid-1\",\"Labels\":{\"io.floci.eks.node-capacity\":\"m5.large:unbounded\"}}")));
|
||||
when(lifecycleManager.adopt(eq("cid-1"), eq(List.of(6443)), any()))
|
||||
.thenReturn(new ContainerInfo("cid-1", Map.of(), Map.of(6443, 6512)));
|
||||
|
||||
Cluster cluster = cluster();
|
||||
manager.restoreCluster(cluster);
|
||||
|
||||
assertEquals("cid-1", cluster.getContainerId());
|
||||
verify(lifecycleManager, never()).create(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void recreatesAnOldSurvivorWithoutCapacityLimits() {
|
||||
when(lifecycleManager.findByName("floci-eks-demo"))
|
||||
@@ -2231,6 +2377,87 @@ class EksClusterManagerTest {
|
||||
verify(copyCmd, atLeastOnce()).exec();
|
||||
}
|
||||
|
||||
@Test
|
||||
void startClusterAddsTheConfiguredEndpointHostToTheCertificate(@TempDir Path tempDir) {
|
||||
when(eks.irsaSigningKey()).thenReturn(false);
|
||||
when(eks.dataPath()).thenReturn(tempDir.toString());
|
||||
when(eks.endpointHost()).thenReturn(Optional.of("aws.example.test"));
|
||||
|
||||
Cluster cluster = new Cluster();
|
||||
cluster.setName("my-cluster");
|
||||
|
||||
manager.startCluster(cluster);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<String>> cmdCaptor = ArgumentCaptor.forClass(List.class);
|
||||
verify(builder).withCmd(cmdCaptor.capture());
|
||||
List<String> cmd = cmdCaptor.getValue();
|
||||
|
||||
assertTrue(cmd.contains("--tls-san=localhost"));
|
||||
assertTrue(cmd.contains("--tls-san=aws.example.test"));
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Map<String, String>> labels = ArgumentCaptor.forClass(Map.class);
|
||||
verify(builder).withLabels(labels.capture());
|
||||
assertEquals("aws.example.test", labels.getValue().get("io.floci.eks.endpoint-host"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void startClusterAddsABareIpv6EndpointHostToTheCertificate(@TempDir Path tempDir) {
|
||||
when(eks.irsaSigningKey()).thenReturn(false);
|
||||
when(eks.dataPath()).thenReturn(tempDir.toString());
|
||||
when(eks.endpointHost()).thenReturn(Optional.of("[fd00::24]"));
|
||||
|
||||
Cluster cluster = new Cluster();
|
||||
cluster.setName("my-cluster");
|
||||
|
||||
manager.startCluster(cluster);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<String>> cmdCaptor = ArgumentCaptor.forClass(List.class);
|
||||
verify(builder).withCmd(cmdCaptor.capture());
|
||||
|
||||
assertTrue(cmdCaptor.getValue().contains("--tls-san=fd00::24"), cmdCaptor.getValue()::toString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void startClusterAddsLocalhostOnceWhenItIsTheConfiguredEndpointHost(@TempDir Path tempDir) {
|
||||
when(eks.irsaSigningKey()).thenReturn(false);
|
||||
when(eks.dataPath()).thenReturn(tempDir.toString());
|
||||
when(eks.endpointHost()).thenReturn(Optional.of("LOCALHOST"));
|
||||
|
||||
Cluster cluster = new Cluster();
|
||||
cluster.setName("my-cluster");
|
||||
|
||||
manager.startCluster(cluster);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<String>> cmdCaptor = ArgumentCaptor.forClass(List.class);
|
||||
verify(builder).withCmd(cmdCaptor.capture());
|
||||
|
||||
assertEquals(List.of("--tls-san=localhost"),
|
||||
cmdCaptor.getValue().stream().filter(a -> a.startsWith("--tls-san=")).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void startClusterAddsNoExtraCertificateNameWithoutAnEndpointHost(@TempDir Path tempDir) {
|
||||
when(eks.irsaSigningKey()).thenReturn(false);
|
||||
when(eks.dataPath()).thenReturn(tempDir.toString());
|
||||
|
||||
Cluster cluster = new Cluster();
|
||||
cluster.setName("my-cluster");
|
||||
|
||||
manager.startCluster(cluster);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<String>> cmdCaptor = ArgumentCaptor.forClass(List.class);
|
||||
verify(builder).withCmd(cmdCaptor.capture());
|
||||
List<String> cmd = cmdCaptor.getValue();
|
||||
|
||||
assertEquals(List.of("--tls-san=localhost"),
|
||||
cmd.stream().filter(a -> a.startsWith("--tls-san=")).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void startClusterOmitsIrsaArgsAndKeyInjectionWhenDisabled(@TempDir Path tempDir) {
|
||||
when(eks.irsaSigningKey()).thenReturn(false);
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package io.github.hectorvent.floci.services.eks;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.quarkus.runtime.StartupEvent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class EksStartupValidatorTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"https://aws.example.test", "aws.example.test:6443", "a.example.test,b.example.test"})
|
||||
void invalidEndpointHostIsRejectedByStartupObserver(String value) {
|
||||
EksStartupValidator validator = validator(Optional.of(value));
|
||||
assertThrows(IllegalArgumentException.class, () -> validator.onStart(new StartupEvent()));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"", "aws.example.test", "192.168.8.24", "[fd00::24]"})
|
||||
void validOrUnsetEndpointHostIsAcceptedByStartupObserver(String value) {
|
||||
EksStartupValidator validator = validator(value.isEmpty() ? Optional.empty() : Optional.of(value));
|
||||
assertDoesNotThrow(() -> validator.onStart(new StartupEvent()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidEndpointHostIsIgnoredWhenEksIsMetadataOnly() {
|
||||
EksStartupValidator disabled = validator(Optional.of("https://aws.example.test"), false, false);
|
||||
EksStartupValidator mock = validator(Optional.of("https://aws.example.test"), true, true);
|
||||
assertDoesNotThrow(() -> disabled.onStart(new StartupEvent()));
|
||||
assertDoesNotThrow(() -> mock.onStart(new StartupEvent()));
|
||||
}
|
||||
|
||||
private static EksStartupValidator validator(Optional<String> endpointHost) {
|
||||
return validator(endpointHost, true, false);
|
||||
}
|
||||
|
||||
private static EksStartupValidator validator(Optional<String> endpointHost, boolean enabled, boolean mock) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().eks().endpointHost()).thenReturn(endpointHost);
|
||||
when(config.services().eks().enabled()).thenReturn(enabled);
|
||||
when(config.services().eks().mock()).thenReturn(mock);
|
||||
return new EksStartupValidator(config);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user