-
Notifications
You must be signed in to change notification settings - Fork 53
feat: added custom grpc resolver #1008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
toddbaert
merged 11 commits into
open-feature:main
from
bookingcom:pmishra/995_support_grpc_custom_scheme
Oct 16, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b98cc93
feat: added custom grpc resolver
pradeepbbl 5815e5f
feat: fix lint errors
pradeepbbl 2553c84
feat: remove unsued var
pradeepbbl ba55d2d
feat: doc update
pradeepbbl c36ced9
Update providers/flagd/README.md
pradeepbbl 089848e
Update providers/flagd/README.md
pradeepbbl e0868fc
Update providers/flagd/README.md
pradeepbbl cce43cd
fix: custom resolver priority
pradeepbbl 8d7cee6
fix: added e2e test
pradeepbbl d85ac8e
fix: review fixes
pradeepbbl 5c00d9c
fix: remove typo ci fixes
pradeepbbl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.