diff --git a/pkg/apis/v1alpha/storage/storage_handler.go b/pkg/apis/v1alpha/storage/storage_handler.go index fd9c7170e..1964cf31b 100644 --- a/pkg/apis/v1alpha/storage/storage_handler.go +++ b/pkg/apis/v1alpha/storage/storage_handler.go @@ -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) diff --git a/pkg/apis/v1alpha/volumes/volumes_handler.go b/pkg/apis/v1alpha/volumes/volumes_handler.go index 57ba71495..4115f96e7 100644 --- a/pkg/apis/v1alpha/volumes/volumes_handler.go +++ b/pkg/apis/v1alpha/volumes/volumes_handler.go @@ -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") diff --git a/pkg/models/storage.go b/pkg/models/storage.go index 53a7a6a90..4980d1629 100644 --- a/pkg/models/storage.go +++ b/pkg/models/storage.go @@ -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 diff --git a/pkg/models/volumes.go b/pkg/models/volumes.go index 3d6aa4d74..2eddec52f 100644 --- a/pkg/models/volumes.go +++ b/pkg/models/volumes.go @@ -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 &&