From 09568ce7b5d5436e8cf504bb2af506821aeff1c9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 5 Mar 2018 19:56:52 -0800 Subject: [PATCH] agent/consul/state: service registration with proxy works --- agent/consul/state/catalog_test.go | 34 ++++++++++++++++++++++++++++++ agent/structs/structs.go | 28 ++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/agent/consul/state/catalog_test.go b/agent/consul/state/catalog_test.go index a2b56cad8..dcd1d505a 100644 --- a/agent/consul/state/catalog_test.go +++ b/agent/consul/state/catalog_test.go @@ -981,6 +981,40 @@ func TestStateStore_EnsureService(t *testing.T) { } } +func TestStateStore_EnsureService_connectProxy(t *testing.T) { + s := testStateStore(t) + + // Create the service registration. + ns1 := &structs.NodeService{ + Kind: structs.ServiceKindConnectProxy, + ID: "connect-proxy", + Service: "connect-proxy", + Address: "1.1.1.1", + Port: 1111, + ProxyDestination: "foo", + } + + // Service successfully registers into the state store. + testRegisterNode(t, s, 0, "node1") + if err := s.EnsureService(10, "node1", ns1); err != nil { + t.Fatalf("err: %s", err) + } + + // Retrieve and verify + _, out, err := s.NodeServices(nil, "node1") + if err != nil { + t.Fatalf("err: %s", err) + } + if out == nil || len(out.Services) != 1 { + t.Fatalf("bad: %#v", out) + } + expect1 := *ns1 + expect1.CreateIndex, expect1.ModifyIndex = 10, 10 + if svc := out.Services["connect-proxy"]; !reflect.DeepEqual(&expect1, svc) { + t.Fatalf("bad: %#v", svc) + } +} + func TestStateStore_Services(t *testing.T) { s := testStateStore(t) diff --git a/agent/structs/structs.go b/agent/structs/structs.go index 8a1860912..23cd41acc 100644 --- a/agent/structs/structs.go +++ b/agent/structs/structs.go @@ -388,6 +388,7 @@ type ServiceNode struct { Datacenter string TaggedAddresses map[string]string NodeMeta map[string]string + ServiceKind ServiceKind ServiceID string ServiceName string ServiceTags []string @@ -395,6 +396,7 @@ type ServiceNode struct { ServiceMeta map[string]string ServicePort int ServiceEnableTagOverride bool + ServiceProxyDestination string RaftIndex } @@ -431,6 +433,7 @@ func (s *ServiceNode) PartialClone() *ServiceNode { // ToNodeService converts the given service node to a node service. func (s *ServiceNode) ToNodeService() *NodeService { return &NodeService{ + Kind: s.ServiceKind, ID: s.ServiceID, Service: s.ServiceName, Tags: s.ServiceTags, @@ -438,6 +441,7 @@ func (s *ServiceNode) ToNodeService() *NodeService { Port: s.ServicePort, Meta: s.ServiceMeta, EnableTagOverride: s.ServiceEnableTagOverride, + ProxyDestination: s.ServiceProxyDestination, RaftIndex: RaftIndex{ CreateIndex: s.CreateIndex, ModifyIndex: s.ModifyIndex, @@ -447,8 +451,26 @@ func (s *ServiceNode) ToNodeService() *NodeService { type ServiceNodes []*ServiceNode +// ServiceKind is the kind of service being registered. +type ServiceKind string + +const ( + // ServiceKindTypical is a typical, classic Consul service. + ServiceKindTypical ServiceKind = "typical" + + // ServiceKindConnectProxy is a proxy for the Connect feature. This + // service proxies another service within Consul and speaks the connect + // protocol. + ServiceKindConnectProxy ServiceKind = "connect-proxy" +) + // NodeService is a service provided by a node type NodeService struct { + // Kind is the kind of service this is. Different kinds of services may + // have differing validation, DNS behavior, etc. An empty kind will default + // to the Default kind. See ServiceKind for the full list of kinds. + Kind ServiceKind + ID string Service string Tags []string @@ -457,6 +479,10 @@ type NodeService struct { Port int EnableTagOverride bool + // ProxyDestination is the name of the service that this service is + // a Connect proxy for. This is only valid if Kind is "connect-proxy". + ProxyDestination string + RaftIndex } @@ -485,6 +511,7 @@ func (s *NodeService) ToServiceNode(node string) *ServiceNode { Node: node, // Skip Address, see ServiceNode definition. // Skip TaggedAddresses, see ServiceNode definition. + ServiceKind: s.Kind, ServiceID: s.ID, ServiceName: s.Service, ServiceTags: s.Tags, @@ -492,6 +519,7 @@ func (s *NodeService) ToServiceNode(node string) *ServiceNode { ServicePort: s.Port, ServiceMeta: s.Meta, ServiceEnableTagOverride: s.EnableTagOverride, + ServiceProxyDestination: s.ProxyDestination, RaftIndex: RaftIndex{ CreateIndex: s.CreateIndex, ModifyIndex: s.ModifyIndex,