Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[need #12] Refactor DNS name extraction #13

Open
wants to merge 1 commit into
base: maiao.I5f01e93edd2b8c48689588a871905047d4fbed8f
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")),
),
),
),
)
}
Loading