diff --git a/agent/agent_endpoint_test.go b/agent/agent_endpoint_test.go index 3adda1db46..c963682e65 100644 --- a/agent/agent_endpoint_test.go +++ b/agent/agent_endpoint_test.go @@ -1536,7 +1536,7 @@ func TestAgent_RegisterService_ConnectNative(t *testing.T) { Check: structs.CheckType{ TTL: 15 * time.Second, }, - Connect: &structs.ServiceDefinitionConnect{ + Connect: &structs.ServiceConnect{ Native: true, }, } @@ -1550,7 +1550,7 @@ func TestAgent_RegisterService_ConnectNative(t *testing.T) { // Ensure the service svc, ok := a.State.Services()["web"] assert.True(ok, "has service") - assert.True(svc.ConnectNative) + assert.True(svc.Connect.Native) } func TestAgent_DeregisterService(t *testing.T) { @@ -2260,7 +2260,7 @@ func TestAgentConnectCALeafCert_aclDefaultDeny(t *testing.T) { Check: structs.CheckType{ TTL: 15 * time.Second, }, - Connect: &structs.ServiceDefinitionConnect{ + Connect: &structs.ServiceConnect{ Proxy: &structs.ServiceDefinitionConnectProxy{}, }, } @@ -2300,7 +2300,7 @@ func TestAgentConnectCALeafCert_aclProxyToken(t *testing.T) { Check: structs.CheckType{ TTL: 15 * time.Second, }, - Connect: &structs.ServiceDefinitionConnect{ + Connect: &structs.ServiceConnect{ Proxy: &structs.ServiceDefinitionConnectProxy{}, }, } @@ -2349,7 +2349,7 @@ func TestAgentConnectCALeafCert_aclProxyTokenOther(t *testing.T) { Check: structs.CheckType{ TTL: 15 * time.Second, }, - Connect: &structs.ServiceDefinitionConnect{ + Connect: &structs.ServiceConnect{ Proxy: &structs.ServiceDefinitionConnectProxy{}, }, } @@ -2371,7 +2371,7 @@ func TestAgentConnectCALeafCert_aclProxyTokenOther(t *testing.T) { Check: structs.CheckType{ TTL: 15 * time.Second, }, - Connect: &structs.ServiceDefinitionConnect{ + Connect: &structs.ServiceConnect{ Proxy: &structs.ServiceDefinitionConnectProxy{}, }, } @@ -2417,7 +2417,7 @@ func TestAgentConnectCALeafCert_aclServiceWrite(t *testing.T) { Check: structs.CheckType{ TTL: 15 * time.Second, }, - Connect: &structs.ServiceDefinitionConnect{ + Connect: &structs.ServiceConnect{ Proxy: &structs.ServiceDefinitionConnectProxy{}, }, } @@ -2478,7 +2478,7 @@ func TestAgentConnectCALeafCert_aclServiceReadDeny(t *testing.T) { Check: structs.CheckType{ TTL: 15 * time.Second, }, - Connect: &structs.ServiceDefinitionConnect{ + Connect: &structs.ServiceConnect{ Proxy: &structs.ServiceDefinitionConnectProxy{}, }, } @@ -2758,7 +2758,7 @@ func TestAgentConnectProxyConfig_Blocking(t *testing.T) { Check: structs.CheckType{ TTL: 15 * time.Second, }, - Connect: &structs.ServiceDefinitionConnect{ + Connect: &structs.ServiceConnect{ Proxy: &structs.ServiceDefinitionConnectProxy{ Command: []string{"tubes.sh"}, Config: map[string]interface{}{ @@ -2964,7 +2964,7 @@ func TestAgentConnectProxyConfig_aclDefaultDeny(t *testing.T) { Check: structs.CheckType{ TTL: 15 * time.Second, }, - Connect: &structs.ServiceDefinitionConnect{ + Connect: &structs.ServiceConnect{ Proxy: &structs.ServiceDefinitionConnectProxy{}, }, } @@ -3003,7 +3003,7 @@ func TestAgentConnectProxyConfig_aclProxyToken(t *testing.T) { Check: structs.CheckType{ TTL: 15 * time.Second, }, - Connect: &structs.ServiceDefinitionConnect{ + Connect: &structs.ServiceConnect{ Proxy: &structs.ServiceDefinitionConnectProxy{}, }, } @@ -3054,7 +3054,7 @@ func TestAgentConnectProxyConfig_aclServiceWrite(t *testing.T) { Check: structs.CheckType{ TTL: 15 * time.Second, }, - Connect: &structs.ServiceDefinitionConnect{ + Connect: &structs.ServiceConnect{ Proxy: &structs.ServiceDefinitionConnectProxy{}, }, } @@ -3116,7 +3116,7 @@ func TestAgentConnectProxyConfig_aclServiceReadDeny(t *testing.T) { Check: structs.CheckType{ TTL: 15 * time.Second, }, - Connect: &structs.ServiceDefinitionConnect{ + Connect: &structs.ServiceConnect{ Proxy: &structs.ServiceDefinitionConnectProxy{}, }, } @@ -3171,7 +3171,7 @@ func TestAgentConnectProxyConfig_ConfigHandling(t *testing.T) { Check: structs.CheckType{ TTL: 15 * time.Second, }, - Connect: &structs.ServiceDefinitionConnect{}, + Connect: &structs.ServiceConnect{}, } tests := []struct { diff --git a/agent/config/builder.go b/agent/config/builder.go index 88f65ec76b..c974ef0fd8 100644 --- a/agent/config/builder.go +++ b/agent/config/builder.go @@ -1049,7 +1049,7 @@ func (b *Builder) serviceVal(v *ServiceDefinition) *structs.ServiceDefinition { } } -func (b *Builder) serviceConnectVal(v *ServiceConnect) *structs.ServiceDefinitionConnect { +func (b *Builder) serviceConnectVal(v *ServiceConnect) *structs.ServiceConnect { if v == nil { return nil } @@ -1063,7 +1063,7 @@ func (b *Builder) serviceConnectVal(v *ServiceConnect) *structs.ServiceDefinitio } } - return &structs.ServiceDefinitionConnect{ + return &structs.ServiceConnect{ Proxy: proxy, } } diff --git a/agent/config/runtime_test.go b/agent/config/runtime_test.go index 2e72b4639d..d315ea8f7e 100644 --- a/agent/config/runtime_test.go +++ b/agent/config/runtime_test.go @@ -2097,7 +2097,7 @@ func TestConfigFlagsAndEdgecases(t *testing.T) { &structs.ServiceDefinition{ Name: "web", Port: 8080, - Connect: &structs.ServiceDefinitionConnect{ + Connect: &structs.ServiceConnect{ Proxy: &structs.ServiceDefinitionConnectProxy{ Config: map[string]interface{}{ "upstreams": []map[string]interface{}{ @@ -2141,7 +2141,7 @@ func TestConfigFlagsAndEdgecases(t *testing.T) { &structs.ServiceDefinition{ Name: "web", Port: 8080, - Connect: &structs.ServiceDefinitionConnect{ + Connect: &structs.ServiceConnect{ Proxy: &structs.ServiceDefinitionConnectProxy{ Config: map[string]interface{}{ "upstreams": []interface{}{ @@ -3699,7 +3699,7 @@ func TestFullConfig(t *testing.T) { DeregisterCriticalServiceAfter: 68482 * time.Second, }, }, - Connect: &structs.ServiceDefinitionConnect{ + Connect: &structs.ServiceConnect{ Proxy: &structs.ServiceDefinitionConnectProxy{ ExecMode: "daemon", Command: []string{"awesome-proxy"}, diff --git a/agent/consul/catalog_endpoint_test.go b/agent/consul/catalog_endpoint_test.go index d1864614be..7873fb7452 100644 --- a/agent/consul/catalog_endpoint_test.go +++ b/agent/consul/catalog_endpoint_test.go @@ -456,7 +456,7 @@ func TestCatalog_Register_ConnectNative(t *testing.T) { defer codec.Close() args := structs.TestRegisterRequest(t) - args.Service.ConnectNative = true + args.Service.Connect.Native = true // Register var out struct{} @@ -472,7 +472,7 @@ func TestCatalog_Register_ConnectNative(t *testing.T) { assert.Len(resp.ServiceNodes, 1) v := resp.ServiceNodes[0] assert.Equal(structs.ServiceKindTypical, v.ServiceKind) - assert.True(v.ServiceConnectNative) + assert.True(v.ServiceConnect.Native) } func TestCatalog_Deregister(t *testing.T) { @@ -1840,7 +1840,7 @@ func TestCatalog_ListServiceNodes_ConnectDestinationNative(t *testing.T) { // Register the native service args := structs.TestRegisterRequest(t) - args.Service.ConnectNative = true + args.Service.Connect.Native = true var out struct{} require.Nil(msgpackrpc.CallWithCodec(codec, "Catalog.Register", args, &out)) @@ -1965,7 +1965,7 @@ func TestCatalog_ListServiceNodes_ConnectNative(t *testing.T) { // Register the service args := structs.TestRegisterRequest(t) - args.Service.ConnectNative = true + args.Service.Connect.Native = true var out struct{} assert.Nil(msgpackrpc.CallWithCodec(codec, "Catalog.Register", args, &out)) @@ -1979,7 +1979,7 @@ func TestCatalog_ListServiceNodes_ConnectNative(t *testing.T) { assert.Nil(msgpackrpc.CallWithCodec(codec, "Catalog.ServiceNodes", &req, &resp)) assert.Len(resp.ServiceNodes, 1) v := resp.ServiceNodes[0] - assert.Equal(args.Service.ConnectNative, v.ServiceConnectNative) + assert.Equal(args.Service.Connect.Native, v.ServiceConnect.Native) } func TestCatalog_NodeServices(t *testing.T) { @@ -2090,7 +2090,7 @@ func TestCatalog_NodeServices_ConnectNative(t *testing.T) { assert.Len(resp.NodeServices.Services, 1) v := resp.NodeServices.Services[args.Service.Service] - assert.Equal(args.Service.ConnectNative, v.ConnectNative) + assert.Equal(args.Service.Connect.Native, v.Connect.Native) } // Used to check for a regression against a known bug diff --git a/agent/consul/state/catalog_test.go b/agent/consul/state/catalog_test.go index 2dd0fdaf29..17a411aabf 100644 --- a/agent/consul/state/catalog_test.go +++ b/agent/consul/state/catalog_test.go @@ -1590,7 +1590,7 @@ func TestStateStore_ConnectServiceNodes(t *testing.T) { assert.Nil(s.EnsureService(13, "bar", &structs.NodeService{ID: "api", Service: "api", Tags: nil, Address: "", Port: 5000})) assert.Nil(s.EnsureService(14, "foo", &structs.NodeService{Kind: structs.ServiceKindConnectProxy, ID: "proxy", Service: "proxy", ProxyDestination: "db", Port: 8000})) assert.Nil(s.EnsureService(15, "bar", &structs.NodeService{Kind: structs.ServiceKindConnectProxy, ID: "proxy", Service: "proxy", ProxyDestination: "db", Port: 8000})) - assert.Nil(s.EnsureService(16, "bar", &structs.NodeService{ID: "native-db", Service: "db", ConnectNative: true})) + assert.Nil(s.EnsureService(16, "bar", &structs.NodeService{ID: "native-db", Service: "db", Connect: structs.ServiceConnect{Native: true}})) assert.Nil(s.EnsureService(17, "bar", &structs.NodeService{ID: "db2", Service: "db", Tags: []string{"slave"}, Address: "", Port: 8001})) assert.True(watchFired(ws)) @@ -1604,7 +1604,7 @@ func TestStateStore_ConnectServiceNodes(t *testing.T) { for _, n := range nodes { assert.True( n.ServiceKind == structs.ServiceKindConnectProxy || - n.ServiceConnectNative, + n.ServiceConnect.Native, "either proxy or connect native") } diff --git a/agent/consul/state/index_connect.go b/agent/consul/state/index_connect.go index dc1390af07..029d8b6964 100644 --- a/agent/consul/state/index_connect.go +++ b/agent/consul/state/index_connect.go @@ -25,7 +25,7 @@ func (idx *IndexConnectService) FromObject(obj interface{}) (bool, []byte, error // For proxies, this service supports Connect for the destination result = []byte(strings.ToLower(sn.ServiceProxyDestination)) - case sn.ServiceConnectNative: + case sn.ServiceConnect.Native: // For native, this service supports Connect directly result = []byte(strings.ToLower(sn.ServiceName)) diff --git a/agent/consul/state/index_connect_test.go b/agent/consul/state/index_connect_test.go index f8e50c91f1..11cad4417e 100644 --- a/agent/consul/state/index_connect_test.go +++ b/agent/consul/state/index_connect_test.go @@ -36,8 +36,8 @@ func TestIndexConnectService_FromObject(t *testing.T) { { "typical service, is native", &structs.ServiceNode{ - ServiceName: "dB", - ServiceConnectNative: true, + ServiceName: "dB", + ServiceConnect: structs.ServiceConnect{Native: true}, }, true, []byte("db\x00"), diff --git a/agent/structs/service_definition.go b/agent/structs/service_definition.go index 26d1585299..c02ea67710 100644 --- a/agent/structs/service_definition.go +++ b/agent/structs/service_definition.go @@ -21,7 +21,7 @@ type ServiceDefinition struct { Token string EnableTagOverride bool ProxyDestination string - Connect *ServiceDefinitionConnect + Connect *ServiceConnect } func (s *ServiceDefinition) NodeService() *NodeService { @@ -37,7 +37,7 @@ func (s *ServiceDefinition) NodeService() *NodeService { ProxyDestination: s.ProxyDestination, } if s.Connect != nil { - ns.ConnectNative = s.Connect.Native + ns.Connect = *s.Connect } if ns.ID == "" && ns.Service != "" { ns.ID = ns.Service @@ -126,17 +126,6 @@ func (s *ServiceDefinition) CheckTypes() (checks CheckTypes, err error) { return checks, nil } -// ServiceDefinitionConnect is the connect block within a service registration. -// Note this is duplicated in config.ServiceConnect and needs to be kept in -// sync. -type ServiceDefinitionConnect struct { - // Native is true when this service can natively understand Connect. - Native bool - - // Proxy configures a connect proxy instance for the service - Proxy *ServiceDefinitionConnectProxy -} - // ServiceDefinitionConnectProxy is the connect proxy config within a service // registration. Note this is duplicated in config.ServiceConnectProxy and needs // to be kept in sync. diff --git a/agent/structs/service_definition_test.go b/agent/structs/service_definition_test.go index 54c711db70..3beefc28f3 100644 --- a/agent/structs/service_definition_test.go +++ b/agent/structs/service_definition_test.go @@ -73,7 +73,7 @@ func TestServiceDefinitionValidate(t *testing.T) { "managed proxy with a port set", func(x *ServiceDefinition) { x.Port = 8080 - x.Connect = &ServiceDefinitionConnect{ + x.Connect = &ServiceConnect{ Proxy: &ServiceDefinitionConnectProxy{}, } }, @@ -83,7 +83,7 @@ func TestServiceDefinitionValidate(t *testing.T) { { "managed proxy with no port set", func(x *ServiceDefinition) { - x.Connect = &ServiceDefinitionConnect{ + x.Connect = &ServiceConnect{ Proxy: &ServiceDefinitionConnectProxy{}, } }, @@ -94,7 +94,7 @@ func TestServiceDefinitionValidate(t *testing.T) { "managed proxy with native set", func(x *ServiceDefinition) { x.Port = 8080 - x.Connect = &ServiceDefinitionConnect{ + x.Connect = &ServiceConnect{ Native: true, Proxy: &ServiceDefinitionConnectProxy{}, } diff --git a/agent/structs/structs.go b/agent/structs/structs.go index 4f8eb9eb68..2cc75e1e96 100644 --- a/agent/structs/structs.go +++ b/agent/structs/structs.go @@ -432,7 +432,7 @@ type ServiceNode struct { ServicePort int ServiceEnableTagOverride bool ServiceProxyDestination string - ServiceConnectNative bool + ServiceConnect ServiceConnect RaftIndex } @@ -461,7 +461,7 @@ func (s *ServiceNode) PartialClone() *ServiceNode { ServiceMeta: nsmeta, ServiceEnableTagOverride: s.ServiceEnableTagOverride, ServiceProxyDestination: s.ServiceProxyDestination, - ServiceConnectNative: s.ServiceConnectNative, + ServiceConnect: s.ServiceConnect, RaftIndex: RaftIndex{ CreateIndex: s.CreateIndex, ModifyIndex: s.ModifyIndex, @@ -481,7 +481,7 @@ func (s *ServiceNode) ToNodeService() *NodeService { Meta: s.ServiceMeta, EnableTagOverride: s.ServiceEnableTagOverride, ProxyDestination: s.ServiceProxyDestination, - ConnectNative: s.ServiceConnectNative, + Connect: s.ServiceConnect, RaftIndex: RaftIndex{ CreateIndex: s.CreateIndex, ModifyIndex: s.ModifyIndex, @@ -529,12 +529,25 @@ type NodeService struct { // earlier than their target services. ProxyDestination string - // ConnectNative is true if this service speaks the Connect protocol. - ConnectNative bool + // Connect are the Connect settings for a service. This is purposely NOT + // a pointer so that we never have to nil-check this. + Connect ServiceConnect RaftIndex } +// ServiceConnect are the shared Connect settings between all service +// definitions from the agent to the state store. +type ServiceConnect struct { + // Native is true when this service can natively understand Connect. + Native bool + + // Proxy configures a connect proxy instance for the service. This is + // only used for agent service definitions and is invalid for non-agent + // (catalog API) definitions. + Proxy *ServiceDefinitionConnectProxy +} + // Validate validates the node service configuration. // // NOTE(mitchellh): This currently only validates fields for a ConnectProxy. @@ -556,7 +569,7 @@ func (s *NodeService) Validate() error { "Port must be set for a Connect proxy")) } - if s.ConnectNative { + if s.Connect.Native { result = multierror.Append(result, fmt.Errorf( "A Proxy cannot also be ConnectNative, only typical services")) } @@ -579,7 +592,7 @@ func (s *NodeService) IsSame(other *NodeService) bool { s.EnableTagOverride != other.EnableTagOverride || s.Kind != other.Kind || s.ProxyDestination != other.ProxyDestination || - s.ConnectNative != other.ConnectNative { + s.Connect != other.Connect { return false } @@ -602,7 +615,7 @@ func (s *NodeService) ToServiceNode(node string) *ServiceNode { ServiceMeta: s.Meta, ServiceEnableTagOverride: s.EnableTagOverride, ServiceProxyDestination: s.ProxyDestination, - ServiceConnectNative: s.ConnectNative, + ServiceConnect: s.Connect, RaftIndex: RaftIndex{ CreateIndex: s.CreateIndex, ModifyIndex: s.ModifyIndex, diff --git a/agent/structs/structs_test.go b/agent/structs/structs_test.go index 95e4f88a79..5699339247 100644 --- a/agent/structs/structs_test.go +++ b/agent/structs/structs_test.go @@ -249,7 +249,7 @@ func TestStructs_NodeService_ValidateConnectProxy(t *testing.T) { { "connect-proxy: ConnectNative set", - func(x *NodeService) { x.ConnectNative = true }, + func(x *NodeService) { x.Connect.Native = true }, "cannot also be", }, } @@ -337,7 +337,7 @@ func TestStructs_NodeService_IsSame(t *testing.T) { check(func() { other.EnableTagOverride = false }, func() { other.EnableTagOverride = true }) check(func() { other.Kind = ServiceKindConnectProxy }, func() { other.Kind = "" }) check(func() { other.ProxyDestination = "" }, func() { other.ProxyDestination = "db" }) - check(func() { other.ConnectNative = true }, func() { other.ConnectNative = false }) + check(func() { other.Connect.Native = true }, func() { other.Connect.Native = false }) } func TestStructs_HealthCheck_IsSame(t *testing.T) { diff --git a/website/source/api/catalog.html.md b/website/source/api/catalog.html.md index 4da3ffa5e3..9e0ae90a33 100644 --- a/website/source/api/catalog.html.md +++ b/website/source/api/catalog.html.md @@ -54,7 +54,7 @@ The table below shows this endpoint's support for - `Service` `(Service: nil)` - Specifies to register a service. If `ID` is not provided, it will be defaulted to the value of the `Service.Service` property. Only one service with a given `ID` may be present per node. The service - `Tags`, `Address`, `ServiceMeta`, `Port`, `ConnectNative` fields are all optional. + `Tags`, `Address`, `ServiceMeta`, and `Port` fields are all optional. - `Check` `(Check: nil)` - Specifies to register a check. The register API manipulates the health check entry in the Catalog, but it does not setup the @@ -485,8 +485,8 @@ $ curl \ - `ServiceProxyDestination` is the name of the service that is being proxied, for "connect-proxy" type services. -- `ServiceConnectNative` is true if this service supports Connect - [natively](/docs/connect/native.html). +- `ServiceConnect` are the [Connect](/docs/connect/index.html) settings. The + value of this struct is equivalent to the `Connect` field for service registration. ## List Nodes for Connect-capable Service