Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
f04135a8
K
kubesphere
项目概览
水淹萌龙
/
kubesphere
与 Fork 源项目一致
Fork自
KubeSphere / kubesphere
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kubesphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
未验证
提交
f04135a8
编写于
7月 16, 2020
作者:
K
KubeSphere CI Bot
提交者:
GitHub
7月 16, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2419 from wanjunlei/auditing-es
lazy initializing es client of auditing
上级
9e1dc89c
1c436f8b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
88 addition
and
38 deletion
+88
-38
pkg/simple/client/auditing/elasticsearch/elasticsearch.go
pkg/simple/client/auditing/elasticsearch/elasticsearch.go
+88
-38
未找到文件。
pkg/simple/client/auditing/elasticsearch/elasticsearch.go
浏览文件 @
f04135a8
...
...
@@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"strings"
"sync"
"time"
es5
"github.com/elastic/go-elasticsearch/v5"
...
...
@@ -29,17 +30,30 @@ import (
"kubesphere.io/kubesphere/pkg/simple/client/auditing"
)
const
(
ElasticV5
=
"5"
ElasticV6
=
"6"
ElasticV7
=
"7"
)
var
json
=
jsoniter
.
ConfigCompatibleWithStandardLibrary
type
Elasticsearch
struct
{
c
client
opts
struct
{
index
string
}
host
string
version
string
index
string
c
client
mux
sync
.
Mutex
}
func
(
es
*
Elasticsearch
)
SearchAuditingEvent
(
filter
*
auditing
.
Filter
,
from
,
size
int64
,
sort
string
)
(
*
auditing
.
Events
,
error
)
{
if
err
:=
es
.
loadClient
();
err
!=
nil
{
return
&
auditing
.
Events
{},
err
}
queryPart
:=
parseToQueryPart
(
filter
)
if
sort
==
""
{
sort
=
"desc"
...
...
@@ -59,7 +73,7 @@ func (es *Elasticsearch) SearchAuditingEvent(filter *auditing.Filter, from, size
return
nil
,
err
}
resp
,
err
:=
es
.
c
.
ExSearch
(
&
Request
{
Index
:
es
.
opts
.
index
,
Index
:
es
.
index
,
Body
:
bytes
.
NewBuffer
(
body
),
})
if
err
!=
nil
||
resp
==
nil
{
...
...
@@ -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
)
{
if
err
:=
es
.
loadClient
();
err
!=
nil
{
return
&
auditing
.
Histogram
{},
err
}
if
interval
==
""
{
interval
=
"15m"
}
...
...
@@ -105,7 +124,7 @@ func (es *Elasticsearch) CountOverTime(filter *auditing.Filter, interval string)
return
nil
,
err
}
resp
,
err
:=
es
.
c
.
ExSearch
(
&
Request
{
Index
:
es
.
opts
.
index
,
Index
:
es
.
index
,
Body
:
bytes
.
NewBuffer
(
body
),
})
if
err
!=
nil
||
resp
==
nil
{
...
...
@@ -135,6 +154,11 @@ func (es *Elasticsearch) CountOverTime(filter *auditing.Filter, interval string)
}
func
(
es
*
Elasticsearch
)
StatisticsOnResources
(
filter
*
auditing
.
Filter
)
(
*
auditing
.
Statistics
,
error
)
{
if
err
:=
es
.
loadClient
();
err
!=
nil
{
return
&
auditing
.
Statistics
{},
err
}
queryPart
:=
parseToQueryPart
(
filter
)
aggName
:=
"resources_count"
aggsPart
:=
map
[
string
]
interface
{}{
...
...
@@ -155,7 +179,7 @@ func (es *Elasticsearch) StatisticsOnResources(filter *auditing.Filter) (*auditi
return
nil
,
err
}
resp
,
err
:=
es
.
c
.
ExSearch
(
&
Request
{
Index
:
es
.
opts
.
index
,
Index
:
es
.
index
,
Body
:
bytes
.
NewBuffer
(
body
),
})
if
err
!=
nil
||
resp
==
nil
{
...
...
@@ -180,63 +204,89 @@ func (es *Elasticsearch) StatisticsOnResources(filter *auditing.Filter) (*auditi
}
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
)
{
c
,
err
:=
es5
.
NewClient
(
es5
.
Config
{
Addresses
:
[]
string
{
options
.
H
ost
}})
c
,
err
:=
es5
.
NewClient
(
es5
.
Config
{
Addresses
:
[]
string
{
es
.
h
ost
}})
if
err
!=
nil
{
return
nil
,
err
}
return
(
*
ClientV5
)(
c
),
nil
}
clientV6
:=
func
()
(
*
ClientV6
,
error
)
{
c
,
err
:=
es6
.
NewClient
(
es6
.
Config
{
Addresses
:
[]
string
{
options
.
H
ost
}})
c
,
err
:=
es6
.
NewClient
(
es6
.
Config
{
Addresses
:
[]
string
{
es
.
h
ost
}})
if
err
!=
nil
{
return
nil
,
err
}
return
(
*
ClientV6
)(
c
),
nil
}
clientV7
:=
func
()
(
*
ClientV7
,
error
)
{
c
,
err
:=
es7
.
NewClient
(
es7
.
Config
{
Addresses
:
[]
string
{
options
.
H
ost
}})
c
,
err
:=
es7
.
NewClient
(
es7
.
Config
{
Addresses
:
[]
string
{
es
.
h
ost
}})
if
err
!=
nil
{
return
nil
,
err
}
return
(
*
ClientV7
)(
c
),
nil
}
var
(
version
=
options
.
Version
es
=
Elasticsearch
{}
err
error
)
es
.
opts
.
index
=
fmt
.
Sprintf
(
"%s*"
,
options
.
IndexPrefix
)
var
err
error
switch
version
{
case
ElasticV5
:
es
.
c
,
err
=
clientV5
()
case
ElasticV6
:
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
==
""
{
var
c5
*
ClientV5
if
c5
,
err
=
clientV5
();
err
==
nil
{
if
version
,
err
=
c5
.
Version
();
err
==
nil
{
es
.
c
=
c5
}
}
func
(
es
*
Elasticsearch
)
loadClient
()
error
{
// Check if Elasticsearch client has been initialized.
if
es
.
c
!=
nil
{
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
]
{
case
"5"
:
if
es
.
c
==
nil
{
es
.
c
,
err
=
clientV5
()
}
case
"6"
:
es
.
c
,
err
=
clientV6
()
case
"7"
:
es
.
c
,
err
=
clientV7
()
default
:
err
=
fmt
.
Errorf
(
"unsupported elasticsearch version %s"
,
version
)
c
,
e
:=
es5
.
NewClient
(
es5
.
Config
{
Addresses
:
[]
string
{
es
.
host
}})
if
e
!=
nil
{
return
e
}
version
,
err
:=
(
*
ClientV5
)(
c
)
.
Version
()
if
err
!=
nil
{
return
err
}
v
:=
strings
.
Split
(
version
,
"."
)[
0
]
err
=
es
.
initEsClient
(
v
)
if
err
!=
nil
{
return
nil
,
err
return
err
}
return
&
es
,
nil
es
.
version
=
v
return
nil
}
func
parseToQueryPart
(
f
*
auditing
.
Filter
)
interface
{}
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录