-
Notifications
You must be signed in to change notification settings - Fork 94
add support for discovery chain get method (#355) #369
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
Changes from 4 commits
f23e782
5ae3604
f0b3b88
2edcf32
f7a285a
cf90c7a
8c9400e
3d0abe4
cea4c2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="DiscoveryChainTest.cs" company="G-Research Limited"> | ||
| // Copyright 2020 G-Research Limited | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"), | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using NuGet.Versioning; | ||
| using Xunit; | ||
|
|
||
| namespace Consul.Test | ||
| { | ||
| public class DiscoveryChainTest : BaseFixture | ||
| { | ||
| public const string TestClusterID = "11111111-2222-3333-4444-555555555555"; | ||
| [Fact] | ||
| public async Task DiscoveryChaing_Get() | ||
| { | ||
| var cutOffVersion = SemanticVersion.Parse("1.11.0"); | ||
| string defaultPart = "default"; | ||
| if (AgentVersion >= cutOffVersion) defaultPart = "default.default"; | ||
|
|
||
| var check = new CompiledDiscoveryChain | ||
| { | ||
| ServiceName = "web", | ||
| Namespace = "default", | ||
| Datacenter = "dc1", | ||
| Protocol = "https", | ||
| StartNode = "resolver:web." + defaultPart + ".dc1", | ||
| Nodes = new Dictionary<string, DiscoveryGraphNode>() | ||
| { | ||
| { "resolver:web." + defaultPart + ".dc1", | ||
| new DiscoveryGraphNode { | ||
| Type = DiscoveryChain.DiscoveryGraphNodeTypeResolver, | ||
| Name = "web." + defaultPart + ".dc1", | ||
| Resolver = new DiscoveryResolver | ||
| { | ||
| Default = true, | ||
| ConnectTimeout = new TimeSpan(0, 0, 5), | ||
| Target = "web." + defaultPart + ".dc1" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| Targets = new Dictionary<string, DiscoveryTarget>() | ||
| { | ||
| { "web." + defaultPart + ".dc1", | ||
| new DiscoveryTarget | ||
| { | ||
| ID = "web." + defaultPart + ".dc1", | ||
| Service = "web", | ||
| Namespace = "default", | ||
| Datacenter = "dc1", | ||
| SNI = "web.default.dc1.internal." + TestClusterID + ".consul", | ||
| Name = "web.default.dc1.internal." + TestClusterID + ".consul" | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| var response = await _client.DiscoveryChain.Get("web"); | ||
| Assert.NotNull(response.Response); | ||
| var chain = response.Response.Chain; | ||
| Assert.Equal(check.ServiceName, chain.ServiceName); | ||
| Assert.Equal(check.Namespace, chain.Namespace); | ||
| Assert.Equal(check.Datacenter, chain.Datacenter); | ||
| Assert.Equal(check.Protocol, chain.Protocol); | ||
|
Check failure on line 83 in Consul.Test/DiscoveryChainTest.cs
|
||
| Assert.Equal(check.StartNode, chain.StartNode); | ||
|
Check failure on line 84 in Consul.Test/DiscoveryChainTest.cs
|
||
| var nodeCheck = check.Nodes["resolver:web." + defaultPart + ".dc1"]; | ||
| var nodeChain = chain.Nodes["resolver:web." + defaultPart + ".dc1"]; | ||
| Assert.Null(nodeCheck.Routes); | ||
| Assert.Equal(nodeCheck.Type, nodeChain.Type); | ||
| Assert.Equal(nodeCheck.Name, nodeChain.Name); | ||
| Assert.Equal(nodeCheck.Resolver.Default, nodeChain.Resolver.Default); | ||
| Assert.Equal(nodeCheck.Resolver.Target, nodeChain.Resolver.Target); | ||
| Assert.Equal(nodeCheck.Resolver.ConnectTimeout.ToString(), nodeChain.Resolver.ConnectTimeout.ToString()); | ||
| var targetCheck = check.Targets["web." + defaultPart + ".dc1"]; | ||
| var targetChain = chain.Targets["web." + defaultPart + ".dc1"]; | ||
| Assert.Equal(targetCheck.ID, targetChain.ID); | ||
| Assert.Equal(targetCheck.Service, targetChain.Service); | ||
| Assert.Equal(targetCheck.Namespace, targetChain.Namespace); | ||
| Assert.Equal(targetCheck.Datacenter, targetChain.Datacenter); | ||
| Assert.Equal(targetCheck.SNI, targetChain.SNI); | ||
| Assert.Equal(targetCheck.Name, targetChain.Name); | ||
|
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="DiscoveryChain.cs" company="G-Research Limited"> | ||
| // Copyright 2020 G-Research Limited | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"), | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Consul.Interfaces; | ||
| using Newtonsoft.Json; | ||
|
|
||
| namespace Consul | ||
| { | ||
| public class DiscoveryChainResponse | ||
| { | ||
| public CompiledDiscoveryChain Chain { get; set; } | ||
| } | ||
|
|
||
| public class CompiledDiscoveryChain | ||
| { | ||
| public string ServiceName { get; set; } | ||
| public string Namespace { get; set; } | ||
| public string Datacenter { get; set; } | ||
|
|
||
| // CustomizationHash is a unique hash of any data that affects the | ||
| // compilation of the discovery chain other than config entries or the | ||
| // name/namespace/datacenter evaluation criteria. | ||
| // | ||
| // If set, this value should be used to prefix/suffix any generated load | ||
| // balancer data plane objects to avoid sharing customized and | ||
| // non-customized versions. | ||
| public string CustomizationHash { get; set; } | ||
|
|
||
| // Protocol is the overall protocol shared by everything in the chain. | ||
| public string Protocol { get; set; } | ||
| // StartNode is the first key into the Nodes map that should be followed | ||
| // when walking the discovery chain. | ||
| public string StartNode { get; set; } | ||
|
|
||
| // Nodes contains all nodes available for traversal in the chain keyed by a | ||
| // unique name. You can walk this by starting with StartNode. | ||
| // | ||
| // NOTE: The names should be treated as opaque values and are only | ||
| // guaranteed to be consistent within a single compilation. | ||
| public Dictionary<string, DiscoveryGraphNode> Nodes { get; set; } | ||
|
|
||
| // Targets is a list of all targets used in this chain. | ||
| // NOTE: The names should be treated as opaque values and are only | ||
| // guaranteed to be consistent within a single compilation. | ||
| public Dictionary<string, DiscoveryTarget> Targets { get; set; } | ||
| } | ||
|
|
||
| public class DiscoveryGraphNode | ||
| { | ||
| public string Type { get; set; } | ||
| public string Name { get; set; } // This is NOT necessarily a service | ||
|
|
||
| // fields for Type==router | ||
| public List<DiscoveryRoute> Routes { get; set; } | ||
|
|
||
| // fields for Type==splitter | ||
| public List<DiscoverySplit> Splits { get; set; } | ||
|
|
||
| // fields for Type==resolver | ||
| public DiscoveryResolver Resolver { get; set; } | ||
|
|
||
| // shared by Type==resolver || Type==splitter | ||
| public LoadBalancerConfig LoadBalancer { get; set; } | ||
| } | ||
|
|
||
| public class DiscoverySplit | ||
| { | ||
| public float Weight { get; set; } | ||
| public string NextNode { get; set; } | ||
| } | ||
|
|
||
| public class DiscoveryRoute | ||
| { | ||
| public Routes Definition { get; set; } | ||
| public string NextNode { get; set; } | ||
| } | ||
|
|
||
| public class DiscoveryResolver | ||
| { | ||
| public bool Default { get; set; } | ||
|
|
||
| [JsonConverter(typeof(DurationTimespanConverter))] | ||
| public TimeSpan? ConnectTimeout { get; set; } | ||
|
|
||
| public string Target { get; set; } | ||
| public DiscoveryFailover Failover { get; set; } | ||
| } | ||
| public class DiscoveryFailover | ||
| { | ||
| public List<string> Targets { get; set; } | ||
| } | ||
|
|
||
| public class DiscoveryTarget | ||
| { | ||
| public string ID { get; set; } | ||
| public string Service { get; set; } | ||
| public string ServiceSubset { get; set; } | ||
| public string Namespace { get; set; } | ||
| public string Datacenter { get; set; } | ||
| public MeshGatewayConfig MeshGateway { get; set; } | ||
| public ServiceResolverEntry Subset { get; set; } | ||
| public bool External { get; set; } | ||
| public string SNI { get; set; } | ||
| public string Name { get; set; } | ||
| } | ||
|
|
||
| public class DiscoveryChain : IDiscoveryChainEndpoint | ||
| { | ||
| public const string DiscoveryGraphNodeTypeRouter = "router"; | ||
| public const string DiscoveryGraphNodeTypeSplitter = "splitter"; | ||
| public const string DiscoveryGraphNodeTypeResolver = "resolver"; | ||
|
|
||
| private readonly ConsulClient _client; | ||
|
|
||
| internal DiscoveryChain(ConsulClient c) | ||
| { | ||
| _client = c; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Get is used to return the compiled discovery chain for a service. | ||
| /// </summary> | ||
| /// <param name="name">Specifies the service to query when compiling the discovery chain</param> | ||
| /// <param name="q">Query Options</param> | ||
| /// <param name="ct">Cancellation Token</param> | ||
| /// <returns>An empty write result</returns> | ||
| public Task<QueryResult<DiscoveryChainResponse>> Get(string name, QueryOptions q, CancellationToken ct = default) | ||
| { | ||
| return _client.Get<DiscoveryChainResponse>($"/v1/discovery-chain/{name}", q).Execute(ct); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Get is used to return the compiled discovery chain for a service. | ||
| /// </summary> | ||
| /// <param name="name">Specifies the service to query when compiling the discovery chain</param> | ||
| /// <param name="ct">Cancellation Token</param> | ||
| /// <returns>An empty write result</returns> | ||
| public Task<QueryResult<DiscoveryChainResponse>> Get(string name, CancellationToken ct = default) | ||
| { | ||
| return Get(name, QueryOptions.Default, ct); | ||
| } | ||
| } | ||
|
|
||
| public partial class ConsulClient : IConsulClient | ||
| { | ||
| private Lazy<DiscoveryChain> _discoveryChain; | ||
|
|
||
| /// <summary> | ||
| /// DiscoveryChain returns a handle to the discovery chain endpoints | ||
| /// </summary> | ||
| public IDiscoveryChainEndpoint DiscoveryChain => _discoveryChain.Value; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="IDiscoveryChainEndpoint.cs" company="G-Research Limited"> | ||
| // Copyright 2020 G-Research Limited | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"), | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Consul.Interfaces | ||
| { | ||
| /// <summary> | ||
| /// The interface for the Discovery Chain API Endpoints | ||
| /// </summary> | ||
| public interface IDiscoveryChainEndpoint | ||
| { | ||
| Task<QueryResult<DiscoveryChainResponse>> Get(string name, QueryOptions q, CancellationToken ct = default); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also need overrides for compile-dc
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay got it 👍 |
||
| Task<QueryResult<DiscoveryChainResponse>> Get(string name, CancellationToken ct = default); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.