/
manifest_files.go
428 lines (390 loc) · 13.5 KB
/
manifest_files.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright 2022 The kubegems.io 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 application
import (
"context"
"fmt"
"os"
"strings"
"time"
rollouts "github.com/argoproj/argo-rollouts/pkg/apis/rollouts"
"github.com/gin-gonic/gin"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/util"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
extensionsv1beta1 "k8s.io/api/networking/v1"
networkingv1 "k8s.io/api/networking/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"kubegems.io/kubegems/pkg/service/handlers"
"kubegems.io/kubegems/pkg/utils/git"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// ResourceWhileList 允许编排的资源类型
var ResourceWhileList = []schema.GroupVersionKind{
{Group: corev1.GroupName}, // pod configmap secret etc.
{Group: appsv1.GroupName}, // deployment statefulset daemonset
{Group: batchv1.GroupName}, // job cronjob (v1,v2)
{Group: extensionsv1beta1.GroupName}, // deprecated, ingress deployment scale etc
{Group: networkingv1.GroupName}, // ingress ingressclass network_policy
{Group: storagev1.GroupName, Kind: "PersistentVolumeClaim"}, // pvc
{Group: rollouts.Group}, // argo rollouts(rollouts,analysis_template etc.)
}
// @Tags ApplicationManifest
// @Summary 写入文件
// @Description 修改应用编排
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param name path string true "name"
// @Param filename path string true "file name"
// @Param body body FileContent true "filecontent"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "ok"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/manifests/{name}/files/{filename} [put]
// @Security JWT
func (h *ManifestHandler) PutFile(c *gin.Context) {
body := &FileContent{}
h.NamedRefFunc(c, body, func(ctx context.Context, ref PathRef) (interface{}, error) {
filename := c.Param("filename")
if filename == "" {
return nil, fmt.Errorf("filename cannot be empty")
}
// audit
h.SetAuditData(c, "修改", "编排文件", ref.Name+"/"+filename)
content := []byte(body.Content)
// 初次验证
obj, err := DecodeResource(content)
if err != nil {
return nil, err
}
if !IsPermmitedResource(obj) {
return nil, fmt.Errorf("resource %s is not permitted to be create", obj.GetObjectKind().GroupVersionKind().String())
}
updatefunc := func(ctx context.Context, fs billy.Filesystem) error {
return util.WriteFile(fs, filename, content, os.ModePerm)
}
if err := h.UpdateContentFunc(ctx, ref, updatefunc, fmt.Sprintf("put file %s", filename)); err != nil {
return nil, err
}
return "ok", nil
})
}
// @Tags ApplicationManifest
// @Summary 写入多个文件
// @Description 修改应用编排
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param name path string true "name"
// @Param filename path string true "file name"
// @Param msg query string true "commit mesage"
// @Param body body []FileContent true "files"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "ok"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/manifests/{name}/files [put]
// @Security JWT
func (h *ManifestHandler) PutFiles(c *gin.Context) {
files := []FileContent{}
h.NamedRefFunc(c, &files, func(ctx context.Context, ref PathRef) (interface{}, error) {
// audit
h.SetAuditData(c, "修改", "编排文件", ref.Name)
msg := c.Query("msg")
updatefunc := func(_ context.Context, fs billy.Filesystem) error {
for _, file := range files {
util.WriteFile(fs, file.Name, []byte(file.Content), os.ModePerm)
}
return nil
}
if msg == "" {
msg = "put files"
}
if err := h.UpdateContentFunc(ctx, ref, updatefunc, msg); err != nil {
return nil, err
}
return "ok", nil
})
}
// @Tags ApplicationManifest
// @Summary 删除文件
// @Description 修改应用编排
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param name path string true "name"
// @Param filename path string true "file name"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "ok"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/manifests/{name}/files/{filename} [delete]
// @Security JWT
func (h *ManifestHandler) RemoveFile(c *gin.Context) {
body := []byte{}
h.NamedRefFunc(c, &body, func(ctx context.Context, ref PathRef) (interface{}, error) {
filename := c.Param("filename")
if filename == "" {
return nil, fmt.Errorf("filename cannot be empty")
}
// audit
h.SetAuditData(c, "删除", "编排文件", ref.Name+"/"+filename)
updatefunc := func(ctx context.Context, fs billy.Filesystem) error {
return util.RemoveAll(fs, filename)
}
if err := h.UpdateContentFunc(ctx, ref, updatefunc, fmt.Sprintf("remve file %s", filename)); err != nil {
return nil, err
}
return "ok", nil
})
}
type FileContent struct {
Name string `json:"name"`
Content string `json:"content"`
}
// @Tags ApplicationManifest
// @Summary 列举文件
// @Description 应用编排内容
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param name path string true "name"
// @Success 200 {object} handlers.ResponseStruct{Data=[]FileContent} "ok"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/manifests/{name}/files [get]
// @Security JWT
func (h *ManifestHandler) ListFiles(c *gin.Context) {
h.NamedRefFunc(c, nil, func(ctx context.Context, ref PathRef) (interface{}, error) {
files := []FileContent{}
fun := func(ctx context.Context, fs billy.Filesystem) error {
return ForFileContentFunc(fs, "", func(filename string, content []byte) error {
if filename == "" || filename[0] == '.' {
return nil
}
// ignore readme file
if strings.ToLower(filename) == "readme.md" {
return nil
}
files = append(files, FileContent{Name: filename, Content: string(content)})
return nil
})
}
if err := h.ContentFunc(ctx, ref, fun); err != nil {
return nil, err
}
return files, nil
})
}
type GitLog struct {
Hash string `json:"hash,omitempty"`
Message string `json:"message,omitempty"`
Author string `json:"author,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
}
// @Tags ApplicationManifest
// @Summary 应用编排文件历史
// @Description 应用编排文件历史
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param name path string true "name"
// @Success 200 {object} handlers.ResponseStruct{Data=[]GitLog} "ok"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/manifests/{name}/gitlog [get]
// @Security JWT
func (h *ManifestHandler) GitLog(c *gin.Context) {
h.NamedRefFunc(c, nil, func(ctx context.Context, ref PathRef) (interface{}, error) {
logs := []GitLog{}
fun := func(ctx context.Context, repository Repository) error {
return repository.HistoryFunc(ctx, func(_ context.Context, commit git.Commit) error {
logs = append(logs, GitLog{
Hash: commit.Hash,
Message: commit.Message,
Author: commit.Author.Name,
Timestamp: commit.Author.When.Format(time.RFC3339),
})
return nil
})
}
if err := h.Func(ctx, ref, fun); err != nil {
return nil, err
}
paged := handlers.NewPageDataFromContext(c, logs, nil, nil)
return paged, nil
})
}
// @Tags ApplicationManifest
// @Summary 应用编排文件diff
// @Description 应用编排文件diff
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param name path string true "application name"
// @Param hash query string true "gitcommit hash"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "ok"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/manifests/{name}/gitdiff [get]
// @Security JWT
func (h *ManifestHandler) GitDiff(c *gin.Context) {
h.NamedRefFunc(c, nil, func(ctx context.Context, ref PathRef) (interface{}, error) {
hash := c.Query("hash")
var ret []git.FileDiff
fun := func(ctx context.Context, repository Repository) error {
diff, err := repository.Diff(ctx, hash)
if err != nil {
return err
}
ret = diff
return nil
}
if err := h.Func(ctx, ref, fun); err != nil {
return nil, err
}
return ret, nil
})
}
// @Tags ApplicationManifest
// @Summary 应用编排文件回滚
// @Description 回滚应用编排文件
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param name path string true "application name"
// @Param hash query string true "gitcommit hash to rollback"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "ok"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/manifests/{name}/gitrevert [get]
// @Security JWT
func (h *ManifestHandler) GitRevert(c *gin.Context) {
h.NamedRefFunc(c, nil, func(ctx context.Context, ref PathRef) (interface{}, error) {
hash := c.Query("hash")
h.SetAuditData(c, "回滚", "编排文件", hash)
err := h.Func(ctx, ref,
Revert(hash),
UpdateKustomizeCommit(fmt.Sprintf("revert to %s", hash)),
)
if err != nil {
return nil, err
}
return "ok", nil
})
}
func Revert(rev string) RepositoryFunc {
return func(ctx context.Context, repository Repository) error {
// get history
commit, err := repository.HistoryFiles(ctx, rev)
if err != nil {
return err
}
fs, err := repository.FS(ctx)
if err != nil {
return nil
}
util.RemoveAll(fs, ".")
// copy to
for _, file := range commit.Files {
_ = util.WriteFile(fs, file.Name, []byte(file.Content), os.ModePerm)
}
return nil
}
}
func IsPermmitedResource(obj runtime.Object) bool {
// allow all resources,set limit at argocd project
return true
gvk := obj.GetObjectKind().GroupVersionKind()
for _, white := range ResourceWhileList {
if white.Group != gvk.Group {
continue
}
// 如果为空则适配所有
if white.Version != "" && white.Version != gvk.Version {
continue
}
// 如果为空则适配所有
if white.Kind != "" && white.Kind != gvk.Kind {
continue
}
return true
}
return false
}
// @Tags ApplicationManifest
// @Summary 应用编排文件刷新
// @Description 应用编排文件刷新(git pull)
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param name path string true "application name"
// @Param hash query string true "gitcommit hash"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "ok"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/manifests/{name}/gitpull [post]
// @Security JWT
func (h *ManifestHandler) GitPull(c *gin.Context) {
h.NamedRefFunc(c, nil, func(ctx context.Context, ref PathRef) (interface{}, error) {
if err := h.Func(ctx, ref, Pull()); err != nil {
return nil, err
}
return "ok", nil
})
}
func Pull() RepositoryFunc {
return func(ctx context.Context, repository Repository) error {
return repository.repo.Pull(ctx)
}
}
type CommitImageDetails struct {
CreatedAt metav1.Time `json:"createdAt,omitempty"`
Creator string `json:"creator,omitempty"`
Images []string `json:"images,omitempty"`
}
func (h *ManifestHandler) parseCommitImagesFunc(ctx context.Context, ref PathRef, hash string) (*CommitImageDetails, error) {
var details *CommitImageDetails
err := h.Func(ctx, ref, func(ctx context.Context, repository Repository) error {
commit, err := repository.HistoryFiles(ctx, hash)
if err != nil {
return err
}
images := []string{}
for _, f := range commit.Files {
if !strings.HasSuffix(f.Name, ".yaml") {
continue
}
obj, err := DecodeResource([]byte(f.Content))
if err != nil {
// ignore
continue
}
images = append(images, ParseImagesFrom(obj)...)
}
details = &CommitImageDetails{
CreatedAt: metav1.NewTime(commit.Author.When),
Creator: commit.Author.Name,
Images: images,
}
return nil
})
return details, err
}
func ParseImagesFrom(obj client.Object) []string {
images := []string{}
updatefunc := func(template *corev1.PodTemplateSpec) {
for _, c := range template.Spec.Containers {
images = append(images, c.Image)
}
}
ObjectPodTemplateFunc(obj, updatefunc)
return images
}