Fix minor metrics bugs and refactor route implementation

This commit is contained in:
jeff
2018-06-16 16:55:39 +08:00
committed by jeff
parent 28dbcc797b
commit 18932ed1d8
429 changed files with 32117 additions and 3988 deletions

View File

@@ -22,7 +22,7 @@ import (
"kubesphere.io/kubesphere/pkg/filter/route"
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/pkg/models"
"kubesphere.io/kubesphere/pkg/models/metrics"
)
func Register(ws *restful.WebService) {
@@ -30,75 +30,48 @@ func Register(ws *restful.WebService) {
ws.Route(ws.GET("/pods").To(handleAllPods).Filter(route.RouteLogging)).
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON)
ws.Route(ws.GET("/namespaces/{namespace}/pods/{podname}").To(handlePodUnderNameSpace).Filter(route.RouteLogging)).
ws.Route(ws.GET("/namespaces/{namespace}/pods/{pod}").To(handlePodUnderNameSpace).Filter(route.RouteLogging)).
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON)
ws.Route(ws.GET("/namespaces/{namespace}/pods").To(handlePodsUnderNameSpace).Filter(route.RouteLogging)).
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON)
ws.Route(ws.GET("/nodes/{nodename}/pods").To(handlePodsUnderNode).Filter(route.RouteLogging)).
ws.Route(ws.GET("/nodes/{node}/pods").To(handlePodsUnderNode).Filter(route.RouteLogging)).
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON)
ws.Route(ws.GET("/nodes/{nodename}/namespaces/{namespace}/pods").To(handlePodsUnderNodeAndNameSpace).Filter(route.RouteLogging)).
ws.Route(ws.GET("/nodes/{node}/namespaces/{namespace}/pods").To(handlePodsUnderNodeAndNameSpace).Filter(route.RouteLogging)).
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON)
}
func handleAllPods(request *restful.Request, response *restful.Response) {
var result constants.PageableResponse
namespaces := models.GetNameSpaces()
var total_count int
for i, namespace := range namespaces {
result = models.FormatPodsMetrics("", namespace)
result.Items = append(result.Items, result)
total_count = i
}
result.TotalCount = total_count
result = metrics.GetAllPodMetrics()
response.WriteAsJson(result)
}
func handlePodsUnderNameSpace(request *restful.Request, response *restful.Response) {
var result constants.PageableResponse
result = models.FormatPodsMetrics("", request.PathParameter("namespace"))
result = metrics.GetPodMetricsInNamespace(request.PathParameter("namespace"))
response.WriteAsJson(result)
}
func handlePodsUnderNode(request *restful.Request, response *restful.Response) {
var result constants.PageableResponse
var resultNameSpace constants.PageableResponse
namespaces := models.GetNameSpaces()
var total_count int
for _, namespace := range namespaces {
resultNameSpace = models.FormatPodsMetrics(request.PathParameter("nodename"), namespace)
var sub_total_count int
for j, pod := range resultNameSpace.Items {
result.Items = append(result.Items, pod)
sub_total_count = j
}
total_count += sub_total_count
}
result.TotalCount = total_count
result = metrics.GetPodMetricsInNode(request.PathParameter("node"))
response.WriteAsJson(result)
}
func handlePodUnderNameSpace(request *restful.Request, response *restful.Response) {
var resultPod models.ResultPod
resultPod = models.FormatPodMetrics(request.PathParameter("namespace"), request.PathParameter("podname"))
var resultPod metrics.PodMetrics
resultPod = metrics.FormatPodMetrics(request.PathParameter("namespace"), request.PathParameter("pod"))
response.WriteAsJson(resultPod)
}
func handlePodsUnderNodeAndNameSpace(request *restful.Request, response *restful.Response) {
var result constants.PageableResponse
result = models.FormatPodsMetrics(request.PathParameter("nodename"), request.PathParameter("namespace"))
nodeName := request.PathParameter("node")
namespace := request.PathParameter("namespace")
result = metrics.GetPodMetricsInNamespaceOfNode(namespace, nodeName)
response.WriteAsJson(result)
}