agent: switch ConnectNative to an embedded struct

This commit is contained in:
Mitchell Hashimoto 2018-06-05 10:51:05 -07:00 committed by Jack Pearkes
parent 1df5d91b39
commit 1830c6b308
12 changed files with 61 additions and 59 deletions

View File

@ -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 {

View File

@ -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,
}
}

View File

@ -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"},

View File

@ -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

View File

@ -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")
}

View File

@ -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))

View File

@ -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"),

View File

@ -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.

View File

@ -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{},
}

View File

@ -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,

View File

@ -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) {

View File

@ -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