Merge pull request #2466 from wanjunlei/auditing-es
reuse package esutil to improve auditing log search performance
This commit is contained in:
@@ -57,7 +57,7 @@ func (e Error) Error() string {
|
||||
type ClientV5 es5.Client
|
||||
|
||||
func (c *ClientV5) ExSearch(r *Request) (*Response, error) {
|
||||
return c.parse(c.Search(c.Search.WithIndex(r.Index), c.Search.WithBody(r.Body)))
|
||||
return c.parse(c.Search(c.Search.WithIndex(r.Index), c.Search.WithBody(r.Body), c.Search.WithIgnoreUnavailable(true)))
|
||||
}
|
||||
func (c *ClientV5) parse(resp *es5api.Response, err error) (*Response, error) {
|
||||
if err != nil {
|
||||
@@ -105,7 +105,7 @@ func (c *ClientV5) Version() (string, error) {
|
||||
type ClientV6 es6.Client
|
||||
|
||||
func (c *ClientV6) ExSearch(r *Request) (*Response, error) {
|
||||
return c.parse(c.Search(c.Search.WithIndex(r.Index), c.Search.WithBody(r.Body)))
|
||||
return c.parse(c.Search(c.Search.WithIndex(r.Index), c.Search.WithBody(r.Body), c.Search.WithIgnoreUnavailable(true)))
|
||||
}
|
||||
func (c *ClientV6) parse(resp *es6api.Response, err error) (*Response, error) {
|
||||
if err != nil {
|
||||
@@ -136,7 +136,7 @@ func (c *ClientV6) parse(resp *es6api.Response, err error) (*Response, error) {
|
||||
type ClientV7 es7.Client
|
||||
|
||||
func (c *ClientV7) ExSearch(r *Request) (*Response, error) {
|
||||
return c.parse(c.Search(c.Search.WithIndex(r.Index), c.Search.WithBody(r.Body)))
|
||||
return c.parse(c.Search(c.Search.WithIndex(r.Index), c.Search.WithBody(r.Body), c.Search.WithIgnoreUnavailable(true)))
|
||||
}
|
||||
func (c *ClientV7) parse(resp *es7api.Response, err error) (*Response, error) {
|
||||
if err != nil {
|
||||
|
||||
@@ -19,6 +19,7 @@ package elasticsearch
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"kubesphere.io/kubesphere/pkg/utils/esutil"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -73,7 +74,7 @@ func (es *Elasticsearch) SearchAuditingEvent(filter *auditing.Filter, from, size
|
||||
return nil, err
|
||||
}
|
||||
resp, err := es.c.ExSearch(&Request{
|
||||
Index: es.index,
|
||||
Index: resolveIndexNames(es.index, filter.StartTime, filter.EndTime),
|
||||
Body: bytes.NewBuffer(body),
|
||||
})
|
||||
if err != nil || resp == nil {
|
||||
@@ -124,7 +125,7 @@ func (es *Elasticsearch) CountOverTime(filter *auditing.Filter, interval string)
|
||||
return nil, err
|
||||
}
|
||||
resp, err := es.c.ExSearch(&Request{
|
||||
Index: es.index,
|
||||
Index: resolveIndexNames(es.index, filter.StartTime, filter.EndTime),
|
||||
Body: bytes.NewBuffer(body),
|
||||
})
|
||||
if err != nil || resp == nil {
|
||||
@@ -179,7 +180,7 @@ func (es *Elasticsearch) StatisticsOnResources(filter *auditing.Filter) (*auditi
|
||||
return nil, err
|
||||
}
|
||||
resp, err := es.c.ExSearch(&Request{
|
||||
Index: es.index,
|
||||
Index: resolveIndexNames(es.index, filter.StartTime, filter.EndTime),
|
||||
Body: bytes.NewBuffer(body),
|
||||
})
|
||||
if err != nil || resp == nil {
|
||||
@@ -207,7 +208,7 @@ func NewClient(options *Options) (*Elasticsearch, error) {
|
||||
es := &Elasticsearch{
|
||||
host: options.Host,
|
||||
version: options.Version,
|
||||
index: fmt.Sprintf("%s*", options.IndexPrefix),
|
||||
index: options.IndexPrefix,
|
||||
}
|
||||
|
||||
err := es.initEsClient(es.version)
|
||||
@@ -452,3 +453,14 @@ func parseToQueryPart(f *auditing.Filter) interface{} {
|
||||
|
||||
return queryBody
|
||||
}
|
||||
|
||||
func resolveIndexNames(prefix string, start, end *time.Time) string {
|
||||
var s, e time.Time
|
||||
if start != nil {
|
||||
s = *start
|
||||
}
|
||||
if end != nil {
|
||||
e = *end
|
||||
}
|
||||
return esutil.ResolveIndexNames(prefix, s, e)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user