agent: agent service registration supports Connect native services

This commit is contained in:
Mitchell Hashimoto 2018-06-03 22:20:16 -07:00 committed by Jack Pearkes
parent d6a823ad0d
commit 424272361d
3 changed files with 63 additions and 5 deletions

View File

@ -1519,6 +1519,40 @@ func TestAgent_RegisterService_UnmanagedConnectProxyInvalid(t *testing.T) {
assert.False(ok) assert.False(ok)
} }
// Tests agent registration of a service that is connect native.
func TestAgent_RegisterService_ConnectNative(t *testing.T) {
t.Parallel()
assert := assert.New(t)
a := NewTestAgent(t.Name(), "")
defer a.Shutdown()
// Register a proxy. Note that the destination doesn't exist here on
// this agent or in the catalog at all. This is intended and part
// of the design.
args := &structs.ServiceDefinition{
Name: "web",
Port: 8000,
Check: structs.CheckType{
TTL: 15 * time.Second,
},
Connect: &structs.ServiceDefinitionConnect{
Native: true,
},
}
req, _ := http.NewRequest("PUT", "/v1/agent/service/register", jsonReader(args))
resp := httptest.NewRecorder()
obj, err := a.srv.AgentRegisterService(resp, req)
assert.Nil(err)
assert.Nil(obj)
// Ensure the service
svc, ok := a.State.Services()["web"]
assert.True(ok, "has service")
assert.True(svc.ConnectNative)
}
func TestAgent_DeregisterService(t *testing.T) { func TestAgent_DeregisterService(t *testing.T) {
t.Parallel() t.Parallel()
a := NewTestAgent(t.Name(), "") a := NewTestAgent(t.Name(), "")

View File

@ -36,6 +36,9 @@ func (s *ServiceDefinition) NodeService() *NodeService {
EnableTagOverride: s.EnableTagOverride, EnableTagOverride: s.EnableTagOverride,
ProxyDestination: s.ProxyDestination, ProxyDestination: s.ProxyDestination,
} }
if s.Connect != nil {
ns.ConnectNative = s.Connect.Native
}
if ns.ID == "" && ns.Service != "" { if ns.ID == "" && ns.Service != "" {
ns.ID = ns.Service ns.ID = ns.Service
} }
@ -83,10 +86,17 @@ func (s *ServiceDefinition) Validate() error {
var result error var result error
if s.Kind == ServiceKindTypical { if s.Kind == ServiceKindTypical {
if s.Connect != nil && s.Connect.Proxy != nil { if s.Connect != nil {
if s.Port == 0 { if s.Connect.Proxy != nil {
result = multierror.Append(result, fmt.Errorf( if s.Connect.Native {
"Services with a Connect managed proxy must have a port set")) result = multierror.Append(result, fmt.Errorf(
"Services that are Connect native may not have a proxy configuration"))
}
if s.Port == 0 {
result = multierror.Append(result, fmt.Errorf(
"Services with a Connect managed proxy must have a port set"))
}
} }
} }
} }
@ -120,7 +130,9 @@ func (s *ServiceDefinition) CheckTypes() (checks CheckTypes, err error) {
// Note this is duplicated in config.ServiceConnect and needs to be kept in // Note this is duplicated in config.ServiceConnect and needs to be kept in
// sync. // sync.
type ServiceDefinitionConnect struct { type ServiceDefinitionConnect struct {
// TODO(banks) add way to specify that the app is connect-native // Native is true when this service can natively understand Connect.
Native bool
// Proxy configures a connect proxy instance for the service // Proxy configures a connect proxy instance for the service
Proxy *ServiceDefinitionConnectProxy Proxy *ServiceDefinitionConnectProxy
} }

View File

@ -89,6 +89,18 @@ func TestServiceDefinitionValidate(t *testing.T) {
}, },
"must have a port", "must have a port",
}, },
{
"managed proxy with native set",
func(x *ServiceDefinition) {
x.Port = 8080
x.Connect = &ServiceDefinitionConnect{
Native: true,
Proxy: &ServiceDefinitionConnectProxy{},
}
},
"may not have a proxy",
},
} }
for _, tc := range cases { for _, tc := range cases {