From 9e7177c994627e04449b38a35994779bfa4fe808 Mon Sep 17 00:00:00 2001 From: chrismark Date: Mon, 10 Jan 2022 11:33:25 +0000 Subject: [PATCH] Add metadata only on demand Signed-off-by: chrismark --- .../elastic-agent-managed-kubernetes.yaml | 3 ++- .../elastic-agent-managed-role.yaml | 3 ++- .../elastic-agent-standalone-kubernetes.yaml | 3 ++- .../elastic-agent-standalone-role.yaml | 3 ++- deploy/kubernetes/metricbeat-kubernetes.yaml | 3 ++- .../metricbeat/metricbeat-role.yaml | 3 ++- .../state_cronjob/_meta/docs.asciidoc | 5 ++++ .../kubernetes/state_cronjob/state_cronjob.go | 27 ++++++++++++++----- .../module/kubernetes/util/kubernetes.go | 6 +++++ 9 files changed, 44 insertions(+), 12 deletions(-) diff --git a/deploy/kubernetes/elastic-agent-managed-kubernetes.yaml b/deploy/kubernetes/elastic-agent-managed-kubernetes.yaml index 39e3352495fb..9389489eed5a 100644 --- a/deploy/kubernetes/elastic-agent-managed-kubernetes.yaml +++ b/deploy/kubernetes/elastic-agent-managed-kubernetes.yaml @@ -165,7 +165,8 @@ rules: - apiGroups: [ "batch" ] resources: - jobs - - cronjobs + # Uncomment if need metadata for cronjob objects in versions >= v1.21 + #- cronjobs verbs: [ "get", "list", "watch" ] # required for apiserver - nonResourceURLs: diff --git a/deploy/kubernetes/elastic-agent-managed/elastic-agent-managed-role.yaml b/deploy/kubernetes/elastic-agent-managed/elastic-agent-managed-role.yaml index a7e3df1279f8..49d4bd129994 100644 --- a/deploy/kubernetes/elastic-agent-managed/elastic-agent-managed-role.yaml +++ b/deploy/kubernetes/elastic-agent-managed/elastic-agent-managed-role.yaml @@ -38,7 +38,8 @@ rules: - apiGroups: [ "batch" ] resources: - jobs - - cronjobs + # Uncomment if need metadata for cronjob objects in versions >= v1.21 + #- cronjobs verbs: [ "get", "list", "watch" ] # required for apiserver - nonResourceURLs: diff --git a/deploy/kubernetes/elastic-agent-standalone-kubernetes.yaml b/deploy/kubernetes/elastic-agent-standalone-kubernetes.yaml index ba5d2b065147..a46d8e3f4f63 100644 --- a/deploy/kubernetes/elastic-agent-standalone-kubernetes.yaml +++ b/deploy/kubernetes/elastic-agent-standalone-kubernetes.yaml @@ -671,7 +671,8 @@ rules: - apiGroups: ["batch"] resources: - jobs - - cronjobs + # Uncomment if need metadata for cronjob objects in versions >= v1.21 + #- cronjobs verbs: ["get", "list", "watch"] - apiGroups: - "" diff --git a/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml b/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml index be051d04452d..f0f6c2ca9134 100644 --- a/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml +++ b/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml @@ -32,7 +32,8 @@ rules: - apiGroups: ["batch"] resources: - jobs - - cronjobs + # Uncomment if need metadata for cronjob objects in versions >= v1.21 + #- cronjobs verbs: ["get", "list", "watch"] - apiGroups: - "" diff --git a/deploy/kubernetes/metricbeat-kubernetes.yaml b/deploy/kubernetes/metricbeat-kubernetes.yaml index 9d5a371c8749..166c195d2f6c 100644 --- a/deploy/kubernetes/metricbeat-kubernetes.yaml +++ b/deploy/kubernetes/metricbeat-kubernetes.yaml @@ -295,7 +295,8 @@ rules: - apiGroups: ["batch"] resources: - jobs - - cronjobs + # Uncomment if need metadata for cronjob objects in versions >= v1.21 + #- cronjobs verbs: ["get", "list", "watch"] - apiGroups: - "" diff --git a/deploy/kubernetes/metricbeat/metricbeat-role.yaml b/deploy/kubernetes/metricbeat/metricbeat-role.yaml index 95824e5f06e9..26ed85ba619e 100644 --- a/deploy/kubernetes/metricbeat/metricbeat-role.yaml +++ b/deploy/kubernetes/metricbeat/metricbeat-role.yaml @@ -31,7 +31,8 @@ rules: - apiGroups: ["batch"] resources: - jobs - - cronjobs + # Uncomment if need metadata for cronjob objects in versions >= v1.21 + #- cronjobs verbs: ["get", "list", "watch"] - apiGroups: - "" diff --git a/metricbeat/module/kubernetes/state_cronjob/_meta/docs.asciidoc b/metricbeat/module/kubernetes/state_cronjob/_meta/docs.asciidoc index 1558a5f00e55..d994fe5dced0 100644 --- a/metricbeat/module/kubernetes/state_cronjob/_meta/docs.asciidoc +++ b/metricbeat/module/kubernetes/state_cronjob/_meta/docs.asciidoc @@ -1 +1,6 @@ This is the `state_cronjob` metricset of the Kubernetes module. + +This metricset does not add metadata by default and hence in order to +add metadata for this one need to configure the metricset with `add_metadata: true` +and uncomment the proper `apiGroup` in the `ClusterRole`. Metadata are only available +for versions of k8s >= v1.21. diff --git a/metricbeat/module/kubernetes/state_cronjob/state_cronjob.go b/metricbeat/module/kubernetes/state_cronjob/state_cronjob.go index 947b5eaf174c..013f2afe9651 100644 --- a/metricbeat/module/kubernetes/state_cronjob/state_cronjob.go +++ b/metricbeat/module/kubernetes/state_cronjob/state_cronjob.go @@ -59,11 +59,15 @@ func NewCronJobMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) { return nil, fmt.Errorf("must be child of kubernetes module") } - return &CronJobMetricSet{ + config := util.GetDefaultDisabledMetaConfig() + if err := base.Module().UnpackConfig(&config); err != nil { + return nil, fmt.Errorf("error loading config of kubernetes module") + } + + ms := CronJobMetricSet{ BaseMetricSet: base, prometheus: prometheus, mod: mod, - enricher: util.NewResourceMetadataEnricher(base, &kubernetes.CronJob{}, false), mapping: &p.MetricsMapping{ Metrics: map[string]p.MetricMap{ "kube_cronjob_info": p.InfoMetric(), @@ -81,7 +85,12 @@ func NewCronJobMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) { "concurrency_policy": p.KeyLabel("concurrency"), }, }, - }, nil + } + if config.AddMetadata { + ms.enricher = util.NewResourceMetadataEnricher( + base, &kubernetes.CronJob{}, false) + } + return &ms, nil } // Fetch prometheus metrics and treats those prefixed by mb.ModuleDataKey as @@ -89,7 +98,9 @@ func NewCronJobMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) { // // Copied from other kube state metrics. func (m *CronJobMetricSet) Fetch(reporter mb.ReporterV2) { - m.enricher.Start() + if m.enricher != nil { + m.enricher.Start() + } families, err := m.mod.GetStateMetricsFamilies(m.prometheus) if err != nil { @@ -104,7 +115,9 @@ func (m *CronJobMetricSet) Fetch(reporter mb.ReporterV2) { return } - m.enricher.Enrich(events) + if m.enricher != nil { + m.enricher.Enrich(events) + } for _, event := range events { e, err := util.CreateEvent(event, "kubernetes.cronjob") if err != nil { @@ -122,6 +135,8 @@ func (m *CronJobMetricSet) Fetch(reporter mb.ReporterV2) { // Close stops this metricset func (m *CronJobMetricSet) Close() error { - m.enricher.Stop() + if m.enricher != nil { + m.enricher.Stop() + } return nil } diff --git a/metricbeat/module/kubernetes/util/kubernetes.go b/metricbeat/module/kubernetes/util/kubernetes.go index 8afe8318b360..6518a258e261 100644 --- a/metricbeat/module/kubernetes/util/kubernetes.go +++ b/metricbeat/module/kubernetes/util/kubernetes.go @@ -306,6 +306,12 @@ func getResourceMetadataWatchers(config *kubernetesConfig, resource kubernetes.R return watcher, nodeWatcher, namespaceWatcher } +func GetDefaultDisabledMetaConfig() *kubernetesConfig { + return &kubernetesConfig{ + AddMetadata: false, + } +} + func validatedConfig(base mb.BaseMetricSet) *kubernetesConfig { config := kubernetesConfig{ AddMetadata: true,