Skip to content

Commit

Permalink
Extract hostname from pod.name when the former is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
pmm-sumo committed Mar 21, 2020
1 parent 5c543a8 commit 706b609
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
9 changes: 8 additions & 1 deletion processor/k8sprocessor/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,14 @@ func (c *WatchClient) extractPodAttributes(pod *api_v1.Pod) map[string]string {
}

if c.Rules.HostName {
tags[c.Rules.Tags.HostName] = pod.Spec.Hostname
// Basing on v1.17 Kubernetes docs, when a hostname is specified, it takes precedence over
// the associated metadata name, see:
// https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pod-s-hostname-and-subdomain-fields
if pod.Spec.Hostname == "" {
tags[c.Rules.Tags.HostName] = pod.Name
} else {
tags[c.Rules.Tags.HostName] = pod.Spec.Hostname
}
}

if c.Rules.ClusterName {
Expand Down
25 changes: 25 additions & 0 deletions processor/k8sprocessor/kube/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,31 @@ func TestDeleteQueue(t *testing.T) {
// test delete loop
}

func TestNoHostnameExtractionRules(t *testing.T) {
c := newTestClientWithRulesAndFilters(t, ExtractionRules{}, Filters{})

podName := "auth-service-abc12-xyz3"

pod := &api_v1.Pod{
ObjectMeta: meta_v1.ObjectMeta{
Name: podName,
},
Spec: api_v1.PodSpec{},
Status: api_v1.PodStatus{
PodIP: "1.1.1.1",
},
}

c.Rules = ExtractionRules{
HostName: true,
Tags: NewExtractionFieldTags(),
}

c.handlePodAdd(pod)
p, _ := c.GetPodByIP(pod.Status.PodIP)
assert.Equal(t, p.Attributes["k8s.pod.hostname"], podName)
}

func TestExtractionRules(t *testing.T) {
c := newTestClientWithRulesAndFilters(t, ExtractionRules{}, Filters{})

Expand Down

0 comments on commit 706b609

Please sign in to comment.