/
deploy_appstore.go
171 lines (159 loc) · 6.29 KB
/
deploy_appstore.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
// 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"
"strings"
"time"
"github.com/gin-gonic/gin"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"kubegems.io/kubegems/pkg/service/handlers"
)
type HelmManifest struct {
Name string `json:"name"`
Description string `json:"description"`
Kind string `json:"kind"`
Creator string `json:"creator"`
CreateAt metav1.Time `json:"createAt"`
RepoURL string `json:"repoURL"` // 仓库index地址
Chart string `json:"chart"` // chart名称
ChartVersion string `json:"chartVersion"` // chart版本
Values string `json:"values"`
}
type DeploiedHelm struct {
HelmManifest `json:",inline"`
Runtime ManifestRuntime `json:"runtime"`
}
// @Tags Application
// @Summary 应用商店应用列表
// @Description 应用商店应用列表
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param environment_id path int true "environment_id"
// @Success 200 {object} handlers.ResponseStruct{Data=[]DeploiedManifest} "Application"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/environment/{environment_id}/appstoreapplications [get]
// @Security JWT
func (h *ApplicationHandler) ListAppstoreApp(c *gin.Context) {
h.NoNameRefFunc(c, nil, func(ctx context.Context, ref PathRef) (interface{}, error) {
// list argo apps
applist, err := h.ArgoCD.ListArgoApp(ctx, labels.Set{
LabelKeyFrom: LabelValueFromAppStore,
LabelTenant: ref.Tenant,
LabelProject: ref.Project,
LabelEnvironment: ref.Env,
}.AsSelector())
if err != nil {
return nil, err
}
list := []*DeploiedHelm{}
for _, app := range applist.Items {
deploied := CompleteDeploiedManifestRuntime(&app, &DeploiedManifest{})
deploiedhelm := &DeploiedHelm{
HelmManifest: HelmManifest{
Name: deploied.Name,
Kind: deploied.Runtime.Kind,
Description: deploied.Description,
Creator: deploied.Creator,
CreateAt: deploied.CreateAt,
RepoURL: app.Spec.Source.RepoURL,
ChartVersion: app.Spec.Source.TargetRevision,
Chart: app.Spec.Source.Chart,
},
Runtime: deploied.Runtime,
}
if helm := app.Spec.Source.Helm; helm != nil {
deploiedhelm.Values = app.Spec.Source.Helm.Values
}
list = append(list, deploiedhelm)
}
namefunc := func(i *DeploiedHelm) string { return i.Name }
timefunc := func(i *DeploiedHelm) time.Time { return i.CreateAt.Time }
return handlers.NewPageDataFromContext(c, list, namefunc, timefunc), nil
})
}
// @Tags Application
// @Summary 应用商店应用
// @Description 应用商店应用
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param environment_id path int true "environment_id"
// @Param name path string true "application name"
// @Success 200 {object} handlers.ResponseStruct{Data=DeploiedManifest} "Application"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/environment/{environment_id}/appstoreapplications/{name} [get]
// @Security JWT
func (h *ApplicationHandler) GetAppstoreApp(c *gin.Context) {
h.NamedRefFunc(c, nil, func(ctx context.Context, ref PathRef) (interface{}, error) {
deploied := &DeploiedManifest{Manifest: Manifest{}}
app, err := h.ArgoCD.GetArgoApp(ctx, ref.FullName())
if err != nil {
return nil, err
}
CompleteDeploiedManifestRuntime(app, deploied)
return deploied, nil
})
}
// @Tags Application
// @Summary 应用商店部署
// @Description 应用商店部署
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param environment_id path int true "environment_id"
// @Param body body AppStoreDeployForm true "chart 部署信息"
// @Success 200 {object} handlers.ResponseStruct{Data=[]DeploiedManifest} "Application"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/environment/{environment_id}/appstoreapplications [post]
// @Security JWT
func (h *ApplicationHandler) CreateAppstoreApp(c *gin.Context) {
body := AppStoreDeployForm{}
h.NoNameRefFunc(c, &body, func(ctx context.Context, ref PathRef) (interface{}, error) {
// ref 中没有name
ref.Name = strings.TrimSpace(body.Name)
ref.Name = strings.ToLower(ref.Name) // 小写
// audit
h.SetAuditData(c, "部署", "应用商店应用", ref.Name)
argoapp, err := h.ApplicationProcessor.deployHelmApplication(ctx, ref, body)
if err != nil {
return nil, err
}
return argoapp, nil
})
}
// @Tags Application
// @Summary 应用商店应用列表
// @Description 应用商店应用列表
// @Accept json
// @Produce json
// @Param tenant_id path int true "tenaut id"
// @Param project_id path int true "project id"
// @Param environment_id path int true "environment_id"
// @Param name path string true "application name"
// @Success 200 {object} handlers.ResponseStruct{Data=[]DeploiedManifest} "Application"
// @Router /v1/tenant/{tenant_id}/project/{project_id}/environment/{environment_id}/appstoreapplications [delete]
// @Security JWT
func (h *ApplicationHandler) RemoveAppstoreApp(c *gin.Context) {
h.NamedRefFunc(c, nil, func(ctx context.Context, ref PathRef) (interface{}, error) {
// audit
h.SetAuditData(c, "删除", "应用商店应用", ref.Name)
if err := h.ApplicationProcessor.Remove(ctx, ref); err != nil {
return nil, err
}
return "ok", nil
})
}