-
Notifications
You must be signed in to change notification settings - Fork 102
/
application.go
454 lines (396 loc) · 14.3 KB
/
application.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
Copyright 2021 The KubeVela 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 model
import (
"fmt"
"time"
workflowv1alpha1 "github.com/kubevela/workflow/api/v1alpha1"
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
"github.com/oam-dev/kubevela/apis/types"
)
func init() {
RegisterModel(&ApplicationComponent{}, &ApplicationPolicy{}, &Application{}, &ApplicationRevision{}, &ApplicationTrigger{})
}
// Application application delivery model
type Application struct {
BaseModel
Name string `json:"name" gorm:"primaryKey"`
Alias string `json:"alias"`
Project string `json:"project"`
Description string `json:"description"`
Icon string `json:"icon"`
Labels map[string]string `json:"labels,omitempty" gorm:"serializer:json"`
Annotations map[string]string `json:"annotations,omitempty" gorm:"serializer:json"`
}
// TableName return custom table name
func (a *Application) TableName() string {
return tableNamePrefix + "application"
}
// ShortTableName is the compressed version of table name for kubeapi storage and others
func (a *Application) ShortTableName() string {
return "app"
}
// PrimaryKey return custom primary key
// the app primary key is the app name, so the app name is globally unique in every namespace
// when the app is synced from CR, the first synced one be same with app name,
// if there's any conflicts, the name will be composed by <appname>-<namespace>
func (a *Application) PrimaryKey() string {
return a.Name
}
// Index return custom index
func (a *Application) Index() map[string]interface{} {
index := make(map[string]interface{})
if a.Name != "" {
index["name"] = a.Name
}
if a.Project != "" {
index["project"] = a.Project
}
return index
}
// GetAppNamespaceForSynced will return the namespace of synced CR
func (a *Application) GetAppNamespaceForSynced() string {
if a.Labels == nil {
return ""
}
return a.Labels[LabelSyncNamespace]
}
// IsSynced answer if the app is synced one
func (a *Application) IsSynced() bool {
if a.Labels == nil {
return false
}
sot := a.Labels[types.LabelSourceOfTruth]
if sot == types.FromCR || sot == types.FromInner {
return true
}
return false
}
// IsReadOnly is readonly app
// Only the source is inner, the app is readonly
func (a *Application) IsReadOnly() bool {
if a.Labels == nil {
return false
}
sot := a.Labels[types.LabelSourceOfTruth]
return sot == types.FromInner
}
// ClusterSelector cluster selector
type ClusterSelector struct {
Name string `json:"name"`
// Adapt to a scenario where only one Namespace is available or a user-defined Namespace is available.
Namespace string `json:"namespace,omitempty"`
}
// ComponentSelector component selector
type ComponentSelector struct {
Components []string `json:"components"`
}
// ApplicationComponent component database model
type ApplicationComponent struct {
BaseModel
AppPrimaryKey string `json:"appPrimaryKey" gorm:"primaryKey"`
Description string `json:"description,omitempty"`
Labels map[string]string `json:"labels,omitempty" gorm:"serializer:json"`
Icon string `json:"icon,omitempty"`
Creator string `json:"creator"`
Name string `json:"name" gorm:"primaryKey"`
Alias string `json:"alias"`
Type string `json:"type"`
Main bool `json:"main"`
// ExternalRevision specified the component revisionName
ExternalRevision string `json:"externalRevision,omitempty"`
Properties *JSONStruct `json:"properties,omitempty" gorm:"serializer:json"`
DependsOn []string `json:"dependsOn,omitempty" gorm:"serializer:json"`
Inputs workflowv1alpha1.StepInputs `json:"inputs,omitempty" gorm:"serializer:json"`
Outputs workflowv1alpha1.StepOutputs `json:"outputs,omitempty" gorm:"serializer:json"`
// Traits define the trait of one component, the type must be array to keep the order.
Traits []ApplicationTrait `json:"traits,omitempty" gorm:"serializer:json"`
// scopes in ApplicationComponent defines the component-level scopes
// the format is <scope-type:scope-instance-name> pairs, the key represents type of `ScopeDefinition` while the value represent the name of scope instance.
Scopes map[string]string `json:"scopes,omitempty" gorm:"serializer:json"`
WorkloadType common.WorkloadTypeDescriptor `json:"workloadType,omitempty" gorm:"serializer:json"`
}
// TableName return custom table name
func (a *ApplicationComponent) TableName() string {
return tableNamePrefix + "application_component"
}
// ShortTableName is the compressed version of table name for kubeapi storage and others
func (a *ApplicationComponent) ShortTableName() string {
return "app_cmp"
}
// PrimaryKey return custom primary key
func (a *ApplicationComponent) PrimaryKey() string {
return fmt.Sprintf("%s-%s", a.AppPrimaryKey, a.Name)
}
// Index return custom index
func (a *ApplicationComponent) Index() map[string]interface{} {
index := make(map[string]interface{})
if a.Name != "" {
index["name"] = a.Name
}
if a.AppPrimaryKey != "" {
index["appPrimaryKey"] = a.AppPrimaryKey
}
if a.Type != "" {
index["type"] = a.Type
}
if a.WorkloadType.Type != "" {
index["workflowType"] = a.WorkloadType.Type
}
return index
}
// ApplicationPolicy app policy
type ApplicationPolicy struct {
BaseModel
AppPrimaryKey string `json:"appPrimaryKey" gorm:"primaryKey"`
Name string `json:"name" gorm:"primaryKey"`
Alias string `json:"alias"`
Description string `json:"description"`
Type string `json:"type"`
Creator string `json:"creator"`
Properties *JSONStruct `json:"properties,omitempty" gorm:"serializer:json"`
// EnvName if it is not empty, the policy is only belong to this environment
// For auto created policies, this field will be assigned a value
EnvName string `json:"envName"`
}
// TableName return custom table name
func (a *ApplicationPolicy) TableName() string {
return tableNamePrefix + "application_policy"
}
// ShortTableName is the compressed version of table name for kubeapi storage and others
func (a *ApplicationPolicy) ShortTableName() string {
return "app_plc"
}
// PrimaryKey return custom primary key
func (a *ApplicationPolicy) PrimaryKey() string {
return fmt.Sprintf("%s-%s", a.AppPrimaryKey, a.Name)
}
// Index return custom index
func (a *ApplicationPolicy) Index() map[string]interface{} {
index := make(map[string]interface{})
if a.Name != "" {
index["name"] = a.Name
}
if a.AppPrimaryKey != "" {
index["appPrimaryKey"] = a.AppPrimaryKey
}
if a.Type != "" {
index["type"] = a.Type
}
if a.EnvName != "" {
index["envName"] = a.EnvName
}
return index
}
// ApplicationTrait application trait
type ApplicationTrait struct {
Alias string `json:"alias"`
Description string `json:"description"`
Type string `json:"type"`
Properties *JSONStruct `json:"properties,omitempty" gorm:"serializer:json"`
CreateTime time.Time `json:"createTime"`
UpdateTime time.Time `json:"updateTime"`
}
// RevisionStatusInit event status init
var RevisionStatusInit = "init"
// RevisionStatusRunning event status running
var RevisionStatusRunning = "running"
// RevisionStatusComplete event status complete
var RevisionStatusComplete = "complete"
// RevisionStatusFail event status failure
var RevisionStatusFail = "failure"
// RevisionStatusTerminated event status terminated
var RevisionStatusTerminated = "terminated"
// RevisionStatusRollback event status rollback
var RevisionStatusRollback = "rollback"
// WorkflowStepPhaseStopped is the stopped phase
var WorkflowStepPhaseStopped workflowv1alpha1.WorkflowStepPhase = "stopped"
// ApplicationRevision be created when an application initiates deployment and describes the phased version of the application.
type ApplicationRevision struct {
BaseModel
AppPrimaryKey string `json:"appPrimaryKey" gorm:"primaryKey"`
Version string `json:"version" gorm:"primaryKey"`
RollbackVersion string `json:"rollbackVersion,omitempty"`
// ApplyAppConfig Stores the application configuration during the current deploy.
ApplyAppConfig string `json:"applyAppConfig,omitempty"`
// RevisionCRName This is associated with the application revision in the cluster.
RevisionCRName string `json:"revisionCRName"`
// Deploy event status
Status string `json:"status"`
Reason string `json:"reason"`
// The user that triggers the deploy.
DeployUser string `json:"deployUser"`
// Information that users can note.
Note string `json:"note"`
// TriggerType the event trigger source, Web、API、SyncFromCR
TriggerType string `json:"triggerType"`
// WorkflowName deploy controller by workflow
WorkflowName string `json:"workflowName"`
// EnvName is the env name of this application revision
EnvName string `json:"envName"`
// CodeInfo is the code info of this application revision
CodeInfo *CodeInfo `json:"codeInfo,omitempty" gorm:"serializer:json"`
// ImageInfo is the image info of this application revision
ImageInfo *ImageInfo `json:"imageInfo,omitempty" gorm:"serializer:json"`
}
// CodeInfo is the code info for webhook request
type CodeInfo struct {
// Commit is the commit hash
Commit string `json:"commit,omitempty"`
// Branch is the branch name
Branch string `json:"branch,omitempty"`
// User is the user name
User string `json:"user,omitempty"`
}
// ImageInfo is the image info for webhook request
type ImageInfo struct {
// Type is the image type, ACR or Harbor or DockerHub
Type string `json:"type"`
// Resource is the image resource
Resource *ImageResource `json:"resource,omitempty"`
// Repository is the image repository
Repository *ImageRepository `json:"repository,omitempty"`
}
// ImageResource is the image resource
type ImageResource struct {
// Digest is the image digest
Digest string `json:"digest"`
// Tag is the image tag
Tag string `json:"tag"`
// URL is the image url
URL string `json:"url"`
// CreateTime is the image create time
CreateTime time.Time `json:"createTime,omitempty"`
}
// ImageRepository is the image repository
type ImageRepository struct {
// Name is the image repository name
Name string `json:"name"`
// Namespace is the image repository namespace
Namespace string `json:"namespace"`
// FullName is the image repository full name
FullName string `json:"fullName"`
// Region is the image repository region
Region string `json:"region,omitempty"`
// Type is the image repository type, public or private
Type string `json:"type"`
// CreateTime is the image repository create time
CreateTime time.Time `json:"createTime,omitempty"`
}
// TableName return custom table name
func (a *ApplicationRevision) TableName() string {
return tableNamePrefix + "application_revision"
}
// ShortTableName is the compressed version of table name for kubeapi storage and others
func (a *ApplicationRevision) ShortTableName() string {
return "app_rev"
}
// PrimaryKey return custom primary key
func (a *ApplicationRevision) PrimaryKey() string {
return fmt.Sprintf("%s-%s", a.AppPrimaryKey, a.Version)
}
// Index return custom index
func (a *ApplicationRevision) Index() map[string]interface{} {
index := make(map[string]interface{})
if a.Version != "" {
index["version"] = a.Version
}
if a.AppPrimaryKey != "" {
index["appPrimaryKey"] = a.AppPrimaryKey
}
if a.WorkflowName != "" {
index["workflowName"] = a.WorkflowName
}
if a.DeployUser != "" {
index["deployUser"] = a.DeployUser
}
if a.Status != "" {
index["status"] = a.Status
}
if a.TriggerType != "" {
index["triggerType"] = a.TriggerType
}
if a.EnvName != "" {
index["envName"] = a.EnvName
}
return index
}
// ApplicationTrigger is the model for trigger
type ApplicationTrigger struct {
BaseModel
AppPrimaryKey string `json:"appPrimaryKey"`
WorkflowName string `json:"workflowName,omitempty"`
Name string `json:"name"`
Alias string `json:"alias,omitempty"`
Description string `json:"description,omitempty"`
Token string `json:"token" gorm:"primaryKey"`
Type string `json:"type"`
PayloadType string `json:"payloadType"`
ComponentName string `json:"componentName"`
Registry string `json:"registry,omitempty"`
}
const (
// PayloadTypeCustom is the payload type custom
PayloadTypeCustom = "custom"
// PayloadTypeDockerhub is the payload type dockerhub
PayloadTypeDockerhub = "dockerhub"
// PayloadTypeACR is the payload type acr
PayloadTypeACR = "acr"
// PayloadTypeHarbor is the payload type harbor
PayloadTypeHarbor = "harbor"
// PayloadTypeJFrog is the payload type jfrog
PayloadTypeJFrog = "jfrog"
// ComponentTypeWebservice is the component type webservice
ComponentTypeWebservice = "webservice"
// ComponentTypeWorker is the component type worker
ComponentTypeWorker = "worker"
// ComponentTypeTask is the component type task
ComponentTypeTask = "task"
)
const (
// HarborEventTypePushArtifact is the event type PUSH_ARTIFACT
HarborEventTypePushArtifact = "PUSH_ARTIFACT"
// JFrogEventTypePush is push event type of jfrog webhook
JFrogEventTypePush = "pushed"
// JFrogDomainDocker is webhook domain of jfrog docker
JFrogDomainDocker = "docker"
)
// TableName return custom table name
func (w *ApplicationTrigger) TableName() string {
return tableNamePrefix + "trigger"
}
// ShortTableName is the compressed version of table name for kubeapi storage and others
func (w *ApplicationTrigger) ShortTableName() string {
return "app_tg"
}
// PrimaryKey return custom primary key
func (w *ApplicationTrigger) PrimaryKey() string {
return w.Token
}
// Index return custom index
func (w *ApplicationTrigger) Index() map[string]interface{} {
index := make(map[string]interface{})
if w.AppPrimaryKey != "" {
index["appPrimaryKey"] = w.AppPrimaryKey
}
if w.Token != "" {
index["token"] = w.Token
}
if w.Name != "" {
index["name"] = w.Name
}
if w.Type != "" {
index["type"] = w.Type
}
return index
}