未验证 提交 1e744d0a 编写于 作者: W Wiley Wang 提交者: GitHub

Merge pull request #70 from wnxn/master

fix using nil pointer bug
......@@ -22,7 +22,7 @@ func Register(ws *restful.WebService, subPath string) {
}
// List all PersistentVolumeClaims of a specific StorageClass
// Extended API URL: "GET /api/v1alpha/storage/storageclasses/{name}/persistentvolumeclaims"
// Extended API URL: "GET /api/v1alpha1/storage/storageclasses/{name}/persistentvolumeclaims"
func GetPvcListBySc(request *restful.Request, response *restful.Response) {
scName := request.PathParameter("storageclass")
claims, err := models.GetPvcListBySc(scName)
......@@ -35,7 +35,7 @@ func GetPvcListBySc(request *restful.Request, response *restful.Response) {
}
// Get metrics of a specific StorageClass
// Extended API URL: "GET /api/v1alpha/storage/storageclasses/{name}/metrics"
// Extended API URL: "GET /api/v1alpha1/storage/storageclasses/{name}/metrics"
func GetScMetrics(request *restful.Request, response *restful.Response) {
scName := request.PathParameter("storageclass")
metrics, err := models.GetScMetrics(scName)
......
......@@ -17,7 +17,7 @@ func Register(ws *restful.WebService, subPath string) {
}
// List all pods of a specific PVC
// Extended API URL: "GET /api/v1alpha/volumes/namespaces/{namespace}/persistentvolumeclaims/{name}/pods"
// Extended API URL: "GET /api/v1alpha1/volumes/namespaces/{namespace}/persistentvolumeclaims/{name}/pods"
func GetPodListByPvc(request *restful.Request, response *restful.Response) {
pvcName := request.PathParameter("pvc")
nsName := request.PathParameter("namespace")
......
......@@ -23,31 +23,40 @@ type StorageMetrics struct {
Usage string `json:"usage,omitempty"`
}
// List persistent volume claims of a specific storage class
func GetPvcListBySc(storageclass string) (res []v12.PersistentVolumeClaim, err error) {
// Create Kubernetes client
cli := client.NewK8sClient()
// Get all persistent volume claims
claimList, err := cli.CoreV1().PersistentVolumeClaims("").List(v1.ListOptions{})
if err != nil {
return nil, err
}
// Select persistent volume claims which
// storage class name is equal to the specific storage class.
for _, claim := range claimList.Items {
if *claim.Spec.StorageClassName != storageclass {
if claim.Spec.StorageClassName != nil &&
*claim.Spec.StorageClassName == storageclass {
res = append(res, claim)
} else {
continue
}
res = append(res, claim)
}
return res, nil
}
// Get metrics of a specific storage class
func GetScMetrics(storageclass string) (res StorageMetrics, err error) {
// Create Kubernetes client
cli := client.NewK8sClient()
// Get persistent volumes
pvList, err := cli.CoreV1().PersistentVolumes().List(v1.ListOptions{})
if err != nil {
return StorageMetrics{}, err
}
var total resource.Quantity
// Gathering metrics of a specific storage class
for _, volume := range pvList.Items {
if volume.Spec.StorageClassName != storageclass {
continue
......
......@@ -13,13 +13,13 @@ type PodListByPvc struct {
Pods []v12.Pod `json:"pods"`
}
// List pods of a specific persistent volume claims
func GetPodListByPvc(pvc string, ns string) (res []v12.Pod, err error) {
cli := client.NewK8sClient()
podList, err := cli.CoreV1().Pods(ns).List(v1.ListOptions{})
if err != nil {
return nil, err
}
for _, pod := range podList.Items {
if IsPvcInPod(pod, pvc) == true {
res = append(res, pod)
......@@ -28,6 +28,7 @@ func GetPodListByPvc(pvc string, ns string) (res []v12.Pod, err error) {
return res, nil
}
// Check if the persistent volume claim is related to the pod
func IsPvcInPod(pod v12.Pod, pvcname string) bool {
for _, v := range pod.Spec.Volumes {
if v.VolumeSource.PersistentVolumeClaim != nil &&
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册