Skip to content

Commit

Permalink
rewritten resolving of seed
Browse files Browse the repository at this point in the history
  • Loading branch information
smiklosovic committed Aug 18, 2020
1 parent 90866c2 commit 75e7ce1
Showing 1 changed file with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public SeedsResolver(String serviceName, AddressTranslator<T> addressTranslator)
}

public List<T> resolve() throws Exception {
List<InetAddress> seeds = resolveSeeds(serviceName);
final List<InetAddress> seeds = resolveSeeds(serviceName);

if (seeds.isEmpty()) {
throw new IllegalStateException("Seed list is empty!");
Expand All @@ -67,18 +67,12 @@ public List<T> resolve() throws Exception {
}

private List<InetAddress> resolveSeeds(String service) throws Exception {
String namespace = readNamespace();

String clusterDomain = getClusterDomain();

String digQuery = constructDomainName(service, namespace, clusterDomain);

List<String> digResult = executeShellCommand("dig", "-t", "SRV", digQuery, "+short");

List<String> endpoints = parseEndpoints(digResult);

List<String> seeds = filterSeeds(endpoints);

final String namespace = readNamespace();
final String digQuery = constructDomainName(service, namespace);
logger.info("Resolved dig query " + digQuery);
final List<String> digResult = executeShellCommand("dig", "-t", "SRV", digQuery, "+short");
final List<String> endpoints = parseEndpoints(digResult);
final List<String> seeds = filterSeeds(endpoints);
return mapEndpointAsInetAddresses(seeds);
}

Expand Down Expand Up @@ -124,35 +118,40 @@ private List<String> parseEndpoints(List<String> digResult) {
}

/**
* We are going to parse the last element in "search" row in /etc/resolv.conf
* We are going to parse the first in "search" row in /etc/resolv.conf
*
* nameserver 10.96.0.10
* search default.svc.cluster.local svc.cluster.local cluster.local
* options ndots:5
*
* and we prepend that with service name
*/
private String getClusterDomain() {
private String constructDomainName(String serviceName, String namespace) {

final String defaultClusterDomain = "cluster.local";
final String defaultDomainName = format("%s.svc.cluster.local", namespace);

try {
return Files.readAllLines(Paths.get("/etc/resolv.conf")).stream()
final List<String> resolvConf = Files.readAllLines(Paths.get("/etc/resolv.conf"));

logger.info("Content of /etc/resolv.conf {}", String.join("\n", resolvConf));

final String resolvedClusterDomain = resolvConf.stream()
.filter(line -> line.startsWith("search"))
.filter(line -> line.split(" ").length > 1)
.map(searchLine -> {
final String[] split = searchLine.split(" ");
return split[split.length - 1];
return split[1];
})
.findFirst()
.orElse(defaultClusterDomain);
.orElse(defaultDomainName);

return format("%s.%s", serviceName, resolvedClusterDomain);
} catch (final Exception ex) {
logger.error("Unable to read /etc/resolv.conf, returning " + defaultClusterDomain);
return defaultClusterDomain;
logger.error("Unable to read /etc/resolv.conf or unable to resolve domain name, returning " + defaultDomainName);
return defaultDomainName;
}
}

private String constructDomainName(String serviceName, String namespace, String clusterDomain) {
return format("%s.%s.svc.%s", serviceName, namespace, clusterDomain);
}

private String readNamespace() throws Exception {
return new String(Files.readAllBytes(Paths.get("/var/run/secrets/kubernetes.io/serviceaccount/namespace")));
}
Expand Down

0 comments on commit 75e7ce1

Please sign in to comment.