From d1b3f8e1762924d1678fb5f3f91252af956df2c4 Mon Sep 17 00:00:00 2001 From: Thibault Jamet Date: Fri, 29 Nov 2024 17:16:18 +0100 Subject: [PATCH] Refactor DNS name extraction Goal --- Ease the implementation of CRD based multi-ingress controller traffic routing Change-Id: I46f513341ce01ec6e9d67897c6d0cb58aeec3021 --- pkg/controllers/ingress_controller.go | 34 +++++++++++++++------- pkg/controllers/ingress_controller_test.go | 19 ++++++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/pkg/controllers/ingress_controller.go b/pkg/controllers/ingress_controller.go index f85572c..ef048a7 100644 --- a/pkg/controllers/ingress_controller.go +++ b/pkg/controllers/ingress_controller.go @@ -89,6 +89,14 @@ func (r *IngressReconciler) ingressAnnotationMatchFilter(ingress netv1.Ingress) return r.ingressHasAnnotationKeyValue(ingress, r.AnnotationFilter.key, r.AnnotationFilter.value) } +func (r *IngressReconciler) listFilteredIngressHosts(ingress *netv1.Ingress) []string { + hosts := []string{} + for _, rule := range r.filterIngressRulesByHost(ingress.Spec.Rules) { + hosts = append(hosts, rule.Host) + } + return hosts +} + func (r *IngressReconciler) filterIngressRulesByHost(rules []netv1.IngressRule) []netv1.IngressRule { rulesToBind := []netv1.IngressRule{} for _, rule := range rules { @@ -192,18 +200,24 @@ func (r *IngressReconciler) newDnsEndpoint(ctx context.Context, dnsEndpoint *ext desiredWeight = 0 } dnsEndpoint.Spec = externaldnsk8siov1alpha1.DNSEndpointSpec{Endpoints: []*externaldnsk8siov1alpha1.Endpoint{}} - for _, rule := range r.filterIngressRulesByHost(ingress.Spec.Rules) { - ep := &externaldnsk8siov1alpha1.Endpoint{ - DNSName: rule.Host, - RecordType: "CNAME", - SetIdentifier: r.ClusterName, - } - r.addIngressTargetsToEndpoint(ep, ingress) + dnsNames := r.listFilteredIngressHosts(&ingress) - setEndpointProviderSpecificProperty(ep, WeightProperty, strconv.FormatUint(uint64(desiredWeight), 10)) - setGlobalHealthCheckID(ep) - dnsEndpoint.Spec.Endpoints = append(dnsEndpoint.Spec.Endpoints, ep) + if len(ingress.Status.LoadBalancer.Ingress) > 0 { + for _, dnsName := range dnsNames { + + ep := &externaldnsk8siov1alpha1.Endpoint{ + DNSName: dnsName, + RecordType: "CNAME", + SetIdentifier: r.ClusterName, + } + r.addIngressTargetsToEndpoint(ep, ingress) + + setEndpointProviderSpecificProperty(ep, WeightProperty, strconv.FormatUint(uint64(desiredWeight), 10)) + + setGlobalHealthCheckID(ep) + dnsEndpoint.Spec.Endpoints = append(dnsEndpoint.Spec.Endpoints, ep) + } } } diff --git a/pkg/controllers/ingress_controller_test.go b/pkg/controllers/ingress_controller_test.go index 947df63..fdecb9d 100644 --- a/pkg/controllers/ingress_controller_test.go +++ b/pkg/controllers/ingress_controller_test.go @@ -510,3 +510,22 @@ func TestEndpointsHasPods(t *testing.T) { assert.False(t, r.endpointsHasPods(MockEndpoints(WithObjectFinalizers[*v1.Endpoints]("test.adevinta.com"), WithObjectDeletionTimestamp[*v1.Endpoints](metav1.Now())))) } + +func TestListFilteredIngressHosts(t *testing.T) { + r := IngressReconciler{ + BindingDomain: "example.com", + } + assert.ElementsMatch( + t, + []string{"host1.example.com", "example.com"}, + r.listFilteredIngressHosts( + MockIngress( + IngressWithRules( + NewRule(RuleWithHost("host1.example.com")), + NewRule(RuleWithHost("example.com")), + NewRule(RuleWithHost("adevinta.com")), + ), + ), + ), + ) +}