未验证 提交 70a3b069 编写于 作者: W Wiley Wang 提交者: GitHub

Merge pull request #79 from wnxn/master

add storageclass metrics list extend API 
......@@ -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)
}
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 class
func GetScMetrics(storageclass string) (res StorageMetrics, err error) {
// Get info of metrics
func GetScEntityMetrics(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 != storageclass {
if volume.Spec.StorageClassName != scname {
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.
先完成此消息的编辑!
想要评论请 注册