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

Add cronjob metadata by default #30637

Merged
merged 19 commits into from Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Expand Up @@ -88,6 +88,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...main[Check the HEAD dif
- Add FIPS configuration option for all AWS API calls. {pull}[28899]
- 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*

Expand Down
3 changes: 1 addition & 2 deletions deploy/kubernetes/elastic-agent-managed-kubernetes.yaml
Expand Up @@ -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:
Expand Down
Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions deploy/kubernetes/elastic-agent-standalone-kubernetes.yaml
Expand Up @@ -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:
- ""
Expand Down
Expand Up @@ -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:
- ""
Expand Down
4 changes: 4 additions & 0 deletions deploy/kubernetes/filebeat-kubernetes.yaml
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions deploy/kubernetes/filebeat/filebeat-role.yaml
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions deploy/kubernetes/heartbeat-kubernetes.yaml
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions deploy/kubernetes/heartbeat/heartbeat-role.yaml
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions deploy/kubernetes/metricbeat-kubernetes.yaml
Expand Up @@ -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:
- ""
Expand Down
3 changes: 1 addition & 2 deletions deploy/kubernetes/metricbeat/metricbeat-role.yaml
Expand Up @@ -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:
- ""
Expand Down
2 changes: 2 additions & 0 deletions libbeat/common/kubernetes/metadata/config.go
Expand Up @@ -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.
Expand All @@ -56,5 +57,6 @@ func GetDefaultResourceMetadataConfig() *AddResourceMetadataConfig {
Node: metaCfg,
Namespace: metaCfg,
Deployment: true,
CronJob: true,
}
}
33 changes: 32 additions & 1 deletion libbeat/common/kubernetes/metadata/pod.go
Expand Up @@ -88,7 +88,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 {
Expand All @@ -99,6 +98,17 @@ func (p *pod) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common.
}
}

// check if Pod is handled by a Job which is controlled by a CronJob
cmacknz marked this conversation as resolved.
Show resolved Hide resolved
//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 {
Expand Down Expand Up @@ -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 ""
}
3 changes: 3 additions & 0 deletions libbeat/docs/shared-autodiscover.asciidoc
Expand Up @@ -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`.
cmacknz marked this conversation as resolved.
Show resolved Hide resolved
Example:

["source","yaml",subs="attributes"]
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions metricbeat/docs/modules/kubernetes.asciidoc
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions metricbeat/metricbeat.reference.yml
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions metricbeat/module/kubernetes/_meta/config.reference.yml
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions metricbeat/module/kubernetes/_meta/config.yml
Expand Up @@ -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
Expand Down
Expand Up @@ -16,7 +16,7 @@ spec:
image: alpine
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo elastic world ; sleep 5']

restartPolicy: OnFailure
terminationGracePeriodSeconds: 0
concurrencyPolicy: Allow
concurrencyPolicy: Allow
20 changes: 12 additions & 8 deletions metricbeat/module/kubernetes/kubernetes.yml
Expand Up @@ -111,20 +111,24 @@ spec:
apiVersion: batch/v1beta1
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
Expand Down
15 changes: 11 additions & 4 deletions metricbeat/module/kubernetes/state_cronjob/_meta/docs.asciidoc
@@ -1,6 +1,13 @@
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`:

["source","yaml",subs="attributes"]
-------------------------------------------------------------------------------------
- apiGroups: [ "batch" ]
resources:
- cronjobs
-------------------------------------------------------------------------------------

Expand Up @@ -90,6 +90,7 @@ func NewCronJobMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) {
ms.enricher = util.NewResourceMetadataEnricher(
base, &kubernetes.CronJob{}, false)
}

return &ms, nil
}

Expand Down
1 change: 1 addition & 0 deletions metricbeat/modules.d/kubernetes.yml.disabled
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions x-pack/metricbeat/metricbeat.reference.yml
Expand Up @@ -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
Expand Down