未验证 提交 f04135a8 编写于 作者: K KubeSphere CI Bot 提交者: GitHub

Merge pull request #2419 from wanjunlei/auditing-es

lazy initializing es client of auditing
...@@ -20,6 +20,7 @@ import ( ...@@ -20,6 +20,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"strings" "strings"
"sync"
"time" "time"
es5 "github.com/elastic/go-elasticsearch/v5" es5 "github.com/elastic/go-elasticsearch/v5"
...@@ -29,17 +30,30 @@ import ( ...@@ -29,17 +30,30 @@ import (
"kubesphere.io/kubesphere/pkg/simple/client/auditing" "kubesphere.io/kubesphere/pkg/simple/client/auditing"
) )
const (
ElasticV5 = "5"
ElasticV6 = "6"
ElasticV7 = "7"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary var json = jsoniter.ConfigCompatibleWithStandardLibrary
type Elasticsearch struct { type Elasticsearch struct {
c client host string
opts struct { version string
index string index string
}
c client
mux sync.Mutex
} }
func (es *Elasticsearch) SearchAuditingEvent(filter *auditing.Filter, from, size int64, func (es *Elasticsearch) SearchAuditingEvent(filter *auditing.Filter, from, size int64,
sort string) (*auditing.Events, error) { sort string) (*auditing.Events, error) {
if err := es.loadClient(); err != nil {
return &auditing.Events{}, err
}
queryPart := parseToQueryPart(filter) queryPart := parseToQueryPart(filter)
if sort == "" { if sort == "" {
sort = "desc" sort = "desc"
...@@ -59,7 +73,7 @@ func (es *Elasticsearch) SearchAuditingEvent(filter *auditing.Filter, from, size ...@@ -59,7 +73,7 @@ func (es *Elasticsearch) SearchAuditingEvent(filter *auditing.Filter, from, size
return nil, err return nil, err
} }
resp, err := es.c.ExSearch(&Request{ resp, err := es.c.ExSearch(&Request{
Index: es.opts.index, Index: es.index,
Body: bytes.NewBuffer(body), Body: bytes.NewBuffer(body),
}) })
if err != nil || resp == nil { if err != nil || resp == nil {
...@@ -80,6 +94,11 @@ func (es *Elasticsearch) SearchAuditingEvent(filter *auditing.Filter, from, size ...@@ -80,6 +94,11 @@ func (es *Elasticsearch) SearchAuditingEvent(filter *auditing.Filter, from, size
} }
func (es *Elasticsearch) CountOverTime(filter *auditing.Filter, interval string) (*auditing.Histogram, error) { func (es *Elasticsearch) CountOverTime(filter *auditing.Filter, interval string) (*auditing.Histogram, error) {
if err := es.loadClient(); err != nil {
return &auditing.Histogram{}, err
}
if interval == "" { if interval == "" {
interval = "15m" interval = "15m"
} }
...@@ -105,7 +124,7 @@ func (es *Elasticsearch) CountOverTime(filter *auditing.Filter, interval string) ...@@ -105,7 +124,7 @@ func (es *Elasticsearch) CountOverTime(filter *auditing.Filter, interval string)
return nil, err return nil, err
} }
resp, err := es.c.ExSearch(&Request{ resp, err := es.c.ExSearch(&Request{
Index: es.opts.index, Index: es.index,
Body: bytes.NewBuffer(body), Body: bytes.NewBuffer(body),
}) })
if err != nil || resp == nil { if err != nil || resp == nil {
...@@ -135,6 +154,11 @@ func (es *Elasticsearch) CountOverTime(filter *auditing.Filter, interval string) ...@@ -135,6 +154,11 @@ func (es *Elasticsearch) CountOverTime(filter *auditing.Filter, interval string)
} }
func (es *Elasticsearch) StatisticsOnResources(filter *auditing.Filter) (*auditing.Statistics, error) { func (es *Elasticsearch) StatisticsOnResources(filter *auditing.Filter) (*auditing.Statistics, error) {
if err := es.loadClient(); err != nil {
return &auditing.Statistics{}, err
}
queryPart := parseToQueryPart(filter) queryPart := parseToQueryPart(filter)
aggName := "resources_count" aggName := "resources_count"
aggsPart := map[string]interface{}{ aggsPart := map[string]interface{}{
...@@ -155,7 +179,7 @@ func (es *Elasticsearch) StatisticsOnResources(filter *auditing.Filter) (*auditi ...@@ -155,7 +179,7 @@ func (es *Elasticsearch) StatisticsOnResources(filter *auditing.Filter) (*auditi
return nil, err return nil, err
} }
resp, err := es.c.ExSearch(&Request{ resp, err := es.c.ExSearch(&Request{
Index: es.opts.index, Index: es.index,
Body: bytes.NewBuffer(body), Body: bytes.NewBuffer(body),
}) })
if err != nil || resp == nil { if err != nil || resp == nil {
...@@ -180,63 +204,89 @@ func (es *Elasticsearch) StatisticsOnResources(filter *auditing.Filter) (*auditi ...@@ -180,63 +204,89 @@ func (es *Elasticsearch) StatisticsOnResources(filter *auditing.Filter) (*auditi
} }
func NewClient(options *Options) (*Elasticsearch, error) { func NewClient(options *Options) (*Elasticsearch, error) {
es := &Elasticsearch{
host: options.Host,
version: options.Version,
index: fmt.Sprintf("%s*", options.IndexPrefix),
}
err := es.initEsClient(es.version)
return es, err
}
func (es *Elasticsearch) initEsClient(version string) error {
clientV5 := func() (*ClientV5, error) { clientV5 := func() (*ClientV5, error) {
c, err := es5.NewClient(es5.Config{Addresses: []string{options.Host}}) c, err := es5.NewClient(es5.Config{Addresses: []string{es.host}})
if err != nil { if err != nil {
return nil, err return nil, err
} }
return (*ClientV5)(c), nil return (*ClientV5)(c), nil
} }
clientV6 := func() (*ClientV6, error) { clientV6 := func() (*ClientV6, error) {
c, err := es6.NewClient(es6.Config{Addresses: []string{options.Host}}) c, err := es6.NewClient(es6.Config{Addresses: []string{es.host}})
if err != nil { if err != nil {
return nil, err return nil, err
} }
return (*ClientV6)(c), nil return (*ClientV6)(c), nil
} }
clientV7 := func() (*ClientV7, error) { clientV7 := func() (*ClientV7, error) {
c, err := es7.NewClient(es7.Config{Addresses: []string{options.Host}}) c, err := es7.NewClient(es7.Config{Addresses: []string{es.host}})
if err != nil { if err != nil {
return nil, err return nil, err
} }
return (*ClientV7)(c), nil return (*ClientV7)(c), nil
} }
var ( var err error
version = options.Version switch version {
es = Elasticsearch{} case ElasticV5:
err error es.c, err = clientV5()
) case ElasticV6:
es.opts.index = fmt.Sprintf("%s*", options.IndexPrefix) es.c, err = clientV6()
case ElasticV7:
es.c, err = clientV7()
case "":
es.c = nil
default:
err = fmt.Errorf("unsupported elasticsearch version %s", es.version)
}
return err
}
if options.Version == "" { func (es *Elasticsearch) loadClient() error {
var c5 *ClientV5
if c5, err = clientV5(); err == nil { // Check if Elasticsearch client has been initialized.
if version, err = c5.Version(); err == nil { if es.c != nil {
es.c = c5 return nil
}
}
} }
if err != nil {
return nil, err // Create Elasticsearch client.
es.mux.Lock()
defer es.mux.Unlock()
if es.c != nil {
return nil
} }
switch strings.Split(version, ".")[0] { c, e := es5.NewClient(es5.Config{Addresses: []string{es.host}})
case "5": if e != nil {
if es.c == nil { return e
es.c, err = clientV5() }
}
case "6": version, err := (*ClientV5)(c).Version()
es.c, err = clientV6() if err != nil {
case "7": return err
es.c, err = clientV7()
default:
err = fmt.Errorf("unsupported elasticsearch version %s", version)
} }
v := strings.Split(version, ".")[0]
err = es.initEsClient(v)
if err != nil { if err != nil {
return nil, err return err
} }
return &es, nil
es.version = v
return nil
} }
func parseToQueryPart(f *auditing.Filter) interface{} { func parseToQueryPart(f *auditing.Filter) interface{} {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册