Skip to content

Commit

Permalink
Refactor DNS name extraction
Browse files Browse the repository at this point in the history
Goal
---

Ease the implementation of CRD based multi-ingress controller traffic routing

Change-Id: I46f513341ce01ec6e9d67897c6d0cb58aeec3021
  • Loading branch information
Thibault Jamet committed Dec 13, 2024
1 parent 64f463d commit 6f3c338
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
34 changes: 24 additions & 10 deletions pkg/controllers/ingress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions pkg/controllers/ingress_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
),
),
),
)
}

0 comments on commit 6f3c338

Please sign in to comment.