From f35eee3532b1b608f1a962b04769c157bd8be70e Mon Sep 17 00:00:00 2001 From: Drew Bailey Date: Tue, 19 Jan 2021 13:00:05 -0500 Subject: [PATCH] set ConsulGatewayBindAddress to empty map instead of nil fix nil assertions for empty map rm unnecessary guard --- command/agent/job_endpoint.go | 3 +-- nomad/job_endpoint_hook_connect.go | 2 +- nomad/job_endpoint_hook_connect_test.go | 4 ++-- nomad/mock/mock.go | 3 ++- nomad/structs/services.go | 9 +++------ nomad/structs/services_test.go | 2 +- 6 files changed, 10 insertions(+), 13 deletions(-) diff --git a/command/agent/job_endpoint.go b/command/agent/job_endpoint.go index a1015b058c6..7f73453ca82 100644 --- a/command/agent/job_endpoint.go +++ b/command/agent/job_endpoint.go @@ -1345,9 +1345,8 @@ func apiConnectGatewayProxyToStructs(in *api.ConsulGatewayProxy) *structs.Consul return nil } - var bindAddresses map[string]*structs.ConsulGatewayBindAddress + bindAddresses := make(map[string]*structs.ConsulGatewayBindAddress) if in.EnvoyGatewayBindAddresses != nil { - bindAddresses = make(map[string]*structs.ConsulGatewayBindAddress) for k, v := range in.EnvoyGatewayBindAddresses { bindAddresses[k] = &structs.ConsulGatewayBindAddress{ Address: v.Address, diff --git a/nomad/job_endpoint_hook_connect.go b/nomad/job_endpoint_hook_connect.go index 40f7eeeb0a1..e7249097f71 100644 --- a/nomad/job_endpoint_hook_connect.go +++ b/nomad/job_endpoint_hook_connect.go @@ -345,7 +345,7 @@ func gatewayProxyForBridge(gateway *structs.ConsulGateway) *structs.ConsulGatewa func gatewayBindAddresses(ingress *structs.ConsulIngressConfigEntry) map[string]*structs.ConsulGatewayBindAddress { if ingress == nil || len(ingress.Listeners) == 0 { - return nil + return make(map[string]*structs.ConsulGatewayBindAddress) } addresses := make(map[string]*structs.ConsulGatewayBindAddress) diff --git a/nomad/job_endpoint_hook_connect_test.go b/nomad/job_endpoint_hook_connect_test.go index 53252c2acf4..494dca56c17 100644 --- a/nomad/job_endpoint_hook_connect_test.go +++ b/nomad/job_endpoint_hook_connect_test.go @@ -412,12 +412,12 @@ func TestJobEndpointConnect_gatewayProxyIsDefault(t *testing.T) { func TestJobEndpointConnect_gatewayBindAddresses(t *testing.T) { t.Run("nil", func(t *testing.T) { result := gatewayBindAddresses(nil) - require.Nil(t, result) + require.Empty(t, result) }) t.Run("no listeners", func(t *testing.T) { result := gatewayBindAddresses(&structs.ConsulIngressConfigEntry{Listeners: nil}) - require.Nil(t, result) + require.Empty(t, result) }) t.Run("simple", func(t *testing.T) { diff --git a/nomad/mock/mock.go b/nomad/mock/mock.go index ccb8a4c1910..a3fe090542c 100644 --- a/nomad/mock/mock.go +++ b/nomad/mock/mock.go @@ -875,7 +875,8 @@ func ConnectIngressGatewayJob(mode string, inject bool) *structs.Job { Connect: &structs.ConsulConnect{ Gateway: &structs.ConsulGateway{ Proxy: &structs.ConsulGatewayProxy{ - ConnectTimeout: helper.TimeToPtr(3 * time.Second), + ConnectTimeout: helper.TimeToPtr(3 * time.Second), + EnvoyGatewayBindAddresses: make(map[string]*structs.ConsulGatewayBindAddress), }, Ingress: &structs.ConsulIngressConfigEntry{ Listeners: []*structs.ConsulIngressListener{{ diff --git a/nomad/structs/services.go b/nomad/structs/services.go index 54935b7310d..2ca1dd81f2d 100644 --- a/nomad/structs/services.go +++ b/nomad/structs/services.go @@ -1352,12 +1352,9 @@ func (p *ConsulGatewayProxy) Copy() *ConsulGatewayProxy { return nil } - var bindAddresses map[string]*ConsulGatewayBindAddress - if p.EnvoyGatewayBindAddresses != nil { - bindAddresses = make(map[string]*ConsulGatewayBindAddress, len(p.EnvoyGatewayBindAddresses)) - for k, v := range p.EnvoyGatewayBindAddresses { - bindAddresses[k] = v.Copy() - } + bindAddresses := make(map[string]*ConsulGatewayBindAddress, len(p.EnvoyGatewayBindAddresses)) + for k, v := range p.EnvoyGatewayBindAddresses { + bindAddresses[k] = v.Copy() } return &ConsulGatewayProxy{ diff --git a/nomad/structs/services_test.go b/nomad/structs/services_test.go index 3b46b4895ac..493c6fefcc2 100644 --- a/nomad/structs/services_test.go +++ b/nomad/structs/services_test.go @@ -299,7 +299,7 @@ func TestConsulConnect_GatewayProxy_CopyEquals(t *testing.T) { c := &ConsulGatewayProxy{ ConnectTimeout: helper.TimeToPtr(1 * time.Second), EnvoyGatewayBindTaggedAddresses: false, - EnvoyGatewayBindAddresses: nil, + EnvoyGatewayBindAddresses: make(map[string]*ConsulGatewayBindAddress), } require.NoError(t, c.Validate())