From 06e7463ba0c0523ae3b0de8698b2052c6942d936 Mon Sep 17 00:00:00 2001 From: wnxn Date: Fri, 25 May 2018 17:45:34 +0800 Subject: [PATCH] 1. add an extended API of listing all pods mounting the specific PVC. 2. remove redundant logs --- pkg/apis/v1alpha/install.go | 2 + pkg/apis/v1alpha/volumes/volumes_handler.go | 14 +++++ pkg/models/storage.go | 5 -- pkg/models/volumes.go | 60 +++++++++++++++++++++ 4 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 pkg/apis/v1alpha/volumes/volumes_handler.go create mode 100644 pkg/models/volumes.go diff --git a/pkg/apis/v1alpha/install.go b/pkg/apis/v1alpha/install.go index 772dc7e9f..9f4a500d5 100644 --- a/pkg/apis/v1alpha/install.go +++ b/pkg/apis/v1alpha/install.go @@ -23,6 +23,7 @@ import ( "kubesphere.io/kubesphere/pkg/apis/v1alpha/nodes" "kubesphere.io/kubesphere/pkg/apis/v1alpha/registries" "kubesphere.io/kubesphere/pkg/apis/v1alpha/storage" + "kubesphere.io/kubesphere/pkg/apis/v1alpha/volumes" ) func init() { @@ -35,6 +36,7 @@ func init() { kubectl.Register(ws, "/namespaces/{namespace}/kubectl") registries.Register(ws, "/registries") storage.Register(ws, "/storage") + volumes.Register(ws, "/volumes") // add webservice to default container restful.Add(ws) diff --git a/pkg/apis/v1alpha/volumes/volumes_handler.go b/pkg/apis/v1alpha/volumes/volumes_handler.go new file mode 100644 index 000000000..8b7bbd95a --- /dev/null +++ b/pkg/apis/v1alpha/volumes/volumes_handler.go @@ -0,0 +1,14 @@ +package volumes + +import ( + "github.com/emicklei/go-restful" + "kubesphere.io/kubesphere/pkg/filter/route" + "kubesphere.io/kubesphere/pkg/models" +) + +func Register(ws *restful.WebService, subPath string) { + ws.Route(ws.GET(subPath+"/namespaces/{namespace}/persistentvolumeclaims/{pvc}/pods"). + To(models.GetPodListByPvc).Filter(route.RouteLogging)). + Consumes(restful.MIME_JSON, restful.MIME_XML). + Produces(restful.MIME_JSON) +} diff --git a/pkg/models/storage.go b/pkg/models/storage.go index 2e58dce98..de445dabf 100644 --- a/pkg/models/storage.go +++ b/pkg/models/storage.go @@ -2,7 +2,6 @@ package models import ( "github.com/emicklei/go-restful" - "github.com/golang/glog" v12 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -29,9 +28,7 @@ type storageMetrics struct { // 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) { - scName := request.PathParameter("storageclass") - glog.Infof("Run GetPvcListBySc: SC = %s", scName) claims, err := getPvcListBySc(scName) if err != nil { response.WriteError(http.StatusInternalServerError, err) @@ -48,8 +45,6 @@ func GetPvcListBySc(request *restful.Request, response *restful.Response) { // 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) diff --git a/pkg/models/volumes.go b/pkg/models/volumes.go new file mode 100644 index 000000000..5c0515e77 --- /dev/null +++ b/pkg/models/volumes.go @@ -0,0 +1,60 @@ +package models + +import ( + "github.com/emicklei/go-restful" + v12 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1" + "kubesphere.io/kubesphere/pkg/client" + "kubesphere.io/kubesphere/pkg/constants" + "net/http" +) + +type podListByPvc struct { + Name string `json:"name"` + Namespace string `json:"namespace"` + Pods []v12.Pod `json:"pods"` +} + +// List all pods of a specific PVC +// Extended API URL: "GET /api/v1alpha/volumes/namespaces/{namespace}/persistentvolumeclaims/{name}/pods" +func GetPodListByPvc(request *restful.Request, response *restful.Response) { + + pvcName := request.PathParameter("pvc") + nsName := request.PathParameter("namespace") + pods, err := getPodListByPvc(pvcName, nsName) + if err != nil { + response.WriteError(http.StatusInternalServerError, err) + } + result := constants.ResultMessage{ + Kind: constants.KIND, + ApiVersion: constants.APIVERSION, + Data: podListByPvc{ + Name: pvcName, Namespace: nsName, Pods: pods}} + + response.WriteAsJson(result) +} + +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) + } + } + return res, nil +} + +func isPvcInPod(pod v12.Pod, pvcname string) bool { + for _, v := range pod.Spec.Volumes { + if v.VolumeSource.PersistentVolumeClaim != nil && + v.VolumeSource.PersistentVolumeClaim.ClaimName == pvcname { + return true + } + } + return false +}