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

Adding support to exclude labels from kubernetes pod metadata #4757

Merged
merged 2 commits into from
Jul 26, 2017
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
2 changes: 2 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ https://github.com/elastic/beats/compare/v6.0.0-beta1...master[Check the HEAD di

*Metricbeat*

- Add support to exclude labels from kubernetes pod metadata. {pull}4757[4757]

*Packetbeat*

*Winlogbeat*
Expand Down
1 change: 1 addition & 0 deletions libbeat/processors/add_kubernetes_metadata/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type kubeAnnotatorConfig struct {
DefaultMatchers Enabled `config:"default_matchers"`
DefaultIndexers Enabled `config:"default_indexers"`
IncludeLabels []string `config:"include_labels"`
ExcludeLabels []string `config:"exclude_labels"`
IncludeAnnotations []string `config:"include_annotations"`
}

Expand Down
10 changes: 8 additions & 2 deletions libbeat/processors/add_kubernetes_metadata/indexing.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ func (m *Matchers) MetadataIndex(event common.MapStr) string {
}

type GenDefaultMeta struct {
annotations []string
labels []string
annotations []string
labels []string
labelsExclude []string
}

// GenerateMetaData generates default metadata for the given pod taking to account certain filters
Expand All @@ -189,6 +190,11 @@ func (g *GenDefaultMeta) GenerateMetaData(pod *Pod) common.MapStr {
labelMap = generateMapSubset(pod.Metadata.Labels, g.labels)
}

// Exclude any labels that are present in the exclude_labels config
for _, label := range g.labelsExclude {
delete(labelMap, label)
}

annotationsMap = generateMapSubset(pod.Metadata.Annotations, g.annotations)

meta := common.MapStr{
Expand Down
46 changes: 46 additions & 0 deletions libbeat/processors/add_kubernetes_metadata/indexing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,49 @@ func TestFilteredGenMeta(t *testing.T) {
ok, _ = annotationsMap.HasKey("a")
assert.Equal(t, ok, true)
}

func TestFilteredGenMetaExclusion(t *testing.T) {
var testConfig = common.NewConfig()

filteredGen := &GenDefaultMeta{
labelsExclude: []string{"x"},
}
podIndexer, err := NewPodNameIndexer(*testConfig, filteredGen)
assert.Nil(t, err)

podName := "testpod"
ns := "testns"
pod := Pod{
Metadata: ObjectMeta{
Name: podName,
Namespace: ns,
Labels: map[string]string{
"foo": "bar",
"x": "y",
},
Annotations: map[string]string{
"a": "b",
"c": "d",
},
},
Spec: PodSpec{},
}

assert.Nil(t, err)

indexers := podIndexer.GetMetadata(&pod)
assert.Equal(t, len(indexers), 1)

rawLabels, _ := indexers[0].Data["labels"]
assert.NotNil(t, rawLabels)

labelMap, ok := rawLabels.(common.MapStr)
assert.Equal(t, ok, true)
assert.Equal(t, len(labelMap), 1)

ok, _ = labelMap.HasKey("foo")
assert.Equal(t, ok, true)

ok, _ = labelMap.HasKey("x")
assert.Equal(t, ok, false)
}
5 changes: 3 additions & 2 deletions libbeat/processors/add_kubernetes_metadata/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ func newKubernetesAnnotator(cfg *common.Config) (processors.Processor, error) {
}

metaGen := &GenDefaultMeta{
labels: config.IncludeLabels,
annotations: config.IncludeAnnotations,
labels: config.IncludeLabels,
annotations: config.IncludeAnnotations,
labelsExclude: config.ExcludeLabels,
}

indexers := Indexers{
Expand Down