diff --git a/internal/ir/infra.go b/internal/ir/infra.go index 7d1c1396bb..81fbbcbdb9 100644 --- a/internal/ir/infra.go +++ b/internal/ir/infra.go @@ -48,6 +48,7 @@ type ProxyListener struct { } // ListenerPort defines a network port of a listener. +// +k8s:deepcopy-gen=true type ListenerPort struct { // Name is the name of the listener port. Name string diff --git a/internal/ir/zz_generated.deepcopy.go b/internal/ir/zz_generated.deepcopy.go index f37cd803ca..636ae75813 100644 --- a/internal/ir/zz_generated.deepcopy.go +++ b/internal/ir/zz_generated.deepcopy.go @@ -118,6 +118,21 @@ func (in *Infra) DeepCopy() *Infra { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ListenerPort) DeepCopyInto(out *ListenerPort) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ListenerPort. +func (in *ListenerPort) DeepCopy() *ListenerPort { + if in == nil { + return nil + } + out := new(ListenerPort) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ProxyInfra) DeepCopyInto(out *ProxyInfra) { *out = *in diff --git a/internal/message/types.go b/internal/message/types.go index 1bd455cd1d..27148205fb 100644 --- a/internal/message/types.go +++ b/internal/message/types.go @@ -22,7 +22,11 @@ type ProviderResources struct { } func (p *ProviderResources) GetGatewayClasses() []*gwapiv1b1.GatewayClass { - res := make([]*gwapiv1b1.GatewayClass, p.GatewayClasses.Len()) + if p.GatewayClasses.Len() == 0 { + return nil + } + + res := make([]*gwapiv1b1.GatewayClass, 0, p.GatewayClasses.Len()) for _, v := range p.GatewayClasses.LoadAll() { res = append(res, v) } @@ -30,7 +34,10 @@ func (p *ProviderResources) GetGatewayClasses() []*gwapiv1b1.GatewayClass { } func (p *ProviderResources) GetGateways() []*gwapiv1b1.Gateway { - res := make([]*gwapiv1b1.Gateway, p.Gateways.Len()) + if p.Gateways.Len() == 0 { + return nil + } + res := make([]*gwapiv1b1.Gateway, 0, p.Gateways.Len()) for _, v := range p.Gateways.LoadAll() { res = append(res, v) } @@ -38,7 +45,10 @@ func (p *ProviderResources) GetGateways() []*gwapiv1b1.Gateway { } func (p *ProviderResources) GetHTTPRoutes() []*gwapiv1b1.HTTPRoute { - res := make([]*gwapiv1b1.HTTPRoute, p.HTTPRoutes.Len()) + if p.HTTPRoutes.Len() == 0 { + return nil + } + res := make([]*gwapiv1b1.HTTPRoute, 0, p.HTTPRoutes.Len()) for _, v := range p.HTTPRoutes.LoadAll() { res = append(res, v) } diff --git a/internal/message/types_test.go b/internal/message/types_test.go new file mode 100644 index 0000000000..386dc4719f --- /dev/null +++ b/internal/message/types_test.go @@ -0,0 +1,127 @@ +package message + +import ( + "testing" + + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + gwapiv1b1 "sigs.k8s.io/gateway-api/apis/v1beta1" + + "github.com/envoyproxy/gateway/internal/ir" +) + +func TestProviderResources(t *testing.T) { + resources := new(ProviderResources) + gc1 := &gwapiv1b1.GatewayClass{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-gc1", + }, + } + gw1 := &gwapiv1b1.Gateway{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test1", + Namespace: "test", + }, + } + r1 := &gwapiv1b1.HTTPRoute{ + ObjectMeta: metav1.ObjectMeta{ + Name: "route1", + Namespace: "test", + }, + } + + // Check init state + assert.Nil(t, resources.GetGatewayClasses()) + assert.Nil(t, resources.GetGateways()) + assert.Nil(t, resources.GetHTTPRoutes()) + + // Add resources + resources.GatewayClasses.Store("test-gc1", gc1) + + gw1Key := types.NamespacedName{ + Namespace: gw1.GetNamespace(), + Name: gw1.GetName(), + } + resources.Gateways.Store(gw1Key, gw1) + + r1Key := types.NamespacedName{ + Namespace: r1.GetNamespace(), + Name: r1.GetName(), + } + resources.HTTPRoutes.Store(r1Key, r1) + + // Test + gcs := resources.GetGatewayClasses() + assert.Equal(t, len(gcs), 1) + + gws := resources.GetGateways() + assert.Equal(t, len(gws), 1) + + hrs := resources.GetHTTPRoutes() + assert.Equal(t, len(hrs), 1) + + // Add more resources + gc2 := &gwapiv1b1.GatewayClass{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-gc2", + }, + } + gw2 := &gwapiv1b1.Gateway{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test2", + Namespace: "test", + }, + } + r2 := &gwapiv1b1.HTTPRoute{ + ObjectMeta: metav1.ObjectMeta{ + Name: "route2", + Namespace: "test", + }, + } + + resources.GatewayClasses.Store("test-gc2", gc2) + gw2Key := types.NamespacedName{ + Namespace: gw2.GetNamespace(), + Name: gw2.GetName(), + } + resources.Gateways.Store(gw2Key, gw2) + + r2Key := types.NamespacedName{ + Namespace: r2.GetNamespace(), + Name: r2.GetName(), + } + resources.HTTPRoutes.Store(r2Key, r2) + + // Test contents + + gcs = resources.GetGatewayClasses() + assert.ElementsMatch(t, gcs, []*gwapiv1b1.GatewayClass{gc1, gc2}) + + gws = resources.GetGateways() + assert.ElementsMatch(t, gws, []*gwapiv1b1.Gateway{gw1, gw2}) + + hrs = resources.GetHTTPRoutes() + assert.ElementsMatch(t, hrs, []*gwapiv1b1.HTTPRoute{r1, r2}) +} + +func TestXdsIR(t *testing.T) { + msg := new(XdsIR) + in := &ir.Xds{ + HTTP: []*ir.HTTPListener{{Name: "test"}}, + } + msg.Store("xds-ir", in) + out := msg.Get() + assert.Equal(t, out, in) +} + +func TestInfraIR(t *testing.T) { + msg := new(InfraIR) + in := &ir.Infra{ + Proxy: &ir.ProxyInfra{Name: "test"}, + } + msg.Store("infra-ir", in) + out := msg.Get() + assert.Equal(t, out, in) + +}