Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
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: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Add support for RFC3339 time zone offsets in JSON output. {pull}13227[13227]
- Add autodetection mode for add_docker_metadata and enable it by default in included configuration files{pull}13374[13374]
- Added `monitoring.cluster_uuid` setting to associate Beat data with specified ES cluster in Stack Monitoring UI. {pull}13182[13182]
- Add autodetection mode for add_kubernetes_metadata and enable it by default in included configuration files{pull}13473[13473]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit.

Suggested change
- Add autodetection mode for add_kubernetes_metadata and enable it by default in included configuration files{pull}13473[13473]
- Add autodetection mode for add_kubernetes_metadata and enable it by default in included configuration files. {pull}13473[13473]



*Auditbeat*

Expand Down
1 change: 1 addition & 0 deletions auditbeat/auditbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~

#================================ Logging =====================================

Expand Down
1 change: 1 addition & 0 deletions filebeat/filebeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~

#================================ Logging =====================================

Expand Down
1 change: 1 addition & 0 deletions journalbeat/journalbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~

#================================ Logging =====================================

Expand Down
1 change: 1 addition & 0 deletions libbeat/_meta/config.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~
{{else}}
processors:
- add_observer_metadata:
Expand Down
1 change: 1 addition & 0 deletions libbeat/common/kubernetes/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const defaultNode = "localhost"
// in cluster configuration based on the secrets mounted in the Pod. If kubeConfig is passed,
// it parses the config file to get the config required to build a client.
func GetKubernetesClient(kubeconfig string) (kubernetes.Interface, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit. Remove this added line.

Suggested change

cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, fmt.Errorf("unable to build kube config due to error: %+v", err)
Expand Down
51 changes: 40 additions & 11 deletions libbeat/processors/add_kubernetes_metadata/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ import (
"github.com/elastic/beats/libbeat/common/kubernetes"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/processors"

k8s "k8s.io/client-go/kubernetes"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This import should be placed before beats imports.

	"fmt"
	"time"

	k8s "k8s.io/client-go/kubernetes"

	"github.com/elastic/beats/libbeat/beat"
	"github.com/elastic/beats/libbeat/common"
	"github.com/elastic/beats/libbeat/common/kubernetes"
	"github.com/elastic/beats/libbeat/logp"
	"github.com/elastic/beats/libbeat/processors"

And I wonder if we should use another alias, k8s is basically the same as kubernetes, maybe k8sclient... Not sure.

)

const (
timeout = time.Second * 5
)

type kubernetesAnnotator struct {
watcher kubernetes.Watcher
indexers *Indexers
matchers *Matchers
cache *cache
watcher kubernetes.Watcher
indexers *Indexers
matchers *Matchers
cache *cache
kubernetesAvailable bool
}

func init() {
Expand All @@ -51,6 +54,16 @@ func init() {
Indexing.AddMatcher(FieldFormatMatcherName, NewFieldFormatMatcher)
}

func isKubernetesAvailable(client k8s.Interface) bool {
Comment thread
ChrsMark marked this conversation as resolved.
Outdated
server, err := client.Discovery().ServerVersion()
if err != nil {
logp.Info("%v: could not detect kubernetes env: %v", "add_kubernetes_metadata", err)
return false
}
logp.Info("%v: kubernetes env detected, with version: %v", "add_kubernetes_metadata", server)
return true
}

// New constructs a new add_kubernetes_metadata processor.
func New(cfg *common.Config) (processors.Processor, error) {
config := defaultKubernetesAnnotatorConfig()
Expand Down Expand Up @@ -91,9 +104,26 @@ func New(cfg *common.Config) (processors.Processor, error) {
return nil, fmt.Errorf("Can not initialize kubernetes plugin with zero matcher plugins")
}

processor := &kubernetesAnnotator{
indexers: indexers,
matchers: matchers,
cache: newCache(config.CleanupTimeout),
kubernetesAvailable: false,
}

client, err := kubernetes.GetKubernetesClient(config.KubeConfig)
if err != nil {
return nil, err
if kubernetes.IsInCluster(config.KubeConfig) {
logp.Err("%v: could not create kubernetes client using in_cluster config", "add_kubernetes_metadata")
} else {
logp.Err("%v: could not create kubernetes client using config: %v", "add_kubernetes_metadata", config.KubeConfig)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also these ones should be logged at debug level, because they are going to be logged in all non-kubernetes deployments.

}
processor.kubernetesAvailable = false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit. Set kubernetesAvailable to false already when initializing the processor (or just don't set it as is the zero value), and only set it to true when all checks have been done.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also here it is not needed to set processor.kubernetesAvailable = false.

Suggested change
processor.kubernetesAvailable = false

return processor, nil
}

if !isKubernetesAvailable(client) {
return processor, nil
}

config.Host = kubernetes.DiscoverKubernetesNode(config.Host, kubernetes.IsInCluster(config.KubeConfig), client)
Expand All @@ -111,12 +141,8 @@ func New(cfg *common.Config) (processors.Processor, error) {
return nil, err
}

processor := &kubernetesAnnotator{
watcher: watcher,
indexers: indexers,
matchers: matchers,
cache: newCache(config.CleanupTimeout),
}
processor.watcher = watcher
processor.kubernetesAvailable = true

watcher.AddEventHandler(kubernetes.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
Expand All @@ -139,6 +165,9 @@ func New(cfg *common.Config) (processors.Processor, error) {
}

func (k *kubernetesAnnotator) Run(event *beat.Event) (*beat.Event, error) {
if !k.kubernetesAvailable {
return event, nil
}
index := k.matchers.MetadataIndex(event.Fields)
if index == "" {
return event, nil
Expand Down
40 changes: 40 additions & 0 deletions libbeat/processors/add_kubernetes_metadata/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestAnnotatorDeepUpdate(t *testing.T) {
matchers: &Matchers{
matchers: []Matcher{matcher},
},
kubernetesAvailable: true,
}

processor.cache.set("foo", common.MapStr{
Expand Down Expand Up @@ -86,3 +87,42 @@ func TestAnnotatorDeepUpdate(t *testing.T) {
},
}, event.Fields)
}

// Test metadata are not included in the event
func TestAnnotatorWithNoKubernetesAvailable(t *testing.T) {
cfg := common.MustNewConfigFrom(map[string]interface{}{
"lookup_fields": []string{"kubernetes.pod.name"},
})
matcher, err := NewFieldMatcher(*cfg)
if err != nil {
t.Fatal(err)
}

processor := kubernetesAnnotator{
cache: newCache(10 * time.Second),
matchers: &Matchers{
matchers: []Matcher{matcher},
},
kubernetesAvailable: false,
}

intialEventMap := common.MapStr{
"kubernetes": common.MapStr{
"pod": common.MapStr{
"name": "foo",
"id": "pod_id",
"metrics": common.MapStr{
"a": 1,
"b": 2,
},
},
},
}

event, err := processor.Run(&beat.Event{
Fields: intialEventMap.Clone(),
})
assert.NoError(t, err)

assert.Equal(t, intialEventMap, event.Fields)
}
1 change: 1 addition & 0 deletions metricbeat/metricbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~

#================================ Logging =====================================

Expand Down
1 change: 1 addition & 0 deletions packetbeat/packetbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~

#================================ Logging =====================================

Expand Down
1 change: 1 addition & 0 deletions winlogbeat/winlogbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~

#================================ Logging =====================================

Expand Down
1 change: 1 addition & 0 deletions x-pack/auditbeat/auditbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~

#================================ Logging =====================================

Expand Down
1 change: 1 addition & 0 deletions x-pack/filebeat/filebeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~

#================================ Logging =====================================

Expand Down
1 change: 1 addition & 0 deletions x-pack/functionbeat/functionbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~

#================================ Logging =====================================

Expand Down
1 change: 1 addition & 0 deletions x-pack/metricbeat/metricbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~

#================================ Logging =====================================

Expand Down
1 change: 1 addition & 0 deletions x-pack/winlogbeat/winlogbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~

#================================ Logging =====================================

Expand Down