Merge pull request #70 from wnxn/master

fix using nil pointer bug
This commit is contained in:
Wiley Wang
2018-06-08 19:03:19 +08:00
committed by GitHub
4 changed files with 18 additions and 8 deletions

View File

@@ -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

View File

@@ -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 &&