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

Refactor kubernetes autodiscover to enable different resource based discovery #14738

Merged
merged 12 commits into from
Dec 3, 2019
Merged
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Add support for API keys in Elasticsearch outputs. {pull}14324[14324]
- Ensure that init containers are no longer tailed after they stop {pull}14394[14394]
- Add consumer_lag in Kafka consumergroup metricset {pull}14822[14822]
- Refactor kubernetes autodiscover to enable different resource based discovery {pull}14738[14738]

*Auditbeat*

Expand Down
2 changes: 1 addition & 1 deletion deploy/kubernetes/filebeat-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data:
#filebeat.autodiscover:
# providers:
# - type: kubernetes
# host: ${NODE_NAME}
# node: ${NODE_NAME}
# hints.enabled: true
# hints.default_config:
# type: container
Expand Down
2 changes: 1 addition & 1 deletion deploy/kubernetes/filebeat/filebeat-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data:
#filebeat.autodiscover:
# providers:
# - type: kubernetes
# host: ${NODE_NAME}
# node: ${NODE_NAME}
# hints.enabled: true
# hints.default_config:
# type: container
Expand Down
2 changes: 1 addition & 1 deletion deploy/kubernetes/metricbeat-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data:
#metricbeat.autodiscover:
# providers:
# - type: kubernetes
# host: ${NODE_NAME}
# node: ${NODE_NAME}
# hints.enabled: true

processors:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data:
#metricbeat.autodiscover:
# providers:
# - type: kubernetes
# host: ${NODE_NAME}
# node: ${NODE_NAME}
# hints.enabled: true

processors:
Expand Down
36 changes: 35 additions & 1 deletion libbeat/autodiscover/providers/kubernetes/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,24 @@ import (

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

// Config for kubernetes autodiscover provider
type Config struct {
KubeConfig string `config:"kube_config"`
Host string `config:"host"`
Namespace string `config:"namespace"`
SyncPeriod time.Duration `config:"sync_period"`
CleanupTimeout time.Duration `config:"cleanup_timeout" validate:"positive"`

// Needed when resource is a pod
HostDeprecated string `config:"host"`
Node string `config:"node"`
// Scope can be either node or cluster.
Scope string `config:"scope"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would also solve #10578 🎉

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can happen in a different PR but I think we should extend this setting to add_kubernetes_metadata processor. I'm unsure about the other (resource), probably not worth it until we see the need.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will create follow up PR

Resource string `config:"resource"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both scope and resource are new, could you update the docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated docs.


Prefix string `config:"prefix"`
Hints *common.Config `config:"hints"`
Builders []*common.Config `config:"builders"`
Expand All @@ -45,6 +53,7 @@ type Config struct {
func defaultConfig() *Config {
return &Config{
SyncPeriod: 10 * time.Minute,
Resource: "pod",
CleanupTimeout: 60 * time.Second,
Prefix: "co.elastic",
}
Expand All @@ -61,5 +70,30 @@ 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 {
case "node", "pod":
if c.Scope == "" {
c.Scope = "node"
}

default:
if c.Scope == "node" {
logp.L().Warnf("can not set scope to `node` when using resource %s. resetting scope to `cluster`", c.Resource)
}
c.Scope = "cluster"
}

if c.Scope != "node" && c.Scope != "cluster" {
return fmt.Errorf("invalid `scope` configured. supported values are `node` and `cluster`")
}

return nil
}
16 changes: 16 additions & 0 deletions libbeat/autodiscover/providers/kubernetes/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ func TestConfigWithCustomBuilders(t *testing.T) {
assert.NotNil(t, err)
}

func TestConfigWithIncorrectScope(t *testing.T) {
cfg := common.MapStr{
"scope": "node",
"resource": "service",
"hints.enabled": true,
}

config := common.MustNewConfigFrom(&cfg)
c := defaultConfig()
err := config.Unpack(&c)
assert.Nil(t, err)

assert.Equal(t, "service", c.Resource)
assert.Equal(t, "cluster", c.Scope)
}

type mockBuilder struct {
}

Expand Down
Loading