-
Notifications
You must be signed in to change notification settings - Fork 5k
Add k8s metadata autodetection #13473
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
Changes from 9 commits
4fd9d44
894f5b0
2a9577f
cbd16eb
edf9793
3c878ef
fccdf5e
965eff1
442f348
9cc29e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -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) { | ||||
|
|
||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||
|
|
||||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -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" | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This import should be placed before beats imports. And I wonder if we should use another alias, |
||||
| ) | ||||
|
|
||||
| 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() { | ||||
|
|
@@ -51,6 +54,16 @@ func init() { | |||
| Indexing.AddMatcher(FieldFormatMatcherName, NewFieldFormatMatcher) | ||||
| } | ||||
|
|
||||
| func isKubernetesAvailable(client k8s.Interface) bool { | ||||
|
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() | ||||
|
|
@@ -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) | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit. Set
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here it is not needed to set
Suggested change
|
||||
| return processor, nil | ||||
| } | ||||
|
|
||||
| if !isKubernetesAvailable(client) { | ||||
| return processor, nil | ||||
| } | ||||
|
|
||||
| config.Host = kubernetes.DiscoverKubernetesNode(config.Host, kubernetes.IsInCluster(config.KubeConfig), client) | ||||
|
|
@@ -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{}) { | ||||
|
|
@@ -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 | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit.