2025-06-13 19:51:08 +08:00
|
|
|
|
package application
|
2025-06-15 18:10:04 +08:00
|
|
|
|
|
|
|
|
|
import (
|
2025-06-22 11:22:04 +08:00
|
|
|
|
"bytes"
|
2025-06-15 18:10:04 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
2025-06-22 12:08:39 +08:00
|
|
|
|
"122.51.31.227/go-course/go18/devcloud/mcenter/apps/policy"
|
2025-06-22 11:22:04 +08:00
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/infraboard/mcube/v2/ioc/config/validator"
|
2025-06-15 18:10:04 +08:00
|
|
|
|
"github.com/infraboard/mcube/v2/tools/pretty"
|
|
|
|
|
)
|
|
|
|
|
|
2025-06-22 11:22:04 +08:00
|
|
|
|
func NewApplication(req CreateApplicationRequest) (*Application, error) {
|
|
|
|
|
app := &Application{
|
|
|
|
|
CreateApplicationRequest: req,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := app.Validate(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 动态计算评审状态
|
|
|
|
|
if len(req.Audits) > 0 {
|
|
|
|
|
app.SetReady(true)
|
|
|
|
|
} else {
|
|
|
|
|
app.SetReady(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.BuildId()
|
|
|
|
|
return app, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-15 18:10:04 +08:00
|
|
|
|
type Application struct {
|
|
|
|
|
// 对象Id
|
2025-06-22 11:22:04 +08:00
|
|
|
|
Id string `json:"id" bson:"_id" gorm:"column:id;primary_key"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 更新时间
|
2025-06-22 11:22:04 +08:00
|
|
|
|
UpdateAt time.Time `json:"update_at" bson:"update_at" gorm:"column:update_at"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 更新人
|
2025-06-22 11:22:04 +08:00
|
|
|
|
UpdateBy string `json:"update_by" bson:"update_by" gorm:"column:update_by"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 创建请求
|
|
|
|
|
CreateApplicationRequest
|
2025-06-22 11:22:04 +08:00
|
|
|
|
// 应用状态
|
|
|
|
|
ApplicationStatus
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *Application) TableName() string {
|
|
|
|
|
return "applications"
|
2025-06-15 18:10:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *Application) String() string {
|
|
|
|
|
return pretty.ToJSON(a)
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-22 11:22:04 +08:00
|
|
|
|
func (a *Application) SetReady(v bool) *Application {
|
|
|
|
|
a.Ready = &v
|
|
|
|
|
return a
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *Application) BuildId() {
|
|
|
|
|
bf := bytes.NewBuffer([]byte{})
|
|
|
|
|
|
|
|
|
|
switch a.Type {
|
|
|
|
|
case TYPE_SOURCE_CODE:
|
|
|
|
|
bf.WriteString(a.CodeRepository.SshUrl)
|
|
|
|
|
case TYPE_CONTAINER_IMAGE:
|
|
|
|
|
bf.WriteString(a.GetImageRepositoryPrimaryAddress())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a.Id = uuid.NewSHA1(uuid.Nil, bf.Bytes()).String()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewCreateApplicationRequest() *CreateApplicationRequest {
|
|
|
|
|
return &CreateApplicationRequest{
|
|
|
|
|
CreateApplicationSpec: CreateApplicationSpec{
|
|
|
|
|
Extras: map[string]string{},
|
|
|
|
|
ImageRepository: []ImageRepository{},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-15 18:10:04 +08:00
|
|
|
|
type CreateApplicationRequest struct {
|
|
|
|
|
// 创建人
|
2025-06-22 11:22:04 +08:00
|
|
|
|
CreateBy string `json:"create_by" bson:"create_by" gorm:"column:create_by" description:"创建人"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 创建时间
|
2025-06-22 11:22:04 +08:00
|
|
|
|
CreateAt time.Time `json:"create_at" bson:"create_at" gorm:"column:create_at" description:"创建时间"`
|
2025-06-22 12:08:39 +08:00
|
|
|
|
// 资源范围, Namespace是继承的, Scope是API添加的
|
|
|
|
|
policy.ResourceLabel
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 应用创建参数
|
|
|
|
|
CreateApplicationSpec
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-22 11:22:04 +08:00
|
|
|
|
func (a *CreateApplicationRequest) Validate() error {
|
|
|
|
|
return validator.Validate(a)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *CreateApplicationRequest) GetImageRepositoryPrimaryAddress() string {
|
|
|
|
|
for _, repo := range a.ImageRepository {
|
|
|
|
|
if repo.IsPrimary {
|
|
|
|
|
return repo.Address
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-15 18:10:04 +08:00
|
|
|
|
type CreateApplicationSpec struct {
|
|
|
|
|
// 应用名称
|
2025-06-22 11:22:04 +08:00
|
|
|
|
Name string `json:"name" bson:"name" gorm:"column:name" description:"应用名称"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 应用描述
|
2025-06-22 11:22:04 +08:00
|
|
|
|
Description string `json:"description" bson:"description" gorm:"column:description" description:"应用描述"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 应用图标
|
2025-06-22 11:22:04 +08:00
|
|
|
|
Icon string `json:"icon" bson:"icon" gorm:"column:icons" description:"应用图标"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 应用类型
|
2025-06-22 11:22:04 +08:00
|
|
|
|
Type TYPE `json:"type" bson:"type" gorm:"column:type" description:"应用类型, SOURCE_CODE, CONTAINER_IMAGE, OTHER"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 应用代码仓库信息
|
2025-06-22 11:22:04 +08:00
|
|
|
|
CodeRepository CodeRepository `json:"code_repository" bson:",inline" gorm:"embedded" description:"应用代码仓库信息"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 应用镜像仓库信息
|
2025-06-22 11:22:04 +08:00
|
|
|
|
ImageRepository []ImageRepository `json:"image_repository" gorm:"column:image_repository;serializer:json;" bson:"image_repository" description:"应用镜像仓库信息"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 应用所有者
|
2025-06-22 11:22:04 +08:00
|
|
|
|
Owner string `json:"owner" bson:"owner" gorm:"column:owner" description:"应用所有者"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 应用等级, 评估这个应用的重要程度
|
2025-06-22 11:22:04 +08:00
|
|
|
|
Level *uint32 `json:"level" bson:"level" gorm:"column:level" description:"应用等级, 评估这个应用的重要程度"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 应用优先级, 应用启动的先后顺序
|
2025-06-22 11:22:04 +08:00
|
|
|
|
Priority *uint32 `json:"priority" bson:"priority" gorm:"column:priority" description:"应用优先级, 应用启动的先后顺序"`
|
|
|
|
|
// 额外的其他属性
|
|
|
|
|
Extras map[string]string `json:"extras" form:"extras" bson:"extras" gorm:"column:extras;serializer:json;"`
|
|
|
|
|
|
|
|
|
|
// 指定应用的评审方
|
|
|
|
|
Audits []ApplicationReadyAudit `json:"audits" bson:"audits" gorm:"column:audits;serializer:json" description:"参与应用准备就绪的评审方"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 服务代码仓库信息
|
|
|
|
|
type CodeRepository struct {
|
|
|
|
|
// 仓库提供商
|
2025-06-22 11:22:04 +08:00
|
|
|
|
Provider SCM_PROVIDER `json:"provider" bson:"provider" gorm:"column:provider"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// token 操作仓库, 比如设置回调
|
2025-06-22 11:22:04 +08:00
|
|
|
|
Token string `json:"token" bson:"token" gorm:"column:token"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 仓库对应的项目Id
|
2025-06-22 11:22:04 +08:00
|
|
|
|
ProjectId string `json:"project_id" bson:"project_id" gorm:"column:project_id"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 仓库对应空间
|
2025-06-22 11:22:04 +08:00
|
|
|
|
Namespace string `json:"namespace" bson:"namespace" gorm:"column:namespace"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 仓库web url地址
|
2025-06-22 11:22:04 +08:00
|
|
|
|
WebUrl string `json:"web_url" bson:"web_url" gorm:"column:web_url"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 仓库ssh url地址
|
2025-06-22 11:22:04 +08:00
|
|
|
|
SshUrl string `json:"ssh_url" bson:"ssh_url" gorm:"column:ssh_url"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 仓库http url地址
|
2025-06-22 11:22:04 +08:00
|
|
|
|
HttpUrl string `json:"http_url" bson:"http_url" gorm:"column:http_url"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 源代码使用的编程语言, 构建时, 不同语言有不同的构建环境
|
2025-06-22 11:22:04 +08:00
|
|
|
|
Language *LANGUAGE `json:"language" bson:"language" gorm:"column:language"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 开启Hook设置
|
2025-06-22 11:22:04 +08:00
|
|
|
|
EnableHook bool `json:"enable_hook" bson:"enable_hook" gorm:"column:enable_hook"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// Hook设置
|
2025-06-22 11:22:04 +08:00
|
|
|
|
HookConfig string `json:"hook_config" bson:"hook_config" gorm:"column:hook_config"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// scm设置Hook后返回的id, 用于删除应用时,取消hook使用
|
2025-06-22 11:22:04 +08:00
|
|
|
|
HookId string `json:"hook_id" bson:"hook_id" gorm:"column:hook_id"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
// 仓库的创建时间
|
2025-06-22 11:22:04 +08:00
|
|
|
|
CreatedAt time.Time `json:"created_at" bson:"created_at" gorm:"column:created_at"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 镜像仓库
|
|
|
|
|
type ImageRepository struct {
|
2025-06-22 11:22:04 +08:00
|
|
|
|
// 服务镜像地址, 比如 gcr.lank8s.cn/kaniko-project/executor
|
2025-06-15 18:10:04 +08:00
|
|
|
|
Address string `json:"address" bson:"address"`
|
2025-06-22 11:22:04 +08:00
|
|
|
|
// 是不是主仓库
|
|
|
|
|
IsPrimary bool `json:"is_primary" bson:"is_primary"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ApplicationStatus struct {
|
|
|
|
|
// 该应用是否已经准备就绪,多方确认的一个过程后计算出来的
|
|
|
|
|
Ready *bool `json:"ready" bson:"ready" gorm:"column:ready" description:"该应用是否已经准备就绪"`
|
|
|
|
|
// 就绪状态修改时间
|
|
|
|
|
UpdateAt time.Time `json:"ready_update_at" bson:"ready_update_at" gorm:"column:ready_update_at" description:"就绪状态修改时间"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 参与应用准备就绪的评审方
|
|
|
|
|
type ApplicationReadyAudit struct {
|
|
|
|
|
// 评审角色, Dev, Test, Ops
|
|
|
|
|
RoleName string `json:"role_name"`
|
|
|
|
|
// 评审人
|
|
|
|
|
AuditBy string `json:"audit_by"`
|
|
|
|
|
// 评审时间
|
|
|
|
|
AuditAt time.Time `json:"audit_at"`
|
|
|
|
|
// 是否就绪
|
|
|
|
|
Ready bool `json:"ready"`
|
|
|
|
|
// 评审建议
|
|
|
|
|
Message string `json:"message"`
|
2025-06-15 18:10:04 +08:00
|
|
|
|
}
|