log: fix elastic version detection

Signed-off-by: huanggze <loganhuang@yunify.com>
This commit is contained in:
huanggze
2019-09-26 17:28:42 +08:00
parent 31e23d73c3
commit 2b30db43af
5 changed files with 27 additions and 10 deletions

View File

@@ -60,7 +60,7 @@ type ElasticSearchClient struct {
} }
func NewLoggingClient(options *ElasticSearchOptions) (*ElasticSearchClient, error) { func NewLoggingClient(options *ElasticSearchOptions) (*ElasticSearchClient, error) {
version := "6" var version, index string
esClient := &ElasticSearchClient{} esClient := &ElasticSearchClient{}
if options.Version == "" { if options.Version == "" {
@@ -69,23 +69,25 @@ func NewLoggingClient(options *ElasticSearchOptions) (*ElasticSearchClient, erro
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else {
version = options.Version
} }
if options.LogstashFormat { if options.LogstashFormat {
if options.LogstashPrefix != "" { if options.LogstashPrefix != "" {
options.Index = options.LogstashPrefix index = options.LogstashPrefix
} else { } else {
options.Index = "logstash" index = "logstash"
} }
} }
switch version { switch version {
case ElasticV5: case ElasticV5:
esClient.client = v5.New(options.Host, options.Index) esClient.client = v5.New(options.Host, index)
case ElasticV6: case ElasticV6:
esClient.client = v6.New(options.Host, options.Index) esClient.client = v6.New(options.Host, index)
case ElasticV7: case ElasticV7:
esClient.client = v7.New(options.Host, options.Index) esClient.client = v7.New(options.Host, index)
default: default:
return nil, fmt.Errorf("unsupported elasticsearch version %s", version) return nil, fmt.Errorf("unsupported elasticsearch version %s", version)
} }

View File

@@ -21,7 +21,7 @@ func NewElasticSearchOptions() *ElasticSearchOptions {
Index: "fluentbit", Index: "fluentbit",
LogstashPrefix: "", LogstashPrefix: "",
Match: "kube.*", Match: "kube.*",
Version: "6", Version: "",
} }
} }

View File

@@ -8,6 +8,7 @@ import (
"github.com/elastic/go-elasticsearch/v5" "github.com/elastic/go-elasticsearch/v5"
"github.com/elastic/go-elasticsearch/v5/esapi" "github.com/elastic/go-elasticsearch/v5/esapi"
"io/ioutil" "io/ioutil"
"k8s.io/klog"
"time" "time"
) )
@@ -17,9 +18,13 @@ type Elastic struct {
} }
func New(address string, index string) *Elastic { func New(address string, index string) *Elastic {
client, _ := elasticsearch.NewClient(elasticsearch.Config{ client, err := elasticsearch.NewClient(elasticsearch.Config{
Addresses: []string{address}, Addresses: []string{address},
}) })
if err != nil {
klog.Error(err)
return nil
}
return &Elastic{client: client, index: index} return &Elastic{client: client, index: index}
} }

View File

@@ -8,6 +8,7 @@ import (
"github.com/elastic/go-elasticsearch/v6" "github.com/elastic/go-elasticsearch/v6"
"github.com/elastic/go-elasticsearch/v6/esapi" "github.com/elastic/go-elasticsearch/v6/esapi"
"io/ioutil" "io/ioutil"
"k8s.io/klog"
"time" "time"
) )
@@ -17,9 +18,13 @@ type Elastic struct {
} }
func New(address string, index string) *Elastic { func New(address string, index string) *Elastic {
client, _ := elasticsearch.NewClient(elasticsearch.Config{ client, err := elasticsearch.NewClient(elasticsearch.Config{
Addresses: []string{address}, Addresses: []string{address},
}) })
if err != nil {
klog.Error(err)
return nil
}
return &Elastic{Client: client, index: index} return &Elastic{Client: client, index: index}
} }

View File

@@ -8,6 +8,7 @@ import (
"github.com/elastic/go-elasticsearch/v7" "github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi" "github.com/elastic/go-elasticsearch/v7/esapi"
"io/ioutil" "io/ioutil"
"k8s.io/klog"
"time" "time"
) )
@@ -17,9 +18,13 @@ type Elastic struct {
} }
func New(address string, index string) *Elastic { func New(address string, index string) *Elastic {
client, _ := elasticsearch.NewClient(elasticsearch.Config{ client, err := elasticsearch.NewClient(elasticsearch.Config{
Addresses: []string{address}, Addresses: []string{address},
}) })
if err != nil {
klog.Error(err)
return nil
}
return &Elastic{client: client, index: index} return &Elastic{client: client, index: index}
} }