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

Avoid missing pod hostname for headless service #429

Merged
merged 1 commit into from Feb 8, 2018
Merged
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
5 changes: 4 additions & 1 deletion source/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ func (sc *serviceSource) extractHeadlessEndpoint(svc *v1.Service, hostname strin
}

for _, v := range pods.Items {
headlessDomain := v.Spec.Hostname + "." + hostname
headlessDomain := hostname
if v.Spec.Hostname != "" {
headlessDomain = v.Spec.Hostname + "." + headlessDomain
}
log.Debugf("Generating matching endpoint %s with HostIP %s", headlessDomain, v.Status.HostIP)
// To reduce traffice on the DNS API only add record for running Pods. Good Idea?
if v.Status.Phase == v1.PodRunning {
Expand Down
36 changes: 33 additions & 3 deletions source/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@ func TestHeadlessServices(t *testing.T) {
hostIP string
selector map[string]string
lbs []string
podnames []string
hostnames []string
phases []v1.PodPhase
expected []*endpoint.Endpoint
Expand All @@ -984,6 +985,7 @@ func TestHeadlessServices(t *testing.T) {
},
[]string{},
[]string{"foo-0", "foo-1"},
[]string{"foo-0", "foo-1"},
[]v1.PodPhase{v1.PodRunning, v1.PodRunning},
[]*endpoint.Endpoint{
{DNSName: "foo-0.service.example.org", Target: "1.1.1.1"},
Expand All @@ -1010,12 +1012,40 @@ func TestHeadlessServices(t *testing.T) {
},
[]string{},
[]string{"foo-0", "foo-1"},
[]string{"foo-0", "foo-1"},
[]v1.PodPhase{v1.PodRunning, v1.PodFailed},
[]*endpoint.Endpoint{
{DNSName: "foo-0.service.example.org", Target: "1.1.1.1"},
},
false,
},
{
"annotated Headless services return endpoints for pods missing hostname",
"",
"testing",
"foo",
v1.ServiceTypeClusterIP,
"",
"",
map[string]string{"component": "foo"},
map[string]string{
hostnameAnnotationKey: "service.example.org",
},
v1.ClusterIPNone,
"1.1.1.1",
map[string]string{
"component": "foo",
},
[]string{},
[]string{"foo-0", "foo-1"},
[]string{"", ""},
[]v1.PodPhase{v1.PodRunning, v1.PodRunning},
[]*endpoint.Endpoint{
{DNSName: "service.example.org", Target: "1.1.1.1"},
{DNSName: "service.example.org", Target: "1.1.1.1"},
},
false,
},
} {
t.Run(tc.title, func(t *testing.T) {
// Create a Kubernetes testing client
Expand All @@ -1038,15 +1068,15 @@ func TestHeadlessServices(t *testing.T) {
_, err := kubernetes.CoreV1().Services(service.Namespace).Create(service)
require.NoError(t, err)

for i, hostname := range tc.hostnames {
for i, podname := range tc.podnames {
pod := &v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{},
Hostname: hostname,
Hostname: tc.hostnames[i],
},
ObjectMeta: metav1.ObjectMeta{
Namespace: tc.svcNamespace,
Name: hostname,
Name: podname,
Labels: tc.labels,
Annotations: tc.annotations,
},
Expand Down