Skip to content

Commit

Permalink
Add simple DNS resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Oct 31, 2023
1 parent 6feefb4 commit 76d8bc5
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.vertx.serviceresolver.dns;

import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.impl.JsonUtil;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Base64;

/**
* Converter and mapper for {@link io.vertx.serviceresolver.dns.DnsResolverOptions}.
* NOTE: This class has been automatically generated from the {@link io.vertx.serviceresolver.dns.DnsResolverOptions} original class using Vert.x codegen.
*/
public class DnsResolverOptionsConverter {


private static final Base64.Decoder BASE64_DECODER = JsonUtil.BASE64_DECODER;
private static final Base64.Encoder BASE64_ENCODER = JsonUtil.BASE64_ENCODER;

static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, DnsResolverOptions obj) {
for (java.util.Map.Entry<String, Object> member : json) {
switch (member.getKey()) {
case "host":
if (member.getValue() instanceof String) {
obj.setHost((String)member.getValue());
}
break;
case "port":
if (member.getValue() instanceof Number) {
obj.setPort(((Number)member.getValue()).intValue());
}
break;
}
}
}

static void toJson(DnsResolverOptions obj, JsonObject json) {
toJson(obj, json.getMap());
}

static void toJson(DnsResolverOptions obj, java.util.Map<String, Object> json) {
if (obj.getHost() != null) {
json.put("host", obj.getHost());
}
json.put("port", obj.getPort());
}
}
22 changes: 22 additions & 0 deletions src/main/java/io/vertx/serviceresolver/dns/DnsResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2011-2023 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.serviceresolver.dns;

import io.vertx.serviceresolver.ServiceResolver;
import io.vertx.serviceresolver.dns.impl.DnsResolverImpl;
import io.vertx.serviceresolver.impl.ServiceResolverImpl;

public interface DnsResolver {

static ServiceResolver create(DnsResolverOptions options) {
return new ServiceResolverImpl((vertx, lookup) -> new DnsResolverImpl(vertx, options, lookup.loadBalancer));
}
}
52 changes: 52 additions & 0 deletions src/main/java/io/vertx/serviceresolver/dns/DnsResolverOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2011-2023 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.serviceresolver.dns;

import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.json.JsonObject;
import io.vertx.serviceresolver.srv.SrvResolverOptionsConverter;

@DataObject(generateConverter = true, publicConverter = false)
public class DnsResolverOptions {

private String host;
private int port;

public DnsResolverOptions() {
}

public DnsResolverOptions(DnsResolverOptions other) {
this.host = other.host;
this.port = other.port;
}

public DnsResolverOptions(JsonObject json) {
DnsResolverOptionsConverter.fromJson(json, this);
}

public String getHost() {
return host;
}

public DnsResolverOptions setHost(String host) {
this.host = host;
return this;
}

public int getPort() {
return port;
}

public DnsResolverOptions setPort(int port) {
this.port = port;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2011-2023 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.serviceresolver.dns.impl;

import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.dns.AddressResolverOptions;
import io.vertx.core.impl.AddressResolver;
import io.vertx.core.impl.VertxInternal;
import io.vertx.core.net.Address;
import io.vertx.core.net.SocketAddress;
import io.vertx.serviceresolver.ServiceAddress;
import io.vertx.serviceresolver.dns.DnsResolverOptions;
import io.vertx.serviceresolver.impl.ResolverBase;
import io.vertx.serviceresolver.loadbalancing.LoadBalancer;
import io.vertx.serviceresolver.srv.SrvResolver;

import java.util.Collections;

public class DnsResolverImpl extends ResolverBase<SocketAddress, SocketAddress, DnsServiceState> implements SrvResolver {

private AddressResolver dnsResolver;

public DnsResolverImpl(Vertx vertx, DnsResolverOptions options, LoadBalancer loadBalancer) {
super(vertx, loadBalancer);

AddressResolverOptions o = new AddressResolverOptions();
o.setServers(Collections.singletonList(options.getHost() + ":" + options.getPort()));
dnsResolver = new AddressResolver(vertx, o);
}

@Override
public SocketAddress tryCast(Address address) {
return address instanceof ServiceAddress ? (SocketAddress) address : null;
}

@Override
public SocketAddress addressOfEndpoint(SocketAddress endpoint) {
return endpoint;
}

@Override
public Future<DnsServiceState> resolve(SocketAddress address) {
Promise<DnsServiceState> promise = Promise.promise();
dnsResolver.resolveHostnameAll(address.host(), ar -> {
if (ar.succeeded()) {
promise.complete(new DnsServiceState(address, 100, ar.result(), loadBalancer));
} else {
promise.fail(ar.cause());
}
});
return promise.future();
}

@Override
public void dispose(DnsServiceState state) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2011-2023 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.serviceresolver.dns.impl;

import io.vertx.core.net.SocketAddress;
import io.vertx.serviceresolver.impl.ServiceState;
import io.vertx.serviceresolver.loadbalancing.LoadBalancer;

import java.net.InetSocketAddress;
import java.util.List;

class DnsServiceState extends ServiceState<SocketAddress> {

final long timestamp;


DnsServiceState(SocketAddress address, long timestamp, List<InetSocketAddress> addresses, LoadBalancer loadBalancer) {
super(address.hostName(), loadBalancer);

for (InetSocketAddress addr : addresses) {
add(SocketAddress.inetSocketAddress(address.port(), addr.getAddress().getHostAddress()));
}

this.timestamp = timestamp;
}

@Override
protected boolean isValid() {
// long now = System.currentTimeMillis();
// for (EndpointImpl<SrvRecord> endpoint : endpoints()) {
// if (now > endpoint.get().ttl() * 1000 + timestamp) {
// return false;
// }
// }
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import io.vertx.serviceresolver.ServiceAddress;
import io.vertx.serviceresolver.loadbalancing.LoadBalancer;

public abstract class ResolverBase<E, T extends ServiceState<E>> implements AddressResolver<T, ServiceAddress, RequestMetric<E>, EndpointImpl<E>> {
public abstract class ResolverBase<A extends Address, E, T extends ServiceState<E>> implements AddressResolver<T, A, RequestMetric<E>, EndpointImpl<E>> {

protected final Vertx vertx;
protected final LoadBalancer loadBalancer;
Expand All @@ -33,11 +33,6 @@ public ResolverBase(Vertx vertx, LoadBalancer loadBalancer) {
this.loadBalancer = loadBalancer;
}

@Override
public ServiceAddress tryCast(Address address) {
return address instanceof ServiceAddress ? (ServiceAddress) address : null;
}

@Override
public EndpointImpl<E> pickEndpoint(T state) {
return (EndpointImpl<E>) state.pickAddress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@
import io.vertx.core.http.*;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.Address;
import io.vertx.core.net.SocketAddress;
import io.vertx.serviceresolver.impl.EndpointImpl;
import io.vertx.serviceresolver.loadbalancing.Endpoint;
import io.vertx.serviceresolver.ServiceAddress;
import io.vertx.serviceresolver.impl.ResolverBase;
import io.vertx.serviceresolver.kube.KubeResolverOptions;
import io.vertx.serviceresolver.loadbalancing.LoadBalancer;

import static io.vertx.core.http.HttpMethod.GET;

public class KubeResolverImpl extends ResolverBase<SocketAddress, KubeServiceState> {
public class KubeResolverImpl extends ResolverBase<ServiceAddress, SocketAddress, KubeServiceState> {

final String host;
final int port;
Expand All @@ -51,6 +50,11 @@ public KubeResolverImpl(Vertx vertx,
this.httpClient = vertx.createHttpClient(httpClientOptions == null ? new HttpClientOptions() : httpClientOptions);
}

@Override
public ServiceAddress tryCast(Address address) {
return address instanceof ServiceAddress ? (ServiceAddress) address : null;
}

@Override
public Future<KubeServiceState> resolve(ServiceAddress serviceName) {
return httpClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import io.vertx.core.impl.VertxInternal;
import io.vertx.core.net.Address;
import io.vertx.core.net.SocketAddress;
import io.vertx.serviceresolver.impl.EndpointImpl;
import io.vertx.serviceresolver.loadbalancing.Endpoint;
import io.vertx.serviceresolver.ServiceAddress;
import io.vertx.serviceresolver.impl.ResolverBase;
import io.vertx.serviceresolver.loadbalancing.LoadBalancer;
Expand All @@ -26,7 +24,7 @@

import java.util.List;

public class SrvResolverImpl extends ResolverBase<SrvRecord, SrvServiceState> implements SrvResolver {
public class SrvResolverImpl extends ResolverBase<ServiceAddress, SrvRecord, SrvServiceState> implements SrvResolver {

final String host;
final int port;
Expand Down
Loading

0 comments on commit 76d8bc5

Please sign in to comment.