From 786eaefb7d3cdee5e26e3614c617b439752a10f2 Mon Sep 17 00:00:00 2001 From: Wang Xin Date: Thu, 31 May 2018 19:51:46 +0800 Subject: [PATCH] move API handlers in webservice resource files --- cmd/kubesphere.go | 2 +- pkg/apis/v1alpha/storage/storage_handler.go | 30 +++++++++++- pkg/apis/v1alpha/volumes/volumes_handler.go | 16 ++++++- pkg/models/storage.go | 51 ++++----------------- pkg/models/volumes.go | 29 ++---------- 5 files changed, 57 insertions(+), 71 deletions(-) diff --git a/cmd/kubesphere.go b/cmd/kubesphere.go index 8f9526ded..214f70c1a 100644 --- a/cmd/kubesphere.go +++ b/cmd/kubesphere.go @@ -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() { diff --git a/pkg/apis/v1alpha/storage/storage_handler.go b/pkg/apis/v1alpha/storage/storage_handler.go index 0c935e881..fa4a70e50 100644 --- a/pkg/apis/v1alpha/storage/storage_handler.go +++ b/pkg/apis/v1alpha/storage/storage_handler.go @@ -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) +} diff --git a/pkg/apis/v1alpha/volumes/volumes_handler.go b/pkg/apis/v1alpha/volumes/volumes_handler.go index 8b7bbd95a..ff41a22bb 100644 --- a/pkg/apis/v1alpha/volumes/volumes_handler.go +++ b/pkg/apis/v1alpha/volumes/volumes_handler.go @@ -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) +} diff --git a/pkg/models/storage.go b/pkg/models/storage.go index 56e4cc2dc..10afddc91 100644 --- a/pkg/models/storage.go +++ b/pkg/models/storage.go @@ -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 } diff --git a/pkg/models/volumes.go b/pkg/models/volumes.go index 77ed368c2..07a150901 100644 --- a/pkg/models/volumes.go +++ b/pkg/models/volumes.go @@ -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 {