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

Support for multiple hostnames in hostname annotation #256

Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 25 additions & 9 deletions source/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,37 @@ func endpointsFromService(svc *v1.Service) []*endpoint.Endpoint {
var endpoints []*endpoint.Endpoint

// Get the desired hostname of the service from the annotation.
hostname, exists := svc.Annotations[hostnameAnnotationKey]
hostnameAnnotation, exists := svc.Annotations[hostnameAnnotationKey]
if !exists {
return nil
}

// Create a corresponding endpoint for each configured external entrypoint.
for _, lb := range svc.Status.LoadBalancer.Ingress {
if lb.IP != "" {
//TODO(ideahitme): consider retrieving record type from resource annotation instead of empty
endpoints = append(endpoints, endpoint.NewEndpoint(hostname, lb.IP, ""))
}
if lb.Hostname != "" {
endpoints = append(endpoints, endpoint.NewEndpoint(hostname, lb.Hostname, ""))
hostnames := parseHostnameAnnotation(hostnameAnnotation)

for _, hostname := range hostnames {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe inline?

for _, hostname := range parseHostnameAnnotation(hostnameAnnotation) {

// Create a corresponding endpoint for each configured external entrypoint.
for _, lb := range svc.Status.LoadBalancer.Ingress {
if lb.IP != "" {
//TODO(ideahitme): consider retrieving record type from resource annotation instead of empty
endpoints = append(endpoints, endpoint.NewEndpoint(hostname, lb.IP, ""))
}
if lb.Hostname != "" {
endpoints = append(endpoints, endpoint.NewEndpoint(hostname, lb.Hostname, ""))
}
}
}

return endpoints
}

// parseHostnameAnnotation splits the hostname annontation and removes the trailing periods
func parseHostnameAnnotation(hostnameAnnotation string) (hostnames []string) {

hostnameList := strings.Split(strings.Replace(hostnameAnnotation, " ", "", -1), ",")

for _, hostname := range hostnameList {
hostnames = append(hostnames, strings.TrimSuffix(hostname, "."))
}

return
}
36 changes: 36 additions & 0 deletions source/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,42 @@ func testServiceSourceEndpoints(t *testing.T) {
},
false,
},
{
"annotated services with multiple hostnames return an endpoint with target IP",
"",
"testing",
"foo",
"",
"",
map[string]string{},
map[string]string{
hostnameAnnotationKey: "foo.example.org., bar.example.org.",
},
[]string{"1.2.3.4"},
[]*endpoint.Endpoint{
{DNSName: "foo.example.org", Target: "1.2.3.4"},
{DNSName: "bar.example.org", Target: "1.2.3.4"},
},
false,
},
{
"annotated services with multiple hostnames and without trailing period return an endpoint with target IP",
"",
"testing",
"foo",
"",
"",
map[string]string{},
map[string]string{
hostnameAnnotationKey: "foo.example.org, bar.example.org",
},
[]string{"1.2.3.4"},
[]*endpoint.Endpoint{
{DNSName: "foo.example.org", Target: "1.2.3.4"},
{DNSName: "bar.example.org", Target: "1.2.3.4"},
},
false,
},
{
"annotated services return an endpoint with target hostname",
"",
Expand Down