/
types.go
307 lines (273 loc) · 11.2 KB
/
types.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
// Copyright (c) 2021 Terminus, Inc.
//
// 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 types
import (
"time"
"github.com/erda-project/erda/apistructs"
)
const (
UnknownType = "unknown"
)
// ModelHeader metadata header
type ModelHeader struct {
ID int64 `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
}
// MetaserverMSG kafka容器信息格式
type MetaserverMSG struct {
Name string `json:"name"` // metaserver_container、metaserver_all_containers
TimeStamp int64 `json:"timestamp"` // 纳秒
Fields map[string]interface{} `json:"fields"` // 全量容器事件时 key: containerID value: container info
Tags map[string]string `json:"tags,omitempty"`
}
var ContainerStatusIndex = map[InstanceStatus]int{
InstanceStatusStarting: 1,
InstanceStatusRunning: 1,
InstanceStatusHealthy: 2,
InstanceStatusUnHealthy: 2,
InstanceStatusUnknown: 2,
InstanceStatusStopped: 3,
InstanceStatusFailed: 3,
InstanceStatusFinished: 3,
InstanceStatusKilled: 4,
InstanceStatusOOM: 5,
}
// InstanceStatus instance status
type InstanceStatus string
const (
InstanceStatusStarting InstanceStatus = "Starting" // 已启动,但未收到健康检查事件,瞬态
InstanceStatusRunning InstanceStatus = "Running"
InstanceStatusHealthy InstanceStatus = "Healthy"
InstanceStatusUnHealthy InstanceStatus = "UnHealthy" // 已启动但收到未通过健康检查事件
InstanceStatusFinished InstanceStatus = "Finished" // 已完成,退出码为0
InstanceStatusFailed InstanceStatus = "Failed" // 已退出,退出码非0
InstanceStatusKilled InstanceStatus = "Killed" // 已被杀
InstanceStatusStopped InstanceStatus = "Stopped" // 已停止,Scheduler与DCOS断连期间事件丢失,后续补偿时,需将Healthy置为Stopped
InstanceStatusUnknown InstanceStatus = "Unknown"
InstanceStatusOOM InstanceStatus = "OOM"
InstanceStatusDead InstanceStatus = "Dead"
)
func IsValidSchedulerInstanceStatus(status string) bool {
switch InstanceStatus(status) {
case InstanceStatusHealthy, InstanceStatusUnHealthy, InstanceStatusFailed, InstanceStatusFinished, InstanceStatusKilled:
return true
default:
return false
}
}
func IsValidAgentInstanceStatus(status string) bool {
switch InstanceStatus(status) {
case InstanceStatusStarting, InstanceStatusStopped, InstanceStatusKilled, InstanceStatusOOM:
return true
default:
return false
}
}
// CmService 服务
type CmService struct {
ModelHeader `json:"-"`
Cluster string `json:"-"` // 集群名
DiceProject string `json:"-"` // 所在大项目名称
DiceApplication string `json:"-"` // 所在项目
DiceRuntime string `json:"-"` // 所在runtime
DiceService string `json:"-"` // 所属应用
PrivateAddr string `json:"private_addr"` // 服务内部地址(lb)
PublicAddr string `json:"public_addr"` // 服务对外地址(lb)
}
const (
DefaultWorkspace apistructs.DiceWorkspace = "DEFAULT"
// DevWorkspace 开发环境
DevWorkspace apistructs.DiceWorkspace = "DEV"
// TestWorkspace 测试环境
TestWorkspace apistructs.DiceWorkspace = "TEST"
// StagingWorkspace 预发环境
StagingWorkspace apistructs.DiceWorkspace = "STAGING"
// ProdWorkspace 生产环境
ProdWorkspace apistructs.DiceWorkspace = "PROD"
)
// NotFound error define for notfound
var NotFound = "not found"
const (
// sys
RoleSysManager = "Manager"
RoleSysAuditor = "Auditor"
// org
RoleOrgManager = "Manager"
RoleOrgDev = "Dev"
RoleOrgOps = "Ops"
RoleOrgSupport = "Support"
RoleOrgDataManager = "DataManager"
RoleOrgDataEngineer = "DataEngineer"
RoleOrgReporter = "Reporter"
RoleOrgEdgeAppEngineer = "EdgeOps"
RoleOrgGuest = "Guest"
RoleOrgAuditor = "Auditor"
// project
RoleProjectOwner = "Owner"
RoleProjectLead = "Lead"
RoleProjectPM = "PM"
RoleProjectPD = "PD"
RoleProjectDev = "Dev"
RoleProjectQA = "QA"
RoleProjectReporter = "Reporter"
RoleProjectGuest = "Guest"
// app
RoleAppOwner = "Owner"
RoleAppLead = "Lead"
RoleAppDev = "Dev"
RoleAppQA = "QA"
RoleAppOps = "Ops"
RoleAppGuest = "Guest"
// publisher
RolePublisherManager = "PublisherManager"
RolePublisherMember = "PublisherMember"
// guest
RoleGuest = "Guest"
//msp
RoleMspOwner = "Owner"
RoleMspLead = "Lead"
RoleMspDev = "Dev"
)
// RoleInfo 角色信息
type RoleInfo struct {
Role string `json:"role"`
IsHide bool `json:"isHide"`
I18nKey string `json:"i18nKey"`
IsManager bool `json:"isManager"`
// 用来排序的字段
Level int `json:"-"`
}
// AllScopeRoleMap 记录所有 scope 下所有 角色信息
var AllScopeRoleMap = map[apistructs.ScopeType]map[string]RoleInfo{
apistructs.SysScope: {
RoleSysManager: {Role: RoleSysManager, IsHide: false, I18nKey: "SysManagerRole", IsManager: true, Level: 0},
RoleSysAuditor: {Role: RoleSysAuditor, IsHide: false, I18nKey: "SysAuditorRole", IsManager: false, Level: 1},
},
apistructs.OrgScope: {
RoleOrgManager: {Role: RoleSysManager, IsHide: false, I18nKey: "OrgManagerRole", IsManager: true, Level: 0},
RoleOrgDev: {Role: RoleOrgDev, IsHide: false, I18nKey: "OrgDevRole", IsManager: false, Level: 1},
RoleOrgOps: {Role: RoleOrgOps, IsHide: false, I18nKey: "OrgOpsRole", IsManager: false, Level: 2},
RoleOrgDataManager: {Role: RoleOrgDataManager, IsHide: false, I18nKey: "OrgDataManagerRole", IsManager: false, Level: 3},
RoleOrgDataEngineer: {Role: RoleOrgDataEngineer, IsHide: false, I18nKey: "OrgDataEngineerRole", IsManager: false, Level: 4},
RoleOrgSupport: {Role: RoleOrgSupport, IsHide: true, I18nKey: "OrgSupportRole", IsManager: false, Level: 5},
RoleOrgReporter: {Role: RoleOrgReporter, IsHide: false, I18nKey: "OrgReporterRole", IsManager: false, Level: 6},
RolePublisherManager: {Role: RolePublisherManager, IsHide: false, I18nKey: "PublisherManagerRole", IsManager: false, Level: 7},
RoleOrgEdgeAppEngineer: {Role: RoleOrgEdgeAppEngineer, IsHide: false, I18nKey: "RoleOrgEdgeAppEngineer", IsManager: false, Level: 8},
RoleOrgGuest: {Role: RoleProjectGuest, IsHide: true, I18nKey: "OrgGuestRole", IsManager: false, Level: 9},
RoleOrgAuditor: {Role: RoleOrgAuditor, IsHide: false, I18nKey: "OrgAuditorRole", IsManager: false, Level: 10},
},
apistructs.ProjectScope: {
RoleProjectOwner: {Role: RoleProjectOwner, IsHide: false, I18nKey: "ProjectOwnerRole", IsManager: true, Level: 0},
RoleProjectLead: {Role: RoleProjectLead, IsHide: false, I18nKey: "ProjectLeadRole", IsManager: true, Level: 1},
RoleProjectPM: {Role: RoleProjectPM, IsHide: false, I18nKey: "ProjectPMRole", IsManager: true, Level: 2},
RoleProjectPD: {Role: RoleProjectPD, IsHide: false, I18nKey: "ProjectPDRole", IsManager: false, Level: 3},
RoleProjectDev: {Role: RoleProjectDev, IsHide: false, I18nKey: "ProjectDevRole", IsManager: false, Level: 4},
RoleProjectQA: {Role: RoleProjectQA, IsHide: false, I18nKey: "ProjectQARole", IsManager: false, Level: 5},
RoleProjectReporter: {Role: RoleProjectReporter, IsHide: false, I18nKey: "ProjectReporterRole", IsManager: false, Level: 6},
RoleProjectGuest: {Role: RoleProjectGuest, IsHide: true, I18nKey: "ProjectGuestRole", IsManager: false, Level: 7},
},
apistructs.AppScope: {
RoleAppOwner: {Role: RoleAppOwner, IsHide: false, I18nKey: "AppOwnerRole", IsManager: true, Level: 0},
RoleAppLead: {Role: RoleAppLead, IsHide: false, I18nKey: "AppLeadRole", IsManager: true, Level: 1},
RoleAppOps: {Role: RoleAppOps, IsHide: false, I18nKey: "AppOpsRole", IsManager: false, Level: 2},
RoleAppDev: {Role: RoleAppDev, IsHide: false, I18nKey: "AppDevRole", IsManager: false, Level: 3},
RoleAppQA: {Role: RoleAppQA, IsHide: false, I18nKey: "AppQARole", IsManager: false, Level: 4},
RoleAppGuest: {Role: RoleAppGuest, IsHide: true, I18nKey: "AppGuestRole", IsManager: false, Level: 5},
},
apistructs.PublisherScope: {
RolePublisherManager: {Role: RolePublisherManager, IsHide: false, I18nKey: "PublisherManagerRole", IsManager: true, Level: 0},
RolePublisherMember: {Role: RolePublisherMember, IsHide: false, I18nKey: "PublisherMemberRole", IsManager: false, Level: 1},
},
}
// GetScopeManagerRoleMap 获取所有 scopeType 下的管理员角色信息 map
func GetScopeManagerRoleMap() map[apistructs.ScopeType]map[string]RoleInfo {
mgrRoles := make(map[apistructs.ScopeType]map[string]RoleInfo)
for scopeType, roles := range AllScopeRoleMap {
for roleName, roleInfo := range roles {
if roleInfo.IsManager {
if _, ok := mgrRoles[scopeType]; !ok {
mgrRoles[scopeType] = make(map[string]RoleInfo)
}
mgrRoles[scopeType][roleName] = roleInfo
}
}
}
return mgrRoles
}
// GetScopeManagerRoleNames 获取 scopeType 下的管理员角色名
func GetScopeManagerRoleNames(scopeType apistructs.ScopeType) []string {
var result []string
for name := range GetScopeManagerRoleMap()[scopeType] {
result = append(result, name)
}
return result
}
// CheckIfRoleIsManager 判断 role 是否是管理员角色
func CheckIfRoleIsManager(role string) bool {
for _, roleInfos := range GetScopeManagerRoleMap() {
for roleName := range roleInfos {
if roleName == role {
return true
}
}
}
return false
}
// CheckIfRoleIsOwner 判断 role 是否是项目/应用所有者角色
func CheckIfRoleIsOwner(role string) bool {
return role == RoleProjectOwner
}
// CheckIfRoleIsValid 判断 role 是否合法
func CheckIfRoleIsValid(role string) bool {
if role == "" {
return false
}
for _, roleInfos := range AllScopeRoleMap {
for roleName := range roleInfos {
if roleName == role {
return true
}
}
}
return false
}
// MemberLabelInfo 成员标签信息
type MemberLabelInfo struct {
Label apistructs.MemeberLabelName `json:"label"`
I18nKey string `json:"i18nKey"`
}
// AllLabelsMap 记录所有的成员标签
var AllLabelsMap = map[apistructs.MemeberLabelName]MemberLabelInfo{
apistructs.LabelNameOutsource: {Label: apistructs.LabelNameOutsource, I18nKey: "MemberLabelOutsource"},
apistructs.LabelNamePartner: {Label: apistructs.LabelNamePartner, I18nKey: "MemberLabelPartner"},
}
// CheckIfMemberLabelIsValid 判断 label 是否合法
func CheckIfMemberLabelIsValid(label string) bool {
if label == "" {
return false
}
if _, ok := AllLabelsMap[apistructs.MemeberLabelName(label)]; !ok {
return false
}
return true
}
// AbilityAppReq 能力请求
type AbilityAppReq struct {
OrgID int64 `json:"orgId"`
ClusterID int64 `json:"clusterId"`
ClusterName string
ApplicationName string
UserID string `json:"operator"`
}