Merge pull request #25 from wnxn/master
Add an API of listing all pods related to specific PVC
This commit is contained in:
@@ -23,6 +23,7 @@ import (
|
|||||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/nodes"
|
"kubesphere.io/kubesphere/pkg/apis/v1alpha/nodes"
|
||||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/registries"
|
"kubesphere.io/kubesphere/pkg/apis/v1alpha/registries"
|
||||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/storage"
|
"kubesphere.io/kubesphere/pkg/apis/v1alpha/storage"
|
||||||
|
"kubesphere.io/kubesphere/pkg/apis/v1alpha/volumes"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -35,6 +36,7 @@ func init() {
|
|||||||
kubectl.Register(ws, "/namespaces/{namespace}/kubectl")
|
kubectl.Register(ws, "/namespaces/{namespace}/kubectl")
|
||||||
registries.Register(ws, "/registries")
|
registries.Register(ws, "/registries")
|
||||||
storage.Register(ws, "/storage")
|
storage.Register(ws, "/storage")
|
||||||
|
volumes.Register(ws, "/volumes")
|
||||||
|
|
||||||
// add webservice to default container
|
// add webservice to default container
|
||||||
restful.Add(ws)
|
restful.Add(ws)
|
||||||
|
|||||||
14
pkg/apis/v1alpha/volumes/volumes_handler.go
Normal file
14
pkg/apis/v1alpha/volumes/volumes_handler.go
Normal 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)
|
||||||
|
}
|
||||||
@@ -2,7 +2,6 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/emicklei/go-restful"
|
"github.com/emicklei/go-restful"
|
||||||
"github.com/golang/glog"
|
|
||||||
v12 "k8s.io/api/core/v1"
|
v12 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/resource"
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1"
|
"k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@@ -29,9 +28,7 @@ type storageMetrics struct {
|
|||||||
// 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/v1alpha/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")
|
||||||
glog.Infof("Run GetPvcListBySc: SC = %s", scName)
|
|
||||||
claims, err := getPvcListBySc(scName)
|
claims, err := getPvcListBySc(scName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.WriteError(http.StatusInternalServerError, err)
|
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"
|
// Extended API URL: "GET /api/v1alpha/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")
|
||||||
glog.Infof("Run GetPvcListBySc: SC = %s", scName)
|
|
||||||
|
|
||||||
metrics, err := getScMetrics(scName)
|
metrics, err := getScMetrics(scName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.WriteError(http.StatusInternalServerError, err)
|
response.WriteError(http.StatusInternalServerError, err)
|
||||||
|
|||||||
60
pkg/models/volumes.go
Normal file
60
pkg/models/volumes.go
Normal 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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user