Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
6cdbf336
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
6cdbf336
编写于
3月 31, 2021
作者:
K
KubeSphere CI Bot
提交者:
GitHub
3月 31, 2021
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #3526 from zhu733756/add-pod-level-metrics
Add pod level metrics for edge node
上级
ea93f383
96d60da9
变更
14
显示空白变更内容
内联
并排
Showing
14 changed file
with
992 addition
and
267 deletion
+992
-267
pkg/simple/client/monitoring/metricsserver/metricsserver.go
pkg/simple/client/monitoring/metricsserver/metricsserver.go
+475
-163
pkg/simple/client/monitoring/metricsserver/metricsserver_test.go
...ple/client/monitoring/metricsserver/metricsserver_test.go
+257
-84
pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-1.json
...t/monitoring/metricsserver/testdata/metrics-matrix-1.json
+4
-4
pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-2.json
...t/monitoring/metricsserver/testdata/metrics-matrix-2.json
+2
-2
pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-3.json
...t/monitoring/metricsserver/testdata/metrics-matrix-3.json
+4
-4
pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-4.json
...t/monitoring/metricsserver/testdata/metrics-matrix-4.json
+42
-0
pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-5.json
...t/monitoring/metricsserver/testdata/metrics-matrix-5.json
+42
-0
pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-6.json
...t/monitoring/metricsserver/testdata/metrics-matrix-6.json
+42
-0
pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-1.json
...t/monitoring/metricsserver/testdata/metrics-vector-1.json
+4
-4
pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-2.json
...t/monitoring/metricsserver/testdata/metrics-vector-2.json
+2
-2
pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-3.json
...t/monitoring/metricsserver/testdata/metrics-vector-3.json
+4
-4
pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-4.json
...t/monitoring/metricsserver/testdata/metrics-vector-4.json
+38
-0
pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-5.json
...t/monitoring/metricsserver/testdata/metrics-vector-5.json
+38
-0
pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-6.json
...t/monitoring/metricsserver/testdata/metrics-vector-6.json
+38
-0
未找到文件。
pkg/simple/client/monitoring/metricsserver/metricsserver.go
浏览文件 @
6cdbf336
...
...
@@ -19,9 +19,12 @@ package metricsserver
import
(
"context"
"errors"
"regexp"
"strings"
"time"
v1
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
...
...
@@ -103,12 +106,28 @@ func (m metricsServer) filterEdgeNodeNames(edgeNodes map[string]v1.Node, opts *m
return
edgeNodeNamesFiltered
}
func
(
m
metricsServer
)
parseEdgePods
(
opts
*
monitoring
.
QueryOptions
)
map
[
string
]
bool
{
edgePods
:=
make
(
map
[
string
]
bool
)
r
,
_
:=
regexp
.
Compile
(
`\s*\|\s*|\$`
)
filters
:=
r
.
Split
(
opts
.
ResourceFilter
,
-
1
)
for
_
,
p
:=
range
filters
{
if
p
!=
""
{
edgePods
[
p
]
=
true
}
}
return
edgePods
}
// node metrics of edge nodes
func
(
m
metricsServer
)
getNodeMetricsFromMetricsAPI
()
(
*
metricsapi
.
NodeMetricsList
,
error
)
{
var
err
error
versionedMetrics
:=
&
metricsV1beta1
.
NodeMetricsList
{}
mc
:=
m
.
metricsClient
.
MetricsV1beta1
()
nm
:=
mc
.
NodeMetricses
()
versionedMetrics
,
err
=
nm
.
List
(
context
.
TODO
(),
metav1
.
ListOptions
{
LabelSelector
:
edgeNodeLabel
})
versionedMetrics
,
err
:
=
nm
.
List
(
context
.
TODO
(),
metav1
.
ListOptions
{
LabelSelector
:
edgeNodeLabel
})
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -120,6 +139,87 @@ func (m metricsServer) getNodeMetricsFromMetricsAPI() (*metricsapi.NodeMetricsLi
return
metrics
,
nil
}
// pods metrics of edge nodes
func
(
m
metricsServer
)
getPodMetricsFromMetricsAPI
(
edgePods
map
[
string
]
bool
,
podName
string
,
ns
string
)
([]
metricsapi
.
PodMetrics
,
error
)
{
mc
:=
m
.
metricsClient
.
MetricsV1beta1
()
// single pod request
if
ns
!=
""
&&
podName
!=
""
{
pm
:=
mc
.
PodMetricses
(
ns
)
versionedMetrics
,
err
:=
pm
.
Get
(
context
.
TODO
(),
podName
,
metav1
.
GetOptions
{})
if
err
!=
nil
{
klog
.
Error
(
"Get pod metrics on edge node error:"
,
err
)
return
nil
,
err
}
metrics
:=
&
metricsapi
.
PodMetrics
{}
err
=
metricsV1beta1
.
Convert_v1beta1_PodMetrics_To_metrics_PodMetrics
(
versionedMetrics
,
metrics
,
nil
)
if
err
!=
nil
{
klog
.
Error
(
"Convert pod metrics on edge node error:"
,
err
)
return
nil
,
err
}
return
[]
metricsapi
.
PodMetrics
{
*
metrics
},
nil
}
if
len
(
edgePods
)
==
0
{
return
nil
,
nil
}
var
isNamespacedEdgePod
bool
for
p
,
_
:=
range
edgePods
{
if
ok
:=
strings
.
Contains
(
p
,
"/"
);
ok
{
isNamespacedEdgePod
=
true
}
break
}
combinedPodMetrics
:=
[]
metricsapi
.
PodMetrics
{}
// handle cases with when edgePodName contains namespaceName
if
isNamespacedEdgePod
{
for
p
,
_
:=
range
edgePods
{
splitedPodName
:=
strings
.
Split
(
p
,
"/"
)
ns
,
p
=
strings
.
ReplaceAll
(
splitedPodName
[
0
],
" "
,
""
),
strings
.
ReplaceAll
(
splitedPodName
[
1
],
" "
,
""
)
pm
:=
mc
.
PodMetricses
(
ns
)
versionedMetrics
,
err
:=
pm
.
Get
(
context
.
TODO
(),
p
,
metav1
.
GetOptions
{})
if
err
!=
nil
{
klog
.
Error
(
"Get pod metrics on edge node error:"
,
err
)
continue
}
metrics
:=
&
metricsapi
.
PodMetrics
{}
err
=
metricsV1beta1
.
Convert_v1beta1_PodMetrics_To_metrics_PodMetrics
(
versionedMetrics
,
metrics
,
nil
)
if
err
!=
nil
{
klog
.
Error
(
"Convert pod metrics on edge node error:"
,
err
)
continue
}
combinedPodMetrics
=
append
(
combinedPodMetrics
,
*
metrics
)
}
return
combinedPodMetrics
,
nil
}
// use list request in other cases
pm
:=
mc
.
PodMetricses
(
ns
)
versionedMetricsList
,
err
:=
pm
.
List
(
context
.
TODO
(),
metav1
.
ListOptions
{})
if
err
!=
nil
{
klog
.
Error
(
"List pod metrics on edge node error:"
,
err
)
return
nil
,
err
}
podMetrics
:=
&
metricsapi
.
PodMetricsList
{}
err
=
metricsV1beta1
.
Convert_v1beta1_PodMetricsList_To_metrics_PodMetricsList
(
versionedMetricsList
,
podMetrics
,
nil
)
if
err
!=
nil
{
klog
.
Error
(
"Convert pod metrics on edge node error:"
,
err
)
return
nil
,
err
}
for
_
,
podMetric
:=
range
podMetrics
.
Items
{
if
_
,
ok
:=
edgePods
[
podMetric
.
Name
];
!
ok
{
continue
}
combinedPodMetrics
=
append
(
combinedPodMetrics
,
podMetric
)
}
return
combinedPodMetrics
,
nil
}
func
NewMetricsClient
(
k
kubernetes
.
Interface
,
options
*
k8s
.
KubernetesOptions
)
monitoring
.
Interface
{
config
,
err
:=
clientcmd
.
BuildConfigFromFlags
(
""
,
options
.
KubeConfig
)
if
err
!=
nil
{
...
...
@@ -172,6 +272,7 @@ func (m metricsServer) GetMetricOverTime(expr string, start, end time.Time, step
return
parsedResp
}
// node metrics definition
const
(
metricsNodeCPUUsage
=
"node_cpu_usage"
metricsNodeCPUTotal
=
"node_cpu_total"
...
...
@@ -183,6 +284,20 @@ const (
var
edgeNodeMetrics
=
[]
string
{
metricsNodeCPUUsage
,
metricsNodeCPUTotal
,
metricsNodeCPUUltilisation
,
metricsNodeMemoryUsageWoCache
,
metricsNodeMemoryTotal
,
metricsNodeMemoryUltilisation
}
// pod metrics definition
const
(
metricsPodCPUUsage
=
"pod_cpu_usage"
metricsPodMemoryUsage
=
"pod_memory_usage_wo_cache"
)
var
(
edgePodMetrics
=
[]
string
{
metricsPodCPUUsage
,
metricsPodMemoryUsage
}
MeasuredResources
=
[]
v1
.
ResourceName
{
v1
.
ResourceCPU
,
v1
.
ResourceMemory
,
}
)
func
(
m
metricsServer
)
parseErrorResp
(
metrics
[]
string
,
err
error
)
[]
monitoring
.
Metric
{
var
res
[]
monitoring
.
Metric
...
...
@@ -199,12 +314,25 @@ func (m metricsServer) GetNamedMetrics(metrics []string, ts time.Time, o monitor
opts
:=
monitoring
.
NewQueryOptions
()
o
.
Apply
(
opts
)
if
opts
.
Level
==
monitoring
.
LevelNode
{
if
!
m
.
metricsAPIAvailable
{
klog
.
Warningf
(
"Metrics API not available."
)
return
m
.
parseErrorResp
(
metrics
,
errors
.
New
(
"Metrics API not available."
))
}
switch
opts
.
Level
{
case
monitoring
.
LevelNode
:
return
m
.
GetNodeLevelNamedMetrics
(
metrics
,
ts
,
opts
)
case
monitoring
.
LevelPod
:
return
m
.
GetPodLevelNamedMetrics
(
metrics
,
ts
,
opts
)
default
:
return
res
}
}
func
(
m
metricsServer
)
GetNodeLevelNamedMetrics
(
metrics
[]
string
,
ts
time
.
Time
,
opts
*
monitoring
.
QueryOptions
)
[]
monitoring
.
Metric
{
var
res
[]
monitoring
.
Metric
edgeNodes
,
err
:=
m
.
listEdgeNodes
()
if
err
!=
nil
{
klog
.
Errorf
(
"List edge nodes error %v
\n
"
,
err
)
...
...
@@ -217,6 +345,11 @@ func (m metricsServer) GetNamedMetrics(metrics []string, ts time.Time, o monitor
return
res
}
status
:=
make
(
map
[
string
]
v1
.
NodeStatus
)
for
n
,
_
:=
range
edgeNodeNamesFiltered
{
status
[
n
]
=
edgeNodes
[
n
]
.
Status
}
metricsResult
,
err
:=
m
.
getNodeMetricsFromMetricsAPI
()
if
err
!=
nil
{
klog
.
Errorf
(
"Get edge node metrics error %v
\n
"
,
err
)
...
...
@@ -228,11 +361,6 @@ func (m metricsServer) GetNamedMetrics(metrics []string, ts time.Time, o monitor
metricsMap
[
m
]
=
true
}
status
:=
make
(
map
[
string
]
v1
.
NodeStatus
)
for
n
,
_
:=
range
edgeNodeNamesFiltered
{
status
[
n
]
=
edgeNodes
[
n
]
.
Status
}
nodeMetrics
:=
make
(
map
[
string
]
*
monitoring
.
MetricData
)
for
_
,
enm
:=
range
edgeNodeMetrics
{
_
,
ok
:=
metricsMap
[
enm
]
...
...
@@ -261,39 +389,35 @@ func (m metricsServer) GetNamedMetrics(metrics []string, ts time.Time, o monitor
metricValues
[
enm
]
.
Metadata
[
"node"
]
=
m
.
Name
metricValues
[
enm
]
.
Metadata
[
"role"
]
=
"edge"
}
for
_
,
addr
:=
range
status
[
m
.
Name
]
.
Addresses
{
if
addr
.
Type
==
v1
.
NodeInternalIP
{
for
_
,
enm
:=
range
edgeNodeMetrics
{
metricValues
[
enm
]
.
Metadata
[
"host_ip"
]
=
addr
.
Address
}
break
}
}
_
,
ok
=
metricsMap
[
metricsNodeCPUUsage
]
if
ok
{
for
k
,
v
:=
range
metricsMap
{
switch
k
{
case
metricsNodeCPUUsage
:
if
v
{
metricValues
[
metricsNodeCPUUsage
]
.
Sample
=
&
monitoring
.
Point
{
float64
(
m
.
Timestamp
.
Unix
()),
float64
(
usage
.
Cpu
()
.
MilliValue
())
/
1000
}
}
_
,
ok
=
metricsMap
[
metricsNodeCPUTotal
]
if
ok
{
case
metricsNodeCPUTotal
:
if
v
{
metricValues
[
metricsNodeCPUTotal
]
.
Sample
=
&
monitoring
.
Point
{
float64
(
m
.
Timestamp
.
Unix
()),
float64
(
cap
.
Cpu
()
.
MilliValue
())
/
1000
}
}
_
,
ok
=
metricsMap
[
metricsNodeCPUUltilisation
]
if
ok
{
case
metricsNodeCPUUltilisation
:
if
v
{
metricValues
[
metricsNodeCPUUltilisation
]
.
Sample
=
&
monitoring
.
Point
{
float64
(
m
.
Timestamp
.
Unix
()),
float64
(
usage
.
Cpu
()
.
MilliValue
())
/
float64
(
cap
.
Cpu
()
.
MilliValue
())}
}
_
,
ok
=
metricsMap
[
metricsNodeMemoryUsageWoCache
]
if
ok
{
case
metricsNodeMemoryUsageWoCache
:
if
v
{
metricValues
[
metricsNodeMemoryUsageWoCache
]
.
Sample
=
&
monitoring
.
Point
{
float64
(
m
.
Timestamp
.
Unix
()),
float64
(
usage
.
Memory
()
.
Value
())}
}
_
,
ok
=
metricsMap
[
metricsNodeMemoryTotal
]
if
ok
{
case
metricsNodeMemoryTotal
:
if
v
{
metricValues
[
metricsNodeMemoryTotal
]
.
Sample
=
&
monitoring
.
Point
{
float64
(
m
.
Timestamp
.
Unix
()),
float64
(
cap
.
Memory
()
.
Value
())}
}
_
,
ok
=
metricsMap
[
metricsNodeMemoryUltilisation
]
if
ok
{
case
metricsNodeMemoryUltilisation
:
if
v
{
metricValues
[
metricsNodeMemoryUltilisation
]
.
Sample
=
&
monitoring
.
Point
{
float64
(
m
.
Timestamp
.
Unix
()),
float64
(
usage
.
Memory
()
.
Value
())
/
float64
(
cap
.
Memory
()
.
Value
())}
}
}
}
for
_
,
enm
:=
range
edgeNodeMetrics
{
_
,
ok
=
metricsMap
[
enm
]
...
...
@@ -309,8 +433,94 @@ func (m metricsServer) GetNamedMetrics(metrics []string, ts time.Time, o monitor
res
=
append
(
res
,
monitoring
.
Metric
{
MetricName
:
enm
,
MetricData
:
*
nodeMetrics
[
enm
]})
}
}
return
res
}
func
(
m
metricsServer
)
GetPodLevelNamedMetrics
(
metrics
[]
string
,
ts
time
.
Time
,
opts
*
monitoring
.
QueryOptions
)
[]
monitoring
.
Metric
{
var
res
[]
monitoring
.
Metric
edgePods
:=
m
.
parseEdgePods
(
opts
)
if
len
(
edgePods
)
==
0
&&
opts
.
PodName
==
""
{
klog
.
Errorf
(
"Edge node filter regexp error: %v
\n
"
,
errors
.
New
(
"no edge node pods metrics is requested or resource filter invalid"
))
return
res
}
podMetricsFromMetricsAPI
,
err
:=
m
.
getPodMetricsFromMetricsAPI
(
edgePods
,
opts
.
PodName
,
opts
.
NamespaceName
)
if
err
!=
nil
{
klog
.
Errorf
(
"Get pod metrics of edge nodes error %v
\n
"
,
err
)
return
m
.
parseErrorResp
(
metrics
,
err
)
}
metricsMap
:=
make
(
map
[
string
]
bool
)
for
_
,
m
:=
range
metrics
{
metricsMap
[
m
]
=
true
}
// init
podMetrics
:=
make
(
map
[
string
]
*
monitoring
.
MetricData
)
for
_
,
epm
:=
range
edgePodMetrics
{
_
,
ok
:=
metricsMap
[
epm
]
if
ok
{
podMetrics
[
epm
]
=
&
monitoring
.
MetricData
{
MetricType
:
monitoring
.
MetricTypeVector
}
}
}
for
_
,
p
:=
range
podMetricsFromMetricsAPI
{
metricValues
:=
make
(
map
[
string
]
*
monitoring
.
MetricValue
)
for
_
,
epm
:=
range
edgePodMetrics
{
metricValues
[
epm
]
=
&
monitoring
.
MetricValue
{
Metadata
:
make
(
map
[
string
]
string
),
}
metricValues
[
epm
]
.
Metadata
[
"pod"
]
=
p
.
Name
metricValues
[
epm
]
.
Metadata
[
"namespace"
]
=
p
.
Namespace
}
podMetricsUsge
:=
make
(
v1
.
ResourceList
)
for
_
,
res
:=
range
MeasuredResources
{
podMetricsUsge
[
res
],
_
=
resource
.
ParseQuantity
(
"0"
)
}
for
_
,
podContainer
:=
range
p
.
Containers
{
for
_
,
res
:=
range
MeasuredResources
{
quantity
:=
podMetricsUsge
[
res
]
quantity
.
Add
(
podContainer
.
Usage
[
res
])
podMetricsUsge
[
res
]
=
quantity
}
}
for
k
,
v
:=
range
metricsMap
{
switch
k
{
case
metricsPodCPUUsage
:
if
v
{
cpuQuantity
:=
podMetricsUsge
[
v1
.
ResourceCPU
]
metricValues
[
metricsPodCPUUsage
]
.
Sample
=
&
monitoring
.
Point
{
float64
(
p
.
Timestamp
.
Unix
()),
float64
(
cpuQuantity
.
MilliValue
())
/
1000
}
}
case
metricsPodMemoryUsage
:
if
v
{
memoryQuantity
:=
podMetricsUsge
[
v1
.
ResourceMemory
]
metricValues
[
metricsPodMemoryUsage
]
.
Sample
=
&
monitoring
.
Point
{
float64
(
p
.
Timestamp
.
Unix
()),
float64
(
memoryQuantity
.
Value
())
/
(
1024
*
1024
)}
}
}
}
for
_
,
epm
:=
range
edgePodMetrics
{
_
,
ok
:=
metricsMap
[
epm
]
if
ok
{
podMetrics
[
epm
]
.
MetricValues
=
append
(
podMetrics
[
epm
]
.
MetricValues
,
*
metricValues
[
epm
])
}
}
}
for
_
,
epm
:=
range
edgePodMetrics
{
_
,
ok
:=
metricsMap
[
epm
]
if
ok
{
res
=
append
(
res
,
monitoring
.
Metric
{
MetricName
:
epm
,
MetricData
:
*
podMetrics
[
epm
]})
}
}
return
res
}
...
...
@@ -319,12 +529,25 @@ func (m metricsServer) GetNamedMetricsOverTime(metrics []string, start, end time
opts
:=
monitoring
.
NewQueryOptions
()
o
.
Apply
(
opts
)
if
opts
.
Level
==
monitoring
.
LevelNode
{
if
!
m
.
metricsAPIAvailable
{
klog
.
Warningf
(
"Metrics API not available."
)
return
m
.
parseErrorResp
(
metrics
,
errors
.
New
(
"Metrics API not available."
))
}
switch
opts
.
Level
{
case
monitoring
.
LevelNode
:
return
m
.
GetNodeLevelNamedMetricsOverTime
(
metrics
,
start
,
end
,
step
,
opts
)
case
monitoring
.
LevelPod
:
return
m
.
GetPodLevelNamedMetricsOverTime
(
metrics
,
start
,
end
,
step
,
opts
)
default
:
return
res
}
}
func
(
m
metricsServer
)
GetNodeLevelNamedMetricsOverTime
(
metrics
[]
string
,
start
,
end
time
.
Time
,
step
time
.
Duration
,
opts
*
monitoring
.
QueryOptions
)
[]
monitoring
.
Metric
{
var
res
[]
monitoring
.
Metric
edgeNodes
,
err
:=
m
.
listEdgeNodes
()
if
err
!=
nil
{
klog
.
Errorf
(
"List edge nodes error %v
\n
"
,
err
)
...
...
@@ -390,33 +613,37 @@ func (m metricsServer) GetNamedMetricsOverTime(metrics []string, start, end time
}
}
_
,
ok
=
metricsMap
[
metricsNodeCPUUsage
]
if
ok
{
for
k
,
v
:=
range
metricsMap
{
switch
k
{
case
metricsNodeCPUUsage
:
if
v
{
metricValues
[
metricsNodeCPUUsage
]
.
Series
=
append
(
metricValues
[
metricsNodeCPUUsage
]
.
Series
,
monitoring
.
Point
{
float64
(
m
.
Timestamp
.
Unix
()),
float64
(
usage
.
Cpu
()
.
MilliValue
())
/
1000
})
}
_
,
ok
=
metricsMap
[
metricsNodeCPUTotal
]
if
ok
{
case
metricsNodeCPUTotal
:
if
v
{
metricValues
[
metricsNodeCPUTotal
]
.
Series
=
append
(
metricValues
[
metricsNodeCPUTotal
]
.
Series
,
monitoring
.
Point
{
float64
(
m
.
Timestamp
.
Unix
()),
float64
(
cap
.
Cpu
()
.
MilliValue
())
/
1000
})
}
_
,
ok
=
metricsMap
[
metricsNodeCPUUltilisation
]
if
ok
{
case
metricsNodeCPUUltilisation
:
if
v
{
metricValues
[
metricsNodeCPUUltilisation
]
.
Series
=
append
(
metricValues
[
metricsNodeCPUUltilisation
]
.
Series
,
monitoring
.
Point
{
float64
(
m
.
Timestamp
.
Unix
()),
float64
(
usage
.
Cpu
()
.
MilliValue
())
/
float64
(
cap
.
Cpu
()
.
MilliValue
())})
}
_
,
ok
=
metricsMap
[
metricsNodeMemoryUsageWoCache
]
if
ok
{
case
metricsNodeMemoryUsageWoCache
:
if
v
{
metricValues
[
metricsNodeMemoryUsageWoCache
]
.
Series
=
append
(
metricValues
[
metricsNodeMemoryUsageWoCache
]
.
Series
,
monitoring
.
Point
{
float64
(
m
.
Timestamp
.
Unix
()),
float64
(
usage
.
Memory
()
.
Value
())})
}
_
,
ok
=
metricsMap
[
metricsNodeMemoryTotal
]
if
ok
{
case
metricsNodeMemoryTotal
:
if
v
{
metricValues
[
metricsNodeMemoryTotal
]
.
Series
=
append
(
metricValues
[
metricsNodeMemoryTotal
]
.
Series
,
monitoring
.
Point
{
float64
(
m
.
Timestamp
.
Unix
()),
float64
(
cap
.
Memory
()
.
Value
())})
}
_
,
ok
=
metricsMap
[
metricsNodeMemoryUltilisation
]
if
ok
{
case
metricsNodeMemoryUltilisation
:
if
v
{
metricValues
[
metricsNodeMemoryUltilisation
]
.
Series
=
append
(
metricValues
[
metricsNodeMemoryUltilisation
]
.
Series
,
monitoring
.
Point
{
float64
(
m
.
Timestamp
.
Unix
()),
float64
(
usage
.
Memory
()
.
Value
())
/
float64
(
cap
.
Memory
()
.
Value
())})
}
}
}
for
_
,
enm
:=
range
edgeNodeMetrics
{
_
,
ok
=
metricsMap
[
enm
]
_
,
ok
:
=
metricsMap
[
enm
]
if
ok
{
nodeMetrics
[
enm
]
.
MetricValues
=
append
(
nodeMetrics
[
enm
]
.
MetricValues
,
*
metricValues
[
enm
])
}
...
...
@@ -429,8 +656,93 @@ func (m metricsServer) GetNamedMetricsOverTime(metrics []string, start, end time
res
=
append
(
res
,
monitoring
.
Metric
{
MetricName
:
enm
,
MetricData
:
*
nodeMetrics
[
enm
]})
}
}
return
res
}
func
(
m
metricsServer
)
GetPodLevelNamedMetricsOverTime
(
metrics
[]
string
,
start
,
end
time
.
Time
,
step
time
.
Duration
,
opts
*
monitoring
.
QueryOptions
)
[]
monitoring
.
Metric
{
var
res
[]
monitoring
.
Metric
edgePods
:=
m
.
parseEdgePods
(
opts
)
if
len
(
edgePods
)
==
0
&&
opts
.
PodName
==
""
{
klog
.
Errorf
(
"Edge node filter regexp error: %v
\n
"
,
errors
.
New
(
"no edge node pods metrics is requested or resource filter invalid"
))
return
res
}
podMetricsFromMetricsAPI
,
err
:=
m
.
getPodMetricsFromMetricsAPI
(
edgePods
,
opts
.
PodName
,
opts
.
NamespaceName
)
if
err
!=
nil
{
klog
.
Errorf
(
"Get pod metrics of edge nodes error %v
\n
"
,
err
)
return
m
.
parseErrorResp
(
metrics
,
err
)
}
metricsMap
:=
make
(
map
[
string
]
bool
)
for
_
,
m
:=
range
metrics
{
metricsMap
[
m
]
=
true
}
// init
podMetrics
:=
make
(
map
[
string
]
*
monitoring
.
MetricData
)
for
_
,
epm
:=
range
edgePodMetrics
{
_
,
ok
:=
metricsMap
[
epm
]
if
ok
{
podMetrics
[
epm
]
=
&
monitoring
.
MetricData
{
MetricType
:
monitoring
.
MetricTypeMatrix
}
}
}
for
_
,
p
:=
range
podMetricsFromMetricsAPI
{
metricValues
:=
make
(
map
[
string
]
*
monitoring
.
MetricValue
)
for
_
,
epm
:=
range
edgePodMetrics
{
metricValues
[
epm
]
=
&
monitoring
.
MetricValue
{
Metadata
:
make
(
map
[
string
]
string
),
}
metricValues
[
epm
]
.
Metadata
[
"pod"
]
=
p
.
Name
metricValues
[
epm
]
.
Metadata
[
"namespace"
]
=
p
.
Namespace
}
podMetricsUsge
:=
make
(
v1
.
ResourceList
)
for
_
,
res
:=
range
MeasuredResources
{
podMetricsUsge
[
res
],
_
=
resource
.
ParseQuantity
(
"0"
)
}
for
_
,
podContainer
:=
range
p
.
Containers
{
for
_
,
res
:=
range
MeasuredResources
{
quantity
:=
podMetricsUsge
[
res
]
quantity
.
Add
(
podContainer
.
Usage
[
res
])
podMetricsUsge
[
res
]
=
quantity
}
}
for
k
,
v
:=
range
metricsMap
{
switch
k
{
case
metricsPodCPUUsage
:
if
v
{
cpuQuantity
:=
podMetricsUsge
[
v1
.
ResourceCPU
]
metricValues
[
metricsPodCPUUsage
]
.
Series
=
append
(
metricValues
[
metricsPodCPUUsage
]
.
Series
,
monitoring
.
Point
{
float64
(
p
.
Timestamp
.
Unix
()),
float64
(
cpuQuantity
.
MilliValue
())
/
1000
})
}
case
metricsPodMemoryUsage
:
if
v
{
memoryQuantity
:=
podMetricsUsge
[
v1
.
ResourceMemory
]
metricValues
[
metricsPodMemoryUsage
]
.
Series
=
append
(
metricValues
[
metricsPodMemoryUsage
]
.
Series
,
monitoring
.
Point
{
float64
(
p
.
Timestamp
.
Unix
()),
float64
(
memoryQuantity
.
Value
())
/
(
1024
*
1024
)})
}
}
}
for
_
,
epm
:=
range
edgePodMetrics
{
_
,
ok
:=
metricsMap
[
epm
]
if
ok
{
podMetrics
[
epm
]
.
MetricValues
=
append
(
podMetrics
[
epm
]
.
MetricValues
,
*
metricValues
[
epm
])
}
}
}
for
_
,
epm
:=
range
edgePodMetrics
{
_
,
ok
:=
metricsMap
[
epm
]
if
ok
{
res
=
append
(
res
,
monitoring
.
Metric
{
MetricName
:
epm
,
MetricData
:
*
podMetrics
[
epm
]})
}
}
return
res
}
...
...
pkg/simple/client/monitoring/metricsserver/metricsserver_test.go
浏览文件 @
6cdbf336
...
...
@@ -2,6 +2,7 @@ package metricsserver
import
(
"fmt"
"strings"
"testing"
"time"
...
...
@@ -71,42 +72,28 @@ var node2 = &v1.Node{
},
}
func
TestGetNamedMetrics
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
metrics
[]
string
filter
string
expected
string
}{
{
metrics
:
[]
string
{
"node_cpu_usage"
,
"node_memory_usage_wo_cache"
},
filter
:
".*"
,
expected
:
"metrics-vector-1.json"
,
},
{
metrics
:
[]
string
{
"node_cpu_usage"
,
"node_cpu_utilisation"
},
filter
:
"edgenode-2"
,
expected
:
"metrics-vector-2.json"
,
},
{
metrics
:
[]
string
{
"node_memory_usage_wo_cache"
,
"node_memory_utilisation"
},
filter
:
"edgenode-1|edgenode-2"
,
expected
:
"metrics-vector-3.json"
,
var
pod1
=
&
v1
.
Pod
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"pod1"
,
Namespace
:
"kubeedge"
,
},
}
}
fakeK8sClient
:=
fakek8s
.
NewSimpleClientset
(
node1
,
node2
)
informer
:=
informers
.
NewSharedInformerFactory
(
fakeK8sClient
,
0
)
informer
.
Core
()
.
V1
()
.
Nodes
()
.
Informer
()
.
GetIndexer
()
.
Add
(
node1
)
informer
.
Core
()
.
V1
()
.
Nodes
()
.
Informer
()
.
GetIndexer
()
.
Add
(
node2
)
var
pod2
=
&
v1
.
Pod
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"pod2"
,
Namespace
:
"kubeedge"
,
},
}
fakeMetricsclient
:=
&
fakemetricsclient
.
Clientset
{}
layout
:
=
"2006-01-02T15:04:05.000Z"
str
:=
"2021-01
-25T12:34:56.789Z"
metricsTime
,
_
:=
time
.
Parse
(
layout
,
str
)
const
(
layout
=
"2006-01-02T15:04:05.000Z"
str
=
"2021-03
-25T12:34:56.789Z"
)
fakeMetricsclient
.
AddReactor
(
"list"
,
"nodes"
,
func
(
action
core
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
metrics
:=
&
metricsV1beta1
.
NodeMetricsList
{}
nodeMetric1
:
=
metricsV1beta1
.
NodeMetrics
{
var
(
metricsTime
,
_
=
time
.
Parse
(
layout
,
str
)
nodeMetric1
=
metricsV1beta1
.
NodeMetrics
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"edgenode-1"
,
Labels
:
map
[
string
]
string
{
...
...
@@ -124,7 +111,7 @@ func TestGetNamedMetrics(t *testing.T) {
resource
.
BinarySI
),
},
}
nodeMetric2
:
=
metricsV1beta1
.
NodeMetrics
{
nodeMetric2
=
metricsV1beta1
.
NodeMetrics
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"edgenode-2"
,
Labels
:
map
[
string
]
string
{
...
...
@@ -142,13 +129,146 @@ func TestGetNamedMetrics(t *testing.T) {
resource
.
BinarySI
),
},
}
podMetric1
=
metricsV1beta1
.
PodMetrics
{
TypeMeta
:
metav1
.
TypeMeta
{
Kind
:
"DaemonSet"
,
},
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"pod1"
,
Namespace
:
"kubeedge"
,
},
Timestamp
:
metav1
.
Time
{
Time
:
metricsTime
},
Window
:
metav1
.
Duration
{
Duration
:
time
.
Minute
},
Containers
:
[]
metricsV1beta1
.
ContainerMetrics
{
metricsV1beta1
.
ContainerMetrics
{
Name
:
"containers-1"
,
Usage
:
v1
.
ResourceList
{
v1
.
ResourceCPU
:
*
resource
.
NewMilliQuantity
(
1
,
resource
.
DecimalSI
),
v1
.
ResourceMemory
:
*
resource
.
NewQuantity
(
int64
(
1024
*
1024
),
resource
.
DecimalSI
),
},
},
},
}
podMetric2
=
metricsV1beta1
.
PodMetrics
{
TypeMeta
:
metav1
.
TypeMeta
{
Kind
:
"DaemonSet"
,
},
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"pod2"
,
Namespace
:
"kubeedge"
,
},
Timestamp
:
metav1
.
Time
{
Time
:
metricsTime
},
Window
:
metav1
.
Duration
{
Duration
:
time
.
Minute
},
Containers
:
[]
metricsV1beta1
.
ContainerMetrics
{
metricsV1beta1
.
ContainerMetrics
{
Name
:
"containers-1"
,
Usage
:
v1
.
ResourceList
{
v1
.
ResourceCPU
:
*
resource
.
NewMilliQuantity
(
1
,
resource
.
DecimalSI
),
v1
.
ResourceMemory
:
*
resource
.
NewQuantity
(
int64
(
1024
*
1024
),
resource
.
DecimalSI
),
},
},
metricsV1beta1
.
ContainerMetrics
{
Name
:
"containers-2"
,
Usage
:
v1
.
ResourceList
{
v1
.
ResourceCPU
:
*
resource
.
NewMilliQuantity
(
1
,
resource
.
DecimalSI
),
v1
.
ResourceMemory
:
*
resource
.
NewQuantity
(
int64
(
1024
*
1024
),
resource
.
DecimalSI
),
},
},
},
}
)
func
TestGetNamedMetrics
(
t
*
testing
.
T
)
{
nodeMetricsTests
:=
[]
struct
{
metrics
[]
string
filter
string
expected
string
}{
{
metrics
:
[]
string
{
"node_cpu_usage"
,
"node_memory_usage_wo_cache"
},
filter
:
".*"
,
expected
:
"metrics-vector-1.json"
,
},
{
metrics
:
[]
string
{
"node_cpu_usage"
,
"node_cpu_utilisation"
},
filter
:
"edgenode-2"
,
expected
:
"metrics-vector-2.json"
,
},
{
metrics
:
[]
string
{
"node_memory_usage_wo_cache"
,
"node_memory_utilisation"
},
filter
:
"edgenode-1|edgenode-2"
,
expected
:
"metrics-vector-3.json"
,
},
}
podMetricsTests
:=
[]
struct
{
metrics
[]
string
filter
string
expected
string
podName
string
namespaceName
string
}{
{
metrics
:
[]
string
{
"pod_cpu_usage"
,
"pod_memory_usage_wo_cache"
},
filter
:
"pod1$"
,
expected
:
"metrics-vector-4.json"
,
podName
:
""
,
namespaceName
:
""
,
},
{
metrics
:
[]
string
{
"pod_cpu_usage"
,
"pod_memory_usage_wo_cache"
},
filter
:
"default/pod2$"
,
expected
:
"metrics-vector-5.json"
,
podName
:
""
,
namespaceName
:
""
,
},
{
metrics
:
[]
string
{
"pod_cpu_usage"
,
"pod_memory_usage_wo_cache"
},
filter
:
""
,
expected
:
"metrics-vector-6.json"
,
podName
:
"pod1"
,
namespaceName
:
"kubeedge"
,
},
}
fakeK8sClient
:=
fakek8s
.
NewSimpleClientset
(
node1
,
node2
,
pod1
,
pod2
)
informer
:=
informers
.
NewSharedInformerFactory
(
fakeK8sClient
,
0
)
informer
.
Core
()
.
V1
()
.
Nodes
()
.
Informer
()
.
GetIndexer
()
.
Add
(
node1
)
informer
.
Core
()
.
V1
()
.
Nodes
()
.
Informer
()
.
GetIndexer
()
.
Add
(
node2
)
informer
.
Core
()
.
V1
()
.
Pods
()
.
Informer
()
.
GetIndexer
()
.
Add
(
pod1
)
informer
.
Core
()
.
V1
()
.
Pods
()
.
Informer
()
.
GetIndexer
()
.
Add
(
pod2
)
fakeMetricsclient
:=
&
fakemetricsclient
.
Clientset
{}
fakeMetricsclient
.
AddReactor
(
"list"
,
"nodes"
,
func
(
action
core
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
metrics
:=
&
metricsV1beta1
.
NodeMetricsList
{}
metrics
.
Items
=
append
(
metrics
.
Items
,
nodeMetric1
)
metrics
.
Items
=
append
(
metrics
.
Items
,
nodeMetric2
)
return
true
,
metrics
,
nil
})
for
i
,
tt
:=
range
tests
{
fakeMetricsclient
.
AddReactor
(
"list"
,
"pods"
,
func
(
action
core
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
metrics
:=
&
metricsV1beta1
.
PodMetricsList
{}
metrics
.
Items
=
append
(
metrics
.
Items
,
podMetric1
)
metrics
.
Items
=
append
(
metrics
.
Items
,
podMetric2
)
return
true
,
metrics
,
nil
})
// test for node edge
for
i
,
tt
:=
range
nodeMetricsTests
{
t
.
Run
(
fmt
.
Sprintf
(
"%d"
,
i
),
func
(
t
*
testing
.
T
)
{
expected
:=
make
([]
monitoring
.
Metric
,
0
)
err
:=
jsonFromFile
(
tt
.
expected
,
&
expected
)
...
...
@@ -163,10 +283,36 @@ func TestGetNamedMetrics(t *testing.T) {
}
})
}
//test for pods on the node edges
for
i
,
tt
:=
range
podMetricsTests
{
t
.
Run
(
fmt
.
Sprintf
(
"%d"
,
i
),
func
(
t
*
testing
.
T
)
{
expected
:=
make
([]
monitoring
.
Metric
,
0
)
err
:=
jsonFromFile
(
tt
.
expected
,
&
expected
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
tt
.
podName
==
"pod1"
||
strings
.
Contains
(
tt
.
filter
,
"pod1"
)
{
fakeMetricsclient
.
PrependReactor
(
"get"
,
"pods"
,
func
(
action
core
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
return
true
,
&
podMetric1
,
nil
})
}
else
{
fakeMetricsclient
.
PrependReactor
(
"get"
,
"pods"
,
func
(
action
core
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
return
true
,
&
podMetric2
,
nil
})
}
client
:=
NewMetricsServer
(
fakeK8sClient
,
true
,
fakeMetricsclient
)
result
:=
client
.
GetNamedMetrics
(
tt
.
metrics
,
time
.
Now
(),
monitoring
.
PodOption
{
ResourceFilter
:
tt
.
filter
,
PodName
:
tt
.
podName
,
NamespaceName
:
tt
.
namespaceName
})
if
diff
:=
cmp
.
Diff
(
result
,
expected
);
diff
!=
""
{
t
.
Fatalf
(
"%T differ (-got, +want): %s"
,
expected
,
diff
)
}
})
}
}
func
TestGetNamedMetricsOverTime
(
t
*
testing
.
T
)
{
t
ests
:=
[]
struct
{
nodeMetricsT
ests
:=
[]
struct
{
metrics
[]
string
filter
string
expected
string
...
...
@@ -188,61 +334,62 @@ func TestGetNamedMetricsOverTime(t *testing.T) {
},
}
fakeK8sClient
:=
fakek8s
.
NewSimpleClientset
(
node1
,
node2
)
podMetricsTests
:=
[]
struct
{
metrics
[]
string
filter
string
expected
string
podName
string
namespaceName
string
}{
{
metrics
:
[]
string
{
"pod_cpu_usage"
,
"pod_memory_usage_wo_cache"
},
filter
:
"pod1$"
,
expected
:
"metrics-matrix-4.json"
,
podName
:
""
,
namespaceName
:
""
,
},
{
metrics
:
[]
string
{
"pod_cpu_usage"
,
"pod_memory_usage_wo_cache"
},
filter
:
"default/pod2$"
,
expected
:
"metrics-matrix-5.json"
,
podName
:
""
,
namespaceName
:
""
,
},
{
metrics
:
[]
string
{
"pod_cpu_usage"
,
"pod_memory_usage_wo_cache"
},
filter
:
""
,
expected
:
"metrics-matrix-6.json"
,
podName
:
"pod1"
,
namespaceName
:
"kubeedge"
,
},
}
fakeK8sClient
:=
fakek8s
.
NewSimpleClientset
(
node1
,
node2
,
pod1
,
pod2
)
informer
:=
informers
.
NewSharedInformerFactory
(
fakeK8sClient
,
0
)
informer
.
Core
()
.
V1
()
.
Nodes
()
.
Informer
()
.
GetIndexer
()
.
Add
(
node1
)
informer
.
Core
()
.
V1
()
.
Nodes
()
.
Informer
()
.
GetIndexer
()
.
Add
(
node2
)
informer
.
Core
()
.
V1
()
.
Nodes
()
.
Informer
()
.
GetIndexer
()
.
Add
(
pod1
)
informer
.
Core
()
.
V1
()
.
Nodes
()
.
Informer
()
.
GetIndexer
()
.
Add
(
pod2
)
fakeMetricsclient
:=
&
fakemetricsclient
.
Clientset
{}
layout
:=
"2006-01-02T15:04:05.000Z"
str
:=
"2021-01-25T12:34:56.789Z"
metricsTime
,
_
:=
time
.
Parse
(
layout
,
str
)
fakeMetricsclient
.
AddReactor
(
"list"
,
"nodes"
,
func
(
action
core
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
metrics
:=
&
metricsV1beta1
.
NodeMetricsList
{}
nodeMetric1
:=
metricsV1beta1
.
NodeMetrics
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"edgenode-1"
,
Labels
:
map
[
string
]
string
{
"node-role.kubernetes.io/edge"
:
""
,
},
},
Timestamp
:
metav1
.
Time
{
Time
:
metricsTime
},
Window
:
metav1
.
Duration
{
Duration
:
time
.
Minute
},
Usage
:
v1
.
ResourceList
{
v1
.
ResourceCPU
:
*
resource
.
NewMilliQuantity
(
int64
(
1000
),
resource
.
DecimalSI
),
v1
.
ResourceMemory
:
*
resource
.
NewQuantity
(
int64
(
1024
*
1024
),
resource
.
BinarySI
),
},
}
nodeMetric2
:=
metricsV1beta1
.
NodeMetrics
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"edgenode-2"
,
Labels
:
map
[
string
]
string
{
"node-role.kubernetes.io/edge"
:
""
,
},
},
Timestamp
:
metav1
.
Time
{
Time
:
metricsTime
},
Window
:
metav1
.
Duration
{
Duration
:
time
.
Minute
},
Usage
:
v1
.
ResourceList
{
v1
.
ResourceCPU
:
*
resource
.
NewMilliQuantity
(
int64
(
2000
),
resource
.
DecimalSI
),
v1
.
ResourceMemory
:
*
resource
.
NewQuantity
(
int64
(
2
*
1024
*
1024
),
resource
.
BinarySI
),
},
}
metrics
.
Items
=
append
(
metrics
.
Items
,
nodeMetric1
)
metrics
.
Items
=
append
(
metrics
.
Items
,
nodeMetric2
)
return
true
,
metrics
,
nil
})
for
i
,
tt
:=
range
tests
{
fakeMetricsclient
.
AddReactor
(
"list"
,
"pods"
,
func
(
action
core
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
metrics
:=
&
metricsV1beta1
.
PodMetricsList
{}
metrics
.
Items
=
append
(
metrics
.
Items
,
podMetric1
)
metrics
.
Items
=
append
(
metrics
.
Items
,
podMetric2
)
return
true
,
metrics
,
nil
})
for
i
,
tt
:=
range
nodeMetricsTests
{
fakeMetricsclient
.
Fake
.
ClearActions
()
t
.
Run
(
fmt
.
Sprintf
(
"%d"
,
i
),
func
(
t
*
testing
.
T
)
{
expected
:=
make
([]
monitoring
.
Metric
,
0
)
err
:=
jsonFromFile
(
tt
.
expected
,
&
expected
)
...
...
@@ -257,6 +404,32 @@ func TestGetNamedMetricsOverTime(t *testing.T) {
}
})
}
for
i
,
tt
:=
range
podMetricsTests
{
t
.
Run
(
fmt
.
Sprintf
(
"%d"
,
i
),
func
(
t
*
testing
.
T
)
{
expected
:=
make
([]
monitoring
.
Metric
,
0
)
err
:=
jsonFromFile
(
tt
.
expected
,
&
expected
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
tt
.
podName
==
"pod1"
||
strings
.
Contains
(
tt
.
filter
,
"pod1"
)
{
fakeMetricsclient
.
PrependReactor
(
"get"
,
"pods"
,
func
(
action
core
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
return
true
,
&
podMetric1
,
nil
})
}
else
{
fakeMetricsclient
.
PrependReactor
(
"get"
,
"pods"
,
func
(
action
core
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
return
true
,
&
podMetric2
,
nil
})
}
client
:=
NewMetricsServer
(
fakeK8sClient
,
true
,
fakeMetricsclient
)
result
:=
client
.
GetNamedMetricsOverTime
(
tt
.
metrics
,
time
.
Now
()
.
Add
(
-
time
.
Minute
*
3
),
time
.
Now
(),
time
.
Minute
,
monitoring
.
PodOption
{
ResourceFilter
:
tt
.
filter
,
PodName
:
tt
.
podName
,
NamespaceName
:
tt
.
namespaceName
})
if
diff
:=
cmp
.
Diff
(
result
,
expected
);
diff
!=
""
{
t
.
Fatalf
(
"%T differ (-got, +want): %s"
,
expected
,
diff
)
}
})
}
}
func
jsonFromFile
(
expectedFile
string
,
expectedJsonPtr
interface
{})
error
{
...
...
pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-1.json
浏览文件 @
6cdbf336
...
...
@@ -11,7 +11,7 @@
},
"values"
:
[
[
161
15780
96
,
161
66756
96
,
"1"
]
]
...
...
@@ -23,7 +23,7 @@
},
"values"
:
[
[
161
15780
96
,
161
66756
96
,
"2"
]
]
...
...
@@ -43,7 +43,7 @@
},
"values"
:
[
[
161
15780
96
,
161
66756
96
,
"1048576"
]
]
...
...
@@ -55,7 +55,7 @@
},
"values"
:
[
[
161
15780
96
,
161
66756
96
,
"2097152"
]
]
...
...
pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-2.json
浏览文件 @
6cdbf336
...
...
@@ -11,7 +11,7 @@
},
"values"
:
[
[
161
15780
96
,
161
66756
96
,
"2"
]
]
...
...
@@ -31,7 +31,7 @@
},
"values"
:
[
[
161
15780
96
,
161
66756
96
,
"0.25"
]
]
...
...
pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-3.json
浏览文件 @
6cdbf336
...
...
@@ -11,7 +11,7 @@
},
"values"
:
[
[
161
15780
96
,
161
66756
96
,
"1048576"
]
]
...
...
@@ -23,7 +23,7 @@
},
"values"
:
[
[
161
15780
96
,
161
66756
96
,
"2097152"
]
]
...
...
@@ -43,7 +43,7 @@
},
"values"
:
[
[
161
15780
96
,
161
66756
96
,
"0.0001220703125"
]
]
...
...
@@ -55,7 +55,7 @@
},
"values"
:
[
[
161
15780
96
,
161
66756
96
,
"0.000244140625"
]
]
...
...
pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-4.json
0 → 100644
浏览文件 @
6cdbf336
[
{
"metric_name"
:
"pod_cpu_usage"
,
"data"
:
{
"resultType"
:
"matrix"
,
"result"
:
[
{
"metric"
:
{
"namespace"
:
"kubeedge"
,
"pod"
:
"pod1"
},
"values"
:[
[
1616675696
,
"0.001"
]
]
}
]
}
},
{
"metric_name"
:
"pod_memory_usage_wo_cache"
,
"data"
:
{
"resultType"
:
"matrix"
,
"result"
:
[
{
"metric"
:
{
"namespace"
:
"kubeedge"
,
"pod"
:
"pod1"
},
"values"
:
[
[
1616675696
,
"1"
]
]
}
]
}
}
]
\ No newline at end of file
pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-5.json
0 → 100644
浏览文件 @
6cdbf336
[
{
"metric_name"
:
"pod_cpu_usage"
,
"data"
:
{
"resultType"
:
"matrix"
,
"result"
:
[
{
"metric"
:
{
"namespace"
:
"kubeedge"
,
"pod"
:
"pod2"
},
"values"
:
[
[
1616675696
,
"0.002"
]
]
}
]
}
},
{
"metric_name"
:
"pod_memory_usage_wo_cache"
,
"data"
:
{
"resultType"
:
"matrix"
,
"result"
:
[
{
"metric"
:
{
"namespace"
:
"kubeedge"
,
"pod"
:
"pod2"
},
"values"
:
[
[
1616675696
,
"2"
]
]
}
]
}
}
]
\ No newline at end of file
pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-6.json
0 → 100644
浏览文件 @
6cdbf336
[
{
"metric_name"
:
"pod_cpu_usage"
,
"data"
:
{
"resultType"
:
"matrix"
,
"result"
:
[
{
"metric"
:
{
"namespace"
:
"kubeedge"
,
"pod"
:
"pod1"
},
"values"
:[
[
1616675696
,
"0.001"
]
]
}
]
}
},
{
"metric_name"
:
"pod_memory_usage_wo_cache"
,
"data"
:
{
"resultType"
:
"matrix"
,
"result"
:
[
{
"metric"
:
{
"namespace"
:
"kubeedge"
,
"pod"
:
"pod1"
},
"values"
:
[
[
1616675696
,
"1"
]
]
}
]
}
}
]
\ No newline at end of file
pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-1.json
浏览文件 @
6cdbf336
...
...
@@ -10,7 +10,7 @@
"role"
:
"edge"
},
"value"
:
[
161
15780
96
,
161
66756
96
,
"1"
]
},
...
...
@@ -20,7 +20,7 @@
"role"
:
"edge"
},
"value"
:
[
161
15780
96
,
161
66756
96
,
"2"
]
}
...
...
@@ -38,7 +38,7 @@
"role"
:
"edge"
},
"value"
:
[
161
15780
96
,
161
66756
96
,
"1048576"
]
},
...
...
@@ -48,7 +48,7 @@
"role"
:
"edge"
},
"value"
:
[
161
15780
96
,
161
66756
96
,
"2097152"
]
}
...
...
pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-2.json
浏览文件 @
6cdbf336
...
...
@@ -10,7 +10,7 @@
"role"
:
"edge"
},
"value"
:
[
161
15780
96
,
161
66756
96
,
"2"
]
}
...
...
@@ -28,7 +28,7 @@
"role"
:
"edge"
},
"value"
:
[
161
15780
96
,
161
66756
96
,
"0.25"
]
}
...
...
pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-3.json
浏览文件 @
6cdbf336
...
...
@@ -10,7 +10,7 @@
"role"
:
"edge"
},
"value"
:
[
161
15780
96
,
161
66756
96
,
"1048576"
]
},
...
...
@@ -20,7 +20,7 @@
"role"
:
"edge"
},
"value"
:
[
161
15780
96
,
161
66756
96
,
"2097152"
]
}
...
...
@@ -38,7 +38,7 @@
"role"
:
"edge"
},
"value"
:
[
161
15780
96
,
161
66756
96
,
"0.0001220703125"
]
},
...
...
@@ -48,7 +48,7 @@
"role"
:
"edge"
},
"value"
:
[
161
15780
96
,
161
66756
96
,
"0.000244140625"
]
}
...
...
pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-4.json
0 → 100644
浏览文件 @
6cdbf336
[
{
"metric_name"
:
"pod_cpu_usage"
,
"data"
:
{
"resultType"
:
"vector"
,
"result"
:
[
{
"metric"
:
{
"namespace"
:
"kubeedge"
,
"pod"
:
"pod1"
},
"value"
:
[
1616675696
,
"0.001"
]
}
]
}
},
{
"metric_name"
:
"pod_memory_usage_wo_cache"
,
"data"
:
{
"resultType"
:
"vector"
,
"result"
:
[
{
"metric"
:
{
"namespace"
:
"kubeedge"
,
"pod"
:
"pod1"
},
"value"
:
[
1616675696
,
"1"
]
}
]
}
}
]
\ No newline at end of file
pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-5.json
0 → 100644
浏览文件 @
6cdbf336
[
{
"metric_name"
:
"pod_cpu_usage"
,
"data"
:
{
"resultType"
:
"vector"
,
"result"
:
[
{
"metric"
:
{
"namespace"
:
"kubeedge"
,
"pod"
:
"pod2"
},
"value"
:
[
1616675696
,
"0.002"
]
}
]
}
},
{
"metric_name"
:
"pod_memory_usage_wo_cache"
,
"data"
:
{
"resultType"
:
"vector"
,
"result"
:
[
{
"metric"
:
{
"namespace"
:
"kubeedge"
,
"pod"
:
"pod2"
},
"value"
:
[
1616675696
,
"2"
]
}
]
}
}
]
\ No newline at end of file
pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-6.json
0 → 100644
浏览文件 @
6cdbf336
[
{
"metric_name"
:
"pod_cpu_usage"
,
"data"
:
{
"resultType"
:
"vector"
,
"result"
:
[
{
"metric"
:
{
"namespace"
:
"kubeedge"
,
"pod"
:
"pod1"
},
"value"
:
[
1616675696
,
"0.001"
]
}
]
}
},
{
"metric_name"
:
"pod_memory_usage_wo_cache"
,
"data"
:
{
"resultType"
:
"vector"
,
"result"
:
[
{
"metric"
:
{
"namespace"
:
"kubeedge"
,
"pod"
:
"pod1"
},
"value"
:
[
1616675696
,
"1"
]
}
]
}
}
]
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录