-
Notifications
You must be signed in to change notification settings - Fork 38
/
cluster.go
384 lines (348 loc) · 17.3 KB
/
cluster.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright The Karpor 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 cluster
import (
"io"
"net/http"
"strconv"
"strings"
"github.com/KusionStack/karpor/pkg/core/handler"
"github.com/KusionStack/karpor/pkg/core/manager/cluster"
"github.com/KusionStack/karpor/pkg/infra/multicluster"
"github.com/KusionStack/karpor/pkg/util/clusterinstall"
"github.com/KusionStack/karpor/pkg/util/ctxutil"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/pkg/errors"
_ "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/server"
"k8s.io/client-go/tools/clientcmd"
k8syaml "sigs.k8s.io/yaml"
)
// Get returns an HTTP handler function that reads a cluster
// detail. It utilizes a ClusterManager to execute the logic.
//
// @Summary Get returns a cluster resource by name.
// @Description This endpoint returns a cluster resource by name.
// @Tags cluster
// @Produce json
// @Param clusterName path string true "The name of the cluster"
// @Param format query string false "The format of the response. Either in json or yaml"
// @Success 200 {object} unstructured.Unstructured "Unstructured object"
// @Failure 400 {string} string "Bad Request"
// @Failure 401 {string} string "Unauthorized"
// @Failure 404 {string} string "Not Found"
// @Failure 405 {string} string "Method Not Allowed"
// @Failure 429 {string} string "Too Many Requests"
// @Failure 500 {string} string "Internal Server Error"
// @Router /rest-api/v1/cluster/{clusterName} [get]
func Get(clusterMgr *cluster.ClusterManager, c *server.CompletedConfig) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Extract the context and logger from the request.
ctx := r.Context()
logger := ctxutil.GetLogger(ctx)
cluster := chi.URLParam(r, "clusterName")
logger.Info("Getting cluster...", "cluster", cluster)
outputFormat := r.URL.Query().Get("format")
client, err := multicluster.BuildMultiClusterClient(r.Context(), c.LoopbackClientConfig, "")
if err != nil {
render.Render(w, r, handler.FailureResponse(r.Context(), err))
return
}
if strings.ToLower(outputFormat) == "yaml" {
clusterYAML, err := clusterMgr.GetYAMLForCluster(r.Context(), client, cluster)
handler.HandleResult(w, r, ctx, err, string(clusterYAML))
} else {
clusterUnstructured, err := clusterMgr.GetCluster(r.Context(), client, cluster)
handler.HandleResult(w, r, ctx, err, clusterUnstructured)
}
}
}
// Create returns an HTTP handler function that creates a cluster
// resource. It utilizes a ClusterManager to execute the logic.
//
// @Summary Create creates a cluster resource.
// @Description This endpoint creates a new cluster resource using the payload.
// @Tags cluster
// @Accept plain
// @Accept json
// @Produce json
// @Param request body ClusterPayload true "cluster to create (either plain text or JSON format)"
// @Param clusterName path string true "The name of the cluster"
// @Success 200 {object} unstructured.Unstructured "Unstructured object"
// @Failure 400 {string} string "Bad Request"
// @Failure 401 {string} string "Unauthorized"
// @Failure 404 {string} string "Not Found"
// @Failure 405 {string} string "Method Not Allowed"
// @Failure 429 {string} string "Too Many Requests"
// @Failure 500 {string} string "Internal Server Error"
// @Router /rest-api/v1/cluster/{clusterName} [post]
func Create(clusterMgr *cluster.ClusterManager, c *server.CompletedConfig) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Extract the context and logger from the request.
ctx := r.Context()
logger := ctxutil.GetLogger(ctx)
cluster := chi.URLParam(r, "clusterName")
logger.Info("Creating cluster...", "cluster", cluster)
// Decode the request body into the payload.
payload := &ClusterPayload{}
if err := payload.Decode(r); err != nil {
render.Render(w, r, handler.FailureResponse(ctx, err))
return
}
client, _ := multicluster.BuildMultiClusterClient(r.Context(), c.LoopbackClientConfig, "")
clusterCreated, err := clusterMgr.CreateCluster(r.Context(), client, cluster, payload.ClusterDisplayName, payload.ClusterDescription, payload.ClusterKubeConfig)
handler.HandleResult(w, r, ctx, err, clusterCreated)
}
}
// Update returns an HTTP handler function that updates a cluster
// resource. It utilizes a ClusterManager to execute the logic.
//
// @Summary Update updates the cluster metadata by name.
// @Description This endpoint updates the display name and description of an existing cluster resource.
// @Tags cluster
// @Accept plain
// @Accept json
// @Produce json
// @Param request body ClusterPayload true "cluster to update (either plain text or JSON format)"
// @Param clusterName path string true "The name of the cluster"
// @Success 200 {object} unstructured.Unstructured "Unstructured object"
// @Failure 400 {string} string "Bad Request"
// @Failure 401 {string} string "Unauthorized"
// @Failure 404 {string} string "Not Found"
// @Failure 405 {string} string "Method Not Allowed"
// @Failure 429 {string} string "Too Many Requests"
// @Failure 500 {string} string "Internal Server Error"
// @Router /rest-api/v1/cluster/{clusterName} [put]
func Update(clusterMgr *cluster.ClusterManager, c *server.CompletedConfig) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Extract the context and logger from the request.
ctx := r.Context()
logger := ctxutil.GetLogger(ctx)
cluster := chi.URLParam(r, "clusterName")
logger.Info("Updating cluster metadata...", "cluster", cluster)
// Decode the request body into the payload.
payload := &ClusterPayload{}
if err := payload.Decode(r); err != nil {
render.Render(w, r, handler.FailureResponse(ctx, err))
return
}
client, _ := multicluster.BuildMultiClusterClient(r.Context(), c.LoopbackClientConfig, "")
if payload.ClusterKubeConfig != "" {
clusterUpdated, err := clusterMgr.UpdateCredential(r.Context(), client, cluster, payload.ClusterKubeConfig)
handler.HandleResult(w, r, ctx, err, clusterUpdated)
} else {
clusterUpdated, err := clusterMgr.UpdateMetadata(r.Context(), client, cluster, payload.ClusterDisplayName, payload.ClusterDescription)
handler.HandleResult(w, r, ctx, err, clusterUpdated)
}
}
}
// List returns an HTTP handler function that lists all cluster
// resources. It utilizes a ClusterManager to execute the logic.
//
// @Summary List lists all cluster resources.
// @Description This endpoint lists all cluster resources.
// @Tags cluster
// @Produce json
// @Param summary query bool false "Whether to display summary or not. Default to false"
// @Param orderBy query string false "The order to list the cluster. Default to order by name"
// @Param descending query bool false "Whether to sort the list in descending order. Default to false"
// @Success 200 {array} unstructured.Unstructured "List of cluster objects"
// @Failure 400 {string} string "Bad Request"
// @Failure 401 {string} string "Unauthorized"
// @Failure 404 {string} string "Not Found"
// @Failure 405 {string} string "Method Not Allowed"
// @Failure 429 {string} string "Too Many Requests"
// @Failure 500 {string} string "Internal Server Error"
// @Router /rest-api/v1/clusters [get]
func List(clusterMgr *cluster.ClusterManager, c *server.CompletedConfig) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Extract the context and logger from the request.
ctx := r.Context()
logger := ctxutil.GetLogger(ctx)
logger.Info("Listing clusters...")
orderBy := r.URL.Query().Get("orderBy")
descending, _ := strconv.ParseBool(r.URL.Query().Get("descending"))
summary, _ := strconv.ParseBool(r.URL.Query().Get("summary"))
client, _ := multicluster.BuildMultiClusterClient(r.Context(), c.LoopbackClientConfig, "")
if summary {
clusterSummary, err := clusterMgr.CountCluster(r.Context(), client, c.LoopbackClientConfig)
handler.HandleResult(w, r, ctx, err, clusterSummary)
} else {
criteria, ok := sortCriteriaMap[orderBy]
if !ok {
criteria = cluster.ByName
}
clusterList, err := clusterMgr.ListCluster(r.Context(), client, criteria, descending)
handler.HandleResult(w, r, ctx, err, clusterList)
}
}
}
// Delete returns an HTTP handler function that deletes a cluster
// resource. It utilizes a ClusterManager to execute the logic.
//
// @Summary Delete removes a cluster resource by name.
// @Description This endpoint deletes the cluster resource by name.
// @Tags cluster
// @Produce json
// @Param clusterName path string true "The name of the cluster"
// @Success 200 {string} string "Operation status"
// @Failure 400 {string} string "Bad Request"
// @Failure 401 {string} string "Unauthorized"
// @Failure 404 {string} string "Not Found"
// @Failure 405 {string} string "Method Not Allowed"
// @Failure 429 {string} string "Too Many Requests"
// @Failure 500 {string} string "Internal Server Error"
// @Router /rest-api/v1/cluster/{clusterName} [delete]
func Delete(clusterMgr *cluster.ClusterManager, c *server.CompletedConfig) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Extract the context and logger from the request.
ctx := r.Context()
logger := ctxutil.GetLogger(ctx)
cluster := chi.URLParam(r, "clusterName")
logger.Info("Deleting cluster...", "cluster", cluster)
client, _ := multicluster.BuildMultiClusterClient(r.Context(), c.LoopbackClientConfig, "")
err := clusterMgr.DeleteCluster(r.Context(), client, cluster)
handler.HandleResult(w, r, ctx, err, "Cluster deleted")
}
}
// @Summary Upload kubeConfig file for cluster
// @Description Uploads a KubeConfig file for cluster, with a maximum size of 2MB.
// @Tags cluster
// @Accept multipart/form-data
// @Produce plain
// @Param file formData file true "Upload file with field name 'file'"
// @Param name formData string true "cluster name"
// @Param displayName formData string true "cluster display name"
// @Param description formData string true "cluster description"
// @Success 200 {object} UploadData "Returns the content of the uploaded KubeConfig file."
// @Failure 400 {string} string "The uploaded file is too large or the request is invalid."
// @Failure 500 {string} string "Internal server error."
// @Router /rest-api/v1/cluster/config/file [post]
func UploadKubeConfig(clusterMgr *cluster.ClusterManager) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Extract the context and logger from the request.
ctx := r.Context()
log := ctxutil.GetLogger(ctx)
// Begin the update process, logging the start.
log.Info("Starting get uploaded kubeconfig file in handler...")
// Limit the size of the request body to prevent overflow.
const maxUploadSize = 2 << 20
r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize)
if err := r.ParseMultipartForm(maxUploadSize); err != nil {
render.Render(w, r, handler.FailureResponse(ctx, errors.Wrapf(err, "failed to parse multipart form")))
return
}
// Retrieve the file from the parsed multipart form.
name := r.FormValue("name")
displayName := r.FormValue("displayName")
description := r.FormValue("description")
file, fileHeader, err := r.FormFile("file")
if err != nil {
render.Render(w, r, handler.FailureResponse(ctx, errors.Wrapf(err, "invalid request")))
return
}
defer file.Close()
log.Info("Uploaded filename", "filename", fileHeader.Filename)
// Read the contents of the file.
buf := make([]byte, maxUploadSize)
fileSize, err := file.Read(buf)
if err != nil && errors.Is(err, io.EOF) {
render.Render(w, r, handler.FailureResponse(ctx, errors.Wrapf(err, "error reading file")))
return
}
plainContent := string(buf[:fileSize])
// Create new restConfig from uploaded kubeconfig.
restConfig, err := clientcmd.RESTConfigFromKubeConfig([]byte(plainContent))
if err != nil {
render.Render(w, r, handler.FailureResponse(ctx, errors.Wrapf(err, "error create new rest config from uploaded kubeconfig")))
return
}
// Convert the rest.Config to Cluster object.
clusterObj, err := clusterinstall.ConvertKubeconfigToCluster(name, displayName, description, restConfig)
if err != nil {
render.Render(w, r, handler.FailureResponse(ctx, errors.Wrapf(err, "error convert kubeconfig to cluster")))
return
}
unstructuredClusterMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(clusterObj)
if err != nil {
render.Render(w, r, handler.FailureResponse(ctx, errors.Wrapf(err, "error convert cluster to unstructured obj")))
return
}
clusterUnstructured := &unstructured.Unstructured{}
clusterUnstructured.SetUnstructuredContent(unstructuredClusterMap)
// Sanitize the cluster object.
sanitizedUnstructuredClusterObj, err := cluster.SanitizeUnstructuredCluster(ctx, clusterUnstructured)
if err != nil {
render.Render(w, r, handler.FailureResponse(ctx, errors.Wrapf(err, "error sanitize unstructured obj")))
return
}
clusterYAML, err := k8syaml.Marshal(sanitizedUnstructuredClusterObj)
if err != nil {
render.Render(w, r, handler.FailureResponse(ctx, errors.Wrapf(err, "error marshal unstructured obj")))
return
}
// Convert the bytes read to a string and return as response.
data := &UploadData{
FileName: fileHeader.Filename,
FileSize: fileSize,
Content: plainContent,
SanitizedClusterContent: string(clusterYAML),
}
render.JSON(w, r, handler.SuccessResponse(ctx, data))
}
}
// ValidateKubeConfig returns an HTTP handler function to validate a KubeConfig.
//
// @Summary Validate KubeConfig
// @Description Validates the provided KubeConfig using cluster manager methods.
// @Tags cluster
// @Accept plain
// @Accept json
// @Produce json
// @Param request body ValidatePayload true "KubeConfig payload to validate"
// @Success 200 {string} string "Verification passed server version"
// @Failure 400 {object} string "Bad Request"
// @Failure 401 {object} string "Unauthorized"
// @Failure 429 {object} string "Too Many Requests"
// @Failure 404 {object} string "Not Found"
// @Failure 500 {object} string "Internal Server Error"
// @Router /rest-api/v1/cluster/config/validate [post]
func ValidateKubeConfig(clusterMgr *cluster.ClusterManager) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Extract the context and logger from the request.
ctx := r.Context()
log := ctxutil.GetLogger(ctx)
// Begin the auditing process, logging the start.
log.Info("Starting validate kubeconfig in handler...")
// Decode the request body into the payload.
payload := &ValidatePayload{}
if err := payload.Decode(r); err != nil {
render.Render(w, r, handler.FailureResponse(ctx, err))
return
}
// Log successful decoding of the request body.
sanitizeConfig, _ := clusterMgr.SanitizeKubeConfigWithYAML(ctx, payload.KubeConfig)
log.Info("Successfully decoded the request body to payload, and sanitize the kubeconfig in payload",
"sanitizeKubeConfig", sanitizeConfig)
// Validate the specified kube config.
if info, err := clusterMgr.ValidateKubeConfigWithYAML(ctx, payload.KubeConfig); err == nil {
render.JSON(w, r, handler.SuccessResponse(ctx, info))
} else {
render.Render(w, r, handler.FailureResponse(ctx, err))
}
}
}