Skip to content

Commit

Permalink
Adding support to exclude labels from kubernetes pod metadata (elasti…
Browse files Browse the repository at this point in the history
…c#4757) (elastic#4993)

(cherry picked from commit 7e1e453)
  • Loading branch information
exekias authored and tsg committed Aug 24, 2017
1 parent 1532341 commit 1f6585c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta1...master[Check the HEAD di
*Metricbeat*

- Add `filesystem.ignore_types` to system module for ignoring filesystem types. {issue}4685[4685]
- Add support to exclude labels from kubernetes pod metadata. {pull}4757[4757]

*Packetbeat*

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 @@ -178,8 +178,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 @@ -195,6 +196,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 @@ -256,3 +256,49 @@ func TestFieldFormatMatcher(t *testing.T) {
out = matcher.MetadataIndex(event)
assert.Equal(t, "foo/bar", out)
}

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 @@ -75,8 +75,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

0 comments on commit 1f6585c

Please sign in to comment.