forked from kubesphere/kubesphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
229 lines (204 loc) · 11.3 KB
/
register.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
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"
"github.com/fearlesschenc/kubesphere/pkg/api"
"github.com/fearlesschenc/kubesphere/pkg/api/resource/v1alpha2"
"github.com/fearlesschenc/kubesphere/pkg/apiserver/runtime"
"github.com/fearlesschenc/kubesphere/pkg/constants"
"github.com/fearlesschenc/kubesphere/pkg/informers"
"github.com/fearlesschenc/kubesphere/pkg/models"
gitmodel "github.com/fearlesschenc/kubesphere/pkg/models/git"
registriesmodel "github.com/fearlesschenc/kubesphere/pkg/models/registries"
"github.com/fearlesschenc/kubesphere/pkg/server/errors"
"github.com/fearlesschenc/kubesphere/pkg/server/params"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"net/http"
)
const (
GroupName = "resources.kubesphere.io"
)
var GroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1alpha2"}
func AddToContainer(c *restful.Container, k8sClient kubernetes.Interface, factory informers.InformerFactory, masterURL string) error {
webservice := runtime.NewWebService(GroupVersion)
handler := newResourceHandler(k8sClient, factory, masterURL)
webservice.Route(webservice.GET("/namespaces/{namespace}/{resources}").
To(handler.handleListNamespaceResources).
Deprecate().
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Doc("Namespace level resource query").
Param(webservice.PathParameter("namespace", "the name of the project")).
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").
Required(false).
DataFormat("key=%s,key~%s")).
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")).
Param(webservice.QueryParameter(params.ReverseParam, "sort parameters, e.g. reverse=true")).
Param(webservice.QueryParameter(params.OrderByParam, "sort parameters, e.g. orderBy=createTime")).
Returns(http.StatusOK, api.StatusOK, models.PageableResponse{}))
webservice.Route(webservice.GET("/{resources}").
To(handler.handleListNamespaceResources).
Deprecate().
Returns(http.StatusOK, api.StatusOK, models.PageableResponse{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ClusterResourcesTag}).
Doc("Cluster level resources").
Param(webservice.PathParameter("resources", "cluster level resource type, e.g. nodes,workspaces,storageclasses,clusterrole.")).
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.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
Required(false).
DataFormat("limit=%d,page=%d").
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")))
webservice.Route(webservice.GET("/users/{user}/kubectl").
To(handler.GetKubectlPod).
Doc("get user's kubectl pod").
Param(webservice.PathParameter("user", "username")).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ToolboxTag}).
Returns(http.StatusOK, api.StatusOK, models.PodInfo{}))
webservice.Route(webservice.GET("/users/{user}/kubeconfig").
Produces("text/plain", restful.MIME_JSON).
To(handler.GetKubeconfig).
Doc("get users' kubeconfig").
Param(webservice.PathParameter("user", "username")).
Returns(http.StatusOK, api.StatusOK, "").
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ToolboxTag}))
webservice.Route(webservice.GET("/components").
To(handler.handleGetComponents).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}).
Doc("List the system components.").
Returns(http.StatusOK, api.StatusOK, []v1alpha2.ComponentStatus{}))
webservice.Route(webservice.GET("/components/{component}").
To(handler.handleGetComponentStatus).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}).
Doc("Describe the specified system component.").
Param(webservice.PathParameter("component", "component name")).
Returns(http.StatusOK, api.StatusOK, v1alpha2.ComponentStatus{}))
webservice.Route(webservice.GET("/componenthealth").
To(handler.handleGetSystemHealthStatus).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}).
Doc("Get the health status of system components.").
Returns(http.StatusOK, api.StatusOK, v1alpha2.HealthStatus{}))
webservice.Route(webservice.GET("/quotas").
To(handler.handleGetClusterQuotas).
Doc("get whole cluster's resource usage").
Returns(http.StatusOK, api.StatusOK, api.ResourceQuota{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ClusterResourcesTag}))
webservice.Route(webservice.GET("/namespaces/{namespace}/quotas").
Doc("get specified namespace's resource quota and usage").
Param(webservice.PathParameter("namespace", "the name of the project")).
Returns(http.StatusOK, api.StatusOK, api.ResourceQuota{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
To(handler.handleGetNamespaceQuotas))
webservice.Route(webservice.POST("registry/verify").
To(handler.handleVerifyRegistryCredential).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.RegistryTag}).
Doc("verify if a user has access to the docker registry").
Reads(api.RegistryCredential{}).
Returns(http.StatusOK, api.StatusOK, errors.Error{}))
webservice.Route(webservice.GET("/registry/blob").
To(handler.handleGetRegistryEntry).
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, api.StatusOK, registriesmodel.ImageDetails{}),
)
webservice.Route(webservice.POST("git/verify").
To(handler.handleVerifyGitCredential).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.GitTag}).
Doc("Verify if the kubernetes secret has read access to the git repository").
Reads(gitmodel.AuthInfo{}).
Returns(http.StatusOK, api.StatusOK, errors.Error{}),
)
webservice.Route(webservice.GET("/namespaces/{namespace}/daemonsets/{daemonset}/revisions/{revision}").
To(handler.handleGetDaemonSetRevision).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
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")).
Returns(http.StatusOK, api.StatusOK, appsv1.DaemonSet{}))
webservice.Route(webservice.GET("/namespaces/{namespace}/deployments/{deployment}/revisions/{revision}").
To(handler.handleGetDeploymentRevision).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
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")).
Returns(http.StatusOK, api.StatusOK, appsv1.ReplicaSet{}))
webservice.Route(webservice.GET("/namespaces/{namespace}/statefulsets/{statefulset}/revisions/{revision}").
To(handler.handleGetStatefulSetRevision).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
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")).
Returns(http.StatusOK, api.StatusOK, appsv1.StatefulSet{}))
webservice.Route(webservice.GET("/namespaces/{namespace}/router").
To(handler.handleGetRouter).
Doc("List router of a specified project").
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Returns(http.StatusOK, api.StatusOK, corev1.Service{}).
Param(webservice.PathParameter("namespace", "the name of the project")))
webservice.Route(webservice.DELETE("/namespaces/{namespace}/router").
To(handler.handleDeleteRouter).
Doc("List router of a specified project").
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Returns(http.StatusOK, api.StatusOK, corev1.Service{}).
Param(webservice.PathParameter("namespace", "the name of the project")))
webservice.Route(webservice.POST("/namespaces/{namespace}/router").
To(handler.handleCreateRouter).
Doc("Create a router for a specified project").
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Returns(http.StatusOK, api.StatusOK, corev1.Service{}).
Param(webservice.PathParameter("namespace", "the name of the project")))
webservice.Route(webservice.PUT("/namespaces/{namespace}/router").
To(handler.handleUpdateRouter).
Doc("Update a router for a specified project").
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Returns(http.StatusOK, api.StatusOK, corev1.Service{}).
Param(webservice.PathParameter("namespace", "the name of the project")))
webservice.Route(webservice.GET("/abnormalworkloads").
Doc("get abnormal workloads' count of whole cluster").
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ClusterResourcesTag}).
Returns(http.StatusOK, api.StatusOK, api.Workloads{}).
To(handler.handleGetNamespacedAbnormalWorkloads))
webservice.Route(webservice.GET("/namespaces/{namespace}/abnormalworkloads").
Doc("get abnormal workloads' count of specified namespace").
Param(webservice.PathParameter("namespace", "the name of the project")).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Returns(http.StatusOK, api.StatusOK, api.Workloads{}).
To(handler.handleGetNamespacedAbnormalWorkloads))
c.Add(webservice)
return nil
}