api: update api module with health.Ingress() method

This commit is contained in:
Chris Piraino 2020-06-09 12:09:59 -05:00
parent 3c037d9b96
commit 4837069fe0
2 changed files with 79 additions and 6 deletions

View File

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

View File

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