Merge pull request #38 from wnxn/master

move API handlers in webservice resource files
This commit is contained in:
Wiley Wang
2018-06-01 10:26:05 +08:00
committed by GitHub
5 changed files with 57 additions and 71 deletions

View File

@@ -17,11 +17,11 @@ limitations under the License.
package main
import (
"github.com/spf13/pflag"
"kubesphere.io/kubesphere/pkg/app"
"kubesphere.io/kubesphere/pkg/logs"
"kubesphere.io/kubesphere/pkg/options"
"kubesphere.io/kubesphere/pkg/version"
"github.com/spf13/pflag"
)
func main() {

View File

@@ -4,16 +4,42 @@ import (
"github.com/emicklei/go-restful"
"kubesphere.io/kubesphere/pkg/filter/route"
"kubesphere.io/kubesphere/pkg/models"
"net/http"
)
func Register(ws *restful.WebService, subPath string) {
ws.Route(ws.GET(subPath+"/storageclasses/{storageclass}/persistentvolumeclaims").
To(models.GetPvcListBySc).Filter(route.RouteLogging)).
To(GetPvcListBySc).Filter(route.RouteLogging)).
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON)
ws.Route(ws.GET(subPath+"/storageclasses/{storageclass}/metrics").
To(models.GetScMetrics).Filter(route.RouteLogging)).
To(GetScMetrics).Filter(route.RouteLogging)).
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON)
}
// 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")
claims, err := models.GetPvcListBySc(scName)
if err != nil {
response.WriteError(http.StatusInternalServerError, err)
}
result := models.PvcListBySc{scName, claims}
response.WriteAsJson(result)
}
// Get metrics of a specific StorageClass
// Extended API URL: "GET /api/v1alpha/storage/storageclasses/{name}/metrics"
func GetScMetrics(request *restful.Request, response *restful.Response) {
scName := request.PathParameter("storageclass")
metrics, err := models.GetScMetrics(scName)
if err != nil {
response.WriteError(http.StatusInternalServerError, err)
}
result := models.ScMetrics{Name: scName, Metrics: metrics}
response.WriteAsJson(result)
}

View File

@@ -4,11 +4,25 @@ import (
"github.com/emicklei/go-restful"
"kubesphere.io/kubesphere/pkg/filter/route"
"kubesphere.io/kubesphere/pkg/models"
"net/http"
)
func Register(ws *restful.WebService, subPath string) {
ws.Route(ws.GET(subPath+"/namespaces/{namespace}/persistentvolumeclaims/{pvc}/pods").
To(models.GetPodListByPvc).Filter(route.RouteLogging)).
To(GetPodListByPvc).Filter(route.RouteLogging)).
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON)
}
// 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 := models.GetPodListByPvc(pvcName, nsName)
if err != nil {
response.WriteError(http.StatusInternalServerError, err)
}
result := models.PodListByPvc{Name: pvcName, Namespace: nsName, Pods: pods}
response.WriteAsJson(result)
}

View File

@@ -1,61 +1,28 @@
package models
import (
"github.com/emicklei/go-restful"
v12 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"kubesphere.io/kubesphere/pkg/client"
"kubesphere.io/kubesphere/pkg/constants"
"net/http"
)
type pvcListBySc struct {
type PvcListBySc struct {
Name string `json:"name"`
Claims []v12.PersistentVolumeClaim `json:"persistentvolumeclaims"`
Claims []v12.PersistentVolumeClaim `json:"items"`
}
type scMetrics struct {
type ScMetrics struct {
Name string `json:"name"`
Metrics storageMetrics `json:"metrics"`
Metrics StorageMetrics `json:"metrics"`
}
type storageMetrics struct {
type StorageMetrics struct {
Capacity string `json:"capacity,omitempty"`
Usage string `json:"usage,omitempty"`
}
// 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")
claims, err := getPvcListBySc(scName)
if err != nil {
response.WriteError(http.StatusInternalServerError, err)
}
result := constants.ResultMessage{
Data: pvcListBySc{scName, claims}}
response.WriteAsJson(result)
}
// Get metrics of a specific StorageClass
// Extended API URL: "GET /api/v1alpha/storage/storageclasses/{name}/metrics"
func GetScMetrics(request *restful.Request, response *restful.Response) {
scName := request.PathParameter("storageclass")
metrics, err := getScMetrics(scName)
if err != nil {
response.WriteError(http.StatusInternalServerError, err)
}
result := constants.ResultMessage{
Data: scMetrics{Name: scName, Metrics: metrics},
}
response.WriteAsJson(result)
}
func getPvcListBySc(storageclass string) (res []v12.PersistentVolumeClaim, err error) {
func GetPvcListBySc(storageclass string) (res []v12.PersistentVolumeClaim, err error) {
cli := client.NewK8sClient()
claimList, err := cli.CoreV1().PersistentVolumeClaims("").List(v1.ListOptions{})
@@ -72,11 +39,11 @@ func getPvcListBySc(storageclass string) (res []v12.PersistentVolumeClaim, err e
return res, nil
}
func getScMetrics(storageclass string) (res storageMetrics, err error) {
func GetScMetrics(storageclass string) (res StorageMetrics, err error) {
cli := client.NewK8sClient()
pvList, err := cli.CoreV1().PersistentVolumes().List(v1.ListOptions{})
if err != nil {
return storageMetrics{}, err
return StorageMetrics{}, err
}
var total resource.Quantity
@@ -86,5 +53,5 @@ func getScMetrics(storageclass string) (res storageMetrics, err error) {
}
total.Add(volume.Spec.Capacity[v12.ResourceStorage])
}
return storageMetrics{Usage: total.String()}, nil
return StorageMetrics{Usage: total.String()}, nil
}

View File

@@ -1,39 +1,18 @@
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 {
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{
Data: podListByPvc{
Name: pvcName, Namespace: nsName, Pods: pods}}
response.WriteAsJson(result)
}
func getPodListByPvc(pvc string, ns string) (res []v12.Pod, err error) {
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 {
@@ -41,14 +20,14 @@ func getPodListByPvc(pvc string, ns string) (res []v12.Pod, err error) {
}
for _, pod := range podList.Items {
if isPvcInPod(pod, pvc) == true {
if IsPvcInPod(pod, pvc) == true {
res = append(res, pod)
}
}
return res, nil
}
func isPvcInPod(pod v12.Pod, pvcname string) bool {
func IsPvcInPod(pod v12.Pod, pvcname string) bool {
for _, v := range pod.Spec.Volumes {
if v.VolumeSource.PersistentVolumeClaim != nil &&
v.VolumeSource.PersistentVolumeClaim.ClaimName == pvcname {