mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(eks): resolve pod names through Floci's embedded DNS (#8924)
Pods resolve through CoreDNS, which forwards to the resolver file k3s hands them. k3s takes the node's /etc/resolv.conf, and on a user-defined Docker network that names only Docker's loopback 127.0.0.11, which k3s refuses; it then writes a file naming a public resolver. So Route 53 private zones, Cloud Map names and the other names only Floci answers never resolved from a pod. When embedded DNS is on and running, and the cluster's Docker network can reach it, Floci now copies a resolver file naming only its DNS server into the cluster container before it starts, and passes it to k3s with --resolv-conf. With Floci in a container that means the cluster is on Floci's own network: Docker isolates user-defined networks, and pods given only an unreachable resolver would lose the public one too. The public fallback resolvers stay out of it, because CoreDNS picks among its upstreams at random. A cluster that sets the kubelet's resolv-conf through its tags keeps its own file. A file that cannot be copied fails the start, as a container that cannot start does, since k3s would hand its pods a missing file.
This commit is contained in:
1 parent
545006249b
commit
1d8499e837
9 files changed
+407
-21
No files matched your search
@@ -98,6 +98,26 @@ public class ContainerBuilder {
|
||||
return resolveDockerNetwork(serviceNetwork, config, currentContainerNetworkResolver);
|
||||
}
|
||||
|
||||
/**
|
||||
* The address {@link Builder#withEmbeddedDns} hands a container as its first resolver, when a
|
||||
* container on the network {@link Builder#withDockerNetwork} picks for {@code serviceNetwork} can
|
||||
* reach it; empty when it cannot or the embedded DNS server is not running. Exposed for a container
|
||||
* that must name that resolver alone in a file of its own, as a k3s node does for its pods, where an
|
||||
* unreachable one leaves no resolver at all. Floci in a container listens on its own address on its
|
||||
* own network, which Docker isolates from every other user-defined network. Floci running natively
|
||||
* listens on the Docker bridge address, which every container already gets as its resolver.
|
||||
*/
|
||||
public Optional<String> embeddedDnsServerIpReachableOn(Optional<String> serviceNetwork) {
|
||||
Optional<String> dnsServerIp = embeddedDnsServer != null ? embeddedDnsServer.getServerIp() : Optional.empty();
|
||||
Optional<String> flociNetwork = currentContainerNetworkResolver != null
|
||||
? currentContainerNetworkResolver.resolveNetworkName()
|
||||
: Optional.empty();
|
||||
if (dnsServerIp.isEmpty() || flociNetwork.isEmpty()) {
|
||||
return dnsServerIp;
|
||||
}
|
||||
return resolveDockerNetwork(serviceNetwork).equals(flociNetwork) ? dnsServerIp : Optional.empty();
|
||||
}
|
||||
|
||||
private static Optional<String> resolveDockerNetwork(Optional<String> serviceNetwork, EmulatorConfig config,
|
||||
CurrentContainerNetworkResolver currentContainerNetworkResolver) {
|
||||
return serviceNetwork
|
||||
|
||||
@@ -181,6 +181,21 @@ public final class EksClusterArgs {
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether {@code clusterArgs} set the kubelet's resolv-conf, the resolver file its pods get. */
|
||||
public static boolean setsKubeletResolvConf(List<String> clusterArgs) {
|
||||
if (clusterArgs == null) {
|
||||
return false;
|
||||
}
|
||||
String prefix = Component.KUBELET.flagPrefix();
|
||||
for (String arg : clusterArgs) {
|
||||
if (arg != null && arg.startsWith(prefix)
|
||||
&& "resolvconf".equals(normalizedFlagName(extractFlagName(arg.substring(prefix.length()))))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private record ParsedTag(Component component, TagKind kind, String prefixMatched) {}
|
||||
|
||||
private enum TagKind {
|
||||
@@ -280,7 +295,7 @@ public final class EksClusterArgs {
|
||||
|
||||
private static void validateCollision(Component component, String flag) {
|
||||
String rawName = extractFlagName(flag);
|
||||
String flagName = rawName.replace("-", "").toLowerCase(Locale.ROOT);
|
||||
String flagName = normalizedFlagName(rawName);
|
||||
|
||||
if (component == Component.KUBELET) {
|
||||
if (FLOCI_MANAGED_KUBELET_FLAGS.contains(flagName)) {
|
||||
@@ -301,6 +316,11 @@ public final class EksClusterArgs {
|
||||
}
|
||||
}
|
||||
|
||||
/** A flag name without its hyphens, lower-cased, so spellings that differ only in those match. */
|
||||
private static String normalizedFlagName(String flagName) {
|
||||
return flagName.replace("-", "").toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
private static String extractFlagName(String flag) {
|
||||
int eqIndex = flag.indexOf('=');
|
||||
String name = (eqIndex >= 0) ? flag.substring(0, eqIndex).trim() : flag.trim();
|
||||
|
||||
@@ -137,6 +137,9 @@ public class EksClusterManager
|
||||
// Tar entry extracted at /etc; the archive path creates /etc/rancher/k3s, which does not
|
||||
// exist yet in a created-but-not-started k3s container.
|
||||
private static final String REGISTRIES_TAR_ENTRY = "rancher/k3s/registries.yaml";
|
||||
// The resolv.conf k3s hands its pods (--resolv-conf), extracted at /etc as registries.yaml is.
|
||||
static final String POD_RESOLV_CONF_TAR_ENTRY = "rancher/k3s/floci-resolv.conf";
|
||||
static final String POD_RESOLV_CONF_PATH = "/etc/" + POD_RESOLV_CONF_TAR_ENTRY;
|
||||
// k3s applies every manifest in its server manifests directory at startup, and again whenever
|
||||
// one changes on disk, so dropping the file in before the container starts is enough to get the
|
||||
// MutatingWebhookConfiguration registered. The directory sits under the cluster's named data
|
||||
@@ -834,6 +837,7 @@ public class EksClusterManager
|
||||
String webhookLocalFile = null;
|
||||
SigningKeyFiles signingKeyFiles = null;
|
||||
String auditPolicyLocalFile = null;
|
||||
String podResolvConf = null;
|
||||
ContainerSpec spec;
|
||||
String containerId;
|
||||
try {
|
||||
@@ -970,6 +974,10 @@ public class EksClusterManager
|
||||
}
|
||||
|
||||
List<String> callerArgs = resolveCallerArgs(cluster);
|
||||
podResolvConf = podResolvConf(callerArgs);
|
||||
if (podResolvConf != null) {
|
||||
serverArgs.add("--resolv-conf=" + POD_RESOLV_CONF_PATH);
|
||||
}
|
||||
if (!callerArgs.isEmpty()) {
|
||||
serverArgs.addAll(callerArgs);
|
||||
}
|
||||
@@ -1044,6 +1052,12 @@ public class EksClusterManager
|
||||
deferGuestSetup(containerId, false);
|
||||
ContainerInfo info;
|
||||
try {
|
||||
if (podResolvConf != null) {
|
||||
// Part of the start: k3s would hand its pods a resolver file that is not there, and no pod could start.
|
||||
RetryingTarCopier.copyBytes(lifecycleManager.getDockerClient(), containerId, "/etc",
|
||||
POD_RESOLV_CONF_TAR_ENTRY, podResolvConf.getBytes(StandardCharsets.UTF_8), 0644,
|
||||
RetryingTarCopier.DEFAULT_MAX_ATTEMPTS, archiveRetryBackoffMillis);
|
||||
}
|
||||
info = lifecycleManager.startCreated(containerId, spec);
|
||||
} catch (Exception e) {
|
||||
abandonGuestSetup(containerId, "its container could not be started");
|
||||
@@ -2042,6 +2056,25 @@ public class EksClusterManager
|
||||
return wrappedCmd;
|
||||
}
|
||||
|
||||
/**
|
||||
* The resolv.conf k3s hands its pods, naming only Floci's embedded DNS server, or null when k3s keeps
|
||||
* its own choice: embedded DNS is off, not running or not reachable from the cluster's Docker network,
|
||||
* or the caller set the kubelet's resolv-conf.
|
||||
* k3s otherwise reads the node's /etc/resolv.conf, which on a user-defined Docker network names only
|
||||
* Docker's loopback resolver 127.0.0.11. k3s refuses a loopback-only file and writes one naming a
|
||||
* public resolver instead, so CoreDNS never asks Floci and names only Floci holds do not resolve.
|
||||
* Floci's public fallback resolvers stay out of the file: CoreDNS picks among its upstreams at random,
|
||||
* so one of them would answer some lookups of Floci's names. Floci forwards every other name itself.
|
||||
*/
|
||||
private String podResolvConf(List<String> callerArgs) {
|
||||
if (!config.services().eks().embeddedDns() || EksClusterArgs.setsKubeletResolvConf(callerArgs)) {
|
||||
return null;
|
||||
}
|
||||
return containerBuilder.embeddedDnsServerIpReachableOn(config.services().eks().dockerNetwork())
|
||||
.map(dnsServerIp -> "nameserver " + dnsServerIp + "\n")
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves caller-supplied k3s arguments configured on the cluster or in its creation tags.
|
||||
*/
|
||||
|
||||
@@ -150,11 +150,11 @@ public class EksPodIdentityWebhook {
|
||||
|
||||
/**
|
||||
* A pod resolves names through CoreDNS, which never sees the cluster container's
|
||||
* {@code /etc/hosts}, where {@code host.docker.internal} lives on Linux. Floci's DNS server does
|
||||
* not reach it either: on a custom Docker network the container's resolver is {@code 127.0.0.11},
|
||||
* which k3s rejects as loopback and replaces with a public resolver, so Floci's hostname resolves
|
||||
* to {@code 127.0.0.1} inside the pod. Only an IP address is reachable, and that is what
|
||||
* {@code baseUrl(false)} returns whenever Floci runs in a container. No endpoint is injected
|
||||
* {@code /etc/hosts}, where {@code host.docker.internal} lives on Linux. Floci's hostname resolves
|
||||
* in a pod only when the cluster hands its pods Floci's embedded DNS server: without it, a custom
|
||||
* Docker network's resolver is {@code 127.0.0.11}, which k3s rejects as loopback and replaces with
|
||||
* a public resolver, so the name resolves to {@code 127.0.0.1}. An IP address is reachable either
|
||||
* way, and that is what {@code baseUrl(false)} returns whenever Floci runs in a container. No endpoint is injected
|
||||
* otherwise, which leaves the SDK default.
|
||||
*/
|
||||
static String podReachableEndpoint(String baseUrl) {
|
||||
|
||||
Reference in new issue
Block a user