Skip to content

Commit

Permalink
Merge pull request #3864 from jamsman94/feature/saeTask
Browse files Browse the repository at this point in the history
add SAE deploy job
  • Loading branch information
jamsman94 authored Nov 29, 2024
2 parents 70859c2 + 945270e commit 92e5bd8
Show file tree
Hide file tree
Showing 15 changed files with 1,154 additions and 12 deletions.
15 changes: 15 additions & 0 deletions pkg/microservice/aslan/config/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ const (
JobBlueKing JobType = "blueking"
JobApproval JobType = "approval"
JobNotification JobType = "notification"
JobSAEDeploy JobType = "sae-deploy"
)

const (
Expand Down Expand Up @@ -284,6 +285,20 @@ const (
WorkWXApproval ApprovalType = "workwx"
)

type SAEUpdateStrategy string

const (
SAEUpdateTypeGrayBatch = "GrayBatchUpdate"
SAEUpdateTypeBatch = "BatchUpdate"
)

type SAEBatchReleaseType string

const (
SAEBatchReleaseTypeAuto = "auto"
SAEBatchReleaseTypeManual = "manual"
)

type ApproveOrReject string

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type SAEApplication struct {
RunningInstances int32 `json:"running_instances"`
Cpu int32 `json:"cpu"`
Mem int32 `json:"mem"`
ServiceName string `json:"service_name"`
ServiceModule string `json:"service_module"`
}

func (SAEEnv) TableName() string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,28 @@ type JobTaskNotificationSpec struct {
Title string `bson:"title" yaml:"title" json:"title"`
}

type JobTaskSAEDeploySpec struct {
// env info
Env string `bson:"env" json:"env" yaml:"env"`
Production bool `bson:"production" json:"production" yaml:"production"`

// service info
AppID string `bson:"app_id" json:"app_id" yaml:"app_id"`
AppName string `bson:"app_name" json:"app_name" yaml:"app_name"`
ServiceName string `bson:"service_name" json:"service_name" yaml:"service_name"`
ServiceModule string `bson:"service_module" json:"service_module" yaml:"service_module"`
RegionID string `bson:"region_id" json:"region_id" yaml:"region_id"`

// deploy info
Image string `bson:"image" json:"image" yaml:"image"`
UpdateStrategy *SAEUpdateStrategy `bson:"update_strategy" json:"update_strategy" yaml:"update_strategy"`
BatchWaitTime int32 `bson:"batch_wait_time" json:"batch_wait_time" yaml:"batch_wait_time"`
MinReadyInstances int32 `bson:"min_ready_instances" json:"min_ready_instances" yaml:"min_ready_instances"`
MinReadyInstanceRatio int32 `bson:"min_ready_instance_ratio" json:"min_ready_instance_ratio" yaml:"min_ready_instance_ratio"`
Envs []*SAEKV `bson:"envs" json:"envs" yaml:"envs"`
ChangeOrderID string `bson:"change_order_id" json:"change_order_id" yaml:"change_order_id"`
}

type Event struct {
EventType string `bson:"event_type" json:"event_type" yaml:"event_type"`
Time string `bson:"time" json:"time" yaml:"time"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,8 @@ func (n *NotificationJobSpec) GenerateNewNotifyConfigWithOldData() error {
return fmt.Errorf("lark_person_notification_config cannot be empty for type feishu_person notification")
}
default:
return fmt.Errorf("unsupported notification type: %s", n.WebHookType)
// TODO: this code is commented because of chagee old data. uncomment it if possible
//return fmt.Errorf("unsupported notification type: %s", n.WebHookType)
}

return nil
Expand Down Expand Up @@ -1096,6 +1097,93 @@ type WebhookNotificationConfig struct {
Token string `bson:"token" yaml:"token" json:"token"`
}

type SAEDeployJobSpec struct {
DockerRegistryID string `bson:"docker_registry_id" yaml:"docker_registry_id" json:"docker_registry_id"`
EnvConfig *DeployEnvConfig `bson:"env_config" yaml:"env_config" json:"env_config"`
EnvOptions []*SAEEnvInfo `bson:"-" yaml:"env_options" json:"env_options"`
Production bool `bson:"production" yaml:"production" json:"production"`
ServiceConfig *SAEDeployServiceConfig `bson:"service_config" yaml:"service_config" json:"service_config"`

// 当 source 为 fromjob 时需要,指定部署镜像来源是上游哪一个构建任务
JobName string `bson:"job_name" yaml:"job_name" json:"job_name"`
// save the origin quoted job name
OriginJobName string `bson:"origin_job_name" yaml:"origin_job_name" json:"origin_job_name"`
}

type DeployEnvConfig struct {
// supported value: runtime/fixed
Source config.DeploySourceType `bson:"source" json:"source" yaml:"source"`
Name string `bson:"name" json:"name" yaml:"name"`
}

type SAEDeployServiceConfig struct {
// supported value: runtime/fromjob
Source config.DeploySourceType `bson:"source" json:"source" yaml:"source"`
Services []*SAEDeployServiceInfo `bson:"services" json:"services" yaml:"services"`
DefaultServices []*ServiceNameAndModule `bson:"default_services" json:"default_services" yaml:"default_services"`
}

type SAEEnvInfo struct {
Env string `bson:"-" json:"env" yaml:"env"`
Services []*SAEServiceInfo `bson:"-" json:"services" yaml:"services"`
}

type SAEKV struct {
Name string `bson:"name" json:"name" yaml:"name"`
Value string `bson:"value" json:"value" yaml:"value"`
ConfigMapID int `bson:"config_map_id" json:"config_map_id" yaml:"config_map_id"`
Key string `bson:"key" json:"key" yaml:"key"`
}

// SAEServiceInfo is the service info in the SAE env, with its bound service
type SAEServiceInfo struct {
AppID string `bson:"app_id" json:"app_id" yaml:"app_id"`
AppName string `bson:"app_name" json:"app_name" yaml:"app_name"`
Image string `bson:"image" json:"image" yaml:"image"`
Instances int32 `bson:"instances" json:"instances" yaml:"instances"`
Envs []*SAEKV `bson:"envs" json:"envs" yaml:"envs"`

ServiceName string `bson:"service_name" json:"service_name" yaml:"service_name"`
ServiceModule string `bson:"service_module" json:"service_module" yaml:"service_module"`
}

// SAEDeployServiceInfo is the deployment configuration for sae deployment job
type SAEDeployServiceInfo struct {
AppID string `bson:"app_id" json:"app_id" yaml:"app_id"`
AppName string `bson:"app_name" json:"app_name" yaml:"app_name"`
Image string `bson:"image" json:"image" yaml:"image"`

ServiceName string `bson:"service_name" json:"service_name" yaml:"service_name"`
ServiceModule string `bson:"service_module" json:"service_module" yaml:"service_module"`

// sae instance count. used only for frontend to do rendering
Instances int32 `bson:"instances" json:"instances" yaml:"instances"`

// field description: https://api.aliyun.com/document/sae/2019-05-06/DeployApplication
// note that the camelcase has been converted to snake case.
UpdateStrategy *SAEUpdateStrategy `bson:"update_strategy" json:"update_strategy" yaml:"update_strategy"`
BatchWaitTime int32 `bson:"batch_wait_time" json:"batch_wait_time" yaml:"batch_wait_time"`
MinReadyInstances int32 `bson:"min_ready_instances" json:"min_ready_instances" yaml:"min_ready_instances"`
MinReadyInstanceRatio int32 `bson:"min_ready_instance_ratio" json:"min_ready_instance_ratio" yaml:"min_ready_instance_ratio"`
Envs []*SAEKV `bson:"envs" json:"envs" yaml:"envs"`
}

type SAEUpdateStrategy struct {
Type config.SAEUpdateStrategy `bson:"type" json:"type" yaml:"type"`
BatchUpdate *SAEBatchUpdateConfig `bson:"batch_update" json:"batch_update" yaml:"batch_update"`
GrayUpdate *SAEGrayUpdateConfig `bson:"gray_update" json:"gray_update" yaml:"gray_update"`
}

type SAEBatchUpdateConfig struct {
Batch int `bson:"batch" json:"batch" yaml:"batch"`
ReleaseType config.SAEBatchReleaseType `bson:"release_type" json:"release_type" yaml:"release_type"`
BatchWaitTime int `bson:"batch_wait_time" json:"batch_wait_time" yaml:"batch_wait_time"`
}

type SAEGrayUpdateConfig struct {
Gray int `bson:"gray" json:"gray" yaml:"gray"`
}

type JenkinsJobInfo struct {
JobName string `bson:"job_name" json:"job_name" yaml:"job_name"`
Parameters []*JenkinsJobParameter `bson:"parameters" json:"parameters" yaml:"parameters"`
Expand Down
71 changes: 71 additions & 0 deletions pkg/microservice/aslan/core/common/service/sae/sae.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package sae

import (
"encoding/json"
"fmt"

sae "github.com/alibabacloud-go/sae-20190506/client"
Expand Down Expand Up @@ -109,3 +110,73 @@ func validateSAE(args *commonmodels.SAE) error {

return nil
}

type SAEKV struct {
Name string `json:"name"`
Value string `json:"value,omitempty"`
ValueFrom *SAEValueFrom `json:"valueFrom,omitempty"`
}

type SAEValueFrom struct {
ConfigMapRef *SAEConfigMapRef `json:"configMapRef,omitempty"`
}

type SAEConfigMapRef struct {
ConfigMapID int `json:"configMapId"`
Key string `json:"key"`
}

// CreateKVMap takes a string and de-serialize it into a struct that we can use
func CreateKVMap(kv *string) (map[string]*commonmodels.SAEKV, error) {
envList := make([]*SAEKV, 0)
err := json.Unmarshal([]byte(tea.StringValue(kv)), &envList)
if err != nil {
return nil, err
}

resp := make(map[string]*commonmodels.SAEKV)

for _, env := range envList {
resp[env.Name] = &commonmodels.SAEKV{
Name: env.Name,
Value: env.Value,
//ValueFrom: env.ValueFrom,

}

if env.ValueFrom != nil && env.ValueFrom.ConfigMapRef != nil {
resp[env.Name].ConfigMapID = env.ValueFrom.ConfigMapRef.ConfigMapID
resp[env.Name].Key = env.ValueFrom.ConfigMapRef.Key
}
}

return resp, nil
}

func ToSAEKVString(envs []*commonmodels.SAEKV) (*string, error) {
if len(envs) == 0 {
return nil, nil
}
resp := make([]*SAEKV, 0)
for _, kv := range envs {
saekv := &SAEKV{
Name: kv.Name,
Value: kv.Value,
}

if kv.ConfigMapID != 0 {
saekv.ValueFrom = &SAEValueFrom{ConfigMapRef: &SAEConfigMapRef{
ConfigMapID: kv.ConfigMapID,
Key: kv.Key,
}}
}
resp = append(resp, saekv)
}

envStr, err := json.Marshal(resp)
if err != nil {
return nil, err
}

return tea.String(string(envStr)), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ func initJobCtl(job *commonmodels.JobTask, workflowCtx *commonmodels.WorkflowTas
jobCtl = NewApprovalJobCtl(job, workflowCtx, ack, logger)
case string(config.JobNotification):
jobCtl = NewNotificationJobCtl(job, workflowCtx, ack, logger)
case string(config.JobSAEDeploy):
jobCtl = NewSAEDeployJobCtl(job, workflowCtx, ack, logger)
default:
jobCtl = NewFreestyleJobCtl(job, workflowCtx, ack, logger)
}
Expand Down
Loading

0 comments on commit 92e5bd8

Please sign in to comment.