From 11486ac2a142ab7efe5c8a22cdd80451c2deddea Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Tue, 9 Jun 2020 15:13:19 -0700 Subject: [PATCH] Fix a CLI test failure with namespaces in enterprise --- command/connect/expose/expose.go | 16 +++++++++++++++- command/connect/expose/expose_test.go | 23 +++++++++++++++++------ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/command/connect/expose/expose.go b/command/connect/expose/expose.go index 4adf5db1f..1a5bce5a5 100644 --- a/command/connect/expose/expose.go +++ b/command/connect/expose/expose.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/hashicorp/consul/agent" + "github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/api" "github.com/hashicorp/consul/command/flags" "github.com/hashicorp/consul/command/intention/create" @@ -145,7 +146,7 @@ func (c *cmd) Run(args []string) int { // Make sure the service isn't already exposed in this gateway for j, service := range listener.Services { - if service.Name == svc && service.Namespace == svcNamespace { + if service.Name == svc && namespaceMatch(service.Namespace, svcNamespace) { serviceIdx = j c.UI.Output(fmt.Sprintf("Updating service definition for %q on listener with port %d", c.service, listener.Port)) break @@ -220,6 +221,19 @@ func (c *cmd) Run(args []string) int { return 0 } +func namespaceMatch(a, b string) bool { + namespaceA := a + namespaceB := b + if namespaceA == "" { + namespaceA = structs.IntentionDefaultNamespace + } + if namespaceB == "" { + namespaceB = structs.IntentionDefaultNamespace + } + + return namespaceA == namespaceB +} + func (c *cmd) Synopsis() string { return synopsis } diff --git a/command/connect/expose/expose_test.go b/command/connect/expose/expose_test.go index a509aa646..990bbfe59 100644 --- a/command/connect/expose/expose_test.go +++ b/command/connect/expose/expose_test.go @@ -38,16 +38,19 @@ func TestConnectExpose(t *testing.T) { // Make sure the config entry and intention have been created. entry, _, err := client.ConfigEntries().Get(api.IngressGateway, "ingress", nil) require.NoError(err) + ns := entry.(*api.IngressGatewayConfigEntry).Namespace expected := &api.IngressGatewayConfigEntry{ - Kind: api.IngressGateway, - Name: "ingress", + Kind: api.IngressGateway, + Name: "ingress", + Namespace: ns, Listeners: []api.IngressListener{ { Port: 8888, Protocol: "tcp", Services: []api.IngressService{ { - Name: "foo", + Name: "foo", + Namespace: ns, }, }, }, @@ -86,7 +89,8 @@ func TestConnectExpose(t *testing.T) { Protocol: "tcp", Services: []api.IngressService{ { - Name: "foo", + Name: "foo", + Namespace: ns, }, }, }) @@ -254,6 +258,7 @@ func TestConnectExpose_existingConfig(t *testing.T) { entry, _, err := client.ConfigEntries().Get(api.IngressGateway, "ingress", nil) require.NoError(err) + entryConf := entry.(*api.IngressGatewayConfigEntry) ingressConf.Listeners = append(ingressConf.Listeners, api.IngressListener{ Port: 10000, Protocol: "tcp", @@ -263,6 +268,10 @@ func TestConnectExpose_existingConfig(t *testing.T) { }, }, }) + ingressConf.Namespace = entryConf.Namespace + for i, listener := range ingressConf.Listeners { + listener.Services[0].Namespace = entryConf.Listeners[i].Services[0].Namespace + } ingressConf.CreateIndex = entry.GetCreateIndex() ingressConf.ModifyIndex = entry.GetModifyIndex() require.Equal(ingressConf, entry) @@ -292,9 +301,11 @@ func TestConnectExpose_existingConfig(t *testing.T) { entry, _, err := client.ConfigEntries().Get(api.IngressGateway, "ingress", nil) require.NoError(err) + entryConf := entry.(*api.IngressGatewayConfigEntry) ingressConf.Listeners[1].Services = append(ingressConf.Listeners[1].Services, api.IngressService{ - Name: "zoo", - Hosts: []string{"foo.com", "foo.net"}, + Name: "zoo", + Namespace: entryConf.Listeners[1].Services[1].Namespace, + Hosts: []string{"foo.com", "foo.net"}, }) ingressConf.CreateIndex = entry.GetCreateIndex() ingressConf.ModifyIndex = entry.GetModifyIndex()