chore(ingress): added tests to cover specific behaviour (#6060)

Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com>
This commit is contained in:
Ivan Ka 2025-12-29 09:20:35 +00:00 committed by GitHub
parent de93816a91
commit 97352c7869
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -27,6 +27,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes/fake"
"sigs.k8s.io/external-dns/internal/testutils"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/source/annotations"
@ -1498,3 +1499,148 @@ func (ing fakeIngress) Ingress() *networkv1.Ingress {
}
return ingress
}
func TestIngressWithConfiguration(t *testing.T) {
for _, tt := range []struct {
title string
ingresses []*networkv1.Ingress
cfg *Config
expected []*endpoint.Endpoint
}{
{
title: "hostname and targets configured as annotations",
ingresses: []*networkv1.Ingress{
{
ObjectMeta: metav1.ObjectMeta{
Name: "my-ingress",
Namespace: "default",
Annotations: map[string]string{
annotations.HostnameKey: "bla.example.org",
annotations.TargetKey: "target.example.org",
},
},
Spec: networkv1.IngressSpec{
IngressClassName: testutils.ToPtr("nginx"),
Rules: []networkv1.IngressRule{
{Host: "app.example.com"},
},
},
Status: networkv1.IngressStatus{
LoadBalancer: networkv1.IngressLoadBalancerStatus{
Ingress: []networkv1.IngressLoadBalancerIngress{
{IP: "1.2.3.4"},
},
},
},
},
},
expected: []*endpoint.Endpoint{
{DNSName: "app.example.com", Targets: endpoint.Targets{"target.example.org"}, RecordType: endpoint.RecordTypeCNAME},
{DNSName: "bla.example.org", Targets: endpoint.Targets{"target.example.org"}, RecordType: endpoint.RecordTypeCNAME},
},
},
{
title: "ingress with spec.tls with wildcard domains and tls not ignored",
cfg: &Config{
IgnoreIngressTLSSpec: false,
},
ingresses: []*networkv1.Ingress{
{
ObjectMeta: metav1.ObjectMeta{
Name: "my-ingress",
Namespace: "default",
Annotations: map[string]string{},
},
Spec: networkv1.IngressSpec{
IngressClassName: testutils.ToPtr("alb"),
TLS: []networkv1.IngressTLS{
{
Hosts: []string{"*.example.com"},
},
},
Rules: []networkv1.IngressRule{
{Host: "abc.example.com"},
},
},
Status: networkv1.IngressStatus{
LoadBalancer: networkv1.IngressLoadBalancerStatus{
Ingress: []networkv1.IngressLoadBalancerIngress{
{IP: "1.2.3.4"},
},
},
},
},
},
expected: []*endpoint.Endpoint{
{DNSName: "abc.example.com", Targets: endpoint.Targets{"1.2.3.4"}, RecordType: endpoint.RecordTypeA},
{DNSName: "*.example.com", Targets: endpoint.Targets{"1.2.3.4"}, RecordType: endpoint.RecordTypeA},
},
},
{
title: "ingress with spec.tls with wildcard domains and tls is ignored",
cfg: &Config{
IgnoreIngressTLSSpec: true,
},
ingresses: []*networkv1.Ingress{
{
ObjectMeta: metav1.ObjectMeta{
Name: "my-ingress",
Namespace: "default",
},
Spec: networkv1.IngressSpec{
IngressClassName: testutils.ToPtr("alb"),
TLS: []networkv1.IngressTLS{
{
Hosts: []string{"*.example.com"},
},
},
Rules: []networkv1.IngressRule{
{Host: "abc.example.com"},
},
},
Status: networkv1.IngressStatus{
LoadBalancer: networkv1.IngressLoadBalancerStatus{
Ingress: []networkv1.IngressLoadBalancerIngress{
{IP: "1.2.3.4"},
},
},
},
},
},
expected: []*endpoint.Endpoint{
{DNSName: "abc.example.com", Targets: endpoint.Targets{"1.2.3.4"}, RecordType: endpoint.RecordTypeA},
},
},
} {
t.Run(tt.title, func(t *testing.T) {
kubeClient := fake.NewClientset()
for _, el := range tt.ingresses {
_, err := kubeClient.NetworkingV1().Ingresses(el.Namespace).Create(t.Context(), el, metav1.CreateOptions{})
require.NoError(t, err)
}
if tt.cfg == nil {
tt.cfg = &Config{}
}
src, err := NewIngressSource(
t.Context(),
kubeClient,
tt.cfg.Namespace,
tt.cfg.AnnotationFilter,
tt.cfg.FQDNTemplate,
tt.cfg.CombineFQDNAndAnnotation,
tt.cfg.IgnoreHostnameAnnotation,
tt.cfg.IgnoreIngressTLSSpec,
tt.cfg.IgnoreIngressRulesSpec,
labels.Everything(),
tt.cfg.IngressClassNames,
)
require.NoError(t, err)
endpoints, err := src.Endpoints(t.Context())
require.NoError(t, err)
validateEndpoints(t, endpoints, tt.expected)
})
}
}