@@ -27,6 +27,7 @@ import (
2727 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828 "k8s.io/apimachinery/pkg/labels"
2929 "k8s.io/client-go/kubernetes/fake"
30+ "sigs.k8s.io/external-dns/internal/testutils"
3031
3132 "sigs.k8s.io/external-dns/endpoint"
3233 "sigs.k8s.io/external-dns/source/annotations"
@@ -1498,3 +1499,148 @@ func (ing fakeIngress) Ingress() *networkv1.Ingress {
14981499 }
14991500 return ingress
15001501}
1502+
1503+ func TestIngressWithConfiguration (t * testing.T ) {
1504+ for _ , tt := range []struct {
1505+ title string
1506+ ingresses []* networkv1.Ingress
1507+ cfg * Config
1508+ expected []* endpoint.Endpoint
1509+ }{
1510+ {
1511+ title : "hostname and targets configured as annotations" ,
1512+ ingresses : []* networkv1.Ingress {
1513+ {
1514+ ObjectMeta : metav1.ObjectMeta {
1515+ Name : "my-ingress" ,
1516+ Namespace : "default" ,
1517+ Annotations : map [string ]string {
1518+ annotations .HostnameKey : "bla.example.org" ,
1519+ annotations .TargetKey : "target.example.org" ,
1520+ },
1521+ },
1522+ Spec : networkv1.IngressSpec {
1523+ IngressClassName : testutils .ToPtr ("nginx" ),
1524+ Rules : []networkv1.IngressRule {
1525+ {Host : "app.example.com" },
1526+ },
1527+ },
1528+ Status : networkv1.IngressStatus {
1529+ LoadBalancer : networkv1.IngressLoadBalancerStatus {
1530+ Ingress : []networkv1.IngressLoadBalancerIngress {
1531+ {IP : "1.2.3.4" },
1532+ },
1533+ },
1534+ },
1535+ },
1536+ },
1537+ expected : []* endpoint.Endpoint {
1538+ {DNSName : "app.example.com" , Targets : endpoint.Targets {"target.example.org" }, RecordType : endpoint .RecordTypeCNAME },
1539+ {DNSName : "bla.example.org" , Targets : endpoint.Targets {"target.example.org" }, RecordType : endpoint .RecordTypeCNAME },
1540+ },
1541+ },
1542+ {
1543+ title : "ingress with spec.tls with wildcard domains and tls not ignored" ,
1544+ cfg : & Config {
1545+ IgnoreIngressTLSSpec : false ,
1546+ },
1547+ ingresses : []* networkv1.Ingress {
1548+ {
1549+ ObjectMeta : metav1.ObjectMeta {
1550+ Name : "my-ingress" ,
1551+ Namespace : "default" ,
1552+ Annotations : map [string ]string {},
1553+ },
1554+ Spec : networkv1.IngressSpec {
1555+ IngressClassName : testutils .ToPtr ("alb" ),
1556+ TLS : []networkv1.IngressTLS {
1557+ {
1558+ Hosts : []string {"*.example.com" },
1559+ },
1560+ },
1561+ Rules : []networkv1.IngressRule {
1562+ {Host : "abc.example.com" },
1563+ },
1564+ },
1565+ Status : networkv1.IngressStatus {
1566+ LoadBalancer : networkv1.IngressLoadBalancerStatus {
1567+ Ingress : []networkv1.IngressLoadBalancerIngress {
1568+ {IP : "1.2.3.4" },
1569+ },
1570+ },
1571+ },
1572+ },
1573+ },
1574+ expected : []* endpoint.Endpoint {
1575+ {DNSName : "abc.example.com" , Targets : endpoint.Targets {"1.2.3.4" }, RecordType : endpoint .RecordTypeA },
1576+ {DNSName : "*.example.com" , Targets : endpoint.Targets {"1.2.3.4" }, RecordType : endpoint .RecordTypeA },
1577+ },
1578+ },
1579+ {
1580+ title : "ingress with spec.tls with wildcard domains and tls is ignored" ,
1581+ cfg : & Config {
1582+ IgnoreIngressTLSSpec : true ,
1583+ },
1584+ ingresses : []* networkv1.Ingress {
1585+ {
1586+ ObjectMeta : metav1.ObjectMeta {
1587+ Name : "my-ingress" ,
1588+ Namespace : "default" ,
1589+ },
1590+ Spec : networkv1.IngressSpec {
1591+ IngressClassName : testutils .ToPtr ("alb" ),
1592+ TLS : []networkv1.IngressTLS {
1593+ {
1594+ Hosts : []string {"*.example.com" },
1595+ },
1596+ },
1597+ Rules : []networkv1.IngressRule {
1598+ {Host : "abc.example.com" },
1599+ },
1600+ },
1601+ Status : networkv1.IngressStatus {
1602+ LoadBalancer : networkv1.IngressLoadBalancerStatus {
1603+ Ingress : []networkv1.IngressLoadBalancerIngress {
1604+ {IP : "1.2.3.4" },
1605+ },
1606+ },
1607+ },
1608+ },
1609+ },
1610+ expected : []* endpoint.Endpoint {
1611+ {DNSName : "abc.example.com" , Targets : endpoint.Targets {"1.2.3.4" }, RecordType : endpoint .RecordTypeA },
1612+ },
1613+ },
1614+ } {
1615+ t .Run (tt .title , func (t * testing.T ) {
1616+ kubeClient := fake .NewClientset ()
1617+
1618+ for _ , el := range tt .ingresses {
1619+ _ , err := kubeClient .NetworkingV1 ().Ingresses (el .Namespace ).Create (t .Context (), el , metav1.CreateOptions {})
1620+ require .NoError (t , err )
1621+ }
1622+
1623+ if tt .cfg == nil {
1624+ tt .cfg = & Config {}
1625+ }
1626+
1627+ src , err := NewIngressSource (
1628+ t .Context (),
1629+ kubeClient ,
1630+ tt .cfg .Namespace ,
1631+ tt .cfg .AnnotationFilter ,
1632+ tt .cfg .FQDNTemplate ,
1633+ tt .cfg .CombineFQDNAndAnnotation ,
1634+ tt .cfg .IgnoreHostnameAnnotation ,
1635+ tt .cfg .IgnoreIngressTLSSpec ,
1636+ tt .cfg .IgnoreIngressRulesSpec ,
1637+ labels .Everything (),
1638+ tt .cfg .IngressClassNames ,
1639+ )
1640+ require .NoError (t , err )
1641+ endpoints , err := src .Endpoints (t .Context ())
1642+ require .NoError (t , err )
1643+ validateEndpoints (t , endpoints , tt .expected )
1644+ })
1645+ }
1646+ }
0 commit comments