register.go 12.5 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
	"kubesphere.io/kubesphere/pkg/apiserver/workloadstatuses"
H
hongming 已提交
36
	"kubesphere.io/kubesphere/pkg/constants"
H
hongming 已提交
37
	"kubesphere.io/kubesphere/pkg/models"
R
runzexia 已提交
38
	gitmodel "kubesphere.io/kubesphere/pkg/models/git"
H
hongming 已提交
39 40
	registriesmodel "kubesphere.io/kubesphere/pkg/models/registries"
	"kubesphere.io/kubesphere/pkg/models/status"
J
Jeff 已提交
41 42
	"kubesphere.io/kubesphere/pkg/server/errors"
	"kubesphere.io/kubesphere/pkg/server/params"
H
hongming 已提交
43
	"net/http"
J
jeff 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
)

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 已提交
59
	ok := "ok"
H
hongming 已提交
60 61

	webservice.Route(webservice.GET("/namespaces/{namespace}/{resources}").
H
hongming 已提交
62
		To(resources.ListNamespacedResources).
H
hongming 已提交
63
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
H
hongming 已提交
64
		Doc("Namespace level resource query").
R
Ray Zhou 已提交
65
		Param(webservice.PathParameter("namespace", "the name of the project")).
H
hongming 已提交
66 67
		Param(webservice.PathParameter("resources", "namespace level resource type, e.g. pods,jobs,configmaps,services.")).
		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").
H
hongming 已提交
68 69
			Required(false).
			DataFormat("key=%s,key~%s")).
H
hongming 已提交
70
		Param(webservice.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
H
hongming 已提交
71 72 73
			Required(false).
			DataFormat("limit=%d,page=%d").
			DefaultValue("limit=10,page=1")).
H
hongming 已提交
74 75
		Param(webservice.QueryParameter(params.ReverseParam, "sort parameters, e.g. reverse=true")).
		Param(webservice.QueryParameter(params.OrderByParam, "sort parameters, e.g. orderBy=createTime")).
H
hongming 已提交
76
		Returns(http.StatusOK, ok, models.PageableResponse{}))
H
hongming 已提交
77

H
hongming 已提交
78 79
	webservice.Route(webservice.POST("/namespaces/{namespace}/jobs/{job}").
		To(operations.RerunJob).
H
hongming 已提交
80
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
H
hongming 已提交
81 82
		Doc("Rerun job whether the job is complete or not").
		Param(webservice.PathParameter("job", "job name")).
R
rayzhou2017 已提交
83
		Param(webservice.PathParameter("namespace", "the name of the namespace where the job runs in")).
H
hongming 已提交
84
		Param(webservice.QueryParameter("action", "action must be \"rerun\"")).
R
runzexia 已提交
85
		Param(webservice.QueryParameter("resourceVersion", "version of job, rerun when the version matches")).
H
hongming 已提交
86 87
		Returns(http.StatusOK, ok, errors.Error{}))

H
hongming 已提交
88
	webservice.Route(webservice.GET("/{resources}").
H
hongming 已提交
89
		To(resources.ListResources).
H
hongming 已提交
90
		Returns(http.StatusOK, ok, models.PageableResponse{}).
H
hongming 已提交
91
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.ClusterResourcesTag}).
H
hongming 已提交
92
		Doc("Cluster level resource query").
H
hongming 已提交
93 94
		Param(webservice.PathParameter("resources", "cluster level resource type, e.g. nodes,workspaces,storageclasses,clusterroles.")).
		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").
H
hongming 已提交
95 96 97
			Required(false).
			DataFormat("key=value,key~value").
			DefaultValue("")).
H
hongming 已提交
98
		Param(webservice.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
H
hongming 已提交
99 100
			Required(false).
			DataFormat("limit=%d,page=%d").
H
hongming 已提交
101 102 103
			DefaultValue("limit=10,page=1")).
		Param(webservice.QueryParameter(params.ReverseParam, "sort parameters, e.g. reverse=true")).
		Param(webservice.QueryParameter(params.OrderByParam, "sort parameters, e.g. orderBy=createTime")))
H
hongming 已提交
104

H
hongming 已提交
105 106
	webservice.Route(webservice.POST("/nodes/{node}/drainage").
		To(operations.DrainNode).
H
hongming 已提交
107
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.ClusterResourcesTag}).
108
		Doc("remove a node from service, safely evict all of your pods from a node and you can power down the node. More info: https://kubernetes.io/docs/tasks/administer-cluster/safely-drain-node/").
H
hongming 已提交
109 110 111 112
		Param(webservice.PathParameter("node", "node name")).
		Returns(http.StatusOK, ok, errors.Error{}))

	webservice.Route(webservice.GET("/users/{user}/kubectl").
H
hongming 已提交
113 114
		To(resources.GetKubectl).
		Doc("get user's kubectl pod").
H
hongming 已提交
115
		Param(webservice.PathParameter("user", "username")).
H
hongming 已提交
116
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.UserResourcesTag}).
H
hongming 已提交
117
		Returns(http.StatusOK, ok, models.PodInfo{}))
H
hongming 已提交
118

H
hongming 已提交
119
	webservice.Route(webservice.GET("/users/{user}/kubeconfig").
H
hongming 已提交
120
		Produces("text/plain", restful.MIME_JSON).
H
hongming 已提交
121 122
		To(resources.GetKubeconfig).
		Doc("get users' kubeconfig").
H
hongming 已提交
123
		Param(webservice.PathParameter("user", "username")).
H
hongming 已提交
124
		Returns(http.StatusOK, ok, "").
H
hongming 已提交
125
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.UserResourcesTag}))
H
hongming 已提交
126 127 128

	webservice.Route(webservice.GET("/components").
		To(components.GetComponents).
H
hongming 已提交
129
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}).
R
Ray Zhou 已提交
130
		Doc("List the system components.").
H
hongming 已提交
131
		Returns(http.StatusOK, ok, []models.ComponentStatus{}))
H
hongming 已提交
132 133
	webservice.Route(webservice.GET("/components/{component}").
		To(components.GetComponentStatus).
H
hongming 已提交
134
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}).
R
Ray Zhou 已提交
135
		Doc("Describe the specified system component.").
H
hongming 已提交
136
		Param(webservice.PathParameter("component", "component name")).
H
hongming 已提交
137
		Returns(http.StatusOK, ok, models.ComponentStatus{}))
H
hongming 已提交
138
	webservice.Route(webservice.GET("/componenthealth").
H
hongming 已提交
139
		To(components.GetSystemHealthStatus).
H
hongming 已提交
140
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}).
R
Ray Zhou 已提交
141
		Doc("Get the health status of system components.").
H
hongming 已提交
142
		Returns(http.StatusOK, ok, models.HealthStatus{}))
H
hongming 已提交
143 144 145 146

	webservice.Route(webservice.GET("/quotas").
		To(quotas.GetClusterQuotas).
		Doc("get whole cluster's resource usage").
H
hongming 已提交
147
		Returns(http.StatusOK, ok, models.ResourceQuota{}).
H
hongming 已提交
148
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.ClusterResourcesTag}))
H
hongming 已提交
149 150 151

	webservice.Route(webservice.GET("/namespaces/{namespace}/quotas").
		Doc("get specified namespace's resource quota and usage").
R
Ray Zhou 已提交
152
		Param(webservice.PathParameter("namespace", "the name of the project")).
H
hongming 已提交
153
		Returns(http.StatusOK, ok, models.ResourceQuota{}).
H
hongming 已提交
154
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
H
hongming 已提交
155 156
		To(quotas.GetNamespaceQuotas))

H
hongming 已提交
157
	webservice.Route(webservice.POST("registry/verify").
H
hongming 已提交
158
		To(registries.RegistryVerify).
H
hongming 已提交
159
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.VerificationTag}).
R
Ray Zhou 已提交
160
		Doc("verify if a user has access to the docker registry").
H
hongming 已提交
161 162
		Reads(registriesmodel.AuthInfo{}).
		Returns(http.StatusOK, ok, errors.Error{}))
H
hongming 已提交
163

S
soulseen 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	webservice.Route(webservice.GET("/registry/blob").
		To(registries.RegistryImageBlob).
		Param(webservice.QueryParameter("image", "query image, condition for filtering.").
			Required(true).
			DataFormat("image=%s")).
		Param(webservice.QueryParameter("namespace", "namespace which secret in.").
			Required(false).
			DataFormat("namespace=%s")).
		Param(webservice.QueryParameter("secret", "secret name").
			Required(false).
			DataFormat("secret=%s")).
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.RegistryTag}).
		Doc("Retrieve the blob from the registry identified").
		Writes(registriesmodel.ImageDetails{}).
		Returns(http.StatusOK, ok, registriesmodel.ImageDetails{}),
	)

R
runzexia 已提交
181
	webservice.Route(webservice.POST("git/verify").
H
hongming 已提交
182 183
		To(git.GitReadVerify).
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.VerificationTag}).
R
Ray Zhou 已提交
184
		Doc("Verify if the kubernetes secret has read access to the git repository").
R
runzexia 已提交
185 186 187
		Reads(gitmodel.AuthInfo{}).
		Returns(http.StatusOK, ok, errors.Error{}),
	)
H
hongming 已提交
188 189 190

	webservice.Route(webservice.GET("/namespaces/{namespace}/daemonsets/{daemonset}/revisions/{revision}").
		To(revisions.GetDaemonSetRevision).
H
hongming 已提交
191
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
R
Ray Zhou 已提交
192 193 194 195
		Doc("Get the specified daemonset revision").
		Param(webservice.PathParameter("daemonset", "the name of the daemonset")).
		Param(webservice.PathParameter("namespace", "the namespace of the daemonset")).
		Param(webservice.PathParameter("revision", "the revision of the daemonset")).
H
hongming 已提交
196
		Returns(http.StatusOK, ok, appsv1.DaemonSet{}))
H
hongming 已提交
197 198
	webservice.Route(webservice.GET("/namespaces/{namespace}/deployments/{deployment}/revisions/{revision}").
		To(revisions.GetDeployRevision).
H
hongming 已提交
199
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
R
Ray Zhou 已提交
200 201 202 203
		Doc("Get the specified deployment revision").
		Param(webservice.PathParameter("deployment", "the name of deployment")).
		Param(webservice.PathParameter("namespace", "the namespace of the deployment")).
		Param(webservice.PathParameter("revision", "the revision of the deployment")).
H
hongming 已提交
204
		Returns(http.StatusOK, ok, appsv1.ReplicaSet{}))
H
hongming 已提交
205 206
	webservice.Route(webservice.GET("/namespaces/{namespace}/statefulsets/{statefulset}/revisions/{revision}").
		To(revisions.GetStatefulSetRevision).
H
hongming 已提交
207
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
R
Ray Zhou 已提交
208 209 210 211
		Doc("Get the specified statefulset revision").
		Param(webservice.PathParameter("statefulset", "the name of the statefulset")).
		Param(webservice.PathParameter("namespace", "the namespace of the statefulset")).
		Param(webservice.PathParameter("revision", "the revision of the statefulset")).
H
hongming 已提交
212
		Returns(http.StatusOK, ok, appsv1.StatefulSet{}))
H
hongming 已提交
213 214 215

	webservice.Route(webservice.GET("/routers").
		To(routers.GetAllRouters).
J
Jeff 已提交
216
		Doc("List all routers of all projects").
H
hongming 已提交
217
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.ClusterResourcesTag}).
J
Jeff 已提交
218
		Returns(http.StatusOK, ok, corev1.ServiceList{}))
H
hongming 已提交
219 220 221

	webservice.Route(webservice.GET("/namespaces/{namespace}/router").
		To(routers.GetRouter).
H
hongming 已提交
222
		Doc("List router of a specified project").
H
hongming 已提交
223
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
J
Jeff 已提交
224
		Returns(http.StatusOK, ok, corev1.Service{}).
R
Ray Zhou 已提交
225
		Param(webservice.PathParameter("namespace", "the name of the project")))
H
hongming 已提交
226 227 228

	webservice.Route(webservice.DELETE("/namespaces/{namespace}/router").
		To(routers.DeleteRouter).
H
hongming 已提交
229
		Doc("List router of a specified project").
H
hongming 已提交
230
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
J
Jeff 已提交
231
		Returns(http.StatusOK, ok, corev1.Service{}).
R
Ray Zhou 已提交
232
		Param(webservice.PathParameter("namespace", "the name of the project")))
H
hongming 已提交
233 234 235 236

	webservice.Route(webservice.POST("/namespaces/{namespace}/router").
		To(routers.CreateRouter).
		Doc("Create a router for a specified project").
H
hongming 已提交
237
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
J
Jeff 已提交
238
		Returns(http.StatusOK, ok, corev1.Service{}).
R
Ray Zhou 已提交
239
		Param(webservice.PathParameter("namespace", "the name of the project")))
H
hongming 已提交
240 241 242 243

	webservice.Route(webservice.PUT("/namespaces/{namespace}/router").
		To(routers.UpdateRouter).
		Doc("Update a router for a specified project").
H
hongming 已提交
244
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
J
Jeff 已提交
245
		Returns(http.StatusOK, ok, corev1.Service{}).
R
Ray Zhou 已提交
246
		Param(webservice.PathParameter("namespace", "the name of the project")))
H
hongming 已提交
247

H
hongming 已提交
248
	webservice.Route(webservice.GET("/abnormalworkloads").
H
hongming 已提交
249
		Doc("get abnormal workloads' count of whole cluster").
H
hongming 已提交
250
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.ClusterResourcesTag}).
H
hongming 已提交
251
		Returns(http.StatusOK, ok, status.WorkLoadStatus{}).
H
hongming 已提交
252 253
		To(workloadstatuses.GetClusterAbnormalWorkloads))
	webservice.Route(webservice.GET("/namespaces/{namespace}/abnormalworkloads").
H
hongming 已提交
254
		Doc("get abnormal workloads' count of specified namespace").
R
Ray Zhou 已提交
255
		Param(webservice.PathParameter("namespace", "the name of the project")).
H
hongming 已提交
256
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
H
hongming 已提交
257
		Returns(http.StatusOK, ok, status.WorkLoadStatus{}).
H
hongming 已提交
258
		To(workloadstatuses.GetNamespacedAbnormalWorkloads))
J
jeff 已提交
259 260 261 262 263

	c.Add(webservice)

	return nil
}