From e20bedadaca99ac6550936f5530274ee0583cb3b Mon Sep 17 00:00:00 2001 From: Jeff Peeler Date: Mon, 30 Oct 2017 22:36:40 -0400 Subject: [PATCH] Remove hard-coded namespace from k8s config The default namespace should be populated by the Kubernetes client. In the case of in-cluster config, the namespace will be read from the /var/run/secrets/kubernetes.io/serviceaccount/namespace file. (Note that this assumes the ServiceAccount admission controller is in use.) In the case of parsing a k8s configuration file, the default namespace is set to the client default of "default". Minor spelling correction of Kubernetes and some additional error information was changed as well. Closes #5378 --- CHANGELOG.asciidoc | 1 + libbeat/processors/add_kubernetes_metadata/config.go | 3 +-- libbeat/processors/add_kubernetes_metadata/kubernetes.go | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 429e4b0e549..8e5c016585d 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -42,6 +42,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta2...master[Check the HEAD di - Do not require template if index change and template disabled {pull}5319[5319] - Correctly send configured `Host` header to the remote server. {issue}4842[4842] - Fix missing ACK in redis output. {issue}5404[5404] +- Change add_kubernetes_metadata to attempt detection of namespace. {pull}5482[5482] *Auditbeat* diff --git a/libbeat/processors/add_kubernetes_metadata/config.go b/libbeat/processors/add_kubernetes_metadata/config.go index 6f1c1f0b898..adeba81a284 100644 --- a/libbeat/processors/add_kubernetes_metadata/config.go +++ b/libbeat/processors/add_kubernetes_metadata/config.go @@ -30,12 +30,11 @@ type Enabled struct { type PluginConfig []map[string]common.Config -func defaultKuberentesAnnotatorConfig() kubeAnnotatorConfig { +func defaultKubernetesAnnotatorConfig() kubeAnnotatorConfig { return kubeAnnotatorConfig{ InCluster: true, SyncPeriod: 1 * time.Second, CleanupTimeout: 60 * time.Second, - Namespace: "kube-system", DefaultMatchers: Enabled{true}, DefaultIndexers: Enabled{true}, } diff --git a/libbeat/processors/add_kubernetes_metadata/kubernetes.go b/libbeat/processors/add_kubernetes_metadata/kubernetes.go index a98b51a07ce..5780aa99cee 100644 --- a/libbeat/processors/add_kubernetes_metadata/kubernetes.go +++ b/libbeat/processors/add_kubernetes_metadata/kubernetes.go @@ -44,7 +44,7 @@ func init() { func newKubernetesAnnotator(cfg *common.Config) (processors.Processor, error) { cfgwarn.Beta("The kubernetes processor is beta") - config := defaultKuberentesAnnotatorConfig() + config := defaultKubernetesAnnotatorConfig() err := cfg.Unpack(&config) if err != nil { @@ -87,7 +87,7 @@ func newKubernetesAnnotator(cfg *common.Config) (processors.Processor, error) { if config.InCluster == true { client, err = k8s.NewInClusterClient() if err != nil { - return nil, fmt.Errorf("Unable to get in cluster configuration") + return nil, fmt.Errorf("Unable to get in cluster configuration: %v", err) } } else { data, err := ioutil.ReadFile(config.KubeConfig) @@ -109,11 +109,11 @@ func newKubernetesAnnotator(cfg *common.Config) (processors.Processor, error) { ctx := context.Background() if config.Host == "" { podName := os.Getenv("HOSTNAME") - logp.Info("Using pod name %s and namespace %s", podName, config.Namespace) + logp.Info("Using pod name %s and namespace %s", podName, client.Namespace) if podName == "localhost" { config.Host = "localhost" } else { - pod, error := client.CoreV1().GetPod(ctx, podName, config.Namespace) + pod, error := client.CoreV1().GetPod(ctx, podName, client.Namespace) if error != nil { logp.Err("Querying for pod failed with error: ", error.Error()) logp.Info("Unable to find pod, setting host to localhost")