Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
KubeSphere
kubesphere
提交
fb2db3e1
K
kubesphere
项目概览
KubeSphere
/
kubesphere
通知
138
Star
32
Fork
5
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
2
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kubesphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
2
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
fb2db3e1
编写于
3月 22, 2021
作者:
Z
zryfish
提交者:
GitHub
3月 22, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix concurrent map writes (#3529)
Signed-off-by:
N
Jeff
<
jeffzhang@yunify.com
>
上级
9c614bef
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
30 addition
and
10 deletion
+30
-10
pkg/models/resources/v1alpha3/node/nodes.go
pkg/models/resources/v1alpha3/node/nodes.go
+28
-10
pkg/models/resources/v1alpha3/node/nodes_test.go
pkg/models/resources/v1alpha3/node/nodes_test.go
+2
-0
未找到文件。
pkg/models/resources/v1alpha3/node/nodes.go
浏览文件 @
fb2db3e1
...
...
@@ -28,10 +28,13 @@ import (
"kubesphere.io/kubesphere/pkg/apiserver/query"
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
"sort"
"sync"
"time"
)
// Those annotations were added to node only for display purposes
const
(
nodeAnnotatedAt
=
"node.kubesphere.io/last-annotated-at"
nodeCPURequests
=
"node.kubesphere.io/cpu-requests"
nodeMemoryRequests
=
"node.kubesphere.io/memory-requests"
nodeCPULimits
=
"node.kubesphere.io/cpu-limits"
...
...
@@ -49,15 +52,17 @@ const (
type
nodesGetter
struct
{
informers
informers
.
SharedInformerFactory
mutex
sync
.
Mutex
}
func
New
(
informers
informers
.
SharedInformerFactory
)
v1alpha3
.
Interface
{
return
&
nodesGetter
{
informers
:
informers
,
mutex
:
sync
.
Mutex
{},
}
}
func
(
c
nodesGetter
)
Get
(
_
,
name
string
)
(
runtime
.
Object
,
error
)
{
func
(
c
*
nodesGetter
)
Get
(
_
,
name
string
)
(
runtime
.
Object
,
error
)
{
node
,
err
:=
c
.
informers
.
Core
()
.
V1
()
.
Nodes
()
.
Lister
()
.
Get
(
name
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -70,7 +75,7 @@ func (c nodesGetter) Get(_, name string) (runtime.Object, error) {
return
node
,
nil
}
func
(
c
nodesGetter
)
List
(
_
string
,
q
*
query
.
Query
)
(
*
api
.
ListResult
,
error
)
{
func
(
c
*
nodesGetter
)
List
(
_
string
,
q
*
query
.
Query
)
(
*
api
.
ListResult
,
error
)
{
nodes
,
err
:=
c
.
informers
.
Core
()
.
V1
()
.
Nodes
()
.
Lister
()
.
List
(
q
.
Selector
())
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -127,7 +132,7 @@ func (c nodesGetter) List(_ string, q *query.Query) (*api.ListResult, error) {
},
nil
}
func
(
c
nodesGetter
)
compare
(
left
runtime
.
Object
,
right
runtime
.
Object
,
field
query
.
Field
)
bool
{
func
(
c
*
nodesGetter
)
compare
(
left
runtime
.
Object
,
right
runtime
.
Object
,
field
query
.
Field
)
bool
{
leftNode
,
ok
:=
left
.
(
*
v1
.
Node
)
if
!
ok
{
return
false
...
...
@@ -141,7 +146,7 @@ func (c nodesGetter) compare(left runtime.Object, right runtime.Object, field qu
return
v1alpha3
.
DefaultObjectMetaCompare
(
leftNode
.
ObjectMeta
,
rightNode
.
ObjectMeta
,
field
)
}
func
(
c
nodesGetter
)
filter
(
object
runtime
.
Object
,
filter
query
.
Filter
)
bool
{
func
(
c
*
nodesGetter
)
filter
(
object
runtime
.
Object
,
filter
query
.
Filter
)
bool
{
node
,
ok
:=
object
.
(
*
v1
.
Node
)
if
!
ok
{
return
false
...
...
@@ -155,7 +160,24 @@ func (c nodesGetter) filter(object runtime.Object, filter query.Filter) bool {
}
// annotateNode adds cpu/memory requests usage data to node's annotations
func
(
c
nodesGetter
)
annotateNode
(
node
*
v1
.
Node
,
pods
[]
*
v1
.
Pod
)
{
// this operation is resource consuming, so avoid annotating on every query
func
(
c
*
nodesGetter
)
annotateNode
(
node
*
v1
.
Node
,
pods
[]
*
v1
.
Pod
)
{
// only annotate node when two consecutive annotating gap bigger than 30s
c
.
mutex
.
Lock
()
if
node
.
Annotations
==
nil
{
node
.
Annotations
=
make
(
map
[
string
]
string
)
}
if
lastAnnotatedAt
,
ok
:=
node
.
Annotations
[
nodeAnnotatedAt
];
ok
{
if
lastAnnotationTimeStamp
,
err
:=
time
.
Parse
(
time
.
RFC3339
,
lastAnnotatedAt
);
err
!=
nil
{
if
lastAnnotationTimeStamp
.
Add
(
30
*
time
.
Second
)
.
After
(
time
.
Now
())
{
return
}
}
}
node
.
Annotations
[
nodeAnnotatedAt
]
=
time
.
Now
()
.
Format
(
time
.
RFC3339
)
c
.
mutex
.
Unlock
()
if
len
(
pods
)
==
0
{
return
}
...
...
@@ -169,10 +191,6 @@ func (c nodesGetter) annotateNode(node *v1.Node, pods []*v1.Pod) {
reqs
,
limits
:=
c
.
getPodsTotalRequestAndLimits
(
nodePods
)
if
node
.
Annotations
==
nil
{
node
.
Annotations
=
make
(
map
[
string
]
string
)
}
cpuReqs
,
cpuLimits
,
memoryReqs
,
memoryLimits
:=
reqs
[
v1
.
ResourceCPU
],
limits
[
v1
.
ResourceCPU
],
reqs
[
v1
.
ResourceMemory
],
limits
[
v1
.
ResourceMemory
]
node
.
Annotations
[
nodeCPURequests
]
=
cpuReqs
.
String
()
node
.
Annotations
[
nodeCPULimits
]
=
cpuLimits
.
String
()
...
...
@@ -197,7 +215,7 @@ func (c nodesGetter) annotateNode(node *v1.Node, pods []*v1.Pod) {
node
.
Annotations
[
nodeMemoryLimitsFraction
]
=
fmt
.
Sprintf
(
"%d%%"
,
int
(
fractionMemoryLimits
))
}
func
(
c
nodesGetter
)
getPodsTotalRequestAndLimits
(
pods
[]
*
v1
.
Pod
)
(
reqs
map
[
v1
.
ResourceName
]
resource
.
Quantity
,
limits
map
[
v1
.
ResourceName
]
resource
.
Quantity
)
{
func
(
c
*
nodesGetter
)
getPodsTotalRequestAndLimits
(
pods
[]
*
v1
.
Pod
)
(
reqs
map
[
v1
.
ResourceName
]
resource
.
Quantity
,
limits
map
[
v1
.
ResourceName
]
resource
.
Quantity
)
{
reqs
,
limits
=
map
[
v1
.
ResourceName
]
resource
.
Quantity
{},
map
[
v1
.
ResourceName
]
resource
.
Quantity
{}
for
_
,
pod
:=
range
pods
{
podReqs
,
podLimits
:=
resourceheper
.
PodRequestsAndLimits
(
pod
)
...
...
pkg/models/resources/v1alpha3/node/nodes_test.go
浏览文件 @
fb2db3e1
...
...
@@ -143,6 +143,8 @@ func TestNodesGetterGet(t *testing.T) {
}
nodeGot
:=
got
.
(
*
corev1
.
Node
)
// ignore last-annotated-at annotation
delete
(
nodeGot
.
Annotations
,
nodeAnnotatedAt
)
if
diff
:=
cmp
.
Diff
(
nodeGot
.
Annotations
,
expectedAnnotations
);
len
(
diff
)
!=
0
{
t
.
Errorf
(
"%T, diff(-got, +expected), %v"
,
expectedAnnotations
,
nodeGot
.
Annotations
)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录