Merge pull request #2195 from zheng1/refactor_op
Refactor with OpenPitrix
This commit is contained in:
@@ -1,3 +1,16 @@
|
||||
/*
|
||||
Copyright 2020 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 v1
|
||||
|
||||
import (
|
||||
@@ -10,7 +23,6 @@ import (
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/informers"
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
"kubesphere.io/kubesphere/pkg/models/openpitrix"
|
||||
"kubesphere.io/kubesphere/pkg/server/errors"
|
||||
"kubesphere.io/kubesphere/pkg/server/params"
|
||||
@@ -32,78 +44,44 @@ func newOpenpitrixHandler(factory informers.InformerFactory, opClient op.Client)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *openpitrixHandler) ListApplications(request *restful.Request, response *restful.Response) {
|
||||
limit, offset := params.ParsePaging(request)
|
||||
namespace := request.PathParameter("namespace")
|
||||
orderBy := params.GetStringValueWithDefault(request, params.OrderByParam, openpitrix.CreateTime)
|
||||
reverse := params.GetBoolValueWithDefault(request, params.ReverseParam, false)
|
||||
conditions, err := params.ParseConditions(request)
|
||||
func (h *openpitrixHandler) ListApplications(req *restful.Request, resp *restful.Response) {
|
||||
limit, offset := params.ParsePaging(req)
|
||||
clusterName := req.PathParameter("cluster")
|
||||
namespace := req.PathParameter("namespace")
|
||||
orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
|
||||
reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
|
||||
conditions, err := params.ParseConditions(req)
|
||||
|
||||
if err != nil {
|
||||
klog.V(4).Infoln(err)
|
||||
api.HandleBadRequest(response, nil, err)
|
||||
api.HandleBadRequest(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
// filter namespaced applications by runtime_id
|
||||
if namespace != "" {
|
||||
ns, err := h.informers.Core().V1().Namespaces().Lister().Get(namespace)
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
api.HandleInternalError(response, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
runtimeId := ns.Annotations[constants.OpenPitrixRuntimeAnnotationKey]
|
||||
|
||||
if runtimeId == "" {
|
||||
// runtime id not exist,return empty response
|
||||
response.WriteAsJson(models.PageableResponse{Items: []interface{}{}, TotalCount: 0})
|
||||
return
|
||||
} else {
|
||||
// filter by runtime id
|
||||
conditions.Match[openpitrix.RuntimeId] = runtimeId
|
||||
}
|
||||
}
|
||||
conditions.Match[openpitrix.Zone] = namespace
|
||||
// in openpitrix, runtime id is the cluster name
|
||||
conditions.Match[openpitrix.RuntimeId] = clusterName
|
||||
|
||||
result, err := h.openpitrix.ListApplications(conditions, limit, offset, orderBy, reverse)
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
api.HandleInternalError(response, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteAsJson(result)
|
||||
}
|
||||
|
||||
func (h *openpitrixHandler) DescribeApplication(req *restful.Request, resp *restful.Response) {
|
||||
clusterId := req.PathParameter("application")
|
||||
namespace := req.PathParameter("namespace")
|
||||
|
||||
app, err := h.openpitrix.DescribeApplication(namespace, clusterId)
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
handleOpenpitrixError(resp, err)
|
||||
return
|
||||
}
|
||||
|
||||
ns, err := h.informers.Core().V1().Namespaces().Lister().Get(namespace)
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
api.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
runtimeId := ns.Annotations[constants.OpenPitrixRuntimeAnnotationKey]
|
||||
resp.WriteAsJson(result)
|
||||
}
|
||||
|
||||
if runtimeId != app.Cluster.RuntimeId {
|
||||
err = fmt.Errorf("rumtime not match %s,%s", app.Cluster.RuntimeId, runtimeId)
|
||||
klog.V(4).Infoln(err)
|
||||
api.HandleForbidden(resp, nil, err)
|
||||
func (h *openpitrixHandler) DescribeApplication(req *restful.Request, resp *restful.Response) {
|
||||
clusterName := req.PathParameter("cluster")
|
||||
namespace := req.PathParameter("namespace")
|
||||
applicationId := req.PathParameter("application")
|
||||
|
||||
app, err := h.openpitrix.DescribeApplication(namespace, applicationId, clusterName)
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
handleOpenpitrixError(resp, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -112,6 +90,7 @@ func (h *openpitrixHandler) DescribeApplication(req *restful.Request, resp *rest
|
||||
}
|
||||
|
||||
func (h *openpitrixHandler) CreateApplication(req *restful.Request, resp *restful.Response) {
|
||||
clusterName := req.PathParameter("cluster")
|
||||
namespace := req.PathParameter("namespace")
|
||||
var createClusterRequest openpitrix.CreateClusterRequest
|
||||
err := req.ReadEntity(&createClusterRequest)
|
||||
@@ -123,7 +102,7 @@ func (h *openpitrixHandler) CreateApplication(req *restful.Request, resp *restfu
|
||||
|
||||
createClusterRequest.Username = req.HeaderParameter(constants.UserNameHeader)
|
||||
|
||||
err = h.openpitrix.CreateApplication(namespace, createClusterRequest)
|
||||
err = h.openpitrix.CreateApplication(clusterName, namespace, createClusterRequest)
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
@@ -136,7 +115,8 @@ func (h *openpitrixHandler) CreateApplication(req *restful.Request, resp *restfu
|
||||
|
||||
func (h *openpitrixHandler) ModifyApplication(req *restful.Request, resp *restful.Response) {
|
||||
var modifyClusterAttributesRequest openpitrix.ModifyClusterAttributesRequest
|
||||
clusterId := req.PathParameter("application")
|
||||
clusterName := req.PathParameter("cluster")
|
||||
applicationId := req.PathParameter("application")
|
||||
namespace := req.PathParameter("namespace")
|
||||
err := req.ReadEntity(&modifyClusterAttributesRequest)
|
||||
if err != nil {
|
||||
@@ -145,7 +125,7 @@ func (h *openpitrixHandler) ModifyApplication(req *restful.Request, resp *restfu
|
||||
return
|
||||
}
|
||||
|
||||
app, err := h.openpitrix.DescribeApplication(namespace, clusterId)
|
||||
app, err := h.openpitrix.DescribeApplication(namespace, applicationId, clusterName)
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
@@ -153,18 +133,8 @@ func (h *openpitrixHandler) ModifyApplication(req *restful.Request, resp *restfu
|
||||
return
|
||||
}
|
||||
|
||||
ns, err := h.informers.Core().V1().Namespaces().Lister().Get(namespace)
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
api.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
runtimeId := ns.Annotations[constants.OpenPitrixRuntimeAnnotationKey]
|
||||
|
||||
if runtimeId != app.Cluster.RuntimeId {
|
||||
err = fmt.Errorf("rumtime not match %s,%s", app.Cluster.RuntimeId, runtimeId)
|
||||
if clusterName != app.Cluster.RuntimeId {
|
||||
err = fmt.Errorf("runtime and cluster not match %s,%s", app.Cluster.RuntimeId, clusterName)
|
||||
klog.V(4).Infoln(err)
|
||||
api.HandleForbidden(resp, nil, err)
|
||||
return
|
||||
@@ -182,9 +152,10 @@ func (h *openpitrixHandler) ModifyApplication(req *restful.Request, resp *restfu
|
||||
}
|
||||
|
||||
func (h *openpitrixHandler) DeleteApplication(req *restful.Request, resp *restful.Response) {
|
||||
clusterId := req.PathParameter("application")
|
||||
clusterName := req.PathParameter("cluster")
|
||||
applicationId := req.PathParameter("application")
|
||||
namespace := req.PathParameter("namespace")
|
||||
app, err := h.openpitrix.DescribeApplication(namespace, clusterId)
|
||||
app, err := h.openpitrix.DescribeApplication(namespace, applicationId, clusterName)
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
@@ -192,24 +163,14 @@ func (h *openpitrixHandler) DeleteApplication(req *restful.Request, resp *restfu
|
||||
return
|
||||
}
|
||||
|
||||
ns, err := h.informers.Core().V1().Namespaces().Lister().Get(namespace)
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
api.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
runtimeId := ns.Annotations[constants.OpenPitrixRuntimeAnnotationKey]
|
||||
|
||||
if runtimeId != app.Cluster.RuntimeId {
|
||||
err = fmt.Errorf("rumtime not match %s,%s", app.Cluster.RuntimeId, runtimeId)
|
||||
if clusterName != app.Cluster.RuntimeId {
|
||||
err = fmt.Errorf("runtime and cluster not match %s,%s", app.Cluster.RuntimeId, clusterName)
|
||||
klog.V(4).Infoln(err)
|
||||
api.HandleForbidden(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.openpitrix.DeleteApplication(clusterId)
|
||||
err = h.openpitrix.DeleteApplication(applicationId)
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
@@ -220,6 +181,46 @@ func (h *openpitrixHandler) DeleteApplication(req *restful.Request, resp *restfu
|
||||
resp.WriteEntity(errors.None)
|
||||
}
|
||||
|
||||
func (h *openpitrixHandler) UpgradeApplication(req *restful.Request, resp *restful.Response) {
|
||||
clusterName := req.PathParameter("cluster")
|
||||
namespace := req.PathParameter("namespace")
|
||||
applicationId := req.PathParameter("application")
|
||||
var upgradeClusterRequest openpitrix.UpgradeClusterRequest
|
||||
err := req.ReadEntity(&upgradeClusterRequest)
|
||||
if err != nil {
|
||||
klog.V(4).Infoln(err)
|
||||
api.HandleBadRequest(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
upgradeClusterRequest.Username = req.HeaderParameter(constants.UserNameHeader)
|
||||
|
||||
app, err := h.openpitrix.DescribeApplication(namespace, applicationId, clusterName)
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
handleOpenpitrixError(resp, err)
|
||||
return
|
||||
}
|
||||
|
||||
if clusterName != app.Cluster.RuntimeId {
|
||||
err = fmt.Errorf("runtime and cluster not match %s,%s", app.Cluster.RuntimeId, clusterName)
|
||||
klog.V(4).Infoln(err)
|
||||
api.HandleForbidden(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.openpitrix.UpgradeApplication(upgradeClusterRequest)
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
api.HandleInternalError(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
resp.WriteEntity(errors.None)
|
||||
}
|
||||
|
||||
func (h *openpitrixHandler) GetAppVersionPackage(req *restful.Request, resp *restful.Response) {
|
||||
appId := req.PathParameter("app")
|
||||
versionId := req.PathParameter("version")
|
||||
@@ -833,6 +834,7 @@ func (h *openpitrixHandler) DescribeRepo(req *restful.Request, resp *restful.Res
|
||||
|
||||
resp.WriteEntity(result)
|
||||
}
|
||||
|
||||
func (h *openpitrixHandler) ListRepos(req *restful.Request, resp *restful.Response) {
|
||||
limit, offset := params.ParsePaging(req)
|
||||
orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
|
||||
@@ -856,6 +858,29 @@ func (h *openpitrixHandler) ListRepos(req *restful.Request, resp *restful.Respon
|
||||
resp.WriteEntity(result)
|
||||
}
|
||||
|
||||
func (h *openpitrixHandler) ListEvents(req *restful.Request, resp *restful.Response) {
|
||||
limit, offset := params.ParsePaging(req)
|
||||
orderBy := params.GetStringValueWithDefault(req, params.OrderByParam, openpitrix.CreateTime)
|
||||
reverse := params.GetBoolValueWithDefault(req, params.ReverseParam, false)
|
||||
conditions, err := params.ParseConditions(req)
|
||||
|
||||
if err != nil {
|
||||
klog.V(4).Infoln(err)
|
||||
api.HandleBadRequest(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.openpitrix.ListEvents(conditions, orderBy, reverse, limit, offset)
|
||||
|
||||
if err != nil {
|
||||
klog.Errorln(err)
|
||||
handleOpenpitrixError(resp, err)
|
||||
return
|
||||
}
|
||||
|
||||
resp.WriteEntity(result)
|
||||
}
|
||||
|
||||
func (h *openpitrixHandler) ListRepoEvents(req *restful.Request, resp *restful.Response) {
|
||||
repoId := req.PathParameter("repo")
|
||||
limit, offset := params.ParsePaging(req)
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
/*
|
||||
Copyright 2019 The KubeSphere Authors.
|
||||
|
||||
Copyright 2020 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 v1
|
||||
|
||||
import (
|
||||
@@ -54,13 +52,27 @@ func AddToContainer(c *restful.Container, factory informers.InformerFactory, op
|
||||
Required(false).
|
||||
DataFormat("key=value,key~value").
|
||||
DefaultValue("")).
|
||||
Param(webservice.PathParameter("namespace", "the name of the project")).
|
||||
Param(webservice.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
|
||||
Required(false).
|
||||
DataFormat("limit=%d,page=%d").
|
||||
DefaultValue("limit=10,page=1")))
|
||||
|
||||
webservice.Route(webservice.GET("/namespaces/{namespace}/applications").
|
||||
webservice.Route(webservice.GET("/clusters/{cluster}/applications").
|
||||
To(handler.ListApplications).
|
||||
Returns(http.StatusOK, api.StatusOK, models.PageableResponse{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
|
||||
Doc("List all applications in special cluster").
|
||||
Param(webservice.QueryParameter(params.ConditionsParam, "query conditions, connect multiple conditions with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").
|
||||
Required(false).
|
||||
DataFormat("key=value,key~value").
|
||||
DefaultValue("")).
|
||||
Param(webservice.PathParameter("cluster", "the name of the cluster.").Required(true)).
|
||||
Param(webservice.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
|
||||
Required(false).
|
||||
DataFormat("limit=%d,page=%d").
|
||||
DefaultValue("limit=10,page=1")))
|
||||
|
||||
webservice.Route(webservice.GET("/clusters/{cluster}/namespaces/{namespace}/applications").
|
||||
To(handler.ListApplications).
|
||||
Returns(http.StatusOK, api.StatusOK, models.PageableResponse{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
|
||||
@@ -69,45 +81,61 @@ func AddToContainer(c *restful.Container, factory informers.InformerFactory, op
|
||||
Required(false).
|
||||
DataFormat("key=value,key~value").
|
||||
DefaultValue("")).
|
||||
Param(webservice.PathParameter("namespace", "the name of the project")).
|
||||
Param(webservice.PathParameter("cluster", "the name of the cluster.").Required(true)).
|
||||
Param(webservice.PathParameter("namespace", "the name of the project").Required(true)).
|
||||
Param(webservice.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
|
||||
Required(false).
|
||||
DataFormat("limit=%d,page=%d").
|
||||
DefaultValue("limit=10,page=1")))
|
||||
|
||||
webservice.Route(webservice.GET("/namespaces/{namespace}/applications/{application}").
|
||||
webservice.Route(webservice.GET("/clusters/{cluster}/namespaces/{namespace}/applications/{application}").
|
||||
To(handler.DescribeApplication).
|
||||
Returns(http.StatusOK, api.StatusOK, openpitrix2.Application{}).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
|
||||
Doc("Describe the specified application of the namespace").
|
||||
Param(webservice.PathParameter("namespace", "the name of the project")).
|
||||
Param(webservice.PathParameter("application", "application ID")))
|
||||
Param(webservice.PathParameter("cluster", "the name of the cluster.").Required(true)).
|
||||
Param(webservice.PathParameter("namespace", "the name of the project").Required(true)).
|
||||
Param(webservice.PathParameter("application", "the id of the application").Required(true)))
|
||||
|
||||
webservice.Route(webservice.POST("/namespaces/{namespace}/applications").
|
||||
webservice.Route(webservice.POST("/clusters/{cluster}/namespaces/{namespace}/applications").
|
||||
To(handler.CreateApplication).
|
||||
Doc("Deploy a new application").
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
|
||||
Reads(openpitrix2.CreateClusterRequest{}).
|
||||
Returns(http.StatusOK, api.StatusOK, errors.Error{}).
|
||||
Param(webservice.PathParameter("namespace", "the name of the project")))
|
||||
Param(webservice.PathParameter("cluster", "the name of the cluster.").Required(true)).
|
||||
Param(webservice.PathParameter("namespace", "the name of the project").Required(true)))
|
||||
|
||||
webservice.Route(webservice.PATCH("/namespaces/{namespace}/applications/{application}").
|
||||
webservice.Route(webservice.PATCH("/clusters/{cluster}/namespaces/{namespace}/applications/{application}").
|
||||
Consumes(mimePatch...).
|
||||
To(handler.ModifyApplication).
|
||||
Doc("Modify application").
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
|
||||
Reads(openpitrix2.ModifyClusterAttributesRequest{}).
|
||||
Returns(http.StatusOK, api.StatusOK, errors.Error{}).
|
||||
Param(webservice.PathParameter("namespace", "the name of the project")).
|
||||
Param(webservice.PathParameter("application", "the id of the application cluster")))
|
||||
Param(webservice.PathParameter("cluster", "the name of the cluster.").Required(true)).
|
||||
Param(webservice.PathParameter("namespace", "the name of the project").Required(true)).
|
||||
Param(webservice.PathParameter("application", "the id of the application").Required(true)))
|
||||
|
||||
webservice.Route(webservice.DELETE("/namespaces/{namespace}/applications/{application}").
|
||||
webservice.Route(webservice.DELETE("/clusters/{cluster}/namespaces/{namespace}/applications/{application}").
|
||||
To(handler.DeleteApplication).
|
||||
Doc("Delete the specified application").
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
|
||||
Returns(http.StatusOK, api.StatusOK, errors.Error{}).
|
||||
Param(webservice.PathParameter("namespace", "the name of the project")).
|
||||
Param(webservice.PathParameter("application", "the id of the application cluster")))
|
||||
Param(webservice.PathParameter("cluster", "the name of the cluster.").Required(true)).
|
||||
Param(webservice.PathParameter("namespace", "the name of the project").Required(true)).
|
||||
Param(webservice.PathParameter("application", "the id of the application").Required(true)))
|
||||
|
||||
webservice.Route(webservice.POST("/clusters/{cluster}/namespaces/{namespace}/applications/{application}").
|
||||
Consumes(mimePatch...).
|
||||
To(handler.UpgradeApplication).
|
||||
Doc("Upgrade application").
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
|
||||
Reads(openpitrix2.UpgradeClusterRequest{}).
|
||||
Returns(http.StatusOK, api.StatusOK, errors.Error{}).
|
||||
Param(webservice.PathParameter("cluster", "the name of the cluster.").Required(true)).
|
||||
Param(webservice.PathParameter("namespace", "the name of the project").Required(true)).
|
||||
Param(webservice.PathParameter("application", "the id of the application").Required(true)))
|
||||
|
||||
webservice.Route(webservice.POST("/apps/{app}/versions").
|
||||
To(handler.CreateAppVersion).
|
||||
@@ -333,6 +361,13 @@ func AddToContainer(c *restful.Container, factory informers.InformerFactory, op
|
||||
Reads(openpitrix2.RepoActionRequest{}).
|
||||
Returns(http.StatusOK, api.StatusOK, errors.Error{}).
|
||||
Param(webservice.PathParameter("repo", "repo id")))
|
||||
webservice.Route(webservice.GET("/events").
|
||||
To(handler.ListEvents).
|
||||
Doc("Get events").
|
||||
Param(webservice.QueryParameter(params.ConditionsParam, "query conditions,connect multiple conditions with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").
|
||||
Required(false).
|
||||
DataFormat("key=%s,key~%s")).
|
||||
Returns(http.StatusOK, api.StatusOK, models.PageableResponse{}))
|
||||
webservice.Route(webservice.GET("/repos/{repo}/events").
|
||||
To(handler.ListRepoEvents).
|
||||
Doc("Get repository events").
|
||||
|
||||
Reference in New Issue
Block a user