register.go 12.3 KB
Newer Older
J
jeff 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*

 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 v1alpha2

import (
	"github.com/emicklei/go-restful"
	"github.com/emicklei/go-restful-openapi"
H
hongming 已提交
23 24
	appsv1 "k8s.io/api/apps/v1"
	corev1 "k8s.io/api/core/v1"
J
jeff 已提交
25
	"k8s.io/apimachinery/pkg/runtime/schema"
H
hongming 已提交
26
	"kubesphere.io/kubesphere/pkg/apiserver/components"
R
runzexia 已提交
27
	"kubesphere.io/kubesphere/pkg/apiserver/git"
H
hongming 已提交
28
	"kubesphere.io/kubesphere/pkg/apiserver/operations"
H
hongming 已提交
29 30
	"kubesphere.io/kubesphere/pkg/apiserver/quotas"
	"kubesphere.io/kubesphere/pkg/apiserver/registries"
J
jeff 已提交
31
	"kubesphere.io/kubesphere/pkg/apiserver/resources"
H
hongming 已提交
32 33
	"kubesphere.io/kubesphere/pkg/apiserver/revisions"
	"kubesphere.io/kubesphere/pkg/apiserver/routers"
J
jeff 已提交
34
	"kubesphere.io/kubesphere/pkg/apiserver/runtime"
H
hongming 已提交
35 36 37
	"kubesphere.io/kubesphere/pkg/apiserver/workloadstatuses"
	"kubesphere.io/kubesphere/pkg/errors"
	"kubesphere.io/kubesphere/pkg/models"
H
hongming 已提交
38
	"kubesphere.io/kubesphere/pkg/models/applications"
R
runzexia 已提交
39
	gitmodel "kubesphere.io/kubesphere/pkg/models/git"
H
hongming 已提交
40 41
	registriesmodel "kubesphere.io/kubesphere/pkg/models/registries"
	"kubesphere.io/kubesphere/pkg/models/status"
H
hongming 已提交
42
	"kubesphere.io/kubesphere/pkg/params"
H
hongming 已提交
43
	"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
H
hongming 已提交
44
	"net/http"
J
jeff 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
)

const GroupName = "resources.kubesphere.io"

var GroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1alpha2"}

var (
	WebServiceBuilder = runtime.NewContainerBuilder(addWebService)
	AddToContainer    = WebServiceBuilder.AddToContainer
)

func addWebService(c *restful.Container) error {

	webservice := runtime.NewWebService(GroupVersion)

H
hongming 已提交
60
	tags := []string{"Namespace resources"}
H
hongming 已提交
61
	ok := "ok"
H
hongming 已提交
62 63

	webservice.Route(webservice.GET("/namespaces/{namespace}/{resources}").
H
hongming 已提交
64
		To(resources.ListNamespacedResources).
H
hongming 已提交
65 66 67 68 69 70 71 72 73 74 75
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("Namespace level resource query").
		Param(webservice.PathParameter("namespace", "which namespace")).
		Param(webservice.PathParameter("resources", "namespace level resource type")).
		Param(webservice.QueryParameter(params.ConditionsParam, "query conditions").
			Required(false).
			DataFormat("key=%s,key~%s")).
		Param(webservice.QueryParameter(params.PagingParam, "page").
			Required(false).
			DataFormat("limit=%d,page=%d").
			DefaultValue("limit=10,page=1")).
H
hongming 已提交
76
		Returns(http.StatusOK, ok, models.PageableResponse{}))
H
hongming 已提交
77

H
hongming 已提交
78 79 80 81 82 83 84 85 86
	webservice.Route(webservice.POST("/namespaces/{namespace}/jobs/{job}").
		To(operations.RerunJob).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("Rerun job whether the job is complete or not").
		Param(webservice.PathParameter("job", "job name")).
		Param(webservice.PathParameter("namespace", "job's namespace")).
		Param(webservice.QueryParameter("a", "action")).
		Returns(http.StatusOK, ok, errors.Error{}))

H
hongming 已提交
87 88 89
	tags = []string{"Cluster resources"}

	webservice.Route(webservice.GET("/{resources}").
H
hongming 已提交
90
		To(resources.ListResources).
H
hongming 已提交
91
		Returns(http.StatusOK, ok, models.PageableResponse{}).
H
hongming 已提交
92 93
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("Cluster level resource query").
H
hongming 已提交
94
		Param(webservice.PathParameter("resources", "cluster level resource type")).
H
hongming 已提交
95 96 97 98 99 100 101
		Param(webservice.QueryParameter(params.ConditionsParam, "query conditions").
			Required(false).
			DataFormat("key=value,key~value").
			DefaultValue("")).
		Param(webservice.QueryParameter(params.PagingParam, "page").
			Required(false).
			DataFormat("limit=%d,page=%d").
H
hongming 已提交
102
			DefaultValue("limit=10,page=1")))
H
hongming 已提交
103

H
hongming 已提交
104 105 106 107 108 109 110
	webservice.Route(webservice.POST("/nodes/{node}/drainage").
		To(operations.DrainNode).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("Drain node").
		Param(webservice.PathParameter("node", "node name")).
		Returns(http.StatusOK, ok, errors.Error{}))

Z
zryfish 已提交
111 112 113
	tags = []string{"Applications"}

	webservice.Route(webservice.GET("/applications").
H
hongming 已提交
114
		To(resources.ListApplication).
H
hongming 已提交
115
		Returns(http.StatusOK, ok, models.PageableResponse{}).
Z
zryfish 已提交
116
		Metadata(restfulspec.KeyOpenAPITags, tags).
H
hongming 已提交
117
		Doc("List applications in cluster").
Z
zryfish 已提交
118 119 120 121 122 123 124 125 126 127 128
		Param(webservice.QueryParameter(params.ConditionsParam, "query conditions").
			Required(false).
			DataFormat("key=value,key~value").
			DefaultValue("")).
		Param(webservice.QueryParameter("cluster_id", "cluster id")).
		Param(webservice.QueryParameter("runtime_id", "runtime id")).
		Param(webservice.QueryParameter(params.PagingParam, "page").
			Required(false).
			DataFormat("limit=%d,page=%d").
			DefaultValue("limit=10,page=1")))

H
hongming 已提交
129
	webservice.Route(webservice.GET("/namespaces/{namespace}/applications").
H
hongming 已提交
130
		To(resources.ListNamespacedApplication).
H
hongming 已提交
131
		Returns(http.StatusOK, ok, models.PageableResponse{}).
H
hongming 已提交
132 133 134 135 136 137 138 139 140 141 142 143
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("List applications").
		Param(webservice.QueryParameter(params.ConditionsParam, "query conditions").
			Required(false).
			DataFormat("key=value,key~value").
			DefaultValue("")).
		Param(webservice.PathParameter("namespace", "namespace")).
		Param(webservice.QueryParameter(params.PagingParam, "page").
			Required(false).
			DataFormat("limit=%d,page=%d").
			DefaultValue("limit=10,page=1")))

H
hongming 已提交
144
	webservice.Route(webservice.GET("/namespaces/{namespace}/applications/{application}").
H
hongming 已提交
145
		To(resources.DescribeApplication).
H
hongming 已提交
146
		Returns(http.StatusOK, ok, applications.Application{}).
H
hongming 已提交
147 148 149
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("Describe application").
		Param(webservice.PathParameter("namespace", "namespace name")).
H
hongming 已提交
150
		Param(webservice.PathParameter("application", "application id")))
H
hongming 已提交
151 152 153 154 155 156

	webservice.Route(webservice.POST("/namespaces/{namespace}/applications").
		To(resources.DeployApplication).
		Doc("Deploy application").
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Reads(openpitrix.CreateClusterRequest{}).
H
hongming 已提交
157
		Returns(http.StatusOK, ok, errors.Error{}).
H
hongming 已提交
158 159
		Param(webservice.PathParameter("namespace", "namespace name")))

H
hongming 已提交
160
	webservice.Route(webservice.DELETE("/namespaces/{namespace}/applications/{application}").
H
hongming 已提交
161 162 163
		To(resources.DeleteApplication).
		Doc("Delete application").
		Metadata(restfulspec.KeyOpenAPITags, tags).
H
hongming 已提交
164 165
		Returns(http.StatusOK, ok, errors.Error{}).
		Param(webservice.PathParameter("namespace", "namespace name")).
H
hongming 已提交
166
		Param(webservice.PathParameter("application", "application id")))
H
hongming 已提交
167 168 169

	tags = []string{"User resources"}

H
hongming 已提交
170
	webservice.Route(webservice.GET("/users/{user}/kubectl").
H
hongming 已提交
171 172
		To(resources.GetKubectl).
		Doc("get user's kubectl pod").
H
hongming 已提交
173
		Param(webservice.PathParameter("user", "username")).
H
hongming 已提交
174
		Metadata(restfulspec.KeyOpenAPITags, tags).
H
hongming 已提交
175
		Returns(http.StatusOK, ok, models.PodInfo{}))
H
hongming 已提交
176

H
hongming 已提交
177
	webservice.Route(webservice.GET("/users/{user}/kubeconfig").
H
hongming 已提交
178
		Produces("text/plain").
H
hongming 已提交
179 180
		To(resources.GetKubeconfig).
		Doc("get users' kubeconfig").
H
hongming 已提交
181
		Param(webservice.PathParameter("user", "username")).
H
hongming 已提交
182
		Returns(http.StatusOK, ok, "").
H
hongming 已提交
183 184 185 186 187 188 189
		Metadata(restfulspec.KeyOpenAPITags, tags))

	tags = []string{"Components"}

	webservice.Route(webservice.GET("/components").
		To(components.GetComponents).
		Metadata(restfulspec.KeyOpenAPITags, tags).
R
Ray Zhou 已提交
190
		Doc("List the system components.").
H
hongming 已提交
191
		Returns(http.StatusOK, ok, map[string]models.Component{}))
H
hongming 已提交
192 193 194
	webservice.Route(webservice.GET("/components/{component}").
		To(components.GetComponentStatus).
		Metadata(restfulspec.KeyOpenAPITags, tags).
R
Ray Zhou 已提交
195
		Doc("Describe the specified system component.").
H
hongming 已提交
196
		Param(webservice.PathParameter("component", "component name")).
H
hongming 已提交
197
		Returns(http.StatusOK, ok, models.Component{}))
H
hongming 已提交
198
	webservice.Route(webservice.GET("/componenthealth").
H
hongming 已提交
199 200
		To(components.GetSystemHealthStatus).
		Metadata(restfulspec.KeyOpenAPITags, tags).
R
Ray Zhou 已提交
201
		Doc("Get the health status of system components.").
H
hongming 已提交
202
		Returns(http.StatusOK, ok, map[string]int{}))
H
hongming 已提交
203 204 205 206 207

	tags = []string{"Quotas"}

	webservice.Route(webservice.GET("/quotas").
		To(quotas.GetClusterQuotas).
J
Jeff 已提交
208
		Deprecate().
H
hongming 已提交
209
		Doc("get whole cluster's resource usage").
H
hongming 已提交
210
		Returns(http.StatusOK, ok, models.ResourceQuota{}).
H
hongming 已提交
211 212 213 214 215
		Metadata(restfulspec.KeyOpenAPITags, tags))

	webservice.Route(webservice.GET("/namespaces/{namespace}/quotas").
		Doc("get specified namespace's resource quota and usage").
		Param(webservice.PathParameter("namespace", "namespace's name")).
H
hongming 已提交
216
		Returns(http.StatusOK, ok, models.ResourceQuota{}).
H
hongming 已提交
217 218 219 220 221
		Metadata(restfulspec.KeyOpenAPITags, tags).
		To(quotas.GetNamespaceQuotas))

	tags = []string{"Registries"}

H
hongming 已提交
222
	webservice.Route(webservice.POST("registry/verify").
H
hongming 已提交
223 224 225
		To(registries.RegistryVerify).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("docker registry verify").
H
hongming 已提交
226 227
		Reads(registriesmodel.AuthInfo{}).
		Returns(http.StatusOK, ok, errors.Error{}))
H
hongming 已提交
228

R
runzexia 已提交
229
	tags = []string{"Git"}
R
runzexia 已提交
230
	webservice.Route(webservice.POST("/git/readverify").
R
runzexia 已提交
231 232 233 234 235
		To(
			git.GitReadVerify).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("secret git read verify").
		Reads(gitmodel.AuthInfo{}).
H
hongming 已提交
236
		Returns(http.StatusOK, ok, errors.Error{}),
R
runzexia 已提交
237
	)
H
hongming 已提交
238 239 240 241 242 243 244 245 246
	tags = []string{"Revision"}

	webservice.Route(webservice.GET("/namespaces/{namespace}/daemonsets/{daemonset}/revisions/{revision}").
		To(revisions.GetDaemonSetRevision).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("Handle daemonset operation").
		Param(webservice.PathParameter("daemonset", "daemonset's name")).
		Param(webservice.PathParameter("namespace", "daemonset's namespace")).
		Param(webservice.PathParameter("revision", "daemonset's revision")).
H
hongming 已提交
247
		Returns(http.StatusOK, ok, appsv1.DaemonSet{}))
H
hongming 已提交
248 249 250 251 252 253 254
	webservice.Route(webservice.GET("/namespaces/{namespace}/deployments/{deployment}/revisions/{revision}").
		To(revisions.GetDeployRevision).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("Handle deployment operation").
		Param(webservice.PathParameter("deployment", "deployment's name")).
		Param(webservice.PathParameter("namespace", "deployment's namespace")).
		Param(webservice.PathParameter("revision", "deployment's revision")).
H
hongming 已提交
255
		Returns(http.StatusOK, ok, appsv1.ReplicaSet{}))
H
hongming 已提交
256 257 258 259 260 261 262
	webservice.Route(webservice.GET("/namespaces/{namespace}/statefulsets/{statefulset}/revisions/{revision}").
		To(revisions.GetStatefulSetRevision).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("Handle statefulset operation").
		Param(webservice.PathParameter("statefulset", "statefulset's name")).
		Param(webservice.PathParameter("namespace", "statefulset's namespace")).
		Param(webservice.PathParameter("revision", "statefulset's revision")).
H
hongming 已提交
263
		Returns(http.StatusOK, ok, appsv1.StatefulSet{}))
H
hongming 已提交
264 265 266 267 268

	tags = []string{"Router"}

	webservice.Route(webservice.GET("/routers").
		To(routers.GetAllRouters).
H
hongming 已提交
269
		Doc("List all routers").
H
hongming 已提交
270
		Metadata(restfulspec.KeyOpenAPITags, tags).
H
hongming 已提交
271
		Returns(http.StatusOK, ok, corev1.Service{}))
H
hongming 已提交
272 273 274

	webservice.Route(webservice.GET("/namespaces/{namespace}/router").
		To(routers.GetRouter).
H
hongming 已提交
275
		Doc("List router of a specified project").
H
hongming 已提交
276 277 278 279 280
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Param(webservice.PathParameter("namespace", "name of the project")))

	webservice.Route(webservice.DELETE("/namespaces/{namespace}/router").
		To(routers.DeleteRouter).
H
hongming 已提交
281
		Doc("List router of a specified project").
H
hongming 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Param(webservice.PathParameter("namespace", "name of the project")))

	webservice.Route(webservice.POST("/namespaces/{namespace}/router").
		To(routers.CreateRouter).
		Doc("Create a router for a specified project").
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Param(webservice.PathParameter("namespace", "name of the project")))

	webservice.Route(webservice.PUT("/namespaces/{namespace}/router").
		To(routers.UpdateRouter).
		Doc("Update a router for a specified project").
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Param(webservice.PathParameter("namespace", "name of the project")))

	tags = []string{"WorkloadStatus"}

H
hongming 已提交
299
	webservice.Route(webservice.GET("/abnormalworkloads").
H
hongming 已提交
300 301
		Doc("get abnormal workloads' count of whole cluster").
		Metadata(restfulspec.KeyOpenAPITags, tags).
H
hongming 已提交
302
		Returns(http.StatusOK, ok, status.WorkLoadStatus{}).
H
hongming 已提交
303 304
		To(workloadstatuses.GetClusterAbnormalWorkloads))
	webservice.Route(webservice.GET("/namespaces/{namespace}/abnormalworkloads").
H
hongming 已提交
305 306 307
		Doc("get abnormal workloads' count of specified namespace").
		Param(webservice.PathParameter("namespace", "the name of namespace")).
		Metadata(restfulspec.KeyOpenAPITags, tags).
H
hongming 已提交
308
		Returns(http.StatusOK, ok, status.WorkLoadStatus{}).
H
hongming 已提交
309
		To(workloadstatuses.GetNamespacedAbnormalWorkloads))
J
jeff 已提交
310 311 312 313 314

	c.Add(webservice)

	return nil
}