From 4837069fe08823d52c094e3db9e1d42dd3634b1b Mon Sep 17 00:00:00 2001 From: Chris Piraino Date: Tue, 9 Jun 2020 12:09:59 -0500 Subject: [PATCH] api: update api module with health.Ingress() method --- api/health.go | 22 +++++++++++----- api/health_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/api/health.go b/api/health.go index 052c82902f..37d25d000b 100644 --- a/api/health.go +++ b/api/health.go @@ -269,11 +269,11 @@ func (h *Health) Service(service, tag string, passingOnly bool, q *QueryOptions) if tag != "" { tags = []string{tag} } - return h.service(service, tags, passingOnly, q, false) + return h.service(service, tags, passingOnly, q, false, false) } func (h *Health) ServiceMultipleTags(service string, tags []string, passingOnly bool, q *QueryOptions) ([]*ServiceEntry, *QueryMeta, error) { - return h.service(service, tags, passingOnly, q, false) + return h.service(service, tags, passingOnly, q, false, false) } // Connect is equivalent to Service except that it will only return services @@ -286,16 +286,23 @@ func (h *Health) Connect(service, tag string, passingOnly bool, q *QueryOptions) if tag != "" { tags = []string{tag} } - return h.service(service, tags, passingOnly, q, true) + return h.service(service, tags, passingOnly, q, true, false) } func (h *Health) ConnectMultipleTags(service string, tags []string, passingOnly bool, q *QueryOptions) ([]*ServiceEntry, *QueryMeta, error) { - return h.service(service, tags, passingOnly, q, true) + return h.service(service, tags, passingOnly, q, true, false) } -func (h *Health) service(service string, tags []string, passingOnly bool, q *QueryOptions, connect bool) ([]*ServiceEntry, *QueryMeta, error) { +// Ingress is equivalent to Connect except that it will only return associated +// ingress gateways for the requested service. +func (h *Health) Ingress(service string, passingOnly bool, q *QueryOptions) ([]*ServiceEntry, *QueryMeta, error) { + var tags []string + return h.service(service, tags, passingOnly, q, false, true) +} + +func (h *Health) service(service string, tags []string, passingOnly bool, q *QueryOptions, connect, ingress bool) ([]*ServiceEntry, *QueryMeta, error) { path := "/v1/health/service/" + service - if connect { + if connect || ingress { path = "/v1/health/connect/" + service } r := h.c.newRequest("GET", path) @@ -308,6 +315,9 @@ func (h *Health) service(service string, tags []string, passingOnly bool, q *Que if passingOnly { r.params.Set(HealthPassing, "1") } + if ingress { + r.params.Set("ingress", "1") + } rtt, resp, err := requireOK(h.c.doRequest(r)) if err != nil { return nil, nil, err diff --git a/api/health_test.go b/api/health_test.go index 858d63bed9..e097ff2d6a 100644 --- a/api/health_test.go +++ b/api/health_test.go @@ -546,6 +546,69 @@ func TestAPI_HealthConnect_Filter(t *testing.T) { require.Len(t, services, 1) } +func TestAPI_HealthConnect_Ingress(t *testing.T) { + t.Parallel() + c, s := makeClient(t) + defer s.Stop() + + agent := c.Agent() + health := c.Health() + + s.WaitForSerfCheck(t) + + // Make a service with a proxy + reg := &AgentServiceRegistration{ + Name: "foo", + Port: 8000, + } + err := agent.ServiceRegister(reg) + require.NoError(t, err) + defer agent.ServiceDeregister("foo") + + // Register the gateway + gatewayReg := &AgentServiceRegistration{ + Name: "foo-gateway", + Port: 8001, + Kind: ServiceKindIngressGateway, + } + err = agent.ServiceRegister(gatewayReg) + require.NoError(t, err) + defer agent.ServiceDeregister("foo-gateway") + + // Associate service and gateway + gatewayConfig := &IngressGatewayConfigEntry{ + Kind: IngressGateway, + Name: "foo-gateway", + Listeners: []IngressListener{ + { + Port: 2222, + Protocol: "tcp", + Services: []IngressService{ + { + Name: "foo", + }, + }, + }, + }, + } + _, wm, err := c.ConfigEntries().Set(gatewayConfig, nil) + require.NoError(t, err) + require.NotNil(t, wm) + + retry.Run(t, func(r *retry.R) { + services, meta, err := health.Ingress("foo", true, nil) + require.NoError(r, err) + + require.NotZero(r, meta.LastIndex) + + // Should be exactly 1 service - the original shouldn't show up as a connect + // endpoint, only it's proxy. + require.Len(r, services, 1) + require.Equal(r, services[0].Node.Datacenter, "dc1") + require.Equal(r, services[0].Service.Service, gatewayReg.Name) + }) +} + func TestAPI_HealthState(t *testing.T) { t.Parallel() c, s := makeClient(t)