application.go 6.0 KB
Newer Older
Z
zryfish 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*

 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 resources

import (
H
hongming 已提交
21
	"fmt"
Z
zryfish 已提交
22
	"github.com/emicklei/go-restful"
H
hongming 已提交
23
	"github.com/golang/glog"
H
hongming 已提交
24 25
	"k8s.io/api/core/v1"
	"kubesphere.io/kubesphere/pkg/constants"
Z
zryfish 已提交
26
	"kubesphere.io/kubesphere/pkg/errors"
不羁 已提交
27
	"kubesphere.io/kubesphere/pkg/models"
Z
zryfish 已提交
28
	"kubesphere.io/kubesphere/pkg/models/applications"
H
hongming 已提交
29
	"kubesphere.io/kubesphere/pkg/models/resources"
Z
zryfish 已提交
30
	"kubesphere.io/kubesphere/pkg/params"
H
hongming 已提交
31
	"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
Z
zryfish 已提交
32 33 34
	"net/http"
)

H
hongming 已提交
35
func ListApplication(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
36
	limit, offset := params.ParsePaging(req.QueryParameter(params.PagingParam))
Z
zryfish 已提交
37 38
	clusterId := req.QueryParameter("cluster_id")
	runtimeId := req.QueryParameter("runtime_id")
H
hongming 已提交
39
	conditions, err := params.ParseConditions(req.QueryParameter(params.ConditionsParam))
Z
zryfish 已提交
40 41 42 43 44 45 46 47 48
	if err != nil {
		if err != nil {
			resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
			return
		}
	}
	if len(clusterId) > 0 {
		app, err := applications.GetApp(clusterId)
		if err != nil {
H
hongming 已提交
49
			glog.Errorln("get application error", err)
Z
zryfish 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
			resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
			return
		}
		resp.WriteEntity(app)
		return
	}

	result, err := applications.ListApplication(runtimeId, conditions, limit, offset)

	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		return
	}

	resp.WriteAsJson(result)

}
H
hongming 已提交
67

H
hongming 已提交
68
func ListNamespacedApplication(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
69 70
	limit, offset := params.ParsePaging(req.QueryParameter(params.PagingParam))
	namespaceName := req.PathParameter("namespace")
H
hongming 已提交
71
	clusterId := req.QueryParameter("cluster_id")
H
hongming 已提交
72 73
	conditions, err := params.ParseConditions(req.QueryParameter(params.ConditionsParam))
	if err != nil {
H
hongming 已提交
74 75
		resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
		return
H
hongming 已提交
76
	}
H
hongming 已提交
77 78 79
	if len(clusterId) > 0 {
		app, err := applications.GetApp(clusterId)
		if err != nil {
H
hongming 已提交
80
			glog.Errorln("get app failed", err)
H
hongming 已提交
81 82 83 84 85 86
			resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
			return
		}
		resp.WriteEntity(app)
		return
	}
H
hongming 已提交
87 88 89 90

	namespace, err := resources.GetResource("", resources.Namespaces, namespaceName)

	if err != nil {
H
hongming 已提交
91
		glog.Errorln("get namespace failed", err)
H
hongming 已提交
92 93 94 95 96 97 98 99 100 101 102
		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 == "" {
H
hongming 已提交
103
		glog.Errorln("runtime id not found")
不羁 已提交
104
		resp.WriteAsJson(models.PageableResponse{Items: []interface{}{}, TotalCount: 0})
H
hongming 已提交
105 106 107 108 109 110
		return
	}

	result, err := applications.ListApplication(runtimeId, conditions, limit, offset)

	if err != nil {
H
hongming 已提交
111
		glog.Errorln("list applications failed", err)
H
hongming 已提交
112 113 114 115 116 117
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		return
	}

	resp.WriteAsJson(result)
}
H
hongming 已提交
118 119

func DescribeApplication(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
120
	clusterId := req.PathParameter("application")
H
hongming 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	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) {
H
hongming 已提交
169
	clusterId := req.PathParameter("application")
H
hongming 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	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)
}