Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SAE deploy job #3864

Merged
merged 20 commits into from
Nov 29, 2024
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 @@ -1085,6 +1085,91 @@ 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"`
ValueFrom string `bson:"value_from" json:"value_from" yaml:"value_from"`
ConfigMapID string `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"`

// 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
18 changes: 18 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,20 @@ func validateSAE(args *commonmodels.SAE) error {

return nil
}

// 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([]*commonmodels.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.Key] = env
}

return resp, 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