external-dns/source/contour_httpproxy_test.go
Aleksei Sviridkin 5a55b09f48
feat(annotations): add custom annotation prefix support for split horizon DNS (#5889)
* feat(annotations): add custom annotation prefix support for split horizon DNS

Add --annotation-prefix flag to allow customizing the annotation prefix
used by external-dns. This enables split horizon DNS scenarios where
multiple instances process different sets of annotations from the same
Kubernetes resources.

Changes:
- Add AnnotationPrefix field to Config with validation
- Convert annotation constants to variables that can be reconfigured
- Add SetAnnotationPrefix() function to rebuild annotation keys
- Integrate annotation prefix setting in controller startup
- Update Helm chart with annotationPrefix value
- Add comprehensive split horizon DNS documentation
- Update FAQ with annotation prefix examples

This maintains full backward compatibility - the default prefix remains
"external-dns.alpha.kubernetes.io/".

Co-Authored-By: Claude <noreply@anthropic.com>

* docs(advanced): fix markdown formatting in split-horizon guide

Add blank lines before code blocks to improve markdown rendering
and comply with markdownlint rules.

Co-Authored-By: Claude <noreply@anthropic.com>

* docs(advanced): fix markdown formatting in split-horizon guide

Co-Authored-By: Claude <noreply@anthropic.com>

* docs(charts): regenerate Helm chart documentation

Co-Authored-By: Claude <noreply@anthropic.com>

* test: add AnnotationPrefix field to test configs

Add missing AnnotationPrefix field to minimalConfig and overriddenConfig
test configurations to match the new default value set in NewConfig().

Co-Authored-By: Claude <noreply@anthropic.com>

* test(charts): update error pattern in json-schema test

Update expected error message pattern to match current Helm validation
output format.

Co-Authored-By: Claude <noreply@anthropic.com>

* refactor(annotations): remove init() for explicit initialization

- Remove init() function from annotations package
- Add explicit SetAnnotationPrefix() call in controller/execute.go
- Remove annotation key aliases from source/source.go
- Replace all alias usages with annotations.* references (348 changes in 28 files)
- Add TestMain to existing test files (service_test.go, cloudflare_test.go)

This change makes annotation initialization explicit and predictable,
avoiding hidden global state initialization at import time.

Co-Authored-By: Claude <noreply@anthropic.com>

* docs: update changelog and mkdocs to include annotationPrefix and split horizon DNS

Signed-off-by: Aleksei Sviridkin <f@lex.la>

* docs(split-horizon): fix linting

Signed-off-by: Aleksei Sviridkin <f@lex.la>

* refactor(annotations): replace hardcoded annotation prefix with constant

Replace all hardcoded "external-dns.alpha.kubernetes.io/" strings
with annotations.DefaultAnnotationPrefix constant to establish
a single source of truth.

Changes:
- Add DefaultAnnotationPrefix constant in source/annotations/annotations.go
- Replace hardcoded string in controller/execute.go with constant reference
- Replace hardcoded strings in pkg/apis/externaldns/types.go (2 occurrences)
- Add helm unit tests for annotationPrefix value

This eliminates string duplication and makes future changes easier.

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Signed-off-by: Aleksei Sviridkin <f@lex.la>
Co-authored-by: Claude <noreply@anthropic.com>
2025-11-08 03:56:52 -08:00

1156 lines
29 KiB
Go

/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package source
import (
"context"
"errors"
"testing"
fakeDynamic "k8s.io/client-go/dynamic/fake"
projectcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/source/annotations"
)
// This is a compile-time validation that httpProxySource is a Source.
var _ Source = &httpProxySource{}
type HTTPProxySuite struct {
suite.Suite
source Source
httpProxy *projectcontour.HTTPProxy
}
func newDynamicKubernetesClient() (*fakeDynamic.FakeDynamicClient, *runtime.Scheme) {
s := runtime.NewScheme()
_ = projectcontour.AddToScheme(s)
return fakeDynamic.NewSimpleDynamicClient(s), s
}
type fakeLoadBalancerService struct {
ips []string
hostnames []string
namespace string
name string
}
func (ig fakeLoadBalancerService) Service() *v1.Service {
svc := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: ig.namespace,
Name: ig.name,
},
Status: v1.ServiceStatus{
LoadBalancer: v1.LoadBalancerStatus{
Ingress: []v1.LoadBalancerIngress{},
},
},
}
for _, ip := range ig.ips {
svc.Status.LoadBalancer.Ingress = append(svc.Status.LoadBalancer.Ingress, v1.LoadBalancerIngress{
IP: ip,
})
}
for _, hostname := range ig.hostnames {
svc.Status.LoadBalancer.Ingress = append(svc.Status.LoadBalancer.Ingress, v1.LoadBalancerIngress{
Hostname: hostname,
})
}
return svc
}
func (suite *HTTPProxySuite) SetupTest() {
fakeDynamicClient, s := newDynamicKubernetesClient()
var err error
suite.source, err = NewContourHTTPProxySource(
context.TODO(),
fakeDynamicClient,
"default",
"",
"{{.Name}}",
false,
false,
)
suite.NoError(err, "should initialize httpproxy source")
suite.httpProxy = (fakeHTTPProxy{
name: "foo-httpproxy-with-targets",
namespace: "default",
host: "example.com",
}).HTTPProxy()
// Convert to unstructured
unstructuredHTTPProxy, err := convertHTTPProxyToUnstructured(suite.httpProxy, s)
if err != nil {
suite.Error(err)
}
_, err = fakeDynamicClient.Resource(projectcontour.HTTPProxyGVR).Namespace(suite.httpProxy.Namespace).Create(context.Background(), unstructuredHTTPProxy, metav1.CreateOptions{})
suite.NoError(err, "should succeed")
}
func (suite *HTTPProxySuite) TestResourceLabelIsSet() {
endpoints, _ := suite.source.Endpoints(context.Background())
for _, ep := range endpoints {
suite.Equal("httpproxy/default/foo-httpproxy-with-targets", ep.Labels[endpoint.ResourceLabelKey], "should set correct resource label")
}
}
func convertHTTPProxyToUnstructured(hp *projectcontour.HTTPProxy, s *runtime.Scheme) (*unstructured.Unstructured, error) {
unstructuredHTTPProxy := &unstructured.Unstructured{}
if err := s.Convert(hp, unstructuredHTTPProxy, context.Background()); err != nil {
return nil, err
}
return unstructuredHTTPProxy, nil
}
func TestHTTPProxy(t *testing.T) {
t.Parallel()
suite.Run(t, new(HTTPProxySuite))
t.Run("endpointsFromHTTPProxy", testEndpointsFromHTTPProxy)
t.Run("Endpoints", testHTTPProxyEndpoints)
}
func TestNewContourHTTPProxySource(t *testing.T) {
t.Parallel()
for _, ti := range []struct {
title string
annotationFilter string
fqdnTemplate string
combineFQDNAndAnnotation bool
expectError bool
}{
{
title: "invalid template",
expectError: true,
fqdnTemplate: "{{.Name",
},
{
title: "valid empty template",
expectError: false,
},
{
title: "valid template",
expectError: false,
fqdnTemplate: "{{.Name}}-{{.Namespace}}.ext-dns.test.com",
},
{
title: "valid template",
expectError: false,
fqdnTemplate: "{{.Name}}-{{.Namespace}}.ext-dns.test.com, {{.Name}}-{{.Namespace}}.ext-dna.test.com",
},
{
title: "valid template",
expectError: false,
fqdnTemplate: "{{.Name}}-{{.Namespace}}.ext-dns.test.com, {{.Name}}-{{.Namespace}}.ext-dna.test.com",
combineFQDNAndAnnotation: true,
},
{
title: "non-empty annotation filter label",
expectError: false,
annotationFilter: "contour.heptio.com/ingress.class=contour",
},
} {
t.Run(ti.title, func(t *testing.T) {
t.Parallel()
fakeDynamicClient, _ := newDynamicKubernetesClient()
_, err := NewContourHTTPProxySource(
context.TODO(),
fakeDynamicClient,
"",
ti.annotationFilter,
ti.fqdnTemplate,
ti.combineFQDNAndAnnotation,
false,
)
if ti.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
func testEndpointsFromHTTPProxy(t *testing.T) {
t.Parallel()
for _, ti := range []struct {
title string
httpProxy fakeHTTPProxy
expected []*endpoint.Endpoint
}{
{
title: "one rule.host one lb.hostname",
httpProxy: fakeHTTPProxy{
host: "foo.bar", // Kubernetes requires removal of trailing dot
loadBalancer: fakeLoadBalancerService{
hostnames: []string{"lb.com"}, // Kubernetes omits the trailing dot
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "foo.bar",
RecordType: endpoint.RecordTypeCNAME,
Targets: endpoint.Targets{"lb.com"},
},
},
},
{
title: "one rule.host one lb.IP",
httpProxy: fakeHTTPProxy{
host: "foo.bar",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "foo.bar",
RecordType: endpoint.RecordTypeA,
Targets: endpoint.Targets{"8.8.8.8"},
},
},
},
{
title: "one rule.host two lb.IP and two lb.Hostname",
httpProxy: fakeHTTPProxy{
host: "foo.bar",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8", "127.0.0.1"},
hostnames: []string{"elb.com", "alb.com"},
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "foo.bar",
RecordType: endpoint.RecordTypeA,
Targets: endpoint.Targets{"8.8.8.8", "127.0.0.1"},
},
{
DNSName: "foo.bar",
RecordType: endpoint.RecordTypeCNAME,
Targets: endpoint.Targets{"elb.com", "alb.com"},
},
},
},
{
title: "no rule.host",
httpProxy: fakeHTTPProxy{},
expected: []*endpoint.Endpoint{},
},
{
title: "no targets",
httpProxy: fakeHTTPProxy{},
expected: []*endpoint.Endpoint{},
},
{
title: "delegate httpproxy",
httpProxy: fakeHTTPProxy{
delegate: true,
},
expected: []*endpoint.Endpoint{},
},
} {
t.Run(ti.title, func(t *testing.T) {
t.Parallel()
if source, err := newTestHTTPProxySource(); err != nil {
require.NoError(t, err)
} else if endpoints, err := source.endpointsFromHTTPProxy(ti.httpProxy.HTTPProxy()); err != nil {
require.NoError(t, err)
} else {
validateEndpoints(t, endpoints, ti.expected)
}
})
}
}
func testHTTPProxyEndpoints(t *testing.T) {
t.Parallel()
namespace := "testing"
for _, ti := range []struct {
title string
targetNamespace string
annotationFilter string
loadBalancer fakeLoadBalancerService
httpProxyItems []fakeHTTPProxy
expected []*endpoint.Endpoint
expectError bool
fqdnTemplate string
combineFQDNAndAnnotation bool
ignoreHostnameAnnotation bool
}{
{
title: "no httpproxy",
targetNamespace: "",
},
{
title: "two simple httpproxys",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
hostnames: []string{"lb.com"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
host: "example.org",
},
{
name: "fake2",
namespace: namespace,
host: "new.org",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "example.org",
RecordType: endpoint.RecordTypeA,
Targets: endpoint.Targets{"8.8.8.8"},
},
{
DNSName: "example.org",
RecordType: endpoint.RecordTypeCNAME,
Targets: endpoint.Targets{"lb.com"},
},
{
DNSName: "new.org",
RecordType: endpoint.RecordTypeA,
Targets: endpoint.Targets{"8.8.8.8"},
},
{
DNSName: "new.org",
RecordType: endpoint.RecordTypeCNAME,
Targets: endpoint.Targets{"lb.com"},
},
},
},
{
title: "two simple httpproxys on different namespaces",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
hostnames: []string{"lb.com"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: "testing1",
host: "example.org",
},
{
name: "fake2",
namespace: "testing2",
host: "new.org",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "example.org",
RecordType: endpoint.RecordTypeA,
Targets: endpoint.Targets{"8.8.8.8"},
},
{
DNSName: "example.org",
RecordType: endpoint.RecordTypeCNAME,
Targets: endpoint.Targets{"lb.com"},
},
{
DNSName: "new.org",
RecordType: endpoint.RecordTypeA,
Targets: endpoint.Targets{"8.8.8.8"},
},
{
DNSName: "new.org",
RecordType: endpoint.RecordTypeCNAME,
Targets: endpoint.Targets{"lb.com"},
},
},
},
{
title: "two simple httpproxys on different namespaces and a target namespace",
targetNamespace: "testing1",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
hostnames: []string{"lb.com"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: "testing1",
host: "example.org",
},
{
name: "fake2",
namespace: "testing2",
host: "new.org",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "example.org",
RecordType: endpoint.RecordTypeA,
Targets: endpoint.Targets{"8.8.8.8"},
},
{
DNSName: "example.org",
RecordType: endpoint.RecordTypeCNAME,
Targets: endpoint.Targets{"lb.com"},
},
},
},
{
title: "valid matching annotation filter expression",
targetNamespace: "",
annotationFilter: "contour.heptio.com/ingress.class in (alb, contour)",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
"contour.heptio.com/ingress.class": "contour",
},
host: "example.org",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "example.org",
RecordType: endpoint.RecordTypeA,
Targets: endpoint.Targets{"8.8.8.8"},
},
},
},
{
title: "valid non-matching annotation filter expression",
targetNamespace: "",
annotationFilter: "contour.heptio.com/ingress.class in (alb, contour)",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
"contour.heptio.com/ingress.class": "tectonic",
},
host: "example.org",
},
},
expected: []*endpoint.Endpoint{},
},
{
title: "invalid annotation filter expression",
targetNamespace: "",
annotationFilter: "contour.heptio.com/ingress.name in (a b)",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
"contour.heptio.com/ingress.class": "alb",
},
host: "example.org",
},
},
expected: []*endpoint.Endpoint{},
expectError: true,
},
{
title: "valid matching annotation filter label",
targetNamespace: "",
annotationFilter: "contour.heptio.com/ingress.class=contour",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
"contour.heptio.com/ingress.class": "contour",
},
host: "example.org",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "example.org",
RecordType: endpoint.RecordTypeA,
Targets: endpoint.Targets{"8.8.8.8"},
},
},
},
{
title: "valid non-matching annotation filter label",
targetNamespace: "",
annotationFilter: "contour.heptio.com/ingress.class=contour",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
"contour.heptio.com/ingress.class": "alb",
},
host: "example.org",
},
},
expected: []*endpoint.Endpoint{},
},
{
title: "our controller type is dns-controller",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
annotations.ControllerKey: annotations.ControllerValue,
},
host: "example.org",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "example.org",
RecordType: endpoint.RecordTypeA,
Targets: endpoint.Targets{"8.8.8.8"},
},
},
},
{
title: "different controller types are ignored",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
annotations.ControllerKey: "some-other-tool",
},
host: "example.org",
},
},
expected: []*endpoint.Endpoint{},
},
{
title: "template for httpproxy if host is missing",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
hostnames: []string{"elb.com"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
annotations.ControllerKey: annotations.ControllerValue,
},
host: "",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "fake1.ext-dns.test.com",
RecordType: endpoint.RecordTypeA,
Targets: endpoint.Targets{"8.8.8.8"},
},
{
DNSName: "fake1.ext-dns.test.com",
RecordType: endpoint.RecordTypeCNAME,
Targets: endpoint.Targets{"elb.com"},
},
},
fqdnTemplate: "{{.Name}}.ext-dns.test.com",
},
{
title: "another controller annotation skipped even with template",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
annotations.ControllerKey: "other-controller",
},
host: "",
},
},
expected: []*endpoint.Endpoint{},
fqdnTemplate: "{{.Name}}.ext-dns.test.com",
},
{
title: "multiple FQDN template hostnames",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{},
host: "",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "fake1.ext-dns.test.com",
Targets: endpoint.Targets{"8.8.8.8"},
RecordType: endpoint.RecordTypeA,
},
{
DNSName: "fake1.ext-dna.test.com",
Targets: endpoint.Targets{"8.8.8.8"},
RecordType: endpoint.RecordTypeA,
},
},
fqdnTemplate: "{{.Name}}.ext-dns.test.com, {{.Name}}.ext-dna.test.com",
},
{
title: "multiple FQDN template hostnames",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{},
host: "",
},
{
name: "fake2",
namespace: namespace,
annotations: map[string]string{
annotations.TargetKey: "httpproxy-target.com",
},
host: "example.org",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "fake1.ext-dns.test.com",
Targets: endpoint.Targets{"8.8.8.8"},
RecordType: endpoint.RecordTypeA,
},
{
DNSName: "fake1.ext-dna.test.com",
Targets: endpoint.Targets{"8.8.8.8"},
RecordType: endpoint.RecordTypeA,
},
{
DNSName: "example.org",
Targets: endpoint.Targets{"httpproxy-target.com"},
RecordType: endpoint.RecordTypeCNAME,
},
{
DNSName: "fake2.ext-dns.test.com",
Targets: endpoint.Targets{"httpproxy-target.com"},
RecordType: endpoint.RecordTypeCNAME,
},
{
DNSName: "fake2.ext-dna.test.com",
Targets: endpoint.Targets{"httpproxy-target.com"},
RecordType: endpoint.RecordTypeCNAME,
},
},
fqdnTemplate: "{{.Name}}.ext-dns.test.com, {{.Name}}.ext-dna.test.com",
combineFQDNAndAnnotation: true,
},
{
title: "httpproxy rules with annotation",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
annotations.TargetKey: "httpproxy-target.com",
},
host: "example.org",
},
{
name: "fake2",
namespace: namespace,
annotations: map[string]string{
annotations.TargetKey: "httpproxy-target.com",
},
host: "example2.org",
},
{
name: "fake3",
namespace: namespace,
annotations: map[string]string{
annotations.TargetKey: "1.2.3.4",
},
host: "example3.org",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "example.org",
Targets: endpoint.Targets{"httpproxy-target.com"},
RecordType: endpoint.RecordTypeCNAME,
},
{
DNSName: "example2.org",
Targets: endpoint.Targets{"httpproxy-target.com"},
RecordType: endpoint.RecordTypeCNAME,
},
{
DNSName: "example3.org",
Targets: endpoint.Targets{"1.2.3.4"},
RecordType: endpoint.RecordTypeA,
},
},
},
{
title: "httpproxy rules with hostname annotation",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{"1.2.3.4"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
annotations.HostnameKey: "dns-through-hostname.com",
},
host: "example.org",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "example.org",
Targets: endpoint.Targets{"1.2.3.4"},
RecordType: endpoint.RecordTypeA,
},
{
DNSName: "dns-through-hostname.com",
Targets: endpoint.Targets{"1.2.3.4"},
RecordType: endpoint.RecordTypeA,
},
},
},
{
title: "httpproxy rules with hostname annotation having multiple hostnames",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{"1.2.3.4"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
annotations.HostnameKey: "dns-through-hostname.com, another-dns-through-hostname.com",
},
host: "example.org",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "example.org",
Targets: endpoint.Targets{"1.2.3.4"},
RecordType: endpoint.RecordTypeA,
},
{
DNSName: "dns-through-hostname.com",
Targets: endpoint.Targets{"1.2.3.4"},
RecordType: endpoint.RecordTypeA,
},
{
DNSName: "another-dns-through-hostname.com",
Targets: endpoint.Targets{"1.2.3.4"},
RecordType: endpoint.RecordTypeA,
},
},
},
{
title: "httpproxy rules with hostname and target annotation",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
annotations.HostnameKey: "dns-through-hostname.com",
annotations.TargetKey: "httpproxy-target.com",
},
host: "example.org",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "example.org",
Targets: endpoint.Targets{"httpproxy-target.com"},
RecordType: endpoint.RecordTypeCNAME,
},
{
DNSName: "dns-through-hostname.com",
Targets: endpoint.Targets{"httpproxy-target.com"},
RecordType: endpoint.RecordTypeCNAME,
},
},
},
{
title: "httpproxy rules with annotation and custom TTL",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
annotations.TargetKey: "httpproxy-target.com",
annotations.TtlKey: "6",
},
host: "example.org",
},
{
name: "fake2",
namespace: namespace,
annotations: map[string]string{
annotations.TargetKey: "httpproxy-target.com",
annotations.TtlKey: "1",
},
host: "example2.org",
},
{
name: "fake3",
namespace: namespace,
annotations: map[string]string{
annotations.TargetKey: "httpproxy-target.com",
annotations.TtlKey: "10s",
},
host: "example3.org",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "example.org",
RecordType: endpoint.RecordTypeCNAME,
Targets: endpoint.Targets{"httpproxy-target.com"},
RecordTTL: endpoint.TTL(6),
},
{
DNSName: "example2.org",
RecordType: endpoint.RecordTypeCNAME,
Targets: endpoint.Targets{"httpproxy-target.com"},
RecordTTL: endpoint.TTL(1),
},
{
DNSName: "example3.org",
RecordType: endpoint.RecordTypeCNAME,
Targets: endpoint.Targets{"httpproxy-target.com"},
RecordTTL: endpoint.TTL(10),
},
},
},
{
title: "template for httpproxy with annotation",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{},
hostnames: []string{},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
annotations.TargetKey: "httpproxy-target.com",
},
host: "",
},
{
name: "fake2",
namespace: namespace,
annotations: map[string]string{
annotations.TargetKey: "httpproxy-target.com",
},
host: "",
},
{
name: "fake3",
namespace: namespace,
annotations: map[string]string{
annotations.TargetKey: "1.2.3.4",
},
host: "",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "fake1.ext-dns.test.com",
Targets: endpoint.Targets{"httpproxy-target.com"},
RecordType: endpoint.RecordTypeCNAME,
},
{
DNSName: "fake2.ext-dns.test.com",
Targets: endpoint.Targets{"httpproxy-target.com"},
RecordType: endpoint.RecordTypeCNAME,
},
{
DNSName: "fake3.ext-dns.test.com",
Targets: endpoint.Targets{"1.2.3.4"},
RecordType: endpoint.RecordTypeA,
},
},
fqdnTemplate: "{{.Name}}.ext-dns.test.com",
},
{
title: "httpproxy with empty annotation",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{},
hostnames: []string{},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
annotations.TargetKey: "",
},
host: "",
},
},
expected: []*endpoint.Endpoint{},
fqdnTemplate: "{{.Name}}.ext-dns.test.com",
},
{
title: "ignore hostname annotations",
targetNamespace: "",
loadBalancer: fakeLoadBalancerService{
ips: []string{"8.8.8.8"},
hostnames: []string{"lb.com"},
},
httpProxyItems: []fakeHTTPProxy{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
annotations.HostnameKey: "ignore.me",
},
host: "example.org",
},
{
name: "fake2",
namespace: namespace,
annotations: map[string]string{
annotations.HostnameKey: "ignore.me.too",
},
host: "new.org",
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "example.org",
RecordType: endpoint.RecordTypeA,
Targets: endpoint.Targets{"8.8.8.8"},
},
{
DNSName: "example.org",
RecordType: endpoint.RecordTypeCNAME,
Targets: endpoint.Targets{"lb.com"},
},
{
DNSName: "new.org",
RecordType: endpoint.RecordTypeA,
Targets: endpoint.Targets{"8.8.8.8"},
},
{
DNSName: "new.org",
RecordType: endpoint.RecordTypeCNAME,
Targets: endpoint.Targets{"lb.com"},
},
},
ignoreHostnameAnnotation: true,
},
} {
t.Run(ti.title, func(t *testing.T) {
t.Parallel()
httpProxies := make([]*projectcontour.HTTPProxy, 0)
for _, item := range ti.httpProxyItems {
item.loadBalancer = ti.loadBalancer
httpProxies = append(httpProxies, item.HTTPProxy())
}
fakeDynamicClient, scheme := newDynamicKubernetesClient()
for _, httpProxy := range httpProxies {
converted, err := convertHTTPProxyToUnstructured(httpProxy, scheme)
require.NoError(t, err)
_, err = fakeDynamicClient.Resource(projectcontour.HTTPProxyGVR).Namespace(httpProxy.Namespace).Create(context.Background(), converted, metav1.CreateOptions{})
require.NoError(t, err)
}
httpProxySource, err := NewContourHTTPProxySource(
context.TODO(),
fakeDynamicClient,
ti.targetNamespace,
ti.annotationFilter,
ti.fqdnTemplate,
ti.combineFQDNAndAnnotation,
ti.ignoreHostnameAnnotation,
)
require.NoError(t, err)
res, err := httpProxySource.Endpoints(context.Background())
if ti.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
validateEndpoints(t, res, ti.expected)
})
}
}
// httpproxy specific helper functions
func newTestHTTPProxySource() (*httpProxySource, error) {
fakeDynamicClient, _ := newDynamicKubernetesClient()
src, err := NewContourHTTPProxySource(
context.TODO(),
fakeDynamicClient,
"default",
"",
"{{.Name}}",
false,
false,
)
if err != nil {
return nil, err
}
irsrc, ok := src.(*httpProxySource)
if !ok {
return nil, errors.New("underlying source type was not httpproxy")
}
return irsrc, nil
}
type fakeHTTPProxy struct {
namespace string
name string
annotations map[string]string
host string
delegate bool
loadBalancer fakeLoadBalancerService
}
func (ir fakeHTTPProxy) HTTPProxy() *projectcontour.HTTPProxy {
var spec projectcontour.HTTPProxySpec
if ir.delegate {
spec = projectcontour.HTTPProxySpec{}
} else {
spec = projectcontour.HTTPProxySpec{
VirtualHost: &projectcontour.VirtualHost{
Fqdn: ir.host,
},
}
}
lb := v1.LoadBalancerStatus{
Ingress: []v1.LoadBalancerIngress{},
}
for _, ip := range ir.loadBalancer.ips {
lb.Ingress = append(lb.Ingress, v1.LoadBalancerIngress{
IP: ip,
})
}
for _, hostname := range ir.loadBalancer.hostnames {
lb.Ingress = append(lb.Ingress, v1.LoadBalancerIngress{
Hostname: hostname,
})
}
httpProxy := &projectcontour.HTTPProxy{
ObjectMeta: metav1.ObjectMeta{
Namespace: ir.namespace,
Name: ir.name,
Annotations: ir.annotations,
},
Spec: spec,
Status: projectcontour.HTTPProxyStatus{
LoadBalancer: lb,
},
}
return httpProxy
}