register.go 10.7 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 29
	"kubesphere.io/kubesphere/pkg/apiserver/quotas"
	"kubesphere.io/kubesphere/pkg/apiserver/registries"
J
jeff 已提交
30
	"kubesphere.io/kubesphere/pkg/apiserver/resources"
H
hongming 已提交
31 32
	"kubesphere.io/kubesphere/pkg/apiserver/revisions"
	"kubesphere.io/kubesphere/pkg/apiserver/routers"
J
jeff 已提交
33
	"kubesphere.io/kubesphere/pkg/apiserver/runtime"
H
hongming 已提交
34 35 36
	"kubesphere.io/kubesphere/pkg/apiserver/workloadstatuses"
	"kubesphere.io/kubesphere/pkg/errors"
	"kubesphere.io/kubesphere/pkg/models"
H
hongming 已提交
37
	"kubesphere.io/kubesphere/pkg/models/applications"
R
runzexia 已提交
38
	gitmodel "kubesphere.io/kubesphere/pkg/models/git"
H
hongming 已提交
39
	"kubesphere.io/kubesphere/pkg/params"
H
hongming 已提交
40
	"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
J
jeff 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
)

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 已提交
56 57 58
	tags := []string{"Namespace resources"}

	webservice.Route(webservice.GET("/namespaces/{namespace}/{resources}").
H
hongming 已提交
59
		To(resources.ListResources).
H
hongming 已提交
60 61 62 63 64 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")).
		Writes(models.PageableResponse{}))

	tags = []string{"Cluster resources"}

	webservice.Route(webservice.GET("/{resources}").
H
hongming 已提交
76
		To(resources.ListResources).
H
hongming 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89
		Writes(models.PageableResponse{}).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("Cluster level resource query").
		Param(webservice.PathParameter("resources", "cluster level resource type"))).
		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").
			DefaultValue("limit=10,page=1"))

Z
zryfish 已提交
90 91 92
	tags = []string{"Applications"}

	webservice.Route(webservice.GET("/applications").
H
hongming 已提交
93
		To(resources.ListApplication).
Z
zryfish 已提交
94 95
		Writes(models.PageableResponse{}).
		Metadata(restfulspec.KeyOpenAPITags, tags).
H
hongming 已提交
96
		Doc("List applications in cluster").
Z
zryfish 已提交
97 98 99 100 101 102 103 104 105 106 107
		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 已提交
108
	webservice.Route(webservice.GET("/namespaces/{namespace}/applications").
H
hongming 已提交
109
		To(resources.ListNamespacedApplication).
H
hongming 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122
		Writes(models.PageableResponse{}).
		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 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	webservice.Route(webservice.GET("/namespaces/{namespace}/applications/{cluster_id}").
		To(resources.DescribeApplication).
		Writes(applications.Application{}).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("Describe application").
		Param(webservice.PathParameter("namespace", "namespace name")).
		Param(webservice.PathParameter("cluster_id", "openpitrix cluster id")))

	webservice.Route(webservice.POST("/namespaces/{namespace}/applications").
		To(resources.DeployApplication).
		Doc("Deploy application").
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Reads(openpitrix.CreateClusterRequest{}).
		Param(webservice.PathParameter("namespace", "namespace name")))

	webservice.Route(webservice.DELETE("/namespaces/{namespace}/applications/{cluster_id}").
		To(resources.DeleteApplication).
		Doc("Delete application").
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Param(webservice.PathParameter("namespace", "namespace name")))
H
hongming 已提交
143 144 145 146 147 148 149 150 151 152 153

	tags = []string{"User resources"}

	webservice.Route(webservice.GET("/users/{username}/kubectl").
		To(resources.GetKubectl).
		Doc("get user's kubectl pod").
		Param(webservice.PathParameter("username", "username")).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Writes(models.PodInfo{}))

	webservice.Route(webservice.GET("/users/{username}/kubeconfig").
H
hongming 已提交
154
		Produces("text/plain").
H
hongming 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 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
		To(resources.GetKubeconfig).
		Doc("get users' kubeconfig").
		Param(webservice.PathParameter("username", "username")).
		Metadata(restfulspec.KeyOpenAPITags, tags))

	tags = []string{"Components"}

	webservice.Route(webservice.GET("/components").
		To(components.GetComponents).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("").
		Writes(map[string]models.Component{}))
	webservice.Route(webservice.GET("/components/{component}").
		To(components.GetComponentStatus).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("").
		Param(webservice.PathParameter("component", "component name")).
		Writes(models.Component{}))
	webservice.Route(webservice.GET("/health").
		To(components.GetSystemHealthStatus).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("").
		Writes(map[string]int{}))

	tags = []string{"Quotas"}

	webservice.Route(webservice.GET("/quotas").
		To(quotas.GetClusterQuotas).
		Doc("get whole cluster's resource usage").
		Writes(models.ResourceQuota{}).
		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")).
		Writes(models.ResourceQuota{}).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		To(quotas.GetNamespaceQuotas))

	tags = []string{"Registries"}

	webservice.Route(webservice.POST("registries/verify").
		To(registries.RegistryVerify).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("docker registry verify").
		Writes(errors.Error{}))

R
runzexia 已提交
202
	tags = []string{"Git"}
R
runzexia 已提交
203
	webservice.Route(webservice.POST("/git/readverify").
R
runzexia 已提交
204 205 206 207 208 209 210
		To(
			git.GitReadVerify).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Doc("secret git read verify").
		Reads(gitmodel.AuthInfo{}).
		Writes(errors.Error{}),
	)
H
hongming 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	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")).
		Writes(appsv1.DaemonSet{}))
	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")).
		Writes(appsv1.ReplicaSet{}))
	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")).
		Writes(appsv1.StatefulSet{}))

	tags = []string{"Router"}

	webservice.Route(webservice.GET("/routers").
		To(routers.GetAllRouters).
H
hongming 已提交
242
		Doc("List all routers").
H
hongming 已提交
243 244 245 246 247
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Writes(corev1.Service{}))

	webservice.Route(webservice.GET("/namespaces/{namespace}/router").
		To(routers.GetRouter).
H
hongming 已提交
248
		Doc("List router of a specified project").
H
hongming 已提交
249 250 251 252 253
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Param(webservice.PathParameter("namespace", "name of the project")))

	webservice.Route(webservice.DELETE("/namespaces/{namespace}/router").
		To(routers.DeleteRouter).
H
hongming 已提交
254
		Doc("List router of a specified project").
H
hongming 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
		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"}

	webservice.Route(webservice.GET("/workloadstatuses").
		Doc("get abnormal workloads' count of whole cluster").
		Metadata(restfulspec.KeyOpenAPITags, tags).
		To(workloadstatuses.GetClusterResourceStatus))
	webservice.Route(webservice.GET("/namespaces/{namespace}/workloadstatuses").
		Doc("get abnormal workloads' count of specified namespace").
		Param(webservice.PathParameter("namespace", "the name of namespace")).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		To(workloadstatuses.GetNamespacesResourceStatus))
J
jeff 已提交
281 282 283 284 285

	c.Add(webservice)

	return nil
}