Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: ensure v1/health/ingress/:service endpoint works properly when streaming is enabled #9967

Merged
merged 4 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/9967.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
api: ensure v1/health/ingress/:service endpoint works properly when streaming is enabled
```
8 changes: 7 additions & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ type Agent struct {

// TODO: pass directly to HTTPHandlers and DNSServer once those are passed
// into Agent, which will allow us to remove this field.
rpcClientHealth *health.Client
rpcClientHealth *health.Client
rpcClientHealthNoStreaming *health.Client

// enterpriseAgent embeds fields that we only access in consul-enterprise builds
enterpriseAgent
Expand Down Expand Up @@ -378,6 +379,11 @@ func New(bd BaseDeps) (*Agent, error) {
NetRPC: &a,
CacheName: cacheName,
}
a.rpcClientHealthNoStreaming = &health.Client{
Cache: bd.Cache,
NetRPC: &a,
CacheName: cachetype.HealthServicesName,
}

a.serviceManager = NewServiceManager(&a)

Expand Down
9 changes: 7 additions & 2 deletions agent/health_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,19 @@ func (s *HTTPHandlers) healthServiceNodes(resp http.ResponseWriter, req *http.Re
return nil, nil
}

useStreaming := s.agent.config.UseStreamingBackend && args.MinQueryIndex > 0
useStreaming := s.agent.config.UseStreamingBackend && args.MinQueryIndex > 0 && !args.Ingress
args.QueryOptions.UseCache = s.agent.config.HTTPUseCache && (args.QueryOptions.UseCache || useStreaming)

if args.QueryOptions.UseCache && useStreaming && args.Source.Node != "" {
return nil, BadRequestError{Reason: "'near' query param can not be used with streaming"}
}

out, md, err := s.agent.rpcClientHealth.ServiceNodes(req.Context(), args)
healthClient := s.agent.rpcClientHealth
if args.Ingress {
healthClient = s.agent.rpcClientHealthNoStreaming
}
rboyer marked this conversation as resolved.
Show resolved Hide resolved

out, md, err := healthClient.ServiceNodes(req.Context(), args)
if err != nil {
return nil, err
}
Expand Down
71 changes: 55 additions & 16 deletions agent/health_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1418,13 +1418,22 @@ func TestHealthConnectServiceNodes(t *testing.T) {
}

func TestHealthIngressServiceNodes(t *testing.T) {
t.Run("no streaming", func(t *testing.T) {
testHealthIngressServiceNodes(t, ` rpc { enable_streaming = false } use_streaming_backend = false `)
})
t.Run("cache with streaming", func(t *testing.T) {
testHealthIngressServiceNodes(t, ` rpc { enable_streaming = true } use_streaming_backend = true `)
})
}

func testHealthIngressServiceNodes(t *testing.T, agentHCL string) {
t.Helper()
rboyer marked this conversation as resolved.
Show resolved Hide resolved

if testing.Short() {
t.Skip("too slow for testing.Short")
}

t.Parallel()

a := NewTestAgent(t, "")
a := NewTestAgent(t, agentHCL)
defer a.Shutdown()
testrpc.WaitForLeader(t, a.RPC, "dc1")

Expand Down Expand Up @@ -1461,34 +1470,64 @@ func TestHealthIngressServiceNodes(t *testing.T) {
require.Nil(t, a.RPC("ConfigEntry.Apply", req, &outB))
require.True(t, outB)

t.Run("associated service", func(t *testing.T) {
assert := assert.New(t)
checkResults := func(t *testing.T, obj interface{}) {
nodes := obj.(structs.CheckServiceNodes)
require.Len(t, nodes, 1)
require.Equal(t, structs.ServiceKindIngressGateway, nodes[0].Service.Kind)
require.Equal(t, gatewayArgs.Service.Address, nodes[0].Service.Address)
require.Equal(t, gatewayArgs.Service.Proxy, nodes[0].Service.Proxy)
}

require.True(t, t.Run("associated service", func(t *testing.T) {
req, _ := http.NewRequest("GET", fmt.Sprintf(
"/v1/health/ingress/%s", args.Service.Service), nil)
resp := httptest.NewRecorder()
obj, err := a.srv.HealthIngressServiceNodes(resp, req)
assert.Nil(err)
require.NoError(t, err)
assertIndex(t, resp)

nodes := obj.(structs.CheckServiceNodes)
require.Len(t, nodes, 1)
require.Equal(t, structs.ServiceKindIngressGateway, nodes[0].Service.Kind)
require.Equal(t, gatewayArgs.Service.Address, nodes[0].Service.Address)
require.Equal(t, gatewayArgs.Service.Proxy, nodes[0].Service.Proxy)
})
checkResults(t, obj)
}))

t.Run("non-associated service", func(t *testing.T) {
assert := assert.New(t)
require.True(t, t.Run("non-associated service", func(t *testing.T) {
req, _ := http.NewRequest("GET",
"/v1/health/connect/notexist", nil)
resp := httptest.NewRecorder()
obj, err := a.srv.HealthIngressServiceNodes(resp, req)
assert.Nil(err)
require.NoError(t, err)
assertIndex(t, resp)

nodes := obj.(structs.CheckServiceNodes)
require.Len(t, nodes, 0)
})
}))

require.True(t, t.Run("test caching miss", func(t *testing.T) {
// List instances with cache enabled
req, _ := http.NewRequest("GET", fmt.Sprintf(
"/v1/health/ingress/%s?cached", args.Service.Service), nil)
resp := httptest.NewRecorder()
obj, err := a.srv.HealthIngressServiceNodes(resp, req)
require.NoError(t, err)

checkResults(t, obj)

// Should be a cache miss
require.Equal(t, "MISS", resp.Header().Get("X-Cache"))
}))

require.True(t, t.Run("test caching hit", func(t *testing.T) {
// List instances with cache enabled
req, _ := http.NewRequest("GET", fmt.Sprintf(
"/v1/health/ingress/%s?cached", args.Service.Service), nil)
resp := httptest.NewRecorder()
obj, err := a.srv.HealthIngressServiceNodes(resp, req)
require.NoError(t, err)

checkResults(t, obj)

// Should be a cache HIT now!
require.Equal(t, "HIT", resp.Header().Get("X-Cache"))
}))
}

func TestHealthConnectServiceNodes_Filter(t *testing.T) {
Expand Down