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

Enrich k8s state service events & add selector meta #23730

Merged
merged 6 commits into from
Feb 2, 2021
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.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Add kubernetes.volume.fs.used.pct field. {pull}23564[23564]
- Add the `enable_krb5_fast` flag to the Kafka output to explicitly opt-in to FAST authentication. {pull}23629[23629]
- Add deployment name in pod's meta. {pull}23610[23610]
- Add `selector` information in kubernetes services' metadata. {pull}23730[23730]

*Auditbeat*

Expand Down Expand Up @@ -958,6 +959,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Move IIS module to GA and map fields. {issue}22609[22609] {pull}23024[23024]
- Apache: convert status.total_kbytes to status.total_bytes in fleet mode. {pull}23022[23022]
- Release MSSQL as GA {pull}23146[23146]
- Enrich events of `state_service` metricset with kubernetes services' metadata. {pull}23730[23730]

*Packetbeat*

Expand Down
10 changes: 10 additions & 0 deletions auditbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -11615,6 +11615,16 @@ type: object
Kubernetes annotations map


type: object

--

*`kubernetes.service.selectors.*`*::
+
--
Kubernetes Service selectors map


type: object

--
Expand Down
2 changes: 1 addition & 1 deletion auditbeat/include/fields.go

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions filebeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -85616,6 +85616,16 @@ type: object
Kubernetes annotations map


type: object

--

*`kubernetes.service.selectors.*`*::
+
--
Kubernetes Service selectors map


type: object

--
Expand Down
2 changes: 1 addition & 1 deletion filebeat/include/fields.go

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions heartbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -9113,6 +9113,16 @@ type: object
Kubernetes annotations map


type: object

--

*`kubernetes.service.selectors.*`*::
+
--
Kubernetes Service selectors map


type: object

--
Expand Down
2 changes: 1 addition & 1 deletion heartbeat/include/fields.go

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions journalbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -9458,6 +9458,16 @@ type: object
Kubernetes annotations map


type: object

--

*`kubernetes.service.selectors.*`*::
+
--
Kubernetes Service selectors map


type: object

--
Expand Down
2 changes: 1 addition & 1 deletion journalbeat/include/fields.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions libbeat/common/kubernetes/metadata/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (r *Resource) Generate(kind string, obj kubernetes.Resource, options ...Fie

labelMap := common.MapStr{}
if len(r.config.IncludeLabels) == 0 {
labelMap = generateMap(accessor.GetLabels(), r.config.LabelsDedot)
labelMap = GenerateMap(accessor.GetLabels(), r.config.LabelsDedot)
} else {
labelMap = generateMapSubset(accessor.GetLabels(), r.config.IncludeLabels, r.config.LabelsDedot)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func generateMapSubset(input map[string]string, keys []string, dedot bool) commo
return output
}

func generateMap(input map[string]string, dedot bool) common.MapStr {
func GenerateMap(input map[string]string, dedot bool) common.MapStr {
output := common.MapStr{}
if input == nil {
return output
Expand Down
10 changes: 10 additions & 0 deletions libbeat/common/kubernetes/metadata/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/kubernetes"
"github.com/elastic/beats/v7/libbeat/common/safemapstr"
)

type service struct {
Expand Down Expand Up @@ -57,6 +58,15 @@ func (s *service) Generate(obj kubernetes.Resource, opts ...FieldOptions) common
}
}

selectors := svc.Spec.Selector
if len(selectors) == 0 {
return out
}
svcMap := GenerateMap(selectors, s.resource.config.LabelsDedot)
if len(svcMap) != 0 {
safemapstr.Put(out, "selectors", svcMap)
}

return out
}

Expand Down
20 changes: 20 additions & 0 deletions libbeat/common/kubernetes/metadata/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func TestService_Generate(t *testing.T) {
Kind: "Service",
APIVersion: "v1",
},
Spec: v1.ServiceSpec{
Selector: map[string]string{
"app": "istiod",
"istio": "pilot",
},
},
},
output: common.MapStr{
"service": common.MapStr{
Expand All @@ -68,6 +74,10 @@ func TestService_Generate(t *testing.T) {
"labels": common.MapStr{
"foo": "bar",
},
"selectors": common.MapStr{
"app": "istiod",
"istio": "pilot",
},
"namespace": "default",
},
},
Expand Down Expand Up @@ -96,6 +106,12 @@ func TestService_Generate(t *testing.T) {
Kind: "Service",
APIVersion: "v1",
},
Spec: v1.ServiceSpec{
Selector: map[string]string{
"app": "istiod",
"istio": "pilot",
},
},
},
output: common.MapStr{
"service": common.MapStr{
Expand All @@ -105,6 +121,10 @@ func TestService_Generate(t *testing.T) {
"labels": common.MapStr{
"foo": "bar",
},
"selectors": common.MapStr{
"app": "istiod",
"istio": "pilot",
},
"namespace": "default",
"deployment": common.MapStr{
"name": "owner",
Expand Down
7 changes: 7 additions & 0 deletions libbeat/processors/add_kubernetes_metadata/_meta/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
description: >
Kubernetes annotations map

- name: service.selectors.*
type: object
object_type: keyword
object_type_mapping_type: "*"
description: >
Kubernetes Service selectors map

- name: replicaset.name
type: keyword
description: >
Expand Down
10 changes: 10 additions & 0 deletions metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -27026,6 +27026,16 @@ type: object
Kubernetes annotations map


type: object

--

*`kubernetes.service.selectors.*`*::
+
--
Kubernetes Service selectors map


type: object

--
Expand Down
13 changes: 13 additions & 0 deletions metricbeat/module/kubernetes/state_service/state_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
package state_service

import (
"github.com/elastic/beats/v7/libbeat/common/kubernetes"
p "github.com/elastic/beats/v7/metricbeat/helper/prometheus"
"github.com/elastic/beats/v7/metricbeat/mb"
"github.com/elastic/beats/v7/metricbeat/module/kubernetes/util"
)

func init() {
Expand All @@ -37,6 +39,7 @@ type ServiceMetricSet struct {
mb.BaseMetricSet
prometheus p.Prometheus
mapping *p.MetricsMapping
enricher util.Enricher
}

// NewServiceMetricSet returns a prometheus based metricset for Services
Expand Down Expand Up @@ -75,19 +78,23 @@ func NewServiceMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) {
"hostname": p.Label("ingress_hostname"),
},
},
enricher: util.NewResourceMetadataEnricher(base, &kubernetes.Service{}, false),
}, nil
}

// Fetch prometheus metrics and treats those prefixed by mb.ModuleDataKey as
// module rooted fields at the event that gets reported
func (m *ServiceMetricSet) Fetch(reporter mb.ReporterV2) {
m.enricher.Start()
events, err := m.prometheus.GetProcessedMetrics(m.mapping)
if err != nil {
m.Logger().Error(err)
reporter.Error(err)
return
}

m.enricher.Enrich(events)

for _, event := range events {
event[mb.NamespaceKey] = "service"
reported := reporter.Event(mb.TransformMapStrToEvent("kubernetes", event, nil))
Expand All @@ -98,3 +105,9 @@ func (m *ServiceMetricSet) Fetch(reporter mb.ReporterV2) {
}
return
}

// Close stops this metricset
func (m *ServiceMetricSet) Close() error {
m.enricher.Stop()
return nil
}
3 changes: 3 additions & 0 deletions metricbeat/module/kubernetes/util/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func NewResourceMetadataEnricher(

metaGen := metadata.NewResourceMetadataGenerator(cfg)
podMetaGen := metadata.NewPodMetadataGenerator(cfg, nil, watcher.Client(), nil, nil)
serviceMetaGen := metadata.NewServiceMetadataGenerator(cfg, nil, nil)
enricher := buildMetadataEnricher(watcher,
// update
func(m map[string]common.MapStr, r kubernetes.Resource) {
Expand Down Expand Up @@ -158,6 +159,8 @@ func NewResourceMetadataEnricher(

case *kubernetes.Deployment:
m[id] = metaGen.Generate("deployment", r)
case *kubernetes.Service:
m[id] = serviceMetaGen.Generate(r)
case *kubernetes.StatefulSet:
m[id] = metaGen.Generate("statefulset", r)
case *kubernetes.Namespace:
Expand Down
10 changes: 10 additions & 0 deletions packetbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -10919,6 +10919,16 @@ type: object
Kubernetes annotations map


type: object

--

*`kubernetes.service.selectors.*`*::
+
--
Kubernetes Service selectors map


type: object

--
Expand Down
2 changes: 1 addition & 1 deletion packetbeat/include/fields.go

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions winlogbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -8942,6 +8942,16 @@ type: object
Kubernetes annotations map


type: object

--

*`kubernetes.service.selectors.*`*::
+
--
Kubernetes Service selectors map


type: object

--
Expand Down
2 changes: 1 addition & 1 deletion winlogbeat/include/fields.go

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions x-pack/functionbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -8765,6 +8765,16 @@ type: object
Kubernetes annotations map


type: object

--

*`kubernetes.service.selectors.*`*::
+
--
Kubernetes Service selectors map


type: object

--
Expand Down
2 changes: 1 addition & 1 deletion x-pack/functionbeat/include/fields.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion x-pack/heartbeat/include/fields.go

Large diffs are not rendered by default.