@@ -1,80 +0,0 @@
|
||||
/*
|
||||
|
||||
Copyright 2019 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"github.com/emicklei/go-restful"
|
||||
"kubesphere.io/kubesphere/pkg/errors"
|
||||
"kubesphere.io/kubesphere/pkg/models/storage"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ScMetricsItem struct {
|
||||
Name string `json:"name"`
|
||||
Metrics *storage.ScMetrics `json:"metrics"`
|
||||
}
|
||||
|
||||
// Get StorageClass item
|
||||
// Extended API URL: "GET /api/v1alpha1/storage/storageclasses/{storageclass}/metrics"
|
||||
func GetScMetrics(request *restful.Request, response *restful.Response) {
|
||||
scName := request.PathParameter("storageclass")
|
||||
|
||||
metrics, err := storage.GetScMetrics(scName)
|
||||
|
||||
if err != nil {
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
result := ScMetricsItem{
|
||||
Name: scName, Metrics: metrics,
|
||||
}
|
||||
|
||||
response.WriteAsJson(result)
|
||||
}
|
||||
|
||||
// Get StorageClass item list
|
||||
// Extended API URL: "GET /api/v1alpha1/storage/storageclasses/metrics"
|
||||
func GetScMetricsList(request *restful.Request, response *restful.Response) {
|
||||
scList, err := storage.GetScList()
|
||||
|
||||
if err != nil {
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
// Set return value
|
||||
items := make([]ScMetricsItem, 0)
|
||||
|
||||
for _, v := range scList {
|
||||
metrics, err := storage.GetScMetrics(v.GetName())
|
||||
|
||||
if err != nil {
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
item := ScMetricsItem{
|
||||
Name: v.GetName(), Metrics: metrics,
|
||||
}
|
||||
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
response.WriteAsJson(items)
|
||||
}
|
||||
@@ -18,6 +18,7 @@
|
||||
package resources
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/emicklei/go-restful"
|
||||
"github.com/golang/glog"
|
||||
"k8s.io/api/core/v1"
|
||||
@@ -27,10 +28,11 @@ import (
|
||||
"kubesphere.io/kubesphere/pkg/models/applications"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources"
|
||||
"kubesphere.io/kubesphere/pkg/params"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func ApplicationHandler(req *restful.Request, resp *restful.Response) {
|
||||
func ListApplication(req *restful.Request, resp *restful.Response) {
|
||||
limit, offset := params.ParsePaging(req.QueryParameter(params.PagingParam))
|
||||
clusterId := req.QueryParameter("cluster_id")
|
||||
runtimeId := req.QueryParameter("runtime_id")
|
||||
@@ -63,7 +65,7 @@ func ApplicationHandler(req *restful.Request, resp *restful.Response) {
|
||||
|
||||
}
|
||||
|
||||
func NamespacedApplicationHandler(req *restful.Request, resp *restful.Response) {
|
||||
func ListNamespacedApplication(req *restful.Request, resp *restful.Response) {
|
||||
limit, offset := params.ParsePaging(req.QueryParameter(params.PagingParam))
|
||||
namespaceName := req.PathParameter("namespace")
|
||||
clusterId := req.QueryParameter("cluster_id")
|
||||
@@ -113,3 +115,92 @@ func NamespacedApplicationHandler(req *restful.Request, resp *restful.Response)
|
||||
|
||||
resp.WriteAsJson(result)
|
||||
}
|
||||
|
||||
func DescribeApplication(req *restful.Request, resp *restful.Response) {
|
||||
clusterId := req.PathParameter("cluster_id")
|
||||
namespaceName := req.PathParameter("namespace")
|
||||
app, err := applications.GetApp(clusterId)
|
||||
if err != nil {
|
||||
glog.Errorln("get app failed", err)
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
||||
return
|
||||
}
|
||||
namespace, err := resources.GetResource("", resources.Namespaces, namespaceName)
|
||||
|
||||
if err != nil {
|
||||
glog.Errorln("get namespace failed", err)
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
var runtimeId string
|
||||
|
||||
if ns, ok := namespace.(*v1.Namespace); ok {
|
||||
runtimeId = ns.Annotations[constants.OpenPitrixRuntimeAnnotationKey]
|
||||
}
|
||||
|
||||
if runtimeId != app.RuntimeId {
|
||||
glog.Errorln("runtime not match", app.RuntimeId, runtimeId)
|
||||
resp.WriteHeaderAndEntity(http.StatusForbidden, errors.New(fmt.Sprintf("rumtime not match %s,%s", app.RuntimeId, runtimeId)))
|
||||
return
|
||||
}
|
||||
|
||||
resp.WriteEntity(app)
|
||||
return
|
||||
}
|
||||
|
||||
func DeployApplication(req *restful.Request, resp *restful.Response) {
|
||||
namespace := req.PathParameter("namespace")
|
||||
var app openpitrix.CreateClusterRequest
|
||||
err := req.ReadEntity(&app)
|
||||
if err != nil {
|
||||
resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
|
||||
return
|
||||
}
|
||||
err = applications.DeployApplication(namespace, app)
|
||||
if err != nil {
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
||||
return
|
||||
}
|
||||
resp.WriteEntity(errors.None)
|
||||
}
|
||||
|
||||
func DeleteApplication(req *restful.Request, resp *restful.Response) {
|
||||
clusterId := req.PathParameter("cluster_id")
|
||||
namespaceName := req.PathParameter("namespace")
|
||||
app, err := applications.GetApp(clusterId)
|
||||
if err != nil {
|
||||
glog.Errorln("get app failed", err)
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
||||
return
|
||||
}
|
||||
namespace, err := resources.GetResource("", resources.Namespaces, namespaceName)
|
||||
|
||||
if err != nil {
|
||||
glog.Errorln("get namespace failed", err)
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
var runtimeId string
|
||||
|
||||
if ns, ok := namespace.(*v1.Namespace); ok {
|
||||
runtimeId = ns.Annotations[constants.OpenPitrixRuntimeAnnotationKey]
|
||||
}
|
||||
|
||||
if runtimeId != app.RuntimeId {
|
||||
glog.Errorln("runtime not match", app.RuntimeId, runtimeId)
|
||||
resp.WriteHeaderAndEntity(http.StatusForbidden, errors.New(fmt.Sprintf("rumtime not match %s,%s", app.RuntimeId, runtimeId)))
|
||||
return
|
||||
}
|
||||
|
||||
err = applications.DeleteApplication(clusterId)
|
||||
|
||||
if err != nil {
|
||||
glog.Errorln("delete application failed", err)
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
resp.WriteEntity(errors.None)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user