Add the API of listing all PVC of the specific storageclass

This commit is contained in:
wnxn
2018-05-24 17:21:41 +08:00
parent d2402b33c1
commit b7307da620
3 changed files with 83 additions and 14 deletions

View File

@@ -17,25 +17,26 @@ limitations under the License.
package v1alpha
import (
"github.com/emicklei/go-restful"
"kubesphere.io/kubesphere/pkg/apis/v1alpha/nodes"
"kubesphere.io/kubesphere/pkg/apis/v1alpha/kubeconfig"
"kubesphere.io/kubesphere/pkg/apis/v1alpha/kubectl"
"kubesphere.io/kubesphere/pkg/apis/v1alpha/registries"
"github.com/emicklei/go-restful"
"kubesphere.io/kubesphere/pkg/apis/v1alpha/kubeconfig"
"kubesphere.io/kubesphere/pkg/apis/v1alpha/kubectl"
"kubesphere.io/kubesphere/pkg/apis/v1alpha/nodes"
"kubesphere.io/kubesphere/pkg/apis/v1alpha/registries"
"kubesphere.io/kubesphere/pkg/apis/v1alpha/storage"
)
func init() {
ws := new(restful.WebService)
ws.Path("/api/v1alpha")
ws := new(restful.WebService)
ws.Path("/api/v1alpha")
nodes.Register(ws,"/nodes")
kubeconfig.Register(ws, "/namespaces/{namespace}/kubeconfig")
kubectl.Register(ws, "/namespaces/{namespace}/kubectl")
registries.Register(ws,"/registries")
nodes.Register(ws, "/nodes")
kubeconfig.Register(ws, "/namespaces/{namespace}/kubeconfig")
kubectl.Register(ws, "/namespaces/{namespace}/kubectl")
registries.Register(ws, "/registries")
storage.Register(ws, "/storage")
// add webservice to default container
restful.Add(ws)
// add webservice to default container
restful.Add(ws)
}

View File

@@ -0,0 +1,14 @@
package storage
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+"/storageclasses/{storageclass}/persistentvolumeclaims").
To(models.GetPvcListBySc).Filter(route.RouteLogging)).
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON)
}

54
pkg/models/storage.go Normal file
View File

@@ -0,0 +1,54 @@
package models
import (
"github.com/emicklei/go-restful"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"kubesphere.io/kubesphere/pkg/client"
"kubesphere.io/kubesphere/pkg/constants"
"net/http"
)
type pvcListBySc struct {
Claims []simplePvcList `json:"persistentvolumeclaims"`
Name string `json:"name"`
}
type simplePvcList struct {
Claim string `json:"name"`
Namespace string `json:"namespace"`
}
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)
}
result := constants.ResultMessage{
Kind: constants.KIND,
ApiVersion: constants.APIVERSION,
Data: pvcListBySc{Claims: claims, Name: scName}}
response.WriteAsJson(result)
}
func getPvcListBySc(storageclass string) (res []simplePvcList, err error) {
cli := client.NewK8sClient()
claimList, err := cli.CoreV1().PersistentVolumeClaims("").List(v1.ListOptions{})
if err != nil {
glog.Error("Read all PVC err: ", err)
return nil, err
}
for _, claim := range claimList.Items {
if *claim.Spec.StorageClassName != storageclass {
continue
}
res = append(res, simplePvcList{Claim: claim.Name, Namespace: claim.Namespace})
}
return res, nil
}