Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
b543ae1a
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看板
提交
b543ae1a
编写于
12月 14, 2020
作者:
W
wanjunlei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix bug get goroutine for audit timeout
Signed-off-by:
N
wanjunlei
<
wanjunlei@yunify.com
>
上级
a314b31b
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
138 addition
and
65 deletion
+138
-65
pkg/apiserver/apiserver.go
pkg/apiserver/apiserver.go
+1
-1
pkg/apiserver/auditing/backend.go
pkg/apiserver/auditing/backend.go
+113
-49
pkg/apiserver/auditing/types.go
pkg/apiserver/auditing/types.go
+6
-9
pkg/simple/client/auditing/elasticsearch/options.go
pkg/simple/client/auditing/elasticsearch/options.go
+18
-6
未找到文件。
pkg/apiserver/apiserver.go
浏览文件 @
b543ae1a
...
...
@@ -281,7 +281,7 @@ func (s *APIServer) buildHandlerChain(stopCh <-chan struct{}) {
if
s
.
Config
.
AuditingOptions
.
Enable
{
handler
=
filters
.
WithAuditing
(
handler
,
audit
.
NewAuditing
(
s
.
InformerFactory
,
s
.
Config
.
AuditingOptions
.
WebhookUrl
,
stopCh
))
audit
.
NewAuditing
(
s
.
InformerFactory
,
s
.
Config
.
AuditingOptions
,
stopCh
))
}
var
authorizers
authorizer
.
Authorizer
...
...
pkg/apiserver/auditing/backend.go
浏览文件 @
b543ae1a
...
...
@@ -23,42 +23,62 @@ import (
"encoding/json"
"k8s.io/klog"
"kubesphere.io/kubesphere/pkg/apiserver/auditing/v1alpha1"
options
"kubesphere.io/kubesphere/pkg/simple/client/auditing/elasticsearch"
"net/http"
"time"
)
const
(
WaitTimeout
=
time
.
Second
WebhookURL
=
"https://kube-auditing-webhook-svc.kubesphere-logging-system.svc:443/audit/webhook/event"
WaitTimeout
=
time
.
Second
SendTimeout
=
time
.
Second
*
3
DefaultGoroutinesNum
=
100
DefaultBatchSize
=
100
DefaultBatchWait
=
time
.
Second
*
3
WebhookURL
=
"https://kube-auditing-webhook-svc.kubesphere-logging-system.svc:443/audit/webhook/event"
)
type
Backend
struct
{
url
string
channelCapacity
int
semCh
chan
interface
{}
cache
chan
*
v1alpha1
.
EventList
client
http
.
Client
sendTimeout
time
.
Duration
waitTimeout
time
.
Duration
stopCh
<-
chan
struct
{}
url
string
semCh
chan
interface
{}
cache
chan
*
v1alpha1
.
Event
client
http
.
Client
sendTimeout
time
.
Duration
waitTimeout
time
.
Duration
maxBatchSize
int
maxBatchWait
time
.
Duration
stopCh
<-
chan
struct
{}
}
func
NewBackend
(
url
string
,
channelCapacity
int
,
cache
chan
*
v1alpha1
.
EventList
,
sendTimeout
time
.
Duration
,
stopCh
<-
chan
struct
{})
*
Backend
{
func
NewBackend
(
opts
*
options
.
Options
,
cache
chan
*
v1alpha1
.
Event
,
stopCh
<-
chan
struct
{})
*
Backend
{
b
:=
Backend
{
url
:
u
rl
,
semCh
:
make
(
chan
interface
{},
channelCapacity
)
,
c
hannelCapacity
:
channelCapacity
,
waitTimeout
:
Wait
Timeout
,
cache
:
cach
e
,
sendTimeout
:
sendTimeou
t
,
stopCh
:
stopCh
,
url
:
opts
.
WebhookU
rl
,
waitTimeout
:
WaitTimeout
,
c
ache
:
cache
,
sendTimeout
:
Send
Timeout
,
maxBatchSize
:
opts
.
MaxBatchSiz
e
,
maxBatchWait
:
opts
.
MaxBatchWai
t
,
stopCh
:
stopCh
,
}
if
len
(
b
.
url
)
==
0
{
b
.
url
=
WebhookURL
}
if
b
.
maxBatchWait
==
0
{
b
.
maxBatchWait
=
DefaultBatchWait
}
if
b
.
maxBatchSize
==
0
{
b
.
maxBatchSize
=
DefaultBatchSize
}
goroutinesNum
:=
opts
.
GoroutinesNum
if
goroutinesNum
==
0
{
goroutinesNum
=
DefaultGoroutinesNum
}
b
.
semCh
=
make
(
chan
interface
{},
goroutinesNum
)
b
.
client
=
http
.
Client
{
Transport
:
&
http
.
Transport
{
TLSClientConfig
:
&
tls
.
Config
{
...
...
@@ -76,53 +96,97 @@ func NewBackend(url string, channelCapacity int, cache chan *v1alpha1.EventList,
func
(
b
*
Backend
)
worker
()
{
for
{
events
:=
b
.
getEvents
()
if
events
==
nil
{
break
}
var
event
*
v1alpha1
.
EventList
if
len
(
events
.
Items
)
==
0
{
continue
}
go
b
.
sendEvents
(
events
)
}
}
func
(
b
*
Backend
)
getEvents
()
*
v1alpha1
.
EventList
{
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
b
.
maxBatchWait
)
defer
cancel
()
events
:=
&
v1alpha1
.
EventList
{}
for
{
select
{
case
event
=
<-
b
.
cache
:
case
event
:
=
<-
b
.
cache
:
if
event
==
nil
{
break
}
events
.
Items
=
append
(
events
.
Items
,
*
event
)
if
len
(
events
.
Items
)
>=
b
.
maxBatchSize
{
return
events
}
case
<-
ctx
.
Done
()
:
return
events
case
<-
b
.
stopCh
:
break
return
nil
}
}
}
send
:=
func
(
event
*
v1alpha1
.
EventList
)
{
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
b
.
waitTimeout
)
defer
cancel
()
func
(
b
*
Backend
)
sendEvents
(
events
*
v1alpha1
.
EventList
)
{
select
{
case
<-
ctx
.
Done
()
:
klog
.
Errorf
(
"get goroutine for audit(%s) timeout"
,
event
.
Items
[
0
]
.
AuditID
)
return
case
b
.
semCh
<-
struct
{}{}
:
}
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
b
.
sendTimeout
)
defer
cancel
()
defer
func
()
{
<-
b
.
semCh
}()
stopCh
:=
make
(
chan
struct
{})
bs
,
err
:=
b
.
eventToBytes
(
event
)
if
err
!=
nil
{
klog
.
V
(
6
)
.
Infof
(
"json marshal error, %s"
,
err
)
return
}
send
:=
func
()
{
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
b
.
waitTimeout
)
defer
cancel
()
klog
.
V
(
8
)
.
Infof
(
"%s"
,
string
(
bs
))
select
{
case
<-
ctx
.
Done
()
:
klog
.
Error
(
"get goroutine timeout"
)
return
case
b
.
semCh
<-
struct
{}{}
:
}
response
,
err
:=
b
.
client
.
Post
(
b
.
url
,
"application/json"
,
bytes
.
NewBuffer
(
bs
)
)
if
err
!=
nil
{
klog
.
Errorf
(
"send audit event[%s] error, %s"
,
event
.
Items
[
0
]
.
AuditID
,
err
)
return
}
start
:=
time
.
Now
(
)
defer
func
()
{
stopCh
<-
struct
{}{}
klog
.
V
(
8
)
.
Infof
(
"send %d auditing logs used %d"
,
len
(
events
.
Items
),
time
.
Now
()
.
Sub
(
start
)
.
Milliseconds
())
}()
if
response
.
StatusCode
!=
http
.
StatusOK
{
klog
.
Errorf
(
"send audit event[%s] error[%d]"
,
event
.
Items
[
0
]
.
AuditID
,
response
.
StatusCode
)
return
}
bs
,
err
:=
b
.
eventToBytes
(
events
)
if
err
!=
nil
{
klog
.
V
(
6
)
.
Infof
(
"json marshal error, %s"
,
err
)
return
}
go
send
(
event
)
klog
.
V
(
8
)
.
Infof
(
"%s"
,
string
(
bs
))
response
,
err
:=
b
.
client
.
Post
(
b
.
url
,
"application/json"
,
bytes
.
NewBuffer
(
bs
))
if
err
!=
nil
{
klog
.
Errorf
(
"send audit events error, %s"
,
err
)
return
}
if
response
.
StatusCode
!=
http
.
StatusOK
{
klog
.
Errorf
(
"send audit events error[%d]"
,
response
.
StatusCode
)
return
}
}
go
send
()
defer
func
()
{
<-
b
.
semCh
}()
select
{
case
<-
ctx
.
Done
()
:
klog
.
Error
(
"send audit events timeout"
)
case
<-
stopCh
:
}
}
...
...
pkg/apiserver/auditing/types.go
浏览文件 @
b543ae1a
...
...
@@ -36,6 +36,7 @@ import (
"kubesphere.io/kubesphere/pkg/informers"
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/devops"
options
"kubesphere.io/kubesphere/pkg/simple/client/auditing/elasticsearch"
"kubesphere.io/kubesphere/pkg/utils/iputil"
"net"
"net/http"
...
...
@@ -46,8 +47,6 @@ const (
DefaultWebhook
=
"kube-auditing-webhook"
DefaultCacheCapacity
=
10000
CacheTimeout
=
time
.
Second
SendTimeout
=
time
.
Second
*
3
ChannelCapacity
=
10
)
type
Auditing
interface
{
...
...
@@ -60,19 +59,19 @@ type Auditing interface {
type
auditing
struct
{
webhookLister
v1alpha1
.
WebhookLister
devopsGetter
v1alpha3
.
Interface
cache
chan
*
auditv1alpha1
.
Event
List
cache
chan
*
auditv1alpha1
.
Event
backend
*
Backend
}
func
NewAuditing
(
informers
informers
.
InformerFactory
,
url
string
,
stopCh
<-
chan
struct
{})
Auditing
{
func
NewAuditing
(
informers
informers
.
InformerFactory
,
opts
*
options
.
Options
,
stopCh
<-
chan
struct
{})
Auditing
{
a
:=
&
auditing
{
webhookLister
:
informers
.
KubeSphereSharedInformerFactory
()
.
Auditing
()
.
V1alpha1
()
.
Webhooks
()
.
Lister
(),
devopsGetter
:
devops
.
New
(
informers
.
KubeSphereSharedInformerFactory
()),
cache
:
make
(
chan
*
auditv1alpha1
.
Event
List
,
DefaultCacheCapacity
),
cache
:
make
(
chan
*
auditv1alpha1
.
Event
,
DefaultCacheCapacity
),
}
a
.
backend
=
NewBackend
(
url
,
ChannelCapacity
,
a
.
cache
,
SendTimeout
,
stopCh
)
a
.
backend
=
NewBackend
(
opts
,
a
.
cache
,
stopCh
)
return
a
}
...
...
@@ -226,10 +225,8 @@ func (a *auditing) LogResponseObject(e *auditv1alpha1.Event, resp *ResponseCaptu
func
(
a
*
auditing
)
cacheEvent
(
e
auditv1alpha1
.
Event
)
{
eventList
:=
&
auditv1alpha1
.
EventList
{}
eventList
.
Items
=
append
(
eventList
.
Items
,
e
)
select
{
case
a
.
cache
<-
eventList
:
case
a
.
cache
<-
&
e
:
return
case
<-
time
.
After
(
CacheTimeout
)
:
klog
.
Errorf
(
"cache audit event %s timeout"
,
e
.
AuditID
)
...
...
pkg/simple/client/auditing/elasticsearch/options.go
浏览文件 @
b543ae1a
...
...
@@ -19,14 +19,21 @@ package elasticsearch
import
(
"github.com/spf13/pflag"
"kubesphere.io/kubesphere/pkg/utils/reflectutils"
"time"
)
type
Options
struct
{
Enable
bool
`json:"enable" yaml:"enable"`
WebhookUrl
string
`json:"webhookUrl" yaml:"webhookUrl"`
Host
string
`json:"host" yaml:"host"`
IndexPrefix
string
`json:"indexPrefix,omitempty" yaml:"indexPrefix"`
Version
string
`json:"version" yaml:"version"`
Enable
bool
`json:"enable" yaml:"enable"`
WebhookUrl
string
`json:"webhookUrl" yaml:"webhookUrl"`
// The number of goroutines which send auditing events to webhook.
GoroutinesNum
int
`json:"goroutinesNum" yaml:"goroutinesNum"`
// The max size of the auditing event in a batch.
MaxBatchSize
int
`json:"batchSize" yaml:"batchSize"`
// MaxBatchWait indicates the maximum interval between two batches.
MaxBatchWait
time
.
Duration
`json:"batchTimeout" yaml:"batchTimeout"`
Host
string
`json:"host" yaml:"host"`
IndexPrefix
string
`json:"indexPrefix,omitempty" yaml:"indexPrefix"`
Version
string
`json:"version" yaml:"version"`
}
func
NewElasticSearchOptions
()
*
Options
{
...
...
@@ -52,7 +59,12 @@ func (s *Options) AddFlags(fs *pflag.FlagSet, c *Options) {
fs
.
BoolVar
(
&
s
.
Enable
,
"auditing-enabled"
,
c
.
Enable
,
"Enable auditing component or not. "
)
fs
.
StringVar
(
&
s
.
WebhookUrl
,
"auditing-webhook-url"
,
c
.
WebhookUrl
,
"Auditing wehook url"
)
fs
.
IntVar
(
&
s
.
GoroutinesNum
,
"auditing-goroutines-num"
,
c
.
GoroutinesNum
,
"The number of goroutines which send auditing events to webhook."
)
fs
.
IntVar
(
&
s
.
MaxBatchSize
,
"auditing-batch-max-size"
,
c
.
MaxBatchSize
,
"The max size of the auditing event in a batch."
)
fs
.
DurationVar
(
&
s
.
MaxBatchWait
,
"auditing-batch-max-wait"
,
c
.
MaxBatchWait
,
"MaxBatchWait indicates the maximum interval between two batches."
)
fs
.
StringVar
(
&
s
.
Host
,
"auditing-elasticsearch-host"
,
c
.
Host
,
""
+
"Elasticsearch service host. KubeSphere is using elastic as auditing store, "
+
"if this filed left blank, KubeSphere will use kubernetes builtin event API instead, and"
+
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录