Skip to content

Commit a9b22cd

Browse files
authored
Refactor kubernetes autodiscover to enable different resource based discovery (#14738) (#14924)
(cherry picked from commit 1a029e5)
1 parent 81fc2fb commit a9b22cd

File tree

17 files changed

+1401
-233
lines changed

17 files changed

+1401
-233
lines changed

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
347347
- Add support for API keys in Elasticsearch outputs. {pull}14324[14324]
348348
- Ensure that init containers are no longer tailed after they stop {pull}14394[14394]
349349
- Add consumer_lag in Kafka consumergroup metricset {pull}14822[14822]
350+
- Refactor kubernetes autodiscover to enable different resource based discovery {pull}14738[14738]
350351

351352
*Auditbeat*
352353

deploy/kubernetes/filebeat-kubernetes.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ data:
2323
#filebeat.autodiscover:
2424
# providers:
2525
# - type: kubernetes
26-
# host: ${NODE_NAME}
26+
# node: ${NODE_NAME}
2727
# hints.enabled: true
2828
# hints.default_config:
2929
# type: container

deploy/kubernetes/filebeat/filebeat-configmap.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ data:
2323
#filebeat.autodiscover:
2424
# providers:
2525
# - type: kubernetes
26-
# host: ${NODE_NAME}
26+
# node: ${NODE_NAME}
2727
# hints.enabled: true
2828
# hints.default_config:
2929
# type: container

deploy/kubernetes/metricbeat-kubernetes.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ data:
1818
#metricbeat.autodiscover:
1919
# providers:
2020
# - type: kubernetes
21-
# host: ${NODE_NAME}
21+
# node: ${NODE_NAME}
2222
# hints.enabled: true
2323
2424
processors:

deploy/kubernetes/metricbeat/metricbeat-daemonset-configmap.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ data:
1818
#metricbeat.autodiscover:
1919
# providers:
2020
# - type: kubernetes
21-
# host: ${NODE_NAME}
21+
# node: ${NODE_NAME}
2222
# hints.enabled: true
2323
2424
processors:

libbeat/autodiscover/providers/kubernetes/config.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,24 @@ import (
2525

2626
"github.com/elastic/beats/libbeat/autodiscover/template"
2727
"github.com/elastic/beats/libbeat/common"
28+
"github.com/elastic/beats/libbeat/common/cfgwarn"
29+
"github.com/elastic/beats/libbeat/logp"
2830
)
2931

3032
// Config for kubernetes autodiscover provider
3133
type Config struct {
3234
KubeConfig string `config:"kube_config"`
33-
Host string `config:"host"`
3435
Namespace string `config:"namespace"`
3536
SyncPeriod time.Duration `config:"sync_period"`
3637
CleanupTimeout time.Duration `config:"cleanup_timeout" validate:"positive"`
3738

39+
// Needed when resource is a pod
40+
HostDeprecated string `config:"host"`
41+
Node string `config:"node"`
42+
// Scope can be either node or cluster.
43+
Scope string `config:"scope"`
44+
Resource string `config:"resource"`
45+
3846
Prefix string `config:"prefix"`
3947
Hints *common.Config `config:"hints"`
4048
Builders []*common.Config `config:"builders"`
@@ -45,6 +53,7 @@ type Config struct {
4553
func defaultConfig() *Config {
4654
return &Config{
4755
SyncPeriod: 10 * time.Minute,
56+
Resource: "pod",
4857
CleanupTimeout: 60 * time.Second,
4958
Prefix: "co.elastic",
5059
}
@@ -61,5 +70,30 @@ func (c *Config) Validate() error {
6170
return fmt.Errorf("no configs or hints defined for autodiscover provider")
6271
}
6372

73+
// Check if host is being defined and change it to node instead.
74+
if c.Node == "" && c.HostDeprecated != "" {
75+
c.Node = c.HostDeprecated
76+
cfgwarn.Deprecate("8.0", "`host` will be deprecated, use `node` instead")
77+
}
78+
79+
// Check if resource is either node or pod. If yes then default the scope to "node" if not provided.
80+
// Default the scope to "cluster" for everything else.
81+
switch c.Resource {
82+
case "node", "pod":
83+
if c.Scope == "" {
84+
c.Scope = "node"
85+
}
86+
87+
default:
88+
if c.Scope == "node" {
89+
logp.L().Warnf("can not set scope to `node` when using resource %s. resetting scope to `cluster`", c.Resource)
90+
}
91+
c.Scope = "cluster"
92+
}
93+
94+
if c.Scope != "node" && c.Scope != "cluster" {
95+
return fmt.Errorf("invalid `scope` configured. supported values are `node` and `cluster`")
96+
}
97+
6498
return nil
6599
}

libbeat/autodiscover/providers/kubernetes/config_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ func TestConfigWithCustomBuilders(t *testing.T) {
5353
assert.NotNil(t, err)
5454
}
5555

56+
func TestConfigWithIncorrectScope(t *testing.T) {
57+
cfg := common.MapStr{
58+
"scope": "node",
59+
"resource": "service",
60+
"hints.enabled": true,
61+
}
62+
63+
config := common.MustNewConfigFrom(&cfg)
64+
c := defaultConfig()
65+
err := config.Unpack(&c)
66+
assert.Nil(t, err)
67+
68+
assert.Equal(t, "service", c.Resource)
69+
assert.Equal(t, "cluster", c.Scope)
70+
}
71+
5672
type mockBuilder struct {
5773
}
5874

0 commit comments

Comments
 (0)