diff --git a/agent/agent.go b/agent/agent.go index fa2ed94eb7..13678fc1ff 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -442,6 +442,11 @@ func (a *Agent) Start() error { if err != nil { return err } + go func() { + if err := a.proxyConfig.Run(); err != nil { + a.logger.Printf("[ERR] Proxy Config Manager exited: %s", err) + } + }() // Start watching for critical services to deregister, based on their // checks. diff --git a/agent/agent_endpoint.go b/agent/agent_endpoint.go index 0fcf4c294c..03daa14a7a 100644 --- a/agent/agent_endpoint.go +++ b/agent/agent_endpoint.go @@ -352,6 +352,14 @@ func (s *HTTPServer) AgentService(resp http.ResponseWriter, req *http.Request) ( proxy = svc.Proxy.ToAPI() } + var weights api.AgentWeights + if svc.Weights != nil { + err := mapstructure.Decode(svc.Weights, &weights) + if err != nil { + return "", nil, err + } + } + // Calculate the content hash over the response, minus the hash field reply := &api.AgentService{ Kind: api.ServiceKind(svc.Kind), @@ -362,6 +370,7 @@ func (s *HTTPServer) AgentService(resp http.ResponseWriter, req *http.Request) ( Port: svc.Port, Address: svc.Address, EnableTagOverride: svc.EnableTagOverride, + Weights: weights, Proxy: proxy, Connect: connect, } diff --git a/agent/agent_endpoint_test.go b/agent/agent_endpoint_test.go index c074e50fd9..dea70d4795 100644 --- a/agent/agent_endpoint_test.go +++ b/agent/agent_endpoint_test.go @@ -250,6 +250,10 @@ func TestAgent_Service(t *testing.T) { }, Port: 8000, Proxy: &proxy, + Weights: &structs.Weights{ + Passing: 1, + Warning: 1, + }, } // Define an updated version. Be careful to copy it. @@ -268,20 +272,28 @@ func TestAgent_Service(t *testing.T) { Service: "web-sidecar-proxy", Port: 8000, Proxy: expectProxy.ToAPI(), - ContentHash: "26959a754e182054", + ContentHash: "3442362e971c43d1", + Weights: api.AgentWeights{ + Passing: 1, + Warning: 1, + }, } // Copy and modify updatedResponse := *expectedResponse updatedResponse.Port = 9999 - updatedResponse.ContentHash = "1bdcf042660b33f6" + updatedResponse.ContentHash = "90b5c19bf0f5073" - // Simple response for non-proxy service regustered in TestAgent config + // Simple response for non-proxy service registered in TestAgent config expectWebResponse := &api.AgentService{ ID: "web", Service: "web", Port: 8181, - ContentHash: "7be2b0411161d3b1", + ContentHash: "69351c1ac865b034", + Weights: api.AgentWeights{ + Passing: 1, + Warning: 1, + }, } tests := []struct { diff --git a/agent/config/runtime_test.go b/agent/config/runtime_test.go index cc5f53ba26..fa55a963f7 100644 --- a/agent/config/runtime_test.go +++ b/agent/config/runtime_test.go @@ -2505,8 +2505,16 @@ func TestConfigFlagsAndEdgecases(t *testing.T) { }, }, }, + Weights: &structs.Weights{ + Passing: 1, + Warning: 1, + }, }, }, + Weights: &structs.Weights{ + Passing: 1, + Warning: 1, + }, }, } }, @@ -2592,8 +2600,16 @@ func TestConfigFlagsAndEdgecases(t *testing.T) { }, }, }, + Weights: &structs.Weights{ + Passing: 1, + Warning: 1, + }, }, }, + Weights: &structs.Weights{ + Passing: 1, + Warning: 1, + }, }, } }, @@ -4209,7 +4225,12 @@ func TestFullConfig(t *testing.T) { // it can make intelligent decisions about automatic port assignments // etc. So we expect config just to pass it through verbatim. Connect: &structs.ServiceConnect{ - SidecarService: &structs.ServiceDefinition{}, + SidecarService: &structs.ServiceDefinition{ + Weights: &structs.Weights{ + Passing: 1, + Warning: 1, + }, + }, }, }, { diff --git a/api/agent_test.go b/api/agent_test.go index 27da62eb18..8766e2ff56 100644 --- a/api/agent_test.go +++ b/api/agent_test.go @@ -673,7 +673,7 @@ func TestAPI_AgentService(t *testing.T) { ID: "foo", Service: "foo", Tags: []string{"bar", "baz"}, - ContentHash: "bf5bd67c5d74b26d", + ContentHash: "5d286f5494330b04", Port: 8000, } require.Equal(expect, got) @@ -683,7 +683,7 @@ func TestAPI_AgentService(t *testing.T) { // agent endpoint tests but this ensures that the API package is at least // passing the hash param properly. opts := QueryOptions{ - WaitHash: "bf5bd67c5d74b26d", + WaitHash: "5d286f5494330b04", WaitTime: 100 * time.Millisecond, // Just long enough to be reliably measurable } start := time.Now() diff --git a/command/services/config_test.go b/command/services/config_test.go index 238c286e41..d98e52a19f 100644 --- a/command/services/config_test.go +++ b/command/services/config_test.go @@ -88,9 +88,47 @@ func TestStructsToAgentService(t *testing.T) { }, }, }, + { + "Proxy service", + &structs.ServiceDefinition{ + Name: "web-proxy", + Kind: structs.ServiceKindConnectProxy, + Tags: []string{"leader"}, + Port: 1234, + Proxy: &structs.ConnectProxyConfig{ + DestinationServiceID: "web1", + DestinationServiceName: "web", + LocalServiceAddress: "127.0.0.1", + LocalServicePort: 8181, + Upstreams: structs.TestUpstreams(t), + Config: map[string]interface{}{ + "foo": "bar", + }, + }, + }, + &api.AgentServiceRegistration{ + Name: "web-proxy", + Tags: []string{"leader"}, + Port: 1234, + Kind: api.ServiceKindConnectProxy, + Proxy: &api.AgentServiceConnectProxyConfig{ + DestinationServiceID: "web1", + DestinationServiceName: "web", + LocalServiceAddress: "127.0.0.1", + LocalServicePort: 8181, + Upstreams: structs.TestUpstreams(t).ToAPI(), + Config: map[string]interface{}{ + "foo": "bar", + }, + }, + }, + }, } - for _, tc := range cases { + for _, tt := range cases { + // Capture the loop variable locally otherwise parallel will cause us to run + // N copies of the last test case but with different names!! + tc := tt t.Run(tc.Name, func(t *testing.T) { t.Parallel() require := require.New(t) @@ -100,6 +138,3 @@ func TestStructsToAgentService(t *testing.T) { }) } } - -func intPtr(v int) *int { return &v } -func strPtr(v string) *string { return &v }