Skip to content
Merged
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
9 changes: 6 additions & 3 deletions source/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,17 @@ func (src *gatewayRouteSource) Endpoints(ctx context.Context) ([]*endpoint.Endpo
}

// Create endpoints from hostnames and targets.
var routeEndpoints []*endpoint.Endpoint
resource := fmt.Sprintf("%s/%s/%s", kind, meta.Namespace, meta.Name)
providerSpecific, setIdentifier := getProviderSpecificAnnotations(annots)
ttl := getTTLFromAnnotations(annots, resource)
for host, targets := range hostTargets {
endpoints = append(endpoints, endpointsForHostname(host, targets, ttl, providerSpecific, setIdentifier, resource)...)
routeEndpoints = append(routeEndpoints, endpointsForHostname(host, targets, ttl, providerSpecific, setIdentifier, resource)...)
}
setDualstackLabel(rt, endpoints)
log.Debugf("Endpoints generated from %s %s/%s: %v", src.rtKind, meta.Namespace, meta.Name, endpoints)
setDualstackLabel(rt, routeEndpoints)
log.Debugf("Endpoints generated from %s %s/%s: %v", src.rtKind, meta.Namespace, meta.Name, routeEndpoints)

endpoints = append(endpoints, routeEndpoints...)
}
return endpoints, nil
}
Expand Down
79 changes: 70 additions & 9 deletions source/gateway_httproute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ limitations under the License.
package source

import (
"bytes"
"context"
"testing"

log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
kubefake "k8s.io/client-go/kubernetes/fake"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/internal/testutils"
v1 "sigs.k8s.io/gateway-api/apis/v1"
v1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
gatewayfake "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/fake"
Expand Down Expand Up @@ -107,8 +110,6 @@ func newTestEndpointWithTTL(dnsName, recordType string, ttl int64, targets ...st
}

func TestGatewayHTTPRouteSourceEndpoints(t *testing.T) {
t.Parallel()

fromAll := v1.NamespacesFromAll
fromSame := v1.NamespacesFromSame
fromSelector := v1.NamespacesFromSelector
Expand All @@ -133,12 +134,13 @@ func TestGatewayHTTPRouteSourceEndpoints(t *testing.T) {
hostnames := func(names ...v1.Hostname) []v1.Hostname { return names }

tests := []struct {
title string
config Config
namespaces []*corev1.Namespace
gateways []*v1beta1.Gateway
routes []*v1beta1.HTTPRoute
endpoints []*endpoint.Endpoint
title string
config Config
namespaces []*corev1.Namespace
gateways []*v1beta1.Gateway
routes []*v1beta1.HTTPRoute
endpoints []*endpoint.Endpoint
logExpectations []string
}{
{
title: "GatewayNamespace",
Expand Down Expand Up @@ -1119,11 +1121,62 @@ func TestGatewayHTTPRouteSourceEndpoints(t *testing.T) {
newTestEndpoint("test.example.internal", "A", "4.3.2.1", "2.3.4.5"),
},
},
{
title: "MultipleGatewaysMultipleRoutes",
config: Config{},
namespaces: namespaces("default"),
gateways: []*v1beta1.Gateway{
{
ObjectMeta: objectMeta("default", "one"),
Spec: v1.GatewaySpec{
Listeners: []v1.Listener{{Protocol: v1.HTTPProtocolType}},
},
Status: gatewayStatus("1.2.3.4"),
},
{
ObjectMeta: objectMeta("default", "two"),
Spec: v1.GatewaySpec{
Listeners: []v1.Listener{{Protocol: v1.HTTPProtocolType}},
},
Status: gatewayStatus("2.3.4.5"),
},
},
routes: []*v1beta1.HTTPRoute{
{
ObjectMeta: objectMeta("default", "one"),
Spec: v1.HTTPRouteSpec{
Hostnames: hostnames("test.one.internal"),
},
Status: httpRouteStatus(
gwParentRef("default", "one"),
),
},
{
ObjectMeta: objectMeta("default", "two"),
Spec: v1.HTTPRouteSpec{
Hostnames: hostnames("test.two.internal"),
},
Status: httpRouteStatus(
gwParentRef("default", "two"),
),
},
},
endpoints: []*endpoint.Endpoint{
newTestEndpoint("test.one.internal", "A", "1.2.3.4"),
newTestEndpoint("test.two.internal", "A", "2.3.4.5"),
},
logExpectations: []string{
"level=debug msg=\"Endpoints generated from HTTPRoute default/one: [test.one.internal 0 IN A 1.2.3.4 []]\"",
"level=debug msg=\"Endpoints generated from HTTPRoute default/two: [test.two.internal 0 IN A 2.3.4.5 []]\"",
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.title, func(t *testing.T) {
t.Parallel()
if len(tt.logExpectations) == 0 {
t.Parallel()
}

ctx := context.Background()
gwClient := gatewayfake.NewSimpleClientset()
Expand All @@ -1149,9 +1202,17 @@ func TestGatewayHTTPRouteSourceEndpoints(t *testing.T) {
src, err := NewGatewayHTTPRouteSource(clients, &tt.config)
require.NoError(t, err, "failed to create Gateway HTTPRoute Source")

var b *bytes.Buffer
if len(tt.logExpectations) > 0 {
b = testutils.LogsToBuffer(log.DebugLevel, t)
}
endpoints, err := src.Endpoints(ctx)
require.NoError(t, err, "failed to get Endpoints")
validateEndpoints(t, endpoints, tt.endpoints)

for _, msg := range tt.logExpectations {
require.Contains(t, b.String(), msg)
}
})
}
}
Expand Down
Loading