From 2b30db43af1bacceff8cf68a1b085bcc1d3b00b7 Mon Sep 17 00:00:00 2001 From: huanggze Date: Thu, 26 Sep 2019 17:28:42 +0800 Subject: [PATCH] log: fix elastic version detection Signed-off-by: huanggze --- pkg/simple/client/elasticsearch/esclient.go | 14 ++++++++------ pkg/simple/client/elasticsearch/options.go | 2 +- .../elasticsearch/versions/v5/elasticsearch.go | 7 ++++++- .../elasticsearch/versions/v6/elasticsearch.go | 7 ++++++- .../elasticsearch/versions/v7/elasticsearch.go | 7 ++++++- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/pkg/simple/client/elasticsearch/esclient.go b/pkg/simple/client/elasticsearch/esclient.go index 59dad211f..6c19fb542 100644 --- a/pkg/simple/client/elasticsearch/esclient.go +++ b/pkg/simple/client/elasticsearch/esclient.go @@ -60,7 +60,7 @@ type ElasticSearchClient struct { } func NewLoggingClient(options *ElasticSearchOptions) (*ElasticSearchClient, error) { - version := "6" + var version, index string esClient := &ElasticSearchClient{} if options.Version == "" { @@ -69,23 +69,25 @@ func NewLoggingClient(options *ElasticSearchOptions) (*ElasticSearchClient, erro if err != nil { return nil, err } + } else { + version = options.Version } if options.LogstashFormat { if options.LogstashPrefix != "" { - options.Index = options.LogstashPrefix + index = options.LogstashPrefix } else { - options.Index = "logstash" + index = "logstash" } } switch version { case ElasticV5: - esClient.client = v5.New(options.Host, options.Index) + esClient.client = v5.New(options.Host, index) case ElasticV6: - esClient.client = v6.New(options.Host, options.Index) + esClient.client = v6.New(options.Host, index) case ElasticV7: - esClient.client = v7.New(options.Host, options.Index) + esClient.client = v7.New(options.Host, index) default: return nil, fmt.Errorf("unsupported elasticsearch version %s", version) } diff --git a/pkg/simple/client/elasticsearch/options.go b/pkg/simple/client/elasticsearch/options.go index 5de2c1e26..2ac7bb31b 100644 --- a/pkg/simple/client/elasticsearch/options.go +++ b/pkg/simple/client/elasticsearch/options.go @@ -21,7 +21,7 @@ func NewElasticSearchOptions() *ElasticSearchOptions { Index: "fluentbit", LogstashPrefix: "", Match: "kube.*", - Version: "6", + Version: "", } } diff --git a/pkg/simple/client/elasticsearch/versions/v5/elasticsearch.go b/pkg/simple/client/elasticsearch/versions/v5/elasticsearch.go index 45cad63b2..b5865c95b 100644 --- a/pkg/simple/client/elasticsearch/versions/v5/elasticsearch.go +++ b/pkg/simple/client/elasticsearch/versions/v5/elasticsearch.go @@ -8,6 +8,7 @@ import ( "github.com/elastic/go-elasticsearch/v5" "github.com/elastic/go-elasticsearch/v5/esapi" "io/ioutil" + "k8s.io/klog" "time" ) @@ -17,9 +18,13 @@ type Elastic struct { } func New(address string, index string) *Elastic { - client, _ := elasticsearch.NewClient(elasticsearch.Config{ + client, err := elasticsearch.NewClient(elasticsearch.Config{ Addresses: []string{address}, }) + if err != nil { + klog.Error(err) + return nil + } return &Elastic{client: client, index: index} } diff --git a/pkg/simple/client/elasticsearch/versions/v6/elasticsearch.go b/pkg/simple/client/elasticsearch/versions/v6/elasticsearch.go index 80184497a..33e55261c 100644 --- a/pkg/simple/client/elasticsearch/versions/v6/elasticsearch.go +++ b/pkg/simple/client/elasticsearch/versions/v6/elasticsearch.go @@ -8,6 +8,7 @@ import ( "github.com/elastic/go-elasticsearch/v6" "github.com/elastic/go-elasticsearch/v6/esapi" "io/ioutil" + "k8s.io/klog" "time" ) @@ -17,9 +18,13 @@ type Elastic struct { } func New(address string, index string) *Elastic { - client, _ := elasticsearch.NewClient(elasticsearch.Config{ + client, err := elasticsearch.NewClient(elasticsearch.Config{ Addresses: []string{address}, }) + if err != nil { + klog.Error(err) + return nil + } return &Elastic{Client: client, index: index} } diff --git a/pkg/simple/client/elasticsearch/versions/v7/elasticsearch.go b/pkg/simple/client/elasticsearch/versions/v7/elasticsearch.go index 7777d046d..ff0d69916 100644 --- a/pkg/simple/client/elasticsearch/versions/v7/elasticsearch.go +++ b/pkg/simple/client/elasticsearch/versions/v7/elasticsearch.go @@ -8,6 +8,7 @@ import ( "github.com/elastic/go-elasticsearch/v7" "github.com/elastic/go-elasticsearch/v7/esapi" "io/ioutil" + "k8s.io/klog" "time" ) @@ -17,9 +18,13 @@ type Elastic struct { } func New(address string, index string) *Elastic { - client, _ := elasticsearch.NewClient(elasticsearch.Config{ + client, err := elasticsearch.NewClient(elasticsearch.Config{ Addresses: []string{address}, }) + if err != nil { + klog.Error(err) + return nil + } return &Elastic{client: client, index: index} }