1. Add API of getting SC metrics. 2. Modify response body of the API of listing all PVCs of a SC

This commit is contained in:
wnxn
2018-05-25 14:02:42 +08:00
parent b7307da620
commit 09d7ccaaba
2 changed files with 57 additions and 9 deletions

View File

@@ -11,4 +11,9 @@ func Register(ws *restful.WebService, subPath string) {
To(models.GetPvcListBySc).Filter(route.RouteLogging)). To(models.GetPvcListBySc).Filter(route.RouteLogging)).
Consumes(restful.MIME_JSON, restful.MIME_XML). Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON) Produces(restful.MIME_JSON)
ws.Route(ws.GET(subPath+"/storageclasses/{storageclass}/metrics").
To(models.GetScMetrics).Filter(route.RouteLogging)).
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON)
} }

View File

@@ -3,6 +3,8 @@ package models
import ( import (
"github.com/emicklei/go-restful" "github.com/emicklei/go-restful"
"github.com/golang/glog" "github.com/golang/glog"
v12 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1"
"kubesphere.io/kubesphere/pkg/client" "kubesphere.io/kubesphere/pkg/client"
"kubesphere.io/kubesphere/pkg/constants" "kubesphere.io/kubesphere/pkg/constants"
@@ -10,15 +12,22 @@ import (
) )
type pvcListBySc struct { type pvcListBySc struct {
Claims []simplePvcList `json:"persistentvolumeclaims"`
Name string `json:"name"` Name string `json:"name"`
Claims []v12.PersistentVolumeClaim `json:"persistentvolumeclaims"`
} }
type simplePvcList struct { type scMetrics struct {
Claim string `json:"name"` Name string `json:"name"`
Namespace string `json:"namespace"` Metrics storageMetrics `json:"metrics"`
} }
type storageMetrics struct {
Capacity string `json:"capacity,omitempty"`
Usage string `json:"usage,omitempty"`
}
// List all PersistentVolumeClaims of a specific StorageClass
// Extended API URL: "GET /api/v1alpha/storage/storageclasses/{name}/persistentvolumeclaims"
func GetPvcListBySc(request *restful.Request, response *restful.Response) { func GetPvcListBySc(request *restful.Request, response *restful.Response) {
scName := request.PathParameter("storageclass") scName := request.PathParameter("storageclass")
@@ -30,17 +39,34 @@ func GetPvcListBySc(request *restful.Request, response *restful.Response) {
result := constants.ResultMessage{ result := constants.ResultMessage{
Kind: constants.KIND, Kind: constants.KIND,
ApiVersion: constants.APIVERSION, ApiVersion: constants.APIVERSION,
Data: pvcListBySc{Claims: claims, Name: scName}} Data: pvcListBySc{scName, claims}}
response.WriteAsJson(result) response.WriteAsJson(result)
} }
func getPvcListBySc(storageclass string) (res []simplePvcList, err error) { // Get metrics of a specific StorageClass
// Extended API URL: "GET /api/v1alpha/storage/storageclasses/{name}/metrics"
func GetScMetrics(request *restful.Request, response *restful.Response) {
scName := request.PathParameter("storageclass")
glog.Infof("Run GetPvcListBySc: SC = %s", scName)
metrics, err := getScMetrics(scName)
if err != nil {
response.WriteError(http.StatusInternalServerError, err)
}
result := constants.ResultMessage{
Kind: constants.KIND,
ApiVersion: constants.APIVERSION,
Data: scMetrics{Name: scName, Metrics: metrics},
}
response.WriteAsJson(result)
}
func getPvcListBySc(storageclass string) (res []v12.PersistentVolumeClaim, err error) {
cli := client.NewK8sClient() cli := client.NewK8sClient()
claimList, err := cli.CoreV1().PersistentVolumeClaims("").List(v1.ListOptions{}) claimList, err := cli.CoreV1().PersistentVolumeClaims("").List(v1.ListOptions{})
if err != nil { if err != nil {
glog.Error("Read all PVC err: ", err)
return nil, err return nil, err
} }
@@ -48,7 +74,24 @@ func getPvcListBySc(storageclass string) (res []simplePvcList, err error) {
if *claim.Spec.StorageClassName != storageclass { if *claim.Spec.StorageClassName != storageclass {
continue continue
} }
res = append(res, simplePvcList{Claim: claim.Name, Namespace: claim.Namespace}) res = append(res, claim)
} }
return res, nil return res, nil
} }
func getScMetrics(storageclass string) (res storageMetrics, err error) {
cli := client.NewK8sClient()
pvList, err := cli.CoreV1().PersistentVolumes().List(v1.ListOptions{})
if err != nil {
return storageMetrics{}, err
}
var total resource.Quantity
for _, volume := range pvList.Items {
if volume.Spec.StorageClassName != storageclass {
continue
}
total.Add(volume.Spec.Capacity[v12.ResourceStorage])
}
return storageMetrics{Usage: total.String()}, nil
}