Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion control-plane/api-gateway/binding/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (b *Binder) Snapshot() *Snapshot {
snapshot.Consul.Deregistrations = append(snapshot.Consul.Deregistrations, api.CatalogDeregistration{
Node: service.Node,
ServiceID: service.ServiceID,
Namespace: service.Namespace,
Namespace: b.config.Translator.Namespace(service.Namespace),
})
}
}
Expand Down
9 changes: 5 additions & 4 deletions control-plane/api-gateway/binding/binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

logrtest "github.com/go-logr/logr/testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -849,7 +850,7 @@ func TestBinder_Registrations(t *testing.T) {
}),
expectedRegistrations: []string{"pod1", "pod3", "pod4"},
expectedDeregistrations: []api.CatalogDeregistration{
{Node: "test", ServiceID: "pod2", Namespace: "namespace1"},
{Node: "test", ServiceID: "pod2", Namespace: ""},
},
},
} {
Expand All @@ -872,11 +873,11 @@ func TestBinder_Registrations(t *testing.T) {
registration := actual.Consul.Registrations[i]
expected := tt.expectedRegistrations[i]

require.EqualValues(t, expected, registration.Service.ID)
require.EqualValues(t, "gateway", registration.Service.Service)
assert.EqualValues(t, expected, registration.Service.ID)
assert.EqualValues(t, "gateway", registration.Service.Service)
}

require.EqualValues(t, tt.expectedDeregistrations, actual.Consul.Deregistrations)
assert.EqualValues(t, tt.expectedDeregistrations, actual.Consul.Deregistrations)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion control-plane/api-gateway/common/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (t ResourceTranslator) NormalizedResourceReference(kind, namespace string,
}

func (t ResourceTranslator) Namespace(namespace string) string {
return namespaces.ConsulNamespace(namespace, t.EnableK8sMirroring, t.ConsulDestNamespace, t.EnableK8sMirroring, t.MirroringPrefix)
return namespaces.ConsulNamespace(namespace, t.EnableConsulNamespaces, t.ConsulDestNamespace, t.EnableK8sMirroring, t.MirroringPrefix)
}

// ToAPIGateway translates a kuberenetes API gateway into a Consul APIGateway Config Entry.
Expand Down
73 changes: 73 additions & 0 deletions control-plane/api-gateway/common/translation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -42,6 +44,77 @@ func (v fakeReferenceValidator) TCPRouteCanReferenceBackend(tcpRoute gwv1alpha2.
return true
}

func TestTranslator_Namespace(t *testing.T) {
testCases := []struct {
EnableConsulNamespaces bool
ConsulDestNamespace string
EnableK8sMirroring bool
MirroringPrefix string
Input, ExpectedOutput string
}{
{
EnableConsulNamespaces: false,
ConsulDestNamespace: "default",
EnableK8sMirroring: false,
MirroringPrefix: "",
Input: "namespace-1",
ExpectedOutput: "",
},
{
EnableConsulNamespaces: false,
ConsulDestNamespace: "default",
EnableK8sMirroring: true,
MirroringPrefix: "",
Input: "namespace-1",
ExpectedOutput: "",
},
{
EnableConsulNamespaces: false,
ConsulDestNamespace: "default",
EnableK8sMirroring: true,
MirroringPrefix: "pre-",
Input: "namespace-1",
ExpectedOutput: "",
},
{
EnableConsulNamespaces: true,
ConsulDestNamespace: "default",
EnableK8sMirroring: false,
MirroringPrefix: "",
Input: "namespace-1",
ExpectedOutput: "default",
},
{
EnableConsulNamespaces: true,
ConsulDestNamespace: "default",
EnableK8sMirroring: true,
MirroringPrefix: "",
Input: "namespace-1",
ExpectedOutput: "namespace-1",
},
{
EnableConsulNamespaces: true,
ConsulDestNamespace: "default",
EnableK8sMirroring: true,
MirroringPrefix: "pre-",
Input: "namespace-1",
ExpectedOutput: "pre-namespace-1",
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("%s_%d", t.Name(), i), func(t *testing.T) {
translator := ResourceTranslator{
EnableConsulNamespaces: tc.EnableConsulNamespaces,
ConsulDestNamespace: tc.ConsulDestNamespace,
EnableK8sMirroring: tc.EnableK8sMirroring,
MirroringPrefix: tc.MirroringPrefix,
}
assert.Equal(t, tc.ExpectedOutput, translator.Namespace(tc.Input))
})
}
}

func TestTranslator_ToAPIGateway(t *testing.T) {
t.Parallel()
k8sObjectName := "my-k8s-gw"
Expand Down
4 changes: 2 additions & 2 deletions control-plane/api-gateway/controllers/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (r *GatewayController) Reconcile(ctx context.Context, req ctrl.Request) (ct
r.gatewayCache.RemoveSubscription(nonNormalizedConsulKey)
// make sure we have deregister all services even if they haven't
// hit cache yet
if err := r.deregisterAllServices(ctx, consulKey); err != nil {
if err := r.deregisterAllServices(ctx, nonNormalizedConsulKey); err != nil {
log.Error(err, "error deregistering services")
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -291,7 +291,7 @@ func (r *GatewayController) deregisterAllServices(ctx context.Context, consulKey
if err := r.cache.Deregister(ctx, api.CatalogDeregistration{
Node: service.Node,
ServiceID: service.ServiceID,
Namespace: service.Namespace,
Namespace: r.Translator.Namespace(service.Namespace),
}); err != nil {
return err
}
Expand Down