@@ -22,7 +22,7 @@ func Register(ws *restful.WebService, subPath string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List all PersistentVolumeClaims of a specific StorageClass
|
// 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) {
|
func GetPvcListBySc(request *restful.Request, response *restful.Response) {
|
||||||
scName := request.PathParameter("storageclass")
|
scName := request.PathParameter("storageclass")
|
||||||
claims, err := models.GetPvcListBySc(scName)
|
claims, err := models.GetPvcListBySc(scName)
|
||||||
@@ -35,7 +35,7 @@ func GetPvcListBySc(request *restful.Request, response *restful.Response) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get metrics of a specific StorageClass
|
// 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) {
|
func GetScMetrics(request *restful.Request, response *restful.Response) {
|
||||||
scName := request.PathParameter("storageclass")
|
scName := request.PathParameter("storageclass")
|
||||||
metrics, err := models.GetScMetrics(scName)
|
metrics, err := models.GetScMetrics(scName)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ func Register(ws *restful.WebService, subPath string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List all pods of a specific PVC
|
// 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) {
|
func GetPodListByPvc(request *restful.Request, response *restful.Response) {
|
||||||
pvcName := request.PathParameter("pvc")
|
pvcName := request.PathParameter("pvc")
|
||||||
nsName := request.PathParameter("namespace")
|
nsName := request.PathParameter("namespace")
|
||||||
|
|||||||
@@ -23,31 +23,40 @@ type StorageMetrics struct {
|
|||||||
Usage string `json:"usage,omitempty"`
|
Usage string `json:"usage,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// List persistent volume claims of a specific storage class
|
||||||
func GetPvcListBySc(storageclass string) (res []v12.PersistentVolumeClaim, err error) {
|
func GetPvcListBySc(storageclass string) (res []v12.PersistentVolumeClaim, err error) {
|
||||||
|
// Create Kubernetes client
|
||||||
cli := client.NewK8sClient()
|
cli := client.NewK8sClient()
|
||||||
|
// Get all persistent volume claims
|
||||||
claimList, err := cli.CoreV1().PersistentVolumeClaims("").List(v1.ListOptions{})
|
claimList, err := cli.CoreV1().PersistentVolumeClaims("").List(v1.ListOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// Select persistent volume claims which
|
||||||
|
// storage class name is equal to the specific storage class.
|
||||||
for _, claim := range claimList.Items {
|
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
|
continue
|
||||||
}
|
}
|
||||||
res = append(res, claim)
|
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get metrics of a specific storage class
|
||||||
func GetScMetrics(storageclass string) (res StorageMetrics, err error) {
|
func GetScMetrics(storageclass string) (res StorageMetrics, err error) {
|
||||||
|
// Create Kubernetes client
|
||||||
cli := client.NewK8sClient()
|
cli := client.NewK8sClient()
|
||||||
|
// Get persistent volumes
|
||||||
pvList, err := cli.CoreV1().PersistentVolumes().List(v1.ListOptions{})
|
pvList, err := cli.CoreV1().PersistentVolumes().List(v1.ListOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return StorageMetrics{}, err
|
return StorageMetrics{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var total resource.Quantity
|
var total resource.Quantity
|
||||||
|
// Gathering metrics of a specific storage class
|
||||||
for _, volume := range pvList.Items {
|
for _, volume := range pvList.Items {
|
||||||
if volume.Spec.StorageClassName != storageclass {
|
if volume.Spec.StorageClassName != storageclass {
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -13,13 +13,13 @@ type PodListByPvc struct {
|
|||||||
Pods []v12.Pod `json:"pods"`
|
Pods []v12.Pod `json:"pods"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// List pods of a specific persistent volume claims
|
||||||
func GetPodListByPvc(pvc string, ns string) (res []v12.Pod, err error) {
|
func GetPodListByPvc(pvc string, ns string) (res []v12.Pod, err error) {
|
||||||
cli := client.NewK8sClient()
|
cli := client.NewK8sClient()
|
||||||
podList, err := cli.CoreV1().Pods(ns).List(v1.ListOptions{})
|
podList, err := cli.CoreV1().Pods(ns).List(v1.ListOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, pod := range podList.Items {
|
for _, pod := range podList.Items {
|
||||||
if IsPvcInPod(pod, pvc) == true {
|
if IsPvcInPod(pod, pvc) == true {
|
||||||
res = append(res, pod)
|
res = append(res, pod)
|
||||||
@@ -28,6 +28,7 @@ func GetPodListByPvc(pvc string, ns string) (res []v12.Pod, err error) {
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if the persistent volume claim is related to the pod
|
||||||
func IsPvcInPod(pod v12.Pod, pvcname string) bool {
|
func IsPvcInPod(pod v12.Pod, pvcname string) bool {
|
||||||
for _, v := range pod.Spec.Volumes {
|
for _, v := range pod.Spec.Volumes {
|
||||||
if v.VolumeSource.PersistentVolumeClaim != nil &&
|
if v.VolumeSource.PersistentVolumeClaim != nil &&
|
||||||
|
|||||||
Reference in New Issue
Block a user