Merge pull request #25 from wnxn/master

Add an API of listing all pods related to specific PVC
This commit is contained in:
Wiley Wang
2018-05-26 12:15:31 +08:00
committed by GitHub
4 changed files with 76 additions and 5 deletions

View File

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

View File

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

View File

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

60
pkg/models/volumes.go Normal file
View File

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