Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
70a3b069
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,发现更多精彩内容 >>
未验证
提交
70a3b069
编写于
6月 13, 2018
作者:
W
Wiley Wang
提交者:
GitHub
6月 13, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #79 from wnxn/master
add storageclass metrics list extend API
上级
b10508f0
fc811db9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
111 addition
and
17 deletion
+111
-17
pkg/apis/v1alpha/storage/storage_handler.go
pkg/apis/v1alpha/storage/storage_handler.go
+19
-5
pkg/models/storage.go
pkg/models/storage.go
+92
-12
未找到文件。
pkg/apis/v1alpha/storage/storage_handler.go
浏览文件 @
70a3b069
...
...
@@ -19,10 +19,15 @@ func Register(ws *restful.WebService, subPath string) {
To
(
GetScMetrics
)
.
Filter
(
route
.
RouteLogging
))
.
Consumes
(
restful
.
MIME_JSON
,
restful
.
MIME_XML
)
.
Produces
(
restful
.
MIME_JSON
)
ws
.
Route
(
ws
.
GET
(
subPath
+
"/storageclasses/metrics"
)
.
To
(
GetScMetricsList
)
.
Filter
(
route
.
RouteLogging
))
.
Consumes
(
restful
.
MIME_JSON
,
restful
.
MIME_XML
)
.
Produces
(
restful
.
MIME_JSON
)
}
// List all PersistentVolumeClaims of a specific StorageClass
// Extended API URL: "GET /api/v1alpha1/storage/storageclasses/{
name
}/persistentvolumeclaims"
// Extended API URL: "GET /api/v1alpha1/storage/storageclasses/{
storageclass
}/persistentvolumeclaims"
func
GetPvcListBySc
(
request
*
restful
.
Request
,
response
*
restful
.
Response
)
{
scName
:=
request
.
PathParameter
(
"storageclass"
)
claims
,
err
:=
models
.
GetPvcListBySc
(
scName
)
...
...
@@ -34,14 +39,23 @@ func GetPvcListBySc(request *restful.Request, response *restful.Response) {
response
.
WriteAsJson
(
result
)
}
// Get
metrics of a specific StorageClass
// Extended API URL: "GET /api/v1alpha1/storage/storageclasses/{
name
}/metrics"
// Get
StorageClass item
// Extended API URL: "GET /api/v1alpha1/storage/storageclasses/{
storageclass
}/metrics"
func
GetScMetrics
(
request
*
restful
.
Request
,
response
*
restful
.
Response
)
{
scName
:=
request
.
PathParameter
(
"storageclass"
)
metrics
,
err
:=
models
.
GetScMetrics
(
scName
)
result
,
err
:=
models
.
GetScItemMetrics
(
scName
)
if
err
!=
nil
{
response
.
WriteError
(
http
.
StatusInternalServerError
,
err
)
}
response
.
WriteAsJson
(
result
)
}
// Get StorageClass item list
// Extended API URL: "GET /api/v1alpha1/storage/storageclasses/metrics"
func
GetScMetricsList
(
request
*
restful
.
Request
,
response
*
restful
.
Response
)
{
result
,
err
:=
models
.
GetScItemMetricsList
()
if
err
!=
nil
{
response
.
WriteError
(
http
.
StatusInternalServerError
,
err
)
}
result
:=
models
.
ScMetrics
{
Name
:
scName
,
Metrics
:
metrics
}
response
.
WriteAsJson
(
result
)
}
pkg/models/storage.go
浏览文件 @
70a3b069
package
models
import
(
"strconv"
"github.com/golang/glog"
v12
"k8s.io/api/core/v1"
v13
"k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/apis/meta/v1"
...
...
@@ -14,13 +18,20 @@ type PvcListBySc struct {
}
type
ScMetrics
struct
{
Name
string
`json:"name"`
Metrics
StorageMetrics
`json:"metrics"`
Capacity
string
`json:"capacity,omitempty"`
Usage
string
`json:"usage,omitempty"`
PvcNumber
string
`json:"pvcNumber"`
}
// StorageClass metrics item
type
ScMetricsItem
struct
{
Name
string
`json:"name"`
Metrics
ScMetrics
`json:"metrics"`
}
type
StorageMetrics
struct
{
Capacity
string
`json:"capacity,omitempty"`
Usage
string
`json:"usage,omitempty
"`
// StorageClass metrics items list
type
ScMetricsItemList
struct
{
Items
[]
ScMetricsItem
`json:"items
"`
}
// List persistent volume claims of a specific storage class
...
...
@@ -30,6 +41,7 @@ func GetPvcListBySc(storageclass string) (res []v12.PersistentVolumeClaim, err e
// Get all persistent volume claims
claimList
,
err
:=
cli
.
CoreV1
()
.
PersistentVolumeClaims
(
""
)
.
List
(
v1
.
ListOptions
{})
if
err
!=
nil
{
glog
.
Errorf
(
"list persistent volumes error: name:
\"
%s
\"
, error msg:
\"
%s
\"
"
,
storageclass
,
err
.
Error
())
return
nil
,
err
}
// Select persistent volume claims which
...
...
@@ -45,23 +57,91 @@ func GetPvcListBySc(storageclass string) (res []v12.PersistentVolumeClaim, err e
return
res
,
nil
}
// Get
metrics of a specific storage clas
s
func
GetSc
Metrics
(
storageclass
string
)
(
res
StorageMetrics
,
err
error
)
{
// Get
info of metric
s
func
GetSc
EntityMetrics
(
scname
string
)
(
ScMetrics
,
error
)
{
// Create Kubernetes client
cli
:=
client
.
NewK8sClient
()
// Get persistent volumes
// Get PV
pvList
,
err
:=
cli
.
CoreV1
()
.
PersistentVolumes
()
.
List
(
v1
.
ListOptions
{})
if
err
!=
nil
{
return
StorageMetrics
{},
err
glog
.
Errorf
(
"list persistent volume request error: error msg:
\"
%s
\"
"
,
err
.
Error
())
return
ScMetrics
{},
err
}
// Get PVC
pvcList
,
err
:=
GetPvcListBySc
(
scname
)
if
err
!=
nil
{
return
ScMetrics
{},
err
}
// Get storage usage
// Gathering usage of a specific StorageClass
var
total
resource
.
Quantity
// Gathering metrics of a specific storage class
for
_
,
volume
:=
range
pvList
.
Items
{
if
volume
.
Spec
.
StorageClassName
!=
s
torageclass
{
if
volume
.
Spec
.
StorageClassName
!=
s
cname
{
continue
}
total
.
Add
(
volume
.
Spec
.
Capacity
[
v12
.
ResourceStorage
])
}
return
StorageMetrics
{
Usage
:
total
.
String
()},
nil
usage
:=
total
.
String
()
// Get PVC number
pvcNum
:=
len
(
pvcList
)
return
ScMetrics
{
Usage
:
usage
,
PvcNumber
:
strconv
.
Itoa
(
pvcNum
)},
nil
}
// Get raw information of a SC
func
GetScEntity
(
scname
string
)
(
res
v13
.
StorageClass
,
err
error
)
{
// Create Kubernetes client
cli
:=
client
.
NewK8sClient
()
// Get SC
sc
,
err
:=
cli
.
StorageV1
()
.
StorageClasses
()
.
Get
(
scname
,
v1
.
GetOptions
{})
if
err
!=
nil
{
glog
.
Errorf
(
"get storage class request error: name:
\"
%s
\"
, error msg:
\"
%s
\"
"
,
scname
,
err
.
Error
())
return
v13
.
StorageClass
{},
err
}
return
*
sc
,
nil
}
// Get SC item
func
GetScItemMetrics
(
scname
string
)
(
res
ScMetricsItem
,
err
error
)
{
// Check SC exist
_
,
err
=
GetScEntity
(
scname
)
if
err
!=
nil
{
return
ScMetricsItem
{},
err
}
metrics
,
err
:=
GetScEntityMetrics
(
scname
)
if
err
!=
nil
{
return
ScMetricsItem
{},
err
}
result
:=
ScMetricsItem
{
scname
,
metrics
}
return
result
,
nil
}
// Get SC item list
func
GetScItemMetricsList
()
(
res
ScMetricsItemList
,
err
error
)
{
// Create Kubernetes client
cli
:=
client
.
NewK8sClient
()
// Get StorageClass list
scList
,
err
:=
cli
.
StorageV1
()
.
StorageClasses
()
.
List
(
v1
.
ListOptions
{})
if
err
!=
nil
{
glog
.
Errorf
(
"list storage classes request error: error msg:
\"
%s
\"
"
,
err
.
Error
())
return
ScMetricsItemList
{},
err
}
if
scList
==
nil
{
return
ScMetricsItemList
{},
nil
}
// Set return value
res
=
ScMetricsItemList
{}
for
_
,
v
:=
range
scList
.
Items
{
item
,
err
:=
GetScItemMetrics
(
v
.
GetName
())
if
err
!=
nil
{
return
ScMetricsItemList
{},
err
}
res
.
Items
=
append
(
res
.
Items
,
item
)
}
return
res
,
nil
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录