-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added custom grpc resolver (#1008)
Signed-off-by: Pradeep Mishra <[email protected]> Co-authored-by: Michael Beemer <[email protected]>
- Loading branch information
1 parent
d88a6d2
commit 85403b7
Showing
20 changed files
with
484 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
.../java/dev/openfeature/contrib/providers/flagd/resolver/common/GenericConfigException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dev.openfeature.contrib.providers.flagd.resolver.common; | ||
|
||
/** | ||
* Custom exception for invalid gRPC configurations. | ||
*/ | ||
|
||
public class GenericConfigException extends RuntimeException { | ||
public GenericConfigException(String message) { | ||
super(message); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...rc/main/java/dev/openfeature/contrib/providers/flagd/resolver/common/SupportedScheme.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.openfeature.contrib.providers.flagd.resolver.common; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
enum SupportedScheme { | ||
ENVOY("envoy"), DNS("dns"), XDS("xds"), UDS("uds"); | ||
|
||
private final String scheme; | ||
|
||
SupportedScheme(String scheme) { | ||
this.scheme = scheme; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
.../dev/openfeature/contrib/providers/flagd/resolver/common/nameresolvers/EnvoyResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package dev.openfeature.contrib.providers.flagd.resolver.common.nameresolvers; | ||
|
||
import io.grpc.EquivalentAddressGroup; | ||
import io.grpc.NameResolver; | ||
import java.net.InetSocketAddress; | ||
import io.grpc.Attributes; | ||
import io.grpc.Status; | ||
import java.net.URI; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* Envoy NameResolver, will always override the authority with the specified authority and | ||
* use the socketAddress to connect. | ||
* | ||
* <p>Custom URI Scheme: | ||
* | ||
* <p>envoy://[proxy-agent-host]:[proxy-agent-port]/[service-name] | ||
* | ||
* <p>`service-name` is used as authority instead host | ||
*/ | ||
public class EnvoyResolver extends NameResolver { | ||
private final URI uri; | ||
private final String authority; | ||
private Listener2 listener; | ||
|
||
public EnvoyResolver(URI targetUri) { | ||
this.uri = targetUri; | ||
this.authority = targetUri.getPath().substring(1); | ||
} | ||
|
||
@Override | ||
public String getServiceAuthority() { | ||
return authority; | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
} | ||
|
||
@Override | ||
public void start(Listener2 listener) { | ||
this.listener = listener; | ||
this.resolve(); | ||
} | ||
|
||
@Override | ||
public void refresh() { | ||
this.resolve(); | ||
} | ||
|
||
private void resolve() { | ||
try { | ||
InetSocketAddress address = new InetSocketAddress(this.uri.getHost(), this.uri.getPort()); | ||
Attributes addressGroupAttributes = Attributes.newBuilder() | ||
.set(EquivalentAddressGroup.ATTR_AUTHORITY_OVERRIDE, this.authority) | ||
.build(); | ||
List<EquivalentAddressGroup> equivalentAddressGroup = Collections.singletonList( | ||
new EquivalentAddressGroup(address, addressGroupAttributes) | ||
); | ||
ResolutionResult resolutionResult = ResolutionResult.newBuilder() | ||
.setAddresses(equivalentAddressGroup) | ||
.build(); | ||
this.listener.onResult(resolutionResult); | ||
} catch (Exception e) { | ||
this.listener.onError(Status.UNAVAILABLE.withDescription("Unable to resolve host ").withCause(e)); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...nfeature/contrib/providers/flagd/resolver/common/nameresolvers/EnvoyResolverProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package dev.openfeature.contrib.providers.flagd.resolver.common.nameresolvers; | ||
|
||
import io.grpc.NameResolver; | ||
import io.grpc.NameResolverProvider; | ||
import java.net.URI; | ||
|
||
/** | ||
* A custom NameResolver provider to resolve gRPC target uri for envoy in the | ||
* format of. | ||
* | ||
* <p>envoy://[proxy-agent-host]:[proxy-agent-port]/[service-name] | ||
*/ | ||
public class EnvoyResolverProvider extends NameResolverProvider { | ||
static final String ENVOY_SCHEME = "envoy"; | ||
|
||
@Override | ||
protected boolean isAvailable() { | ||
return true; | ||
} | ||
|
||
// setting priority higher than the default i.e. 5 | ||
// could lead to issue since the resolver override the default | ||
// dns provider. | ||
// https://grpc.github.io/grpc-java/javadoc/io/grpc/NameResolverProvider.html?is-external=true#priority() | ||
@Override | ||
protected int priority() { | ||
return 5; | ||
} | ||
|
||
@Override | ||
public NameResolver newNameResolver(URI targetUri, NameResolver.Args args) { | ||
if (!ENVOY_SCHEME.equals(targetUri.getScheme())) { | ||
return null; | ||
} | ||
|
||
if (!isValidPath(targetUri.getPath()) || targetUri.getHost() == null || targetUri.getPort() == -1) { | ||
throw new IllegalArgumentException("Incorrectly formatted target uri; " | ||
+ "expected: '" + ENVOY_SCHEME + ":[//]<proxy-agent-host>:<proxy-agent-port>/<service-name>';" | ||
+ "but was '" + targetUri + "'"); | ||
} | ||
|
||
return new EnvoyResolver(targetUri); | ||
} | ||
|
||
@Override | ||
public String getDefaultScheme() { | ||
return ENVOY_SCHEME; | ||
} | ||
|
||
private static boolean isValidPath(String path) { | ||
return !path.isEmpty() && !path.substring(1).isEmpty() | ||
&& !path.substring(1).contains("/"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.