Skip to content

Commit

Permalink
fix(konnect): fix controlPlaneKonnectNamespacedRefAsSlice not enqueue…
Browse files Browse the repository at this point in the history
…ing when konnectNamespacedRef has empty namespace (#688)
  • Loading branch information
pmalek authored Oct 3, 2024
1 parent 7c36159 commit acb13d3
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 2 deletions.
7 changes: 5 additions & 2 deletions controller/konnect/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ func controlPlaneKonnectNamespacedRefAsSlice[
}

konnectRef := cpRef.KonnectNamespacedRef
if konnectRef == nil {
// NOTE: cross namespace refs are not supported yet.
// TODO: https://github.com/Kong/kubernetes-configuration/issues/36
// Specifying the same namespace is optional and is supported.
if konnectRef.Namespace != "" && konnectRef.Namespace != ent.GetNamespace() {
return nil
}

return []string{konnectRef.Namespace + "/" + konnectRef.Name}
return []string{ent.GetNamespace() + "/" + konnectRef.Name}
}
169 changes: 169 additions & 0 deletions controller/konnect/index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package konnect

import (
"testing"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kong/gateway-operator/controller/konnect/constraints"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
)

func TestControlPlaneKonnectNamespacedRefAsSlice(t *testing.T) {
t.Run("KongService", func(t *testing.T) {
tests := []struct {
name string
ent *configurationv1alpha1.KongService
expected []string
}{
{
name: "not specifying namespace is supported",
ent: &configurationv1alpha1.KongService{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "obj1",
},
Spec: configurationv1alpha1.KongServiceSpec{
ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{
Name: "cp-1",
},
},
},
},
expected: []string{"default/cp-1"},
},
{
name: "specifying the same namespace is supported",
ent: &configurationv1alpha1.KongService{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "obj1",
},
Spec: configurationv1alpha1.KongServiceSpec{
ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{
Name: "cp-1",
Namespace: "default",
},
},
},
},
expected: []string{"default/cp-1"},
},
{
name: "cross namespace references not supported",
ent: &configurationv1alpha1.KongService{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "obj1",
},
Spec: configurationv1alpha1.KongServiceSpec{
ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{
Name: "cp-1",
Namespace: "different",
},
},
},
},
expected: nil,
},
}

testControlPlaneKonnectNamespacedRefAsSlice(t, tests)
})

t.Run("KongRoute", func(t *testing.T) {
tests := []struct {
name string
ent *configurationv1alpha1.KongRoute
expected []string
}{
{
name: "not specifying namespace is supported",
ent: &configurationv1alpha1.KongRoute{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "obj1",
},
Spec: configurationv1alpha1.KongRouteSpec{
ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{
Name: "cp-1",
},
},
},
},
expected: []string{"default/cp-1"},
},
{
name: "specifying the same namespace is supported",
ent: &configurationv1alpha1.KongRoute{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "obj1",
},
Spec: configurationv1alpha1.KongRouteSpec{
ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{
Name: "cp-1",
Namespace: "default",
},
},
},
},
expected: []string{"default/cp-1"},
},
{
name: "cross namespace references not supported",
ent: &configurationv1alpha1.KongRoute{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "obj1",
},
Spec: configurationv1alpha1.KongRouteSpec{
ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{
Name: "cp-1",
Namespace: "different",
},
},
},
},
expected: nil,
},
}

testControlPlaneKonnectNamespacedRefAsSlice(t, tests)
})
}

func testControlPlaneKonnectNamespacedRefAsSlice[
T constraints.SupportedKonnectEntityType,
TEnt constraints.EntityType[T],
](
t *testing.T,
tests []struct {
name string
ent TEnt
expected []string
},
) {
t.Helper()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := controlPlaneKonnectNamespacedRefAsSlice(tt.ent)
assert.Equal(t, tt.expected, result)
})
}
}

0 comments on commit acb13d3

Please sign in to comment.