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
  • Loading branch information
vjsamuel authored and exekias committed Jul 26, 2017
1 parent 3aa9802 commit 7e1e453
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
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

0 comments on commit 7e1e453

Please sign in to comment.