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

[Kubernetes - Metricbeat] Providing config option to disable Kubeadm config api requests #40086

Merged
merged 29 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
56ca696
updating with config_kubeAdm
gizas Jul 1, 2024
61f848a
updating with config_kubeAdm and setbool option
gizas Jul 2, 2024
acac6ee
updating with events module
gizas Jul 3, 2024
bc3ff53
updating metricbeat.yaml manifest with disable_kubeadm config option
gizas Jul 3, 2024
60292a4
adding changelog line
gizas Jul 3, 2024
d817d1f
Merge branch 'main' of github.com:elastic/beats into kubeadm
gizas Jul 3, 2024
feef113
fixing host in metricbeat.yaml file
gizas Jul 3, 2024
ccb775f
changing variable to use_kubeadm
gizas Jul 19, 2024
499957e
Merge branch 'main' into kubeadm
gizas Jul 19, 2024
3f7fbea
udpating go.mod files
gizas Aug 13, 2024
50d82f7
udpating go.mod files
gizas Aug 13, 2024
09588b0
udpating go.mod files
gizas Aug 13, 2024
85a8695
udpating go.mod files
gizas Aug 13, 2024
3157196
udpating changelog file
gizas Aug 13, 2024
0649650
udpating NOTICE.txt
gizas Aug 13, 2024
e0c227d
udpating tests
gizas Aug 13, 2024
c5262da
Merge branch 'main' into kubeadm
gizas Aug 14, 2024
2fe631e
merging with main
gizas Aug 28, 2024
4583828
Merge branch 'kubeadm' of github.com:elastic/beats into kubeadm
gizas Aug 28, 2024
ffeabd2
removing kubeadm option and use only commonconfig
gizas Sep 2, 2024
64667fe
fixing typos
gizas Sep 2, 2024
5ef848a
fixing reference from central
gizas Sep 2, 2024
8bbe628
fixing newlines
gizas Sep 2, 2024
e3423ad
Merge branch 'main' of github.com:elastic/beats into kubeadm
gizas Sep 2, 2024
20936d6
fixing references to container registry as well
gizas Sep 3, 2024
e2fc92b
adding use_kubeadm in kubernetes config
gizas Sep 6, 2024
9b29bab
reverting change in kubernetes test
gizas Sep 6, 2024
f60ca44
Merge branch 'main' into kubeadm
gizas Sep 6, 2024
b461b38
Merge branch 'main' into kubeadm
gizas Sep 9, 2024
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: 1 addition & 1 deletion CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
- Add new memory/cgroup metrics to Kibana module {pull}37232[37232]
- Add SSL support to mysql module {pull}37997[37997]
- Add SSL support for aerospike module {pull}38126[38126]

- Add `use_kubeadm` config option in kubernetes module in order to toggle kubeadm-config api requests {pull}40086[40086]

*Metricbeat*

Expand Down
4 changes: 2 additions & 2 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12587,11 +12587,11 @@ various licenses:

--------------------------------------------------------------------------------
Dependency : github.com/elastic/elastic-agent-autodiscover
Version: v0.8.1
Version: v0.8.2
Licence type (autodetected): Apache-2.0
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-autodiscover@v0.8.1/LICENSE:
Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-autodiscover@v0.8.2/LICENSE:

Apache License
Version 2.0, January 2004
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ require (
github.com/awslabs/kinesis-aggregation/go/v2 v2.0.0-20220623125934-28468a6701b5
github.com/elastic/bayeux v1.0.5
github.com/elastic/ebpfevents v0.6.0
github.com/elastic/elastic-agent-autodiscover v0.8.1
github.com/elastic/elastic-agent-autodiscover v0.8.2
github.com/elastic/elastic-agent-libs v0.10.0
github.com/elastic/elastic-agent-system-metrics v0.11.1
github.com/elastic/go-elasticsearch/v8 v8.14.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,8 @@ github.com/elastic/dhcp v0.0.0-20200227161230-57ec251c7eb3 h1:lnDkqiRFKm0rxdljqr
github.com/elastic/dhcp v0.0.0-20200227161230-57ec251c7eb3/go.mod h1:aPqzac6AYkipvp4hufTyMj5PDIphF3+At8zr7r51xjY=
github.com/elastic/ebpfevents v0.6.0 h1:BrL3m7JFK7U6h2jkbk3xAWWs//IZnugCHEDds5u2v68=
github.com/elastic/ebpfevents v0.6.0/go.mod h1:ESG9gw7N+n5yCCMgdg1IIJENKWSmX7+X0Fi9GUs9nvU=
github.com/elastic/elastic-agent-autodiscover v0.8.1 h1:u6TWqh7wfevu6S4GUq4SIxYBRo4b/P5RZmx/rSvT10A=
github.com/elastic/elastic-agent-autodiscover v0.8.1/go.mod h1:0gzGsaDCAqBfUZjuCqqWsSI60eaZ778A5tQZV72rPV0=
github.com/elastic/elastic-agent-autodiscover v0.8.2 h1:Fs2FhR33AMBPfm5/jz4drVzaEZaqOIHlDBvGtkUZdIk=
github.com/elastic/elastic-agent-autodiscover v0.8.2/go.mod h1:VZnU53EVaFTxR8Xf6YsLN8FHD5DKQzHSPlKax9/4w+o=
github.com/elastic/elastic-agent-client/v7 v7.15.0 h1:nDB7v8TBoNuD6IIzC3z7Q0y+7bMgXoT2DsHfolO2CHE=
github.com/elastic/elastic-agent-client/v7 v7.15.0/go.mod h1:6h+f9QdIr3GO2ODC0Y8+aEXRwzbA5W4eV4dd/67z7nI=
github.com/elastic/elastic-agent-libs v0.10.0 h1:W7uvay0UYdLPtauXGsMD8Xfoe4qtcVWQR4icBgf/26Q=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ data:
hosts: ["kube-state-metrics:8080"]
period: 10s
add_metadata: true
# use_kubeadm: true
metricsets:
- state_node
- state_deployment
- state_daemonset
- state_replicaset
- state_pod
- state_container
- state_job
- state_cronjob
- state_resourcequota
- state_statefulset
Expand All @@ -45,14 +47,16 @@ data:
metricsets:
- apiserver
hosts: ["https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}"]
# use_kubeadm: true
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
ssl.certificate_authorities:
- /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
period: 30s
# Uncomment this to get k8s events:
#- module: kubernetes
# metricsets:
# - event
- module: kubernetes
metricsets:
- event
# use_kubeadm: true
# To enable hints based autodiscover uncomment this:
#- type: kubernetes
# node: ${NODE_NAME}
Expand Down Expand Up @@ -113,6 +117,7 @@ data:
- volume
period: 10s
host: ${NODE_NAME}
# use_kubeadm: true
hosts: ["https://${NODE_NAME}:10250"]
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
ssl.verification_mode: "none"
Expand All @@ -125,6 +130,7 @@ data:
metricsets:
- proxy
period: 10s
# use_kubeadm: true
host: ${NODE_NAME}
hosts: ["localhost:10249"]
---
Expand All @@ -151,12 +157,12 @@ spec:
dnsPolicy: ClusterFirstWithHostNet
containers:
- name: metricbeat
image: docker.elastic.co/beats/metricbeat:8.0.0-SNAPSHOT
image: docker.elastic.co/beats/metricbeat:8.15.0-SNAPSHOT
command: [ "sleep" ]
args: [ "infinity" ]
env:
- name: ELASTICSEARCH_HOST
value: elasticsearch.default.svc.cluster.local
value: elasticsearch
- name: ELASTICSEARCH_PORT
value: "9200"
- name: ELASTICSEARCH_USERNAME
Expand Down
2 changes: 2 additions & 0 deletions metricbeat/module/kubernetes/event/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

type kubeEventsConfig struct {
KubeConfig string `config:"kube_config"`
KubeAdm bool `config:"use_kubeadm"`
KubeClientOptions kubernetes.KubeClientOptions `config:"kube_client_options"`
Namespace string `config:"namespace"`
SyncPeriod time.Duration `config:"sync_period"`
Expand All @@ -40,6 +41,7 @@ type Enabled struct {
func defaultKubernetesEventsConfig() kubeEventsConfig {
return kubeEventsConfig{
SyncPeriod: 10 * time.Minute,
KubeAdm: true,
LabelsDedot: true,
AnnotationsDedot: true,
SkipOlder: true,
Expand Down
27 changes: 16 additions & 11 deletions metricbeat/module/kubernetes/util/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
type kubernetesConfig struct {
KubeConfig string `config:"kube_config"`
KubeClientOptions kubernetes.KubeClientOptions `config:"kube_client_options"`

Node string `config:"node"`
SyncPeriod time.Duration `config:"sync_period"`
KubeAdm bool `config:"use_kubeadm"`
gizas marked this conversation as resolved.
Show resolved Hide resolved
Node string `config:"node"`
SyncPeriod time.Duration `config:"sync_period"`

// AddMetadata enables enriching metricset events with metadata from the API server
AddMetadata bool `config:"add_metadata"`
Expand Down Expand Up @@ -464,7 +464,7 @@

// createMetadataGen creates and returns the metadata generator for resources other than pod and service
// metaGen is a struct of type Resource and implements Generate method for metadata generation for a given resource kind.
func createMetadataGen(client k8sclient.Interface, commonConfig *conf.C, addResourceMetadata *metadata.AddResourceMetadataConfig,
func createMetadataGen(client k8sclient.Interface, commonConfig *conf.C, addResourceMetadata *metadata.AddResourceMetadataConfig, kubeadm bool,
resourceName string, resourceWatchers *Watchers) (*metadata.Resource, error) {

resourceWatchers.lock.RLock()
Expand All @@ -477,9 +477,10 @@
}

var metaGen *metadata.Resource

namespaceMetaWatcher := resourceWatchers.metaWatchersMap[NamespaceResource]
if namespaceMetaWatcher != nil {
// We initialise the use_kubeadm variable based on modules KubeAdm base configuration
addResourceMetadata.Namespace.SetBool("use_kubeadm", -1, kubeadm)
n := metadata.NewNamespaceMetadataGenerator(addResourceMetadata.Namespace,
(*namespaceMetaWatcher).watcher.Store(), client)
metaGen = metadata.NewNamespaceAwareResourceMetadataGenerator(commonConfig, client, n)
Expand All @@ -492,7 +493,7 @@

// createMetadataGenSpecific creates and returns the metadata generator for a specific resource - pod or service
// A metaGen struct implements a MetaGen interface and is designed to utilize the necessary watchers to collect(Generate) metadata for a specific resource.
func createMetadataGenSpecific(client k8sclient.Interface, commonConfig *conf.C, addResourceMetadata *metadata.AddResourceMetadataConfig,
func createMetadataGenSpecific(client k8sclient.Interface, commonConfig *conf.C, addResourceMetadata *metadata.AddResourceMetadataConfig, kubeadm bool,
resourceName string, resourceWatchers *Watchers) (metadata.MetaGen, error) {

resourceWatchers.lock.RLock()
Expand Down Expand Up @@ -527,6 +528,9 @@
}
// For example for pod named redis in namespace default, the generator uses the pod watcher for pod metadata,
// collects all node metadata using the node watcher's store and all namespace metadata using the namespacewatcher's store.
// We initialise the use_kubeadm variable based on modules KubeAdm base configuration
gizas marked this conversation as resolved.
Show resolved Hide resolved
addResourceMetadata.Namespace.SetBool("use_kubeadm", -1, kubeadm)
addResourceMetadata.Node.SetBool("use_kubeadm", -1, kubeadm)
metaGen = metadata.GetPodMetaGen(commonConfig, mainWatcher, nodeWatcher, namespaceWatcher, replicaSetWatcher,
jobWatcher, addResourceMetadata)
return metaGen, nil
Expand All @@ -535,6 +539,8 @@
if namespaceMetaWatcher == nil {
return nil, fmt.Errorf("could not create the metadata generator, as the watcher for namespace does not exist")
}

addResourceMetadata.Namespace.SetBool("use_kubeadm", -1, kubeadm)
namespaceMeta := metadata.NewNamespaceMetadataGenerator(addResourceMetadata.Namespace,
(*namespaceMetaWatcher).watcher.Store(), client)
metaGen = metadata.NewServiceMetadataGenerator(commonConfig, (*resourceMetaWatcher).watcher.Store(),
Expand Down Expand Up @@ -574,7 +580,6 @@
return &nilEnricher{}
}
commonConfig, _ := conf.NewConfigFrom(&commonMetaConfig)

client, err := kubernetes.GetKubernetesClient(config.KubeConfig, config.KubeClientOptions)
if err != nil {
log.Errorf("Error creating Kubernetes client: %s", err)
Expand All @@ -590,14 +595,14 @@
return &nilEnricher{}
}

var specificMetaGen metadata.MetaGen

Check failure on line 598 in metricbeat/module/kubernetes/util/kubernetes.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

specificMetaGen declared and not used (typecheck)
var generalMetaGen *metadata.Resource

Check failure on line 599 in metricbeat/module/kubernetes/util/kubernetes.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

generalMetaGen declared and not used (typecheck)
// Create the metadata generator to be used in the watcher's event handler.
// Both specificMetaGen and generalMetaGen implement Generate method for metadata collection.
if resourceName == ServiceResource || resourceName == PodResource {
specificMetaGen, err = createMetadataGenSpecific(client, commonConfig, config.AddResourceMetadata, resourceName, resourceWatchers)
specificMetaGen, err = createMetadataGenSpecific(client, commonConfig, config.AddResourceMetadata, config.KubeAdm, resourceName, resourceWatchers)
} else {
generalMetaGen, err = createMetadataGen(client, commonConfig, config.AddResourceMetadata, resourceName, resourceWatchers)
generalMetaGen, err = createMetadataGen(client, commonConfig, config.AddResourceMetadata, config.KubeAdm, resourceName, resourceWatchers)
}
if err != nil {
log.Errorf("Error trying to create the metadata generators: %s", err)
Expand Down Expand Up @@ -741,7 +746,6 @@
return &nilEnricher{}
}
commonConfig, _ := conf.NewConfigFrom(&commonMetaConfig)

client, err := kubernetes.GetKubernetesClient(config.KubeConfig, config.KubeClientOptions)
if err != nil {
log.Errorf("Error creating Kubernetes client: %s", err)
Expand All @@ -756,7 +760,7 @@
return &nilEnricher{}
}

metaGen, err := createMetadataGenSpecific(client, commonConfig, config.AddResourceMetadata, PodResource, resourceWatchers)
metaGen, err := createMetadataGenSpecific(client, commonConfig, config.AddResourceMetadata, commonMetaConfig.KubeAdm, PodResource, resourceWatchers)
if err != nil {
log.Errorf("Error trying to create the metadata generators: %s", err)
return &nilEnricher{}
Expand Down Expand Up @@ -883,6 +887,7 @@
config := &kubernetesConfig{
AddMetadata: true,
SyncPeriod: time.Minute * 10,
KubeAdm: true,
AddResourceMetadata: metadata.GetDefaultResourceMetadataConfig(),
}

Expand Down
23 changes: 17 additions & 6 deletions metricbeat/module/kubernetes/util/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func TestCreateMetaGen(t *testing.T) {
config := &kubernetesConfig{
Namespace: "test-ns",
SyncPeriod: time.Minute,
KubeAdm: true,
Node: "test-node",
AddResourceMetadata: &metadata.AddResourceMetadataConfig{
CronJob: false,
Expand All @@ -243,7 +244,7 @@ func TestCreateMetaGen(t *testing.T) {
}
client := k8sfake.NewSimpleClientset()

_, err = createMetadataGen(client, commonConfig, config.AddResourceMetadata, DeploymentResource, resourceWatchers)
_, err = createMetadataGen(client, commonConfig, config.AddResourceMetadata, config.KubeAdm, DeploymentResource, resourceWatchers)
// At this point, no watchers were created
require.Error(t, err)

Expand All @@ -253,7 +254,7 @@ func TestCreateMetaGen(t *testing.T) {
require.NoError(t, err)

// Create the generators, this time without error
_, err = createMetadataGen(client, commonConfig, config.AddResourceMetadata, DeploymentResource, resourceWatchers)
_, err = createMetadataGen(client, commonConfig, config.AddResourceMetadata, config.KubeAdm, DeploymentResource, resourceWatchers)
require.NoError(t, err)
}

Expand All @@ -271,34 +272,40 @@ func TestCreateMetaGenSpecific(t *testing.T) {
})
require.NoError(t, err)

nodeConfig, err := conf.NewConfigFrom(map[string]interface{}{
"enabled": true,
})
require.NoError(t, err)
config := &kubernetesConfig{
Namespace: "test-ns",
SyncPeriod: time.Minute,
Node: "test-node",
KubeAdm: true,
AddResourceMetadata: &metadata.AddResourceMetadataConfig{
CronJob: false,
Deployment: true,
Namespace: namespaceConfig,
Node: nodeConfig,
},
}
client := k8sfake.NewSimpleClientset()

// For pod:
metricsetPod := "pod"

_, err = createMetadataGenSpecific(client, commonConfig, config.AddResourceMetadata, PodResource, resourceWatchers)
_, err = createMetadataGenSpecific(client, commonConfig, config.AddResourceMetadata, config.KubeAdm, PodResource, resourceWatchers)
// At this point, no watchers were created
require.Error(t, err)

// Create the pod resource + the extras
err = createAllWatchers(client, metricsetPod, PodResource, false, config, log, resourceWatchers)
require.NoError(t, err)

_, err = createMetadataGenSpecific(client, commonConfig, config.AddResourceMetadata, PodResource, resourceWatchers)
_, err = createMetadataGenSpecific(client, commonConfig, config.AddResourceMetadata, config.KubeAdm, PodResource, resourceWatchers)
require.NoError(t, err)

// For service:
_, err = createMetadataGenSpecific(client, commonConfig, config.AddResourceMetadata, ServiceResource, resourceWatchers)
_, err = createMetadataGenSpecific(client, commonConfig, config.AddResourceMetadata, config.KubeAdm, ServiceResource, resourceWatchers)
// At this point, no watchers were created
require.Error(t, err)

Expand All @@ -307,7 +314,7 @@ func TestCreateMetaGenSpecific(t *testing.T) {
err = createAllWatchers(client, metricsetService, ServiceResource, false, config, log, resourceWatchers)
require.NoError(t, err)

_, err = createMetadataGenSpecific(client, commonConfig, config.AddResourceMetadata, ServiceResource, resourceWatchers)
_, err = createMetadataGenSpecific(client, commonConfig, config.AddResourceMetadata, config.KubeAdm, ServiceResource, resourceWatchers)
require.NoError(t, err)
}

Expand Down Expand Up @@ -341,6 +348,7 @@ func TestBuildMetadataEnricher_Start_Stop(t *testing.T) {
Namespace: "test-ns",
SyncPeriod: time.Minute,
Node: "test-node",
KubeAdm: true,
AddResourceMetadata: &metadata.AddResourceMetadataConfig{
CronJob: false,
Deployment: false,
Expand Down Expand Up @@ -425,6 +433,7 @@ func TestBuildMetadataEnricher_Start_Stop_SameResources(t *testing.T) {
Namespace: "test-ns",
SyncPeriod: time.Minute,
Node: "test-node",
KubeAdm: true,
AddResourceMetadata: &metadata.AddResourceMetadataConfig{
CronJob: false,
Deployment: false,
Expand Down Expand Up @@ -496,6 +505,7 @@ func TestBuildMetadataEnricher_EventHandler(t *testing.T) {
Namespace: "test-ns",
SyncPeriod: time.Minute,
Node: "test-node",
KubeAdm: true,
AddResourceMetadata: &metadata.AddResourceMetadataConfig{
CronJob: false,
Deployment: false,
Expand Down Expand Up @@ -639,6 +649,7 @@ func TestBuildMetadataEnricher_EventHandler_PastObjects(t *testing.T) {
Namespace: "test-ns",
SyncPeriod: time.Minute,
Node: "test-node",
KubeAdm: true,
AddResourceMetadata: &metadata.AddResourceMetadataConfig{
CronJob: false,
Deployment: false,
Expand Down
Loading