Skip to content

Commit

Permalink
Allign k8s configuration settings (elastic#29908)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrsMark authored and yashtewari committed Jan 30, 2022
1 parent 044871c commit 329ceab
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Add job.name in pods controlled by Jobs {pull}28954[28954]
- Change Docker base image from CentOS 7 to Ubuntu 20.04 {pull}29681[29681]
- Enrich kubernetes metadata with node annotations. {pull}29605[29605]
- Allign kubernetes configuration settings. {pull}29908[29908]

*Auditbeat*

Expand Down
10 changes: 1 addition & 9 deletions libbeat/autodiscover/providers/kubernetes/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

"github.com/elastic/beats/v7/libbeat/autodiscover/template"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/cfgwarn"
"github.com/elastic/beats/v7/libbeat/logp"
)

Expand All @@ -43,8 +42,7 @@ type Config struct {
CleanupTimeout time.Duration `config:"cleanup_timeout" validate:"positive"`

// Needed when resource is a pod
HostDeprecated string `config:"host"`
Node string `config:"node"`
Node string `config:"node"`
// Scope can be either node or cluster.
Scope string `config:"scope"`
Resource string `config:"resource"`
Expand Down Expand Up @@ -86,12 +84,6 @@ func (c *Config) Validate() error {
return fmt.Errorf("no configs or hints defined for autodiscover provider")
}

// Check if host is being defined and change it to node instead.
if c.Node == "" && c.HostDeprecated != "" {
c.Node = c.HostDeprecated
cfgwarn.Deprecate("8.0", "`host` will be deprecated, use `node` instead")
}

// Check if resource is either node or pod. If yes then default the scope to "node" if not provided.
// Default the scope to "cluster" for everything else.
switch c.Resource {
Expand Down
5 changes: 2 additions & 3 deletions libbeat/autodiscover/providers/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,9 @@ func NewPodEventer(uuid uuid.UUID, cfg *common.Config, client k8s.Interface, pub
options := kubernetes.WatchOptions{
SyncTimeout: config.SyncPeriod,
Node: config.Node,
Namespace: config.Namespace,
}
if config.Namespace != "" {
options.Namespace = config.Namespace
}

metaConf := config.AddResourceMetadata
nodeWatcher, err := kubernetes.NewNamedWatcher("node", client, &kubernetes.Node{}, options, nil)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions libbeat/processors/add_kubernetes_metadata/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
type kubeAnnotatorConfig struct {
KubeConfig string `config:"kube_config"`
KubeClientOptions kubernetes.KubeClientOptions `config:"kube_client_options"`
Host string `config:"host"`
Node string `config:"node"`
Scope string `config:"scope"`
Namespace string `config:"namespace"`
SyncPeriod time.Duration `config:"sync_period"`
Expand Down Expand Up @@ -67,7 +67,7 @@ func (k *kubeAnnotatorConfig) Validate() error {
}

if k.Scope == "cluster" {
k.Host = ""
k.Node = ""
}

// Checks below were added to warn the users early on and avoid initialising the processor in case the `logs_path`
Expand Down
15 changes: 7 additions & 8 deletions libbeat/processors/add_kubernetes_metadata/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,23 @@ func (k *kubernetesAnnotator) init(config kubeAnnotatorConfig, cfg *common.Confi

k.matchers = matchers
nd := &kubernetes.DiscoverKubernetesNodeParams{
ConfigHost: config.Host,
ConfigHost: config.Node,
Client: client,
IsInCluster: kubernetes.IsInCluster(config.KubeConfig),
HostUtils: &kubernetes.DefaultDiscoveryUtils{},
}
if config.Scope == "node" {
config.Host, err = kubernetes.DiscoverKubernetesNode(k.log, nd)
config.Node, err = kubernetes.DiscoverKubernetesNode(k.log, nd)
if err != nil {
k.log.Errorf("Couldn't discover Kubernetes node: %w", err)
return
}
k.log.Debugf("Initializing a new Kubernetes watcher using host: %s", config.Host)
k.log.Debugf("Initializing a new Kubernetes watcher using host: %s", config.Node)
}

watcher, err := kubernetes.NewNamedWatcher("add_kubernetes_metadata_pod", client, &kubernetes.Pod{}, kubernetes.WatchOptions{
SyncTimeout: config.SyncPeriod,
Node: config.Host,
Node: config.Node,
Namespace: config.Namespace,
}, nil)
if err != nil {
Expand All @@ -194,11 +194,10 @@ func (k *kubernetesAnnotator) init(config kubeAnnotatorConfig, cfg *common.Confi

options := kubernetes.WatchOptions{
SyncTimeout: config.SyncPeriod,
Node: config.Host,
}
if config.Namespace != "" {
options.Namespace = config.Namespace
Node: config.Node,
Namespace: config.Namespace,
}

nodeWatcher, err := kubernetes.NewNamedWatcher("add_kubernetes_metadata_node", client, &kubernetes.Node{}, options, nil)
if err != nil {
k.log.Errorf("couldn't create watcher for %T due to error %+v", &kubernetes.Node{}, err)
Expand Down
6 changes: 4 additions & 2 deletions metricbeat/docs/modules/kubernetes.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ metricbeat.modules:
# Enriching parameters:
add_metadata: true
# When used outside the cluster:
#host: node_name
#node: node_name
# If kube_config is not set, KUBECONFIG environment variable will be checked
# and if not present it will fall back to InCluster
#kube_config: ~/.kube/config
Expand Down Expand Up @@ -262,10 +262,12 @@ metricbeat.modules:
# Enriching parameters:
add_metadata: true
# When used outside the cluster:
#host: node_name
#node: node_name
# If kube_config is not set, KUBECONFIG environment variable will be checked
# and if not present it will fall back to InCluster
#kube_config: ~/.kube/config
# Set the namespace to watch for resources
#namespace: staging
# To configure additionally node and namespace metadata, added to pod, service and container resource types,
# `add_resource_metadata` can be defined.
# By default all labels will be included while annotations are not added by default.
Expand Down
6 changes: 4 additions & 2 deletions metricbeat/metricbeat.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ metricbeat.modules:
# Enriching parameters:
add_metadata: true
# When used outside the cluster:
#host: node_name
#node: node_name
# If kube_config is not set, KUBECONFIG environment variable will be checked
# and if not present it will fall back to InCluster
#kube_config: ~/.kube/config
Expand Down Expand Up @@ -540,10 +540,12 @@ metricbeat.modules:
# Enriching parameters:
add_metadata: true
# When used outside the cluster:
#host: node_name
#node: node_name
# If kube_config is not set, KUBECONFIG environment variable will be checked
# and if not present it will fall back to InCluster
#kube_config: ~/.kube/config
# Set the namespace to watch for resources
#namespace: staging
# To configure additionally node and namespace metadata, added to pod, service and container resource types,
# `add_resource_metadata` can be defined.
# By default all labels will be included while annotations are not added by default.
Expand Down
6 changes: 4 additions & 2 deletions metricbeat/module/kubernetes/_meta/config.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# Enriching parameters:
add_metadata: true
# When used outside the cluster:
#host: node_name
#node: node_name
# If kube_config is not set, KUBECONFIG environment variable will be checked
# and if not present it will fall back to InCluster
#kube_config: ~/.kube/config
Expand Down Expand Up @@ -63,10 +63,12 @@
# Enriching parameters:
add_metadata: true
# When used outside the cluster:
#host: node_name
#node: node_name
# If kube_config is not set, KUBECONFIG environment variable will be checked
# and if not present it will fall back to InCluster
#kube_config: ~/.kube/config
# Set the namespace to watch for resources
#namespace: staging
# To configure additionally node and namespace metadata, added to pod, service and container resource types,
# `add_resource_metadata` can be defined.
# By default all labels will be included while annotations are not added by default.
Expand Down
6 changes: 4 additions & 2 deletions metricbeat/module/kubernetes/_meta/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
#labels.dedot: true
#annotations.dedot: true
# When used outside the cluster:
#host: node_name
#node: node_name
# If kube_config is not set, KUBECONFIG environment variable will be checked
# and if not present it will fall back to InCluster
#kube_config: ~/.kube/config
# Set the namespace to watch for resources
#namespace: staging
# To configure additionally node and namespace metadata, added to pod, service and container resource types,
# `add_resource_metadata` can be defined.
# By default all labels will be included while annotations are not added by default.
Expand All @@ -32,7 +35,6 @@
# include_labels: ["nodelabel2"]
# include_annotations: ["nodeannotation1"]
# deployment: false
#kube_config: ~/.kube/config
# Kubernetes client QPS and burst can be configured additionally
#kube_client_options:
# qps: 5
Expand Down
8 changes: 5 additions & 3 deletions metricbeat/module/kubernetes/util/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ type kubernetesConfig struct {
KubeConfig string `config:"kube_config"`
KubeClientOptions kubernetes.KubeClientOptions `config:"kube_client_options"`

Host string `config:"host"`
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"`
AddResourceMetadata *metadata.AddResourceMetadataConfig `config:"add_resource_metadata"`
Namespace string `config:"namespace"`
}

type enricher struct {
Expand Down Expand Up @@ -262,14 +263,15 @@ func getResourceMetadataWatchers(config *kubernetesConfig, resource kubernetes.R

options := kubernetes.WatchOptions{
SyncTimeout: config.SyncPeriod,
Namespace: config.Namespace,
}

log := logp.NewLogger(selector)

// Watch objects in the node only
if nodeScope {
nd := &kubernetes.DiscoverKubernetesNodeParams{
ConfigHost: config.Host,
ConfigHost: config.Node,
Client: client,
IsInCluster: kubernetes.IsInCluster(config.KubeConfig),
HostUtils: &kubernetes.DefaultDiscoveryUtils{},
Expand All @@ -281,7 +283,7 @@ func getResourceMetadataWatchers(config *kubernetesConfig, resource kubernetes.R
}
}

log.Debugf("Initializing a new Kubernetes watcher using host: %v", config.Host)
log.Debugf("Initializing a new Kubernetes watcher using host: %v", config.Node)

watcher, err := kubernetes.NewNamedWatcher("resource_metadata_enricher", client, resource, options, nil)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions metricbeat/modules.d/kubernetes.yml.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
#labels.dedot: true
#annotations.dedot: true
# When used outside the cluster:
#host: node_name
#node: node_name
# If kube_config is not set, KUBECONFIG environment variable will be checked
# and if not present it will fall back to InCluster
#kube_config: ~/.kube/config
# Set the namespace to watch for resources
#namespace: staging
# To configure additionally node and namespace metadata, added to pod, service and container resource types,
# `add_resource_metadata` can be defined.
# By default all labels will be included while annotations are not added by default.
Expand All @@ -35,7 +38,6 @@
# include_labels: ["nodelabel2"]
# include_annotations: ["nodeannotation1"]
# deployment: false
#kube_config: ~/.kube/config
# Kubernetes client QPS and burst can be configured additionally
#kube_client_options:
# qps: 5
Expand Down
6 changes: 4 additions & 2 deletions x-pack/metricbeat/metricbeat.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ metricbeat.modules:
# Enriching parameters:
add_metadata: true
# When used outside the cluster:
#host: node_name
#node: node_name
# If kube_config is not set, KUBECONFIG environment variable will be checked
# and if not present it will fall back to InCluster
#kube_config: ~/.kube/config
Expand Down Expand Up @@ -925,10 +925,12 @@ metricbeat.modules:
# Enriching parameters:
add_metadata: true
# When used outside the cluster:
#host: node_name
#node: node_name
# If kube_config is not set, KUBECONFIG environment variable will be checked
# and if not present it will fall back to InCluster
#kube_config: ~/.kube/config
# Set the namespace to watch for resources
#namespace: staging
# To configure additionally node and namespace metadata, added to pod, service and container resource types,
# `add_resource_metadata` can be defined.
# By default all labels will be included while annotations are not added by default.
Expand Down

0 comments on commit 329ceab

Please sign in to comment.