From 7d9a50f1a5d02a203e008063577139fe14414106 Mon Sep 17 00:00:00 2001 From: chrismark Date: Wed, 2 Mar 2022 08:44:44 +0000 Subject: [PATCH 01/17] Add cronjob metadata by default 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 +- libbeat/common/kubernetes/metadata/config.go | 2 ++ libbeat/common/kubernetes/metadata/pod.go | 33 ++++++++++++++++++- libbeat/docs/shared-autodiscover.asciidoc | 3 ++ metricbeat/docs/modules/kubernetes.asciidoc | 1 + metricbeat/metricbeat.reference.yml | 1 + .../kubernetes/_meta/config.reference.yml | 1 + metricbeat/module/kubernetes/_meta/config.yml | 1 + .../kubernetes/state_cronjob/state_cronjob.go | 11 ++----- .../state_cronjob_integration_test.go | 2 +- .../module/kubernetes/test/integration.go | 12 ------- metricbeat/modules.d/kubernetes.yml.disabled | 1 + x-pack/metricbeat/metricbeat.reference.yml | 1 + 18 files changed, 52 insertions(+), 35 deletions(-) diff --git a/deploy/kubernetes/elastic-agent-managed-kubernetes.yaml b/deploy/kubernetes/elastic-agent-managed-kubernetes.yaml index 6fe269fe3a04..57c177746978 100644 --- a/deploy/kubernetes/elastic-agent-managed-kubernetes.yaml +++ b/deploy/kubernetes/elastic-agent-managed-kubernetes.yaml @@ -165,8 +165,7 @@ rules: - apiGroups: [ "batch" ] resources: - jobs - # Uncomment if need metadata for cronjob objects in versions >= v1.21 - #- cronjobs + - 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 49d4bd129994..a7e3df1279f8 100644 --- a/deploy/kubernetes/elastic-agent-managed/elastic-agent-managed-role.yaml +++ b/deploy/kubernetes/elastic-agent-managed/elastic-agent-managed-role.yaml @@ -38,8 +38,7 @@ rules: - apiGroups: [ "batch" ] resources: - jobs - # Uncomment if need metadata for cronjob objects in versions >= v1.21 - #- cronjobs + - 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 b848d6dbfa9b..151248bf473e 100644 --- a/deploy/kubernetes/elastic-agent-standalone-kubernetes.yaml +++ b/deploy/kubernetes/elastic-agent-standalone-kubernetes.yaml @@ -703,8 +703,7 @@ rules: - apiGroups: ["batch"] resources: - jobs - # Uncomment if need metadata for cronjob objects in versions >= v1.21 - #- cronjobs + - 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 f0f6c2ca9134..be051d04452d 100644 --- a/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml +++ b/deploy/kubernetes/elastic-agent-standalone/elastic-agent-standalone-role.yaml @@ -32,8 +32,7 @@ rules: - apiGroups: ["batch"] resources: - jobs - # Uncomment if need metadata for cronjob objects in versions >= v1.21 - #- cronjobs + - cronjobs verbs: ["get", "list", "watch"] - apiGroups: - "" diff --git a/deploy/kubernetes/metricbeat-kubernetes.yaml b/deploy/kubernetes/metricbeat-kubernetes.yaml index 3c8a373c35b5..94390075f71a 100644 --- a/deploy/kubernetes/metricbeat-kubernetes.yaml +++ b/deploy/kubernetes/metricbeat-kubernetes.yaml @@ -298,8 +298,7 @@ rules: - apiGroups: ["batch"] resources: - jobs - # Uncomment if need metadata for cronjob objects in versions >= v1.21 - #- cronjobs + - cronjobs verbs: ["get", "list", "watch"] - apiGroups: - "" diff --git a/deploy/kubernetes/metricbeat/metricbeat-role.yaml b/deploy/kubernetes/metricbeat/metricbeat-role.yaml index 26ed85ba619e..95824e5f06e9 100644 --- a/deploy/kubernetes/metricbeat/metricbeat-role.yaml +++ b/deploy/kubernetes/metricbeat/metricbeat-role.yaml @@ -31,8 +31,7 @@ rules: - apiGroups: ["batch"] resources: - jobs - # Uncomment if need metadata for cronjob objects in versions >= v1.21 - #- cronjobs + - cronjobs verbs: ["get", "list", "watch"] - apiGroups: - "" diff --git a/libbeat/common/kubernetes/metadata/config.go b/libbeat/common/kubernetes/metadata/config.go index 5edc061d2f21..26f56d298021 100644 --- a/libbeat/common/kubernetes/metadata/config.go +++ b/libbeat/common/kubernetes/metadata/config.go @@ -35,6 +35,7 @@ type AddResourceMetadataConfig struct { Node *common.Config `config:"node"` Namespace *common.Config `config:"namespace"` Deployment bool `config:"deployment"` + CronJob bool `config:"cronjob"` } // InitDefaults initializes the defaults for the config. @@ -56,5 +57,6 @@ func GetDefaultResourceMetadataConfig() *AddResourceMetadataConfig { Node: metaCfg, Namespace: metaCfg, Deployment: true, + CronJob: true, } } diff --git a/libbeat/common/kubernetes/metadata/pod.go b/libbeat/common/kubernetes/metadata/pod.go index 49bd51309a99..4007bb84af70 100644 --- a/libbeat/common/kubernetes/metadata/pod.go +++ b/libbeat/common/kubernetes/metadata/pod.go @@ -19,7 +19,6 @@ package metadata import ( "context" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" k8s "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/cache" @@ -99,6 +98,17 @@ func (p *pod) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common. } } + if p.addResourceMetadata.CronJob { + jobName, _ := out.GetValue("job.name") + if jobName, ok := jobName.(string); ok { + dep := p.getCronjobOfJob(jobName, po.GetNamespace()) + if dep != "" { + out.Put("cronjob.name", dep) + } + } + } + + if p.node != nil { meta := p.node.GenerateFromName(po.Spec.NodeName, WithMetadata("node")) if meta != nil { @@ -162,3 +172,24 @@ func (p *pod) getRSDeployment(rsName string, ns string) string { } return "" } + +//// getCronjobOfJob return the name of the Cronjob object that +//// owns the Job with the given name under the given Namespace +func (p *pod) getCronjobOfJob(jobName string, ns string) string { + if p.client == nil { + return "" + } + cronjob, err := p.client.BatchV1().Jobs(ns).Get(context.TODO(), jobName, metav1.GetOptions{}) + if err != nil { + return "" + } + for _, ref := range cronjob.GetOwnerReferences() { + if ref.Controller != nil && *ref.Controller { + switch ref.Kind { + case "CronJob": + return ref.Name + } + } + } + return "" +} diff --git a/libbeat/docs/shared-autodiscover.asciidoc b/libbeat/docs/shared-autodiscover.asciidoc index 40492d1e0ac6..3467e06b3c32 100644 --- a/libbeat/docs/shared-autodiscover.asciidoc +++ b/libbeat/docs/shared-autodiscover.asciidoc @@ -179,6 +179,8 @@ endif::[] storing requires special handling to avoid overloading the storage output. The enrichment of `node` or `namespace` metadata can be individually disabled by setting `enabled: false`. If resource is `pod` and it is created from a `deployment`, by default the deployment name is added, this can be disabled by set to `false`. + If resource is `pod` and it is created from a `cronjob`, by default + the cronjob name is added, this can be disabled by set to `false`. Example: ["source","yaml",subs="attributes"] @@ -190,6 +192,7 @@ endif::[] include_labels: ["nodelabel2"] include_annotations: ["nodeannotation1"] deployment: false + cronjob: false ------------------------------------------------------------------------------------- `unique`:: (Optional) Defaults to `false`. Marking an autodiscover provider as unique results into diff --git a/metricbeat/docs/modules/kubernetes.asciidoc b/metricbeat/docs/modules/kubernetes.asciidoc index 0d3595c3e4ef..af73023a1d68 100644 --- a/metricbeat/docs/modules/kubernetes.asciidoc +++ b/metricbeat/docs/modules/kubernetes.asciidoc @@ -241,6 +241,7 @@ metricbeat.modules: # include_labels: ["nodelabel2"] # include_annotations: ["nodeannotation1"] # deployment: false + # cronjob: false # Kubernetes client QPS and burst can be configured additionally #kube_client_options: # qps: 5 diff --git a/metricbeat/metricbeat.reference.yml b/metricbeat/metricbeat.reference.yml index bec4fb6bdacf..1e9c57c09b34 100644 --- a/metricbeat/metricbeat.reference.yml +++ b/metricbeat/metricbeat.reference.yml @@ -510,6 +510,7 @@ metricbeat.modules: # include_labels: ["nodelabel2"] # include_annotations: ["nodeannotation1"] # deployment: false + # cronjob: false # Kubernetes client QPS and burst can be configured additionally #kube_client_options: # qps: 5 diff --git a/metricbeat/module/kubernetes/_meta/config.reference.yml b/metricbeat/module/kubernetes/_meta/config.reference.yml index 2db9c919efe8..0d50801ea4cc 100644 --- a/metricbeat/module/kubernetes/_meta/config.reference.yml +++ b/metricbeat/module/kubernetes/_meta/config.reference.yml @@ -33,6 +33,7 @@ # include_labels: ["nodelabel2"] # include_annotations: ["nodeannotation1"] # deployment: false + # cronjob: false # Kubernetes client QPS and burst can be configured additionally #kube_client_options: # qps: 5 diff --git a/metricbeat/module/kubernetes/_meta/config.yml b/metricbeat/module/kubernetes/_meta/config.yml index 85cd0041486a..e29da38718f1 100644 --- a/metricbeat/module/kubernetes/_meta/config.yml +++ b/metricbeat/module/kubernetes/_meta/config.yml @@ -35,6 +35,7 @@ # include_labels: ["nodelabel2"] # include_annotations: ["nodeannotation1"] # deployment: false + # cronjob: false # Kubernetes client QPS and burst can be configured additionally #kube_client_options: # qps: 5 diff --git a/metricbeat/module/kubernetes/state_cronjob/state_cronjob.go b/metricbeat/module/kubernetes/state_cronjob/state_cronjob.go index 013f2afe9651..49d191580fdc 100644 --- a/metricbeat/module/kubernetes/state_cronjob/state_cronjob.go +++ b/metricbeat/module/kubernetes/state_cronjob/state_cronjob.go @@ -59,15 +59,11 @@ func NewCronJobMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) { return nil, fmt.Errorf("must be child of kubernetes module") } - 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(), @@ -86,10 +82,7 @@ func NewCronJobMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) { }, }, } - if config.AddMetadata { - ms.enricher = util.NewResourceMetadataEnricher( - base, &kubernetes.CronJob{}, false) - } + return &ms, nil } diff --git a/metricbeat/module/kubernetes/state_cronjob/state_cronjob_integration_test.go b/metricbeat/module/kubernetes/state_cronjob/state_cronjob_integration_test.go index aa3b003f06a5..68e567b8bea4 100644 --- a/metricbeat/module/kubernetes/state_cronjob/state_cronjob_integration_test.go +++ b/metricbeat/module/kubernetes/state_cronjob/state_cronjob_integration_test.go @@ -30,7 +30,7 @@ import ( ) func TestFetchMetricset(t *testing.T) { - config := test.GetKubeStateMetricsConfigWithMetaDisabled(t, "state_cronjob") + config := test.GetKubeStateMetricsConfig(t, "state_cronjob") metricSet := mbtest.NewFetcher(t, config) events, errs := metricSet.FetchEvents() if len(errs) > 0 { diff --git a/metricbeat/module/kubernetes/test/integration.go b/metricbeat/module/kubernetes/test/integration.go index ba7a7c0256bb..f2675b339bf9 100644 --- a/metricbeat/module/kubernetes/test/integration.go +++ b/metricbeat/module/kubernetes/test/integration.go @@ -49,18 +49,6 @@ func GetKubeStateMetricsConfig(t *testing.T, metricSetName string) map[string]in } } -// GetKubeStateMetricsConfigWithMetaDisabled function returns configuration for talking to kube-state-metrics. -func GetKubeStateMetricsConfigWithMetaDisabled(t *testing.T, metricSetName string) map[string]interface{} { - t.Helper() - return map[string]interface{}{ - "module": "kubernetes", - "metricsets": []string{metricSetName}, - "host": "${NODE_NAME}", - "hosts": []string{"kube-state-metrics:8080"}, - "add_metadata": false, - } -} - // GetKubeletConfig function returns configuration for talking to Kubelet API. func GetKubeletConfig(t *testing.T, metricSetName string) map[string]interface{} { t.Helper() diff --git a/metricbeat/modules.d/kubernetes.yml.disabled b/metricbeat/modules.d/kubernetes.yml.disabled index 11f6af1e983e..b45f6a7ac606 100644 --- a/metricbeat/modules.d/kubernetes.yml.disabled +++ b/metricbeat/modules.d/kubernetes.yml.disabled @@ -38,6 +38,7 @@ # include_labels: ["nodelabel2"] # include_annotations: ["nodeannotation1"] # deployment: false + # cronjob: false # Kubernetes client QPS and burst can be configured additionally #kube_client_options: # qps: 5 diff --git a/x-pack/metricbeat/metricbeat.reference.yml b/x-pack/metricbeat/metricbeat.reference.yml index 19479fb56f59..c0ef59b80202 100644 --- a/x-pack/metricbeat/metricbeat.reference.yml +++ b/x-pack/metricbeat/metricbeat.reference.yml @@ -898,6 +898,7 @@ metricbeat.modules: # include_labels: ["nodelabel2"] # include_annotations: ["nodeannotation1"] # deployment: false + # cronjob: false # Kubernetes client QPS and burst can be configured additionally #kube_client_options: # qps: 5 From 831a5a4cda1f8050f78353faed40d2b5c57f4e04 Mon Sep 17 00:00:00 2001 From: chrismark Date: Wed, 2 Mar 2022 08:57:19 +0000 Subject: [PATCH 02/17] Add changelog Signed-off-by: chrismark --- CHANGELOG.next.asciidoc | 1 + .../module/kubernetes/state_cronjob/_meta/docs.asciidoc | 7 +++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index bd82ecd0a21b..6a3bcb572c08 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -97,6 +97,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...main[Check the HEAD dif - Add metadata change support for some processors {pull}30183[30183] - Add support for non-unique Kafka headers for output messages. {pull}30369[30369] - Add action_input_type for the .fleet-actions-results {pull}30562[30562] +- Add cronjob metadata by default {pull}30637[30637] *Auditbeat* diff --git a/metricbeat/module/kubernetes/state_cronjob/_meta/docs.asciidoc b/metricbeat/module/kubernetes/state_cronjob/_meta/docs.asciidoc index d994fe5dced0..af589e49755b 100644 --- a/metricbeat/module/kubernetes/state_cronjob/_meta/docs.asciidoc +++ b/metricbeat/module/kubernetes/state_cronjob/_meta/docs.asciidoc @@ -1,6 +1,5 @@ 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. +This metricset adds metadata by default only for versions of k8s >= v1.21. +For older versions the APIs are not compatible and one need to configure the +metricset with `add_metadata: false` and remove the proper `apiGroup` in the `ClusterRole`. From e24689adfe36fe6495ea959f6d61d4817efa1a75 Mon Sep 17 00:00:00 2001 From: chrismark Date: Wed, 2 Mar 2022 09:42:57 +0000 Subject: [PATCH 03/17] fixes Signed-off-by: chrismark --- libbeat/common/kubernetes/metadata/pod.go | 2 +- .../kubernetes/state_cronjob/_meta/docs.asciidoc | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/libbeat/common/kubernetes/metadata/pod.go b/libbeat/common/kubernetes/metadata/pod.go index 4007bb84af70..23b6a113314b 100644 --- a/libbeat/common/kubernetes/metadata/pod.go +++ b/libbeat/common/kubernetes/metadata/pod.go @@ -87,7 +87,6 @@ func (p *pod) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common. out := p.resource.GenerateK8s("pod", obj, opts...) // check if Pod is handled by a ReplicaSet which is controlled by a Deployment - // TODO: same happens with CronJob vs Job. The hierarcy there is CronJob->Job->Pod if p.addResourceMetadata.Deployment { rsName, _ := out.GetValue("replicaset.name") if rsName, ok := rsName.(string); ok { @@ -98,6 +97,7 @@ func (p *pod) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common. } } + // check if Pod is handled by a Job which is controlled by a CronJob if p.addResourceMetadata.CronJob { jobName, _ := out.GetValue("job.name") if jobName, ok := jobName.(string); ok { diff --git a/metricbeat/module/kubernetes/state_cronjob/_meta/docs.asciidoc b/metricbeat/module/kubernetes/state_cronjob/_meta/docs.asciidoc index af589e49755b..da12473d43e5 100644 --- a/metricbeat/module/kubernetes/state_cronjob/_meta/docs.asciidoc +++ b/metricbeat/module/kubernetes/state_cronjob/_meta/docs.asciidoc @@ -2,4 +2,12 @@ This is the `state_cronjob` metricset of the Kubernetes module. This metricset adds metadata by default only for versions of k8s >= v1.21. For older versions the APIs are not compatible and one need to configure the -metricset with `add_metadata: false` and remove the proper `apiGroup` in the `ClusterRole`. +metricset with `add_metadata: false` and remove the proper `apiGroup` in the `ClusterRole`: + +["source","yaml",subs="attributes"] +------------------------------------------------------------------------------------- +- apiGroups: [ "batch" ] + resources: + - cronjobs +------------------------------------------------------------------------------------- + From 4bfd660373da1238e69c4b568cccf5b50b282352 Mon Sep 17 00:00:00 2001 From: chrismark Date: Wed, 2 Mar 2022 10:00:11 +0000 Subject: [PATCH 04/17] fmt Signed-off-by: chrismark --- libbeat/common/kubernetes/metadata/config.go | 4 ++-- libbeat/common/kubernetes/metadata/pod.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libbeat/common/kubernetes/metadata/config.go b/libbeat/common/kubernetes/metadata/config.go index 26f56d298021..b5c71312b955 100644 --- a/libbeat/common/kubernetes/metadata/config.go +++ b/libbeat/common/kubernetes/metadata/config.go @@ -35,7 +35,7 @@ type AddResourceMetadataConfig struct { Node *common.Config `config:"node"` Namespace *common.Config `config:"namespace"` Deployment bool `config:"deployment"` - CronJob bool `config:"cronjob"` + CronJob bool `config:"cronjob"` } // InitDefaults initializes the defaults for the config. @@ -57,6 +57,6 @@ func GetDefaultResourceMetadataConfig() *AddResourceMetadataConfig { Node: metaCfg, Namespace: metaCfg, Deployment: true, - CronJob: true, + CronJob: true, } } diff --git a/libbeat/common/kubernetes/metadata/pod.go b/libbeat/common/kubernetes/metadata/pod.go index 23b6a113314b..7ad49428fbcb 100644 --- a/libbeat/common/kubernetes/metadata/pod.go +++ b/libbeat/common/kubernetes/metadata/pod.go @@ -19,6 +19,7 @@ package metadata import ( "context" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" k8s "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/cache" @@ -108,7 +109,6 @@ func (p *pod) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common. } } - if p.node != nil { meta := p.node.GenerateFromName(po.Spec.NodeName, WithMetadata("node")) if meta != nil { From 41c3233cea80387603822be9e6a1ab789e44192c Mon Sep 17 00:00:00 2001 From: chrismark Date: Wed, 2 Mar 2022 12:36:53 +0000 Subject: [PATCH 05/17] test with latest version Signed-off-by: chrismark --- deploy/kubernetes/Jenkinsfile.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy/kubernetes/Jenkinsfile.yml b/deploy/kubernetes/Jenkinsfile.yml index 9638fd1163cf..c9a6cfb0b746 100644 --- a/deploy/kubernetes/Jenkinsfile.yml +++ b/deploy/kubernetes/Jenkinsfile.yml @@ -18,5 +18,5 @@ stages: make check-no-changes; stage: lint k8sTest: - k8sTest: "v1.23.0,v1.22.0,v1.21.1" + k8sTest: "v1.23.0" stage: mandatory From 12239b63ccbbda7e4497f8823f2aa463a261d656 Mon Sep 17 00:00:00 2001 From: chrismark Date: Thu, 3 Mar 2022 14:53:23 +0000 Subject: [PATCH 06/17] Fixes Signed-off-by: chrismark --- deploy/kubernetes/Jenkinsfile.yml | 2 +- metricbeat/module/kubernetes/kubernetes.yml | 205 ++++++++++++++++---- 2 files changed, 171 insertions(+), 36 deletions(-) diff --git a/deploy/kubernetes/Jenkinsfile.yml b/deploy/kubernetes/Jenkinsfile.yml index c9a6cfb0b746..9638fd1163cf 100644 --- a/deploy/kubernetes/Jenkinsfile.yml +++ b/deploy/kubernetes/Jenkinsfile.yml @@ -18,5 +18,5 @@ stages: make check-no-changes; stage: lint k8sTest: - k8sTest: "v1.23.0" + k8sTest: "v1.23.0,v1.22.0,v1.21.1" stage: mandatory diff --git a/metricbeat/module/kubernetes/kubernetes.yml b/metricbeat/module/kubernetes/kubernetes.yml index d87cb4f5f9bc..dcf9a26b1c45 100644 --- a/metricbeat/module/kubernetes/kubernetes.yml +++ b/metricbeat/module/kubernetes/kubernetes.yml @@ -1,79 +1,214 @@ apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: + labels: + app.kubernetes.io/component: exporter + app.kubernetes.io/name: kube-state-metrics + app.kubernetes.io/version: 2.3.0 name: kube-state-metrics rules: -- apiGroups: ["*"] - resources: ["*"] - verbs: ["*"] + - apiGroups: + - "" + resources: + - configmaps + - secrets + - nodes + - pods + - services + - resourcequotas + - replicationcontrollers + - limitranges + - persistentvolumeclaims + - persistentvolumes + - namespaces + - endpoints + verbs: + - list + - watch + - apiGroups: + - apps + resources: + - statefulsets + - daemonsets + - deployments + - replicasets + verbs: + - list + - watch + - apiGroups: + - batch + resources: + - cronjobs + - jobs + verbs: + - list + - watch + - apiGroups: + - autoscaling + resources: + - horizontalpodautoscalers + verbs: + - list + - watch + - apiGroups: + - authentication.k8s.io + resources: + - tokenreviews + verbs: + - create + - apiGroups: + - authorization.k8s.io + resources: + - subjectaccessreviews + verbs: + - create + - apiGroups: + - policy + resources: + - poddisruptionbudgets + verbs: + - list + - watch + - apiGroups: + - certificates.k8s.io + resources: + - certificatesigningrequests + verbs: + - list + - watch + - apiGroups: + - storage.k8s.io + resources: + - storageclasses + - volumeattachments + verbs: + - list + - watch + - apiGroups: + - admissionregistration.k8s.io + resources: + - mutatingwebhookconfigurations + - validatingwebhookconfigurations + verbs: + - list + - watch + - apiGroups: + - networking.k8s.io + resources: + - networkpolicies + - ingresses + verbs: + - list + - watch + - apiGroups: + - coordination.k8s.io + resources: + - leases + verbs: + - list + - watch --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: + labels: + app.kubernetes.io/component: exporter + app.kubernetes.io/name: kube-state-metrics + app.kubernetes.io/version: 2.3.0 name: kube-state-metrics roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: kube-state-metrics subjects: -- kind: ServiceAccount - name: kube-state-metrics - namespace: default + - kind: ServiceAccount + name: kube-state-metrics + namespace: kube-system --- apiVersion: v1 +automountServiceAccountToken: false kind: ServiceAccount metadata: + labels: + app.kubernetes.io/component: exporter + app.kubernetes.io/name: kube-state-metrics + app.kubernetes.io/version: 2.3.0 name: kube-state-metrics + namespace: kube-system --- apiVersion: apps/v1 kind: Deployment metadata: - name: kube-state-metrics labels: - app: kube-state-metrics + app.kubernetes.io/component: exporter + app.kubernetes.io/name: kube-state-metrics + app.kubernetes.io/version: 2.3.0 + name: kube-state-metrics + namespace: kube-system spec: replicas: 1 selector: matchLabels: - app: kube-state-metrics + app.kubernetes.io/name: kube-state-metrics template: metadata: labels: - app: kube-state-metrics + app.kubernetes.io/component: exporter + app.kubernetes.io/name: kube-state-metrics + app.kubernetes.io/version: 2.3.0 spec: + automountServiceAccountToken: true containers: - - name: kube-state-metrics - image: quay.io/coreos/kube-state-metrics:v1.8.0 - livenessProbe: - httpGet: - path: /healthz - port: 8080 - initialDelaySeconds: 5 - timeoutSeconds: 5 - ports: - - containerPort: 8080 - name: http-metrics - - containerPort: 8081 - name: telemetry - readinessProbe: - httpGet: - path: / - port: 8081 - initialDelaySeconds: 5 - timeoutSeconds: 5 + - image: k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.3.0 + livenessProbe: + httpGet: + path: /healthz + port: 8080 + initialDelaySeconds: 5 + timeoutSeconds: 5 + name: kube-state-metrics + ports: + - containerPort: 8080 + name: http-metrics + - containerPort: 8081 + name: telemetry + readinessProbe: + httpGet: + path: / + port: 8081 + initialDelaySeconds: 5 + timeoutSeconds: 5 + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + readOnlyRootFilesystem: true + runAsUser: 65534 + nodeSelector: + kubernetes.io/os: linux serviceAccountName: kube-state-metrics --- apiVersion: v1 kind: Service metadata: + labels: + app.kubernetes.io/component: exporter + app.kubernetes.io/name: kube-state-metrics + app.kubernetes.io/version: 2.3.0 name: kube-state-metrics + namespace: kube-system spec: - type: ClusterIP - selector: - app: kube-state-metrics + clusterIP: None ports: - - port: 8080 - targetPort: 8080 + - name: http-metrics + port: 8080 + targetPort: http-metrics + - name: telemetry + port: 8081 + targetPort: telemetry + selector: + app.kubernetes.io/name: kube-state-metrics --- apiVersion: apps/v1 kind: StatefulSet @@ -108,7 +243,7 @@ spec: requests: storage: 1Mi --- -apiVersion: batch/v1beta1 +apiVersion: batch/v1 kind: CronJob metadata: name: basic-cronjob From 7d9d9f6decd4040fafa59dae4d99cade04559937 Mon Sep 17 00:00:00 2001 From: chrismark Date: Thu, 3 Mar 2022 15:53:36 +0000 Subject: [PATCH 07/17] Try with older ksm Signed-off-by: chrismark --- metricbeat/module/kubernetes/kubernetes.yml | 203 ++++---------------- 1 file changed, 34 insertions(+), 169 deletions(-) diff --git a/metricbeat/module/kubernetes/kubernetes.yml b/metricbeat/module/kubernetes/kubernetes.yml index dcf9a26b1c45..7c4ffb58ae98 100644 --- a/metricbeat/module/kubernetes/kubernetes.yml +++ b/metricbeat/module/kubernetes/kubernetes.yml @@ -1,214 +1,79 @@ apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: - labels: - app.kubernetes.io/component: exporter - app.kubernetes.io/name: kube-state-metrics - app.kubernetes.io/version: 2.3.0 name: kube-state-metrics rules: - - apiGroups: - - "" - resources: - - configmaps - - secrets - - nodes - - pods - - services - - resourcequotas - - replicationcontrollers - - limitranges - - persistentvolumeclaims - - persistentvolumes - - namespaces - - endpoints - verbs: - - list - - watch - - apiGroups: - - apps - resources: - - statefulsets - - daemonsets - - deployments - - replicasets - verbs: - - list - - watch - - apiGroups: - - batch - resources: - - cronjobs - - jobs - verbs: - - list - - watch - - apiGroups: - - autoscaling - resources: - - horizontalpodautoscalers - verbs: - - list - - watch - - apiGroups: - - authentication.k8s.io - resources: - - tokenreviews - verbs: - - create - - apiGroups: - - authorization.k8s.io - resources: - - subjectaccessreviews - verbs: - - create - - apiGroups: - - policy - resources: - - poddisruptionbudgets - verbs: - - list - - watch - - apiGroups: - - certificates.k8s.io - resources: - - certificatesigningrequests - verbs: - - list - - watch - - apiGroups: - - storage.k8s.io - resources: - - storageclasses - - volumeattachments - verbs: - - list - - watch - - apiGroups: - - admissionregistration.k8s.io - resources: - - mutatingwebhookconfigurations - - validatingwebhookconfigurations - verbs: - - list - - watch - - apiGroups: - - networking.k8s.io - resources: - - networkpolicies - - ingresses - verbs: - - list - - watch - - apiGroups: - - coordination.k8s.io - resources: - - leases - verbs: - - list - - watch +- apiGroups: ["*"] + resources: ["*"] + verbs: ["*"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: - labels: - app.kubernetes.io/component: exporter - app.kubernetes.io/name: kube-state-metrics - app.kubernetes.io/version: 2.3.0 name: kube-state-metrics roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: kube-state-metrics subjects: - - kind: ServiceAccount - name: kube-state-metrics - namespace: kube-system +- kind: ServiceAccount + name: kube-state-metrics + namespace: default --- apiVersion: v1 -automountServiceAccountToken: false kind: ServiceAccount metadata: - labels: - app.kubernetes.io/component: exporter - app.kubernetes.io/name: kube-state-metrics - app.kubernetes.io/version: 2.3.0 name: kube-state-metrics - namespace: kube-system --- apiVersion: apps/v1 kind: Deployment metadata: - labels: - app.kubernetes.io/component: exporter - app.kubernetes.io/name: kube-state-metrics - app.kubernetes.io/version: 2.3.0 name: kube-state-metrics - namespace: kube-system + labels: + app: kube-state-metrics spec: replicas: 1 selector: matchLabels: - app.kubernetes.io/name: kube-state-metrics + app: kube-state-metrics template: metadata: labels: - app.kubernetes.io/component: exporter - app.kubernetes.io/name: kube-state-metrics - app.kubernetes.io/version: 2.3.0 + app: kube-state-metrics spec: - automountServiceAccountToken: true containers: - - image: k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.3.0 - livenessProbe: - httpGet: - path: /healthz - port: 8080 - initialDelaySeconds: 5 - timeoutSeconds: 5 - name: kube-state-metrics - ports: - - containerPort: 8080 - name: http-metrics - - containerPort: 8081 - name: telemetry - readinessProbe: - httpGet: - path: / - port: 8081 - initialDelaySeconds: 5 - timeoutSeconds: 5 - securityContext: - allowPrivilegeEscalation: false - capabilities: - drop: - - ALL - readOnlyRootFilesystem: true - runAsUser: 65534 - nodeSelector: - kubernetes.io/os: linux + - name: kube-state-metrics + image: quay.io/coreos/kube-state-metrics:v1.8.0 + livenessProbe: + httpGet: + path: /healthz + port: 8080 + initialDelaySeconds: 5 + timeoutSeconds: 5 + ports: + - containerPort: 8080 + name: http-metrics + - containerPort: 8081 + name: telemetry + readinessProbe: + httpGet: + path: / + port: 8081 + initialDelaySeconds: 5 + timeoutSeconds: 5 serviceAccountName: kube-state-metrics --- apiVersion: v1 kind: Service metadata: - labels: - app.kubernetes.io/component: exporter - app.kubernetes.io/name: kube-state-metrics - app.kubernetes.io/version: 2.3.0 name: kube-state-metrics - namespace: kube-system spec: - clusterIP: None - ports: - - name: http-metrics - port: 8080 - targetPort: http-metrics - - name: telemetry - port: 8081 - targetPort: telemetry + type: ClusterIP selector: - app.kubernetes.io/name: kube-state-metrics + app: kube-state-metrics + ports: + - port: 8080 + targetPort: 8080 --- apiVersion: apps/v1 kind: StatefulSet From 792d4c947b5161fd13206240b805b9b76908e569 Mon Sep 17 00:00:00 2001 From: chrismark Date: Thu, 3 Mar 2022 17:04:03 +0000 Subject: [PATCH 08/17] Change cronjob Signed-off-by: chrismark --- metricbeat/module/kubernetes/kubernetes.yml | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/metricbeat/module/kubernetes/kubernetes.yml b/metricbeat/module/kubernetes/kubernetes.yml index 7c4ffb58ae98..b9bada9eb474 100644 --- a/metricbeat/module/kubernetes/kubernetes.yml +++ b/metricbeat/module/kubernetes/kubernetes.yml @@ -111,20 +111,24 @@ spec: apiVersion: batch/v1 kind: CronJob metadata: - name: basic-cronjob + name: hello + labels: + k8s-app: myjob spec: - schedule: "* * * * *" + schedule: "*/1 * * * *" jobTemplate: spec: template: - metadata: - name: basic-job spec: containers: - - name: hello - image: alpine:3 - command: ["sh", "-c", "echo Hello!"] - restartPolicy: Never + - name: hello + image: busybox + imagePullPolicy: IfNotPresent + command: + - /bin/sh + - -c + - date; echo Hello from the Kubernetes cluster + restartPolicy: never --- apiVersion: v1 kind: ResourceQuota From 30f27a33f029415ae85523854d5a48b4d462bd9f Mon Sep 17 00:00:00 2001 From: chrismark Date: Fri, 4 Mar 2022 09:30:17 +0000 Subject: [PATCH 09/17] Add role for jobs Signed-off-by: chrismark --- deploy/kubernetes/filebeat-kubernetes.yaml | 4 ++++ deploy/kubernetes/filebeat/filebeat-role.yaml | 4 ++++ deploy/kubernetes/heartbeat-kubernetes.yaml | 4 ++++ deploy/kubernetes/heartbeat/heartbeat-role.yaml | 4 ++++ 4 files changed, 16 insertions(+) diff --git a/deploy/kubernetes/filebeat-kubernetes.yaml b/deploy/kubernetes/filebeat-kubernetes.yaml index cc7881a8e9fc..381bbea5d027 100644 --- a/deploy/kubernetes/filebeat-kubernetes.yaml +++ b/deploy/kubernetes/filebeat-kubernetes.yaml @@ -188,6 +188,10 @@ rules: resources: - replicasets verbs: ["get", "list", "watch"] +- apiGroups: ["batch"] + resources: + - jobs + verbs: ["get", "list", "watch"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: Role diff --git a/deploy/kubernetes/filebeat/filebeat-role.yaml b/deploy/kubernetes/filebeat/filebeat-role.yaml index f5d519d273ae..0d0a724974f4 100644 --- a/deploy/kubernetes/filebeat/filebeat-role.yaml +++ b/deploy/kubernetes/filebeat/filebeat-role.yaml @@ -18,6 +18,10 @@ rules: resources: - replicasets verbs: ["get", "list", "watch"] +- apiGroups: ["batch"] + resources: + - jobs + verbs: ["get", "list", "watch"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: Role diff --git a/deploy/kubernetes/heartbeat-kubernetes.yaml b/deploy/kubernetes/heartbeat-kubernetes.yaml index 5d8d21ecd0c5..f4aac378672f 100644 --- a/deploy/kubernetes/heartbeat-kubernetes.yaml +++ b/deploy/kubernetes/heartbeat-kubernetes.yaml @@ -185,6 +185,10 @@ rules: resources: - replicasets verbs: ["get", "list", "watch"] +- apiGroups: ["batch"] + resources: + - jobs + verbs: ["get", "list", "watch"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: Role diff --git a/deploy/kubernetes/heartbeat/heartbeat-role.yaml b/deploy/kubernetes/heartbeat/heartbeat-role.yaml index fc71ee76a3f6..9b9829ede117 100644 --- a/deploy/kubernetes/heartbeat/heartbeat-role.yaml +++ b/deploy/kubernetes/heartbeat/heartbeat-role.yaml @@ -16,6 +16,10 @@ rules: resources: - replicasets verbs: ["get", "list", "watch"] +- apiGroups: ["batch"] + resources: + - jobs + verbs: ["get", "list", "watch"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: Role From 4402ee7a98e5d45fd5317660d127b00d909370a4 Mon Sep 17 00:00:00 2001 From: chrismark Date: Fri, 4 Mar 2022 09:38:40 +0000 Subject: [PATCH 10/17] Fix leftover Signed-off-by: chrismark --- .../kubernetes/_meta/test/docs/02_objects/cronjob.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/metricbeat/module/kubernetes/_meta/test/docs/02_objects/cronjob.yaml b/metricbeat/module/kubernetes/_meta/test/docs/02_objects/cronjob.yaml index eba346cf155d..f724cef28a8b 100644 --- a/metricbeat/module/kubernetes/_meta/test/docs/02_objects/cronjob.yaml +++ b/metricbeat/module/kubernetes/_meta/test/docs/02_objects/cronjob.yaml @@ -1,7 +1,7 @@ # Cronjob that will execute each minute. # It will print a message and sleep (reporting being active) for 5 seconds -apiVersion: batch/v1beta1 +apiVersion: batch/v1 kind: CronJob metadata: name: mycronjob @@ -16,7 +16,7 @@ spec: image: alpine imagePullPolicy: IfNotPresent command: ['sh', '-c', 'echo elastic world ; sleep 5'] - + restartPolicy: OnFailure terminationGracePeriodSeconds: 0 - concurrencyPolicy: Allow \ No newline at end of file + concurrencyPolicy: Allow From e230b9880c68151533400f284b91620d22d6a7aa Mon Sep 17 00:00:00 2001 From: chrismark Date: Tue, 8 Mar 2022 11:39:49 +0000 Subject: [PATCH 11/17] Skip metadata for state_cronjob tests Signed-off-by: chrismark --- libbeat/common/kubernetes/metadata/pod.go | 4 ++-- metricbeat/module/kubernetes/kubernetes.yml | 2 +- .../state_cronjob/state_cronjob_integration_test.go | 2 +- metricbeat/module/kubernetes/test/integration.go | 12 ++++++++++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/libbeat/common/kubernetes/metadata/pod.go b/libbeat/common/kubernetes/metadata/pod.go index 7ad49428fbcb..23032bb7ff03 100644 --- a/libbeat/common/kubernetes/metadata/pod.go +++ b/libbeat/common/kubernetes/metadata/pod.go @@ -173,8 +173,8 @@ func (p *pod) getRSDeployment(rsName string, ns string) string { return "" } -//// getCronjobOfJob return the name of the Cronjob object that -//// owns the Job with the given name under the given Namespace +// getCronjobOfJob return the name of the Cronjob object that +// owns the Job with the given name under the given Namespace func (p *pod) getCronjobOfJob(jobName string, ns string) string { if p.client == nil { return "" diff --git a/metricbeat/module/kubernetes/kubernetes.yml b/metricbeat/module/kubernetes/kubernetes.yml index b9bada9eb474..1aa7e92e6acc 100644 --- a/metricbeat/module/kubernetes/kubernetes.yml +++ b/metricbeat/module/kubernetes/kubernetes.yml @@ -108,7 +108,7 @@ spec: requests: storage: 1Mi --- -apiVersion: batch/v1 +apiVersion: batch/v1beta1 kind: CronJob metadata: name: hello diff --git a/metricbeat/module/kubernetes/state_cronjob/state_cronjob_integration_test.go b/metricbeat/module/kubernetes/state_cronjob/state_cronjob_integration_test.go index 68e567b8bea4..aa3b003f06a5 100644 --- a/metricbeat/module/kubernetes/state_cronjob/state_cronjob_integration_test.go +++ b/metricbeat/module/kubernetes/state_cronjob/state_cronjob_integration_test.go @@ -30,7 +30,7 @@ import ( ) func TestFetchMetricset(t *testing.T) { - config := test.GetKubeStateMetricsConfig(t, "state_cronjob") + config := test.GetKubeStateMetricsConfigWithMetaDisabled(t, "state_cronjob") metricSet := mbtest.NewFetcher(t, config) events, errs := metricSet.FetchEvents() if len(errs) > 0 { diff --git a/metricbeat/module/kubernetes/test/integration.go b/metricbeat/module/kubernetes/test/integration.go index f2675b339bf9..ba7a7c0256bb 100644 --- a/metricbeat/module/kubernetes/test/integration.go +++ b/metricbeat/module/kubernetes/test/integration.go @@ -49,6 +49,18 @@ func GetKubeStateMetricsConfig(t *testing.T, metricSetName string) map[string]in } } +// GetKubeStateMetricsConfigWithMetaDisabled function returns configuration for talking to kube-state-metrics. +func GetKubeStateMetricsConfigWithMetaDisabled(t *testing.T, metricSetName string) map[string]interface{} { + t.Helper() + return map[string]interface{}{ + "module": "kubernetes", + "metricsets": []string{metricSetName}, + "host": "${NODE_NAME}", + "hosts": []string{"kube-state-metrics:8080"}, + "add_metadata": false, + } +} + // GetKubeletConfig function returns configuration for talking to Kubelet API. func GetKubeletConfig(t *testing.T, metricSetName string) map[string]interface{} { t.Helper() From 283c16bee052cabbbd67199f974600f83318050b Mon Sep 17 00:00:00 2001 From: chrismark Date: Tue, 8 Mar 2022 11:44:35 +0000 Subject: [PATCH 12/17] leftover Signed-off-by: chrismark --- .../module/kubernetes/_meta/test/docs/02_objects/cronjob.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metricbeat/module/kubernetes/_meta/test/docs/02_objects/cronjob.yaml b/metricbeat/module/kubernetes/_meta/test/docs/02_objects/cronjob.yaml index f724cef28a8b..3cd85e69668c 100644 --- a/metricbeat/module/kubernetes/_meta/test/docs/02_objects/cronjob.yaml +++ b/metricbeat/module/kubernetes/_meta/test/docs/02_objects/cronjob.yaml @@ -1,7 +1,7 @@ # Cronjob that will execute each minute. # It will print a message and sleep (reporting being active) for 5 seconds -apiVersion: batch/v1 +apiVersion: batch/v1beta1 kind: CronJob metadata: name: mycronjob From 93c46349be8411a8995bcba2a7cd43f63a55b850 Mon Sep 17 00:00:00 2001 From: chrismark Date: Tue, 8 Mar 2022 13:01:33 +0000 Subject: [PATCH 13/17] Init enricher only when metadata are defined Signed-off-by: chrismark --- .../module/kubernetes/state_cronjob/state_cronjob.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/metricbeat/module/kubernetes/state_cronjob/state_cronjob.go b/metricbeat/module/kubernetes/state_cronjob/state_cronjob.go index 49d191580fdc..d5a8f202670f 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") } + 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(), @@ -82,6 +86,10 @@ func NewCronJobMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) { }, }, } + if config.AddMetadata { + ms.enricher = util.NewResourceMetadataEnricher( + base, &kubernetes.CronJob{}, false) + } return &ms, nil } From 0cc1db7d588b1502c608b24be97373d06f40c05e Mon Sep 17 00:00:00 2001 From: chrismark Date: Tue, 8 Mar 2022 14:03:55 +0000 Subject: [PATCH 14/17] Try to fix tests Signed-off-by: chrismark --- libbeat/common/kubernetes/metadata/pod.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/libbeat/common/kubernetes/metadata/pod.go b/libbeat/common/kubernetes/metadata/pod.go index 23032bb7ff03..37a4989febca 100644 --- a/libbeat/common/kubernetes/metadata/pod.go +++ b/libbeat/common/kubernetes/metadata/pod.go @@ -99,15 +99,15 @@ func (p *pod) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common. } // check if Pod is handled by a Job which is controlled by a CronJob - if p.addResourceMetadata.CronJob { - jobName, _ := out.GetValue("job.name") - if jobName, ok := jobName.(string); ok { - dep := p.getCronjobOfJob(jobName, po.GetNamespace()) - if dep != "" { - out.Put("cronjob.name", dep) - } - } - } + //if p.addResourceMetadata.CronJob { + // jobName, _ := out.GetValue("job.name") + // if jobName, ok := jobName.(string); ok { + // dep := p.getCronjobOfJob(jobName, po.GetNamespace()) + // if dep != "" { + // out.Put("cronjob.name", dep) + // } + // } + //} if p.node != nil { meta := p.node.GenerateFromName(po.Spec.NodeName, WithMetadata("node")) From 684d13a94d06d846b8acc13bece0b6c0e3973b6c Mon Sep 17 00:00:00 2001 From: chrismark Date: Wed, 9 Mar 2022 19:00:37 +0000 Subject: [PATCH 15/17] Bring changes back Signed-off-by: chrismark --- libbeat/common/kubernetes/metadata/pod.go | 18 +++++++++--------- .../kubernetes/state_cronjob/state_cronjob.go | 10 +--------- .../state_cronjob_integration_test.go | 2 +- .../module/kubernetes/test/integration.go | 12 ------------ 4 files changed, 11 insertions(+), 31 deletions(-) diff --git a/libbeat/common/kubernetes/metadata/pod.go b/libbeat/common/kubernetes/metadata/pod.go index 37a4989febca..23032bb7ff03 100644 --- a/libbeat/common/kubernetes/metadata/pod.go +++ b/libbeat/common/kubernetes/metadata/pod.go @@ -99,15 +99,15 @@ func (p *pod) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common. } // check if Pod is handled by a Job which is controlled by a CronJob - //if p.addResourceMetadata.CronJob { - // jobName, _ := out.GetValue("job.name") - // if jobName, ok := jobName.(string); ok { - // dep := p.getCronjobOfJob(jobName, po.GetNamespace()) - // if dep != "" { - // out.Put("cronjob.name", dep) - // } - // } - //} + if p.addResourceMetadata.CronJob { + jobName, _ := out.GetValue("job.name") + if jobName, ok := jobName.(string); ok { + dep := p.getCronjobOfJob(jobName, po.GetNamespace()) + if dep != "" { + out.Put("cronjob.name", dep) + } + } + } if p.node != nil { meta := p.node.GenerateFromName(po.Spec.NodeName, WithMetadata("node")) diff --git a/metricbeat/module/kubernetes/state_cronjob/state_cronjob.go b/metricbeat/module/kubernetes/state_cronjob/state_cronjob.go index d5a8f202670f..49d191580fdc 100644 --- a/metricbeat/module/kubernetes/state_cronjob/state_cronjob.go +++ b/metricbeat/module/kubernetes/state_cronjob/state_cronjob.go @@ -59,15 +59,11 @@ func NewCronJobMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) { return nil, fmt.Errorf("must be child of kubernetes module") } - 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(), @@ -86,10 +82,6 @@ func NewCronJobMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) { }, }, } - if config.AddMetadata { - ms.enricher = util.NewResourceMetadataEnricher( - base, &kubernetes.CronJob{}, false) - } return &ms, nil } diff --git a/metricbeat/module/kubernetes/state_cronjob/state_cronjob_integration_test.go b/metricbeat/module/kubernetes/state_cronjob/state_cronjob_integration_test.go index aa3b003f06a5..68e567b8bea4 100644 --- a/metricbeat/module/kubernetes/state_cronjob/state_cronjob_integration_test.go +++ b/metricbeat/module/kubernetes/state_cronjob/state_cronjob_integration_test.go @@ -30,7 +30,7 @@ import ( ) func TestFetchMetricset(t *testing.T) { - config := test.GetKubeStateMetricsConfigWithMetaDisabled(t, "state_cronjob") + config := test.GetKubeStateMetricsConfig(t, "state_cronjob") metricSet := mbtest.NewFetcher(t, config) events, errs := metricSet.FetchEvents() if len(errs) > 0 { diff --git a/metricbeat/module/kubernetes/test/integration.go b/metricbeat/module/kubernetes/test/integration.go index ba7a7c0256bb..f2675b339bf9 100644 --- a/metricbeat/module/kubernetes/test/integration.go +++ b/metricbeat/module/kubernetes/test/integration.go @@ -49,18 +49,6 @@ func GetKubeStateMetricsConfig(t *testing.T, metricSetName string) map[string]in } } -// GetKubeStateMetricsConfigWithMetaDisabled function returns configuration for talking to kube-state-metrics. -func GetKubeStateMetricsConfigWithMetaDisabled(t *testing.T, metricSetName string) map[string]interface{} { - t.Helper() - return map[string]interface{}{ - "module": "kubernetes", - "metricsets": []string{metricSetName}, - "host": "${NODE_NAME}", - "hosts": []string{"kube-state-metrics:8080"}, - "add_metadata": false, - } -} - // GetKubeletConfig function returns configuration for talking to Kubelet API. func GetKubeletConfig(t *testing.T, metricSetName string) map[string]interface{} { t.Helper() From 059c3e754f2296e3bbeced651b32e1f3b498d9a8 Mon Sep 17 00:00:00 2001 From: chrismark Date: Wed, 9 Mar 2022 19:03:04 +0000 Subject: [PATCH 16/17] Improve docs Signed-off-by: chrismark --- libbeat/docs/shared-autodiscover.asciidoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libbeat/docs/shared-autodiscover.asciidoc b/libbeat/docs/shared-autodiscover.asciidoc index 3467e06b3c32..b2896422b5e9 100644 --- a/libbeat/docs/shared-autodiscover.asciidoc +++ b/libbeat/docs/shared-autodiscover.asciidoc @@ -178,9 +178,9 @@ endif::[] while annotations are not added by default. This settings are useful when labels' and annotations' storing requires special handling to avoid overloading the storage output. The enrichment of `node` or `namespace` metadata can be individually disabled by setting `enabled: false`. If resource is `pod` and it is created from a `deployment`, by default - the deployment name is added, this can be disabled by set to `false`. + the deployment name is added, this can be disabled by setting `deployment: false`. If resource is `pod` and it is created from a `cronjob`, by default - the cronjob name is added, this can be disabled by set to `false`. + the cronjob name is added, this can be disabled by setting `cronjob: false`. Example: ["source","yaml",subs="attributes"] From cdafb68364b16087e0725171109fd9cdc641af2c Mon Sep 17 00:00:00 2001 From: chrismark Date: Wed, 9 Mar 2022 20:16:31 +0000 Subject: [PATCH 17/17] Fix Cronjob definition Signed-off-by: chrismark --- metricbeat/module/kubernetes/kubernetes.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metricbeat/module/kubernetes/kubernetes.yml b/metricbeat/module/kubernetes/kubernetes.yml index b9bada9eb474..e7c232fe4682 100644 --- a/metricbeat/module/kubernetes/kubernetes.yml +++ b/metricbeat/module/kubernetes/kubernetes.yml @@ -128,7 +128,7 @@ spec: - /bin/sh - -c - date; echo Hello from the Kubernetes cluster - restartPolicy: never + restartPolicy: Never --- apiVersion: v1 kind: ResourceQuota