From fdffe3bb2c492549119ff9460021378c08bc0c34 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 19 Oct 2023 09:08:16 +0100 Subject: [PATCH 1/6] Added Support for Retrieving Nodes that support Mesh Capable Service --- Consul.Test/CatalogTest.cs | 31 +++++++++++++++++++++ Consul/Catalog.cs | 39 +++++++++++++++++++++++++++ Consul/Interfaces/ICatalogEndpoint.cs | 4 +++ 3 files changed, 74 insertions(+) diff --git a/Consul.Test/CatalogTest.cs b/Consul.Test/CatalogTest.cs index 15142e9a..4853e8e5 100644 --- a/Consul.Test/CatalogTest.cs +++ b/Consul.Test/CatalogTest.cs @@ -174,6 +174,37 @@ public async Task Catalog_GetTaggedAddressesService() Assert.True(services.Response[0].ServiceTaggedAddresses.ContainsKey("lan")); } + [Fact] + public async Task Catalog_GetNodesForMeshCapableService() + { + var svcID = KVTest.GenerateTestKeyName(); + var registration = new CatalogRegistration + { + Datacenter = "dc1", + Node = "foobar", + Address = "192.168.10.10", + Service = new AgentService + { + ID = svcID, + Service = "redis", + Tags = new[] { "master", "v1" }, + Port = 8000, + TaggedAddresses = new Dictionary + { + {"lan", new ServiceTaggedAddress {Address = "127.0.0.1", Port = 80}}, + {"wan", new ServiceTaggedAddress {Address = "192.168.10.10", Port = 8000}} + } + } + }; + + await _client.Catalog.Register(registration); + + var services = await _client.Catalog.NodesForMeshCapableService("redis"); + + Assert.True(services.Response.Length==0); + + } + [Fact] public async Task Catalog_EnableTagOverride() { diff --git a/Consul/Catalog.cs b/Consul/Catalog.cs index 7d1fb59b..5d5d2f35 100644 --- a/Consul/Catalog.cs +++ b/Consul/Catalog.cs @@ -20,6 +20,7 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using Consul.Filtering; using Newtonsoft.Json; namespace Consul @@ -237,6 +238,44 @@ public Task> Service(string service, string tag, Q return req.Execute(ct); } + /// + /// Returns the nodes providing a mesh-capable service in a given datacenter. + /// + /// The service ID + /// Cancellation token for long poll request. If set, OperationCanceledException will be thrown if the request is cancelled before completing + /// + public Task> NodesForMeshCapableService(string service, CancellationToken ct = default) + { + return NodesForMeshCapableService(service,QueryOptions.Default, null, ct); + } + + /// + /// Returns the nodes providing a mesh-capable service in a given datacenter. + /// + /// The service ID + /// Specifies the expression used to filter the queries results prior to returning the data + /// Cancellation token for long poll request. If set, OperationCanceledException will be thrown if the request is cancelled before completing + /// + public Task> NodesForMeshCapableService(string service, Filter filter, CancellationToken ct = default) + { + return NodesForMeshCapableService(service, QueryOptions.Default, filter, ct); + } + + /// + /// Returns the nodes providing a mesh-capable service in a given datacenter. + /// + /// The service ID + /// Customized Query options + /// Specifies the expression used to filter the queries results prior to returning the data + /// Cancellation token for long poll request. If set, OperationCanceledException will be thrown if the request is cancelled before completing + /// + public Task> NodesForMeshCapableService(string service, QueryOptions q, Filter filter, CancellationToken ct = default) + { + var req = _client.Get(string.Format("/v1/catalog/connect/{0}", service), q, filter); + + return req.Execute(ct); + } + /// /// Node is used to query for service information about a single node /// diff --git a/Consul/Interfaces/ICatalogEndpoint.cs b/Consul/Interfaces/ICatalogEndpoint.cs index 1b71bb5d..a0eef7c7 100644 --- a/Consul/Interfaces/ICatalogEndpoint.cs +++ b/Consul/Interfaces/ICatalogEndpoint.cs @@ -21,6 +21,7 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using Consul.Filtering; namespace Consul { @@ -44,5 +45,8 @@ public interface ICatalogEndpoint Task> Service(string service, string tag, QueryOptions q, CancellationToken ct = default); Task>> Services(CancellationToken ct = default); Task>> Services(QueryOptions q, CancellationToken ct = default); + Task> NodesForMeshCapableService(string service, Filter filter, CancellationToken ct = default); + Task> NodesForMeshCapableService(string service, QueryOptions q, Filter filter, CancellationToken ct = default); + Task> NodesForMeshCapableService(string service, CancellationToken ct = default); } } From 47f71d6c11da038bb69ade21dee860817e470829 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 19 Oct 2023 09:29:07 +0100 Subject: [PATCH 2/6] fix: dotnet format --- Consul.Test/CatalogTest.cs | 2 +- Consul/Catalog.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Consul.Test/CatalogTest.cs b/Consul.Test/CatalogTest.cs index 4853e8e5..fef218ac 100644 --- a/Consul.Test/CatalogTest.cs +++ b/Consul.Test/CatalogTest.cs @@ -201,7 +201,7 @@ public async Task Catalog_GetNodesForMeshCapableService() var services = await _client.Catalog.NodesForMeshCapableService("redis"); - Assert.True(services.Response.Length==0); + Assert.True(services.Response.Length == 0); } diff --git a/Consul/Catalog.cs b/Consul/Catalog.cs index 5d5d2f35..ee25840d 100644 --- a/Consul/Catalog.cs +++ b/Consul/Catalog.cs @@ -246,7 +246,7 @@ public Task> Service(string service, string tag, Q /// public Task> NodesForMeshCapableService(string service, CancellationToken ct = default) { - return NodesForMeshCapableService(service,QueryOptions.Default, null, ct); + return NodesForMeshCapableService(service, QueryOptions.Default, null, ct); } /// From b71e6f829e5d0e9ee299268c0e943000ebd3dc97 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 19 Oct 2023 11:20:37 +0100 Subject: [PATCH 3/6] Update Consul/Catalog.cs Co-authored-by: Winston H. <56998716+winstxnhdw@users.noreply.github.com> --- Consul/Catalog.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Consul/Catalog.cs b/Consul/Catalog.cs index ee25840d..99a5b7be 100644 --- a/Consul/Catalog.cs +++ b/Consul/Catalog.cs @@ -243,7 +243,7 @@ public Task> Service(string service, string tag, Q /// /// The service ID /// Cancellation token for long poll request. If set, OperationCanceledException will be thrown if the request is cancelled before completing - /// + /// A list of service instances public Task> NodesForMeshCapableService(string service, CancellationToken ct = default) { return NodesForMeshCapableService(service, QueryOptions.Default, null, ct); From 6c2e853f9e4d6c15c08c1344c8036c994a2bf612 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 19 Oct 2023 11:21:01 +0100 Subject: [PATCH 4/6] Update Consul/Catalog.cs Co-authored-by: Winston H. <56998716+winstxnhdw@users.noreply.github.com> --- Consul/Catalog.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Consul/Catalog.cs b/Consul/Catalog.cs index 99a5b7be..9d4675c7 100644 --- a/Consul/Catalog.cs +++ b/Consul/Catalog.cs @@ -255,7 +255,7 @@ public Task> NodesForMeshCapableService(string ser /// The service ID /// Specifies the expression used to filter the queries results prior to returning the data /// Cancellation token for long poll request. If set, OperationCanceledException will be thrown if the request is cancelled before completing - /// + /// A list of service instances public Task> NodesForMeshCapableService(string service, Filter filter, CancellationToken ct = default) { return NodesForMeshCapableService(service, QueryOptions.Default, filter, ct); From 318941f60fae62918dfd805709d265e8380c59f9 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 19 Oct 2023 11:31:13 +0100 Subject: [PATCH 5/6] Update Consul/Catalog.cs Co-authored-by: Winston H. <56998716+winstxnhdw@users.noreply.github.com> --- Consul/Catalog.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Consul/Catalog.cs b/Consul/Catalog.cs index 9d4675c7..96de1e52 100644 --- a/Consul/Catalog.cs +++ b/Consul/Catalog.cs @@ -268,7 +268,7 @@ public Task> NodesForMeshCapableService(string ser /// Customized Query options /// Specifies the expression used to filter the queries results prior to returning the data /// Cancellation token for long poll request. If set, OperationCanceledException will be thrown if the request is cancelled before completing - /// + /// A list of service instances public Task> NodesForMeshCapableService(string service, QueryOptions q, Filter filter, CancellationToken ct = default) { var req = _client.Get(string.Format("/v1/catalog/connect/{0}", service), q, filter); From 26e10cf67c4b1fadfc635623e48e0603f047b1ba Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Tue, 28 Nov 2023 16:25:04 +0100 Subject: [PATCH 6/6] added support for mesh capabilities. --- Consul.Test/CatalogTest.cs | 34 ++++++++++++++-------------------- Consul.Test/test_config.json | 5 ++++- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/Consul.Test/CatalogTest.cs b/Consul.Test/CatalogTest.cs index fef218ac..a7041155 100644 --- a/Consul.Test/CatalogTest.cs +++ b/Consul.Test/CatalogTest.cs @@ -178,31 +178,25 @@ public async Task Catalog_GetTaggedAddressesService() public async Task Catalog_GetNodesForMeshCapableService() { var svcID = KVTest.GenerateTestKeyName(); - var registration = new CatalogRegistration + var registration = new AgentServiceRegistration { - Datacenter = "dc1", - Node = "foobar", - Address = "192.168.10.10", - Service = new AgentService + Name = svcID, + Port = 8000, + Connect = new AgentServiceConnect { - ID = svcID, - Service = "redis", - Tags = new[] { "master", "v1" }, - Port = 8000, - TaggedAddresses = new Dictionary + + SidecarService = new AgentServiceRegistration { - {"lan", new ServiceTaggedAddress {Address = "127.0.0.1", Port = 80}}, - {"wan", new ServiceTaggedAddress {Address = "192.168.10.10", Port = 8000}} - } - } + Name = "sidecar", + Port = 9000, + }, + }, }; + await _client.Agent.ServiceRegister(registration); - await _client.Catalog.Register(registration); - - var services = await _client.Catalog.NodesForMeshCapableService("redis"); - - Assert.True(services.Response.Length == 0); - + var services = await _client.Catalog.NodesForMeshCapableService(registration.Name); + Assert.NotEmpty(services.Response); + Assert.Equal(services.Response[0].ServiceID, registration.Name + "-sidecar-proxy"); } [Fact] diff --git a/Consul.Test/test_config.json b/Consul.Test/test_config.json index 7ca62e1f..74cc6c0e 100644 --- a/Consul.Test/test_config.json +++ b/Consul.Test/test_config.json @@ -11,6 +11,9 @@ } }, "enable_script_checks": true, + "connect": { + "enabled": true + }, "encrypt": "d8wu8CSUrqgtjVsvcBPmhQ==", - "enable_central_service_config": true + "enable_central_service_config": true }