Skip to content

Commit

Permalink
add alias field for production env in some feature (#3970)
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zhao <[email protected]>
  • Loading branch information
PetrusZ authored Jan 21, 2025
1 parent aa02004 commit feabc99
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ type ServiceAndVMDeploy struct {

type ZadigVMDeployJobSpec struct {
Env string `bson:"env" yaml:"env" json:"env"`
Production bool `bson:"-" yaml:"-" json:"production"`
EnvAlias string `bson:"-" yaml:"-" json:"env_alias"`
EnvOptions []*ZadigVMDeployEnvInformation `bson:"-" yaml:"env_options" json:"env_options"`
S3StorageID string `bson:"s3_storage_id" yaml:"s3_storage_id" json:"s3_storage_id"`
ServiceAndVMDeploys []*ServiceAndVMDeploy `bson:"service_and_vm_deploys" yaml:"service_and_vm_deploys" json:"service_and_vm_deploys"`
Expand All @@ -403,6 +405,8 @@ type ZadigVMDeployJobSpec struct {

type ZadigHelmChartDeployJobSpec struct {
Env string `bson:"env" yaml:"env" json:"env"`
Production bool `bson:"-" yaml:"-" json:"production"`
EnvAlias string `bson:"-" yaml:"-" json:"env_alias"`
EnvOptions []*ZadigHelmDeployEnvInformation `bson:"-" yaml:"env_options" json:"env_options"`
EnvSource string `bson:"env_source" yaml:"env_source" json:"env_source"`
SkipCheckRunStatus bool `bson:"skip_check_run_status" yaml:"skip_check_run_status" json:"skip_check_run_status"`
Expand Down Expand Up @@ -717,6 +721,8 @@ type K8sPatchJobSpec struct {

type ZadigDeployEnvInformation struct {
Env string `json:"env" yaml:"env"`
EnvAlias string `json:"env_alias" yaml:"env_alias"`
Production bool `json:"production" yaml:"production"`
RegistryID string `json:"registry_id" yaml:"registry_id"`
Services []*DeployServiceInfo `json:"services" yaml:"services"`
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/microservice/aslan/core/project/service/bizdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ func SearchBizDirByService(serviceName string, labels []string) ([]*SearchBizDir
type GetBizDirServiceDetailResponse struct {
ProjectName string `json:"project_name"`
EnvName string `json:"env_name"`
EnvAlias string `json:"env_alias"`
Production bool `json:"production"`
Name string `json:"name"`
Type string `json:"type"`
Expand Down Expand Up @@ -337,6 +338,7 @@ func GetBizDirServiceDetail(projectName, serviceName string) ([]GetBizDirService
detail := GetBizDirServiceDetailResponse{
ProjectName: env.ProductName,
EnvName: env.EnvName,
EnvAlias: env.Alias,
Production: env.Production,
Name: serviceName,
Type: setting.K8SDeployType,
Expand Down Expand Up @@ -388,6 +390,7 @@ func GetBizDirServiceDetail(projectName, serviceName string) ([]GetBizDirService
detail := GetBizDirServiceDetailResponse{
ProjectName: env.ProductName,
EnvName: env.EnvName,
EnvAlias: env.Alias,
Production: env.Production,
Name: fmt.Sprintf("%s(%s)", releaseName, serviceName),
Type: setting.HelmDeployType,
Expand Down Expand Up @@ -425,6 +428,7 @@ func GetBizDirServiceDetail(projectName, serviceName string) ([]GetBizDirService
detail := GetBizDirServiceDetailResponse{
ProjectName: env.ProductName,
EnvName: env.EnvName,
EnvAlias: env.Alias,
Production: env.Production,
Name: serviceName,
Type: project.ProductFeature.DeployType,
Expand Down
2 changes: 2 additions & 0 deletions pkg/microservice/aslan/core/system/service/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ func GetMyEnvironment(projectName, envName string, production bool, username, us

return &EnvResponse{
Name: envName,
Alias: envInfo.Alias,
Production: envInfo.Production,
ProjectName: projectName,
UpdateTime: envInfo.UpdateTime,
UpdatedBy: envInfo.UpdateBy,
Expand Down
2 changes: 2 additions & 0 deletions pkg/microservice/aslan/core/system/service/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ type WorkflowResponse struct {

type EnvResponse struct {
Name string `json:"name"`
Alias string `json:"alias"`
Production bool `json:"production"`
ProjectName string `json:"project_name"`
UpdateTime int64 `json:"update_time"`
UpdatedBy string `json:"updated_by"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,15 +361,17 @@ func (j *DeployJob) SetOptions(approvalTicket *commonmodels.ApprovalTicket) erro
envName := strings.ReplaceAll(latestSpec.Env, setting.FixedValueMark, "")

if approvalTicket == nil || isAllowedEnv(envName, approvalTicket.Envs) {
serviceInfo, registryID, err := generateEnvDeployServiceInfo(envName, j.workflow.Project, latestSpec, allowedServices)
serviceInfo, envInfo, err := generateEnvDeployServiceInfo(envName, j.workflow.Project, latestSpec, allowedServices)
if err != nil {
log.Errorf("failed to generate service deployment info for env: %s, error: %s", envName, err)
return err
}

envOptions = append(envOptions, &commonmodels.ZadigDeployEnvInformation{
Env: envName,
RegistryID: registryID,
EnvAlias: envInfo.Alias,
Production: envInfo.Production,
RegistryID: envInfo.RegistryID,
Services: serviceInfo,
})
}
Expand All @@ -395,15 +397,17 @@ func (j *DeployJob) SetOptions(approvalTicket *commonmodels.ApprovalTicket) erro
continue
}

serviceDeployOption, registryID, err := generateEnvDeployServiceInfo(env.EnvName, j.workflow.Project, latestSpec, allowedServices)
serviceDeployOption, envInfo, err := generateEnvDeployServiceInfo(env.EnvName, j.workflow.Project, latestSpec, allowedServices)
if err != nil {
log.Errorf("failed to generate service deployment info for env: %s, error: %s", env.EnvName, err)
return err
}

envOptions = append(envOptions, &commonmodels.ZadigDeployEnvInformation{
Env: env.EnvName,
RegistryID: registryID,
EnvAlias: env.Alias,
Production: env.Production,
RegistryID: envInfo.RegistryID,
Services: serviceDeployOption,
})
}
Expand Down Expand Up @@ -569,7 +573,7 @@ func (j *DeployJob) UpdateWithLatestSetting() error {
}

// generateEnvDeployServiceInfo generates the valid deployable service and calculate the visible kvs defined in the spec
func generateEnvDeployServiceInfo(env, project string, spec *commonmodels.ZadigDeployJobSpec, allowedServices []*commonmodels.ServiceWithModule) ([]*commonmodels.DeployServiceInfo, string, error) {
func generateEnvDeployServiceInfo(env, project string, spec *commonmodels.ZadigDeployJobSpec, allowedServices []*commonmodels.ServiceWithModule) ([]*commonmodels.DeployServiceInfo, *commonmodels.Product, error) {
resp := make([]*commonmodels.DeployServiceInfo, 0)
envInfo, err := commonrepo.NewProductColl().Find(&commonrepo.ProductFindOptions{
Name: project,
Expand All @@ -578,12 +582,12 @@ func generateEnvDeployServiceInfo(env, project string, spec *commonmodels.ZadigD
})

if err != nil {
return nil, "", fmt.Errorf("failed to find env: %s in environments, error: %s", env, err)
return nil, nil, fmt.Errorf("failed to find env: %s in environments, error: %s", env, err)
}

projectInfo, err := templaterepo.NewProductColl().Find(project)
if err != nil {
return nil, "", fmt.Errorf("failed to find project %s, err: %v", project, err)
return nil, nil, fmt.Errorf("failed to find project %s, err: %v", project, err)
}

envServiceMap := envInfo.GetServiceMap()
Expand Down Expand Up @@ -614,7 +618,7 @@ func generateEnvDeployServiceInfo(env, project string, spec *commonmodels.ZadigD
Modules: modules,
})
}
return resp, envInfo.RegistryID, nil
return resp, envInfo, nil
}

serviceDefinitionMap := make(map[string]*commonmodels.Service)
Expand Down Expand Up @@ -650,7 +654,7 @@ func generateEnvDeployServiceInfo(env, project string, spec *commonmodels.ZadigD
}

if err != nil {
return nil, "", fmt.Errorf("failed to list services, error: %s", err)
return nil, nil, fmt.Errorf("failed to list services, error: %s", err)
}

for _, service := range serviceDefinitions {
Expand All @@ -659,7 +663,7 @@ func generateEnvDeployServiceInfo(env, project string, spec *commonmodels.ZadigD

envServices, err := commonservice.ListServicesInEnv(env, project, svcKVsMap, log.SugaredLogger())
if err != nil {
return nil, "", fmt.Errorf("failed to list envService, error: %s", err)
return nil, nil, fmt.Errorf("failed to list envService, error: %s", err)
}

envServiceMap2 := map[string]*commonservice.EnvService{}
Expand Down Expand Up @@ -700,7 +704,7 @@ func generateEnvDeployServiceInfo(env, project string, spec *commonmodels.ZadigD

svcInfo, err := FilterServiceVars(service.ServiceName, spec.DeployContents, deployServiceMap[service.ServiceName], envServiceMap2[service.ServiceName])
if err != nil {
return nil, "", e.ErrFilterWorkflowVars.AddErr(err)
return nil, nil, e.ErrFilterWorkflowVars.AddErr(err)
}

item := &commonmodels.DeployServiceInfo{
Expand Down Expand Up @@ -764,12 +768,12 @@ func generateEnvDeployServiceInfo(env, project string, spec *commonmodels.ZadigD
})

if err != nil {
return nil, "", fmt.Errorf("failed to find default registry for env: %s, error: %s", env, err)
return nil, nil, fmt.Errorf("failed to find default registry for env: %s, error: %s", env, err)
}
envInfo.RegistryID = registry.ID.Hex()
}

return resp, envInfo.RegistryID, nil
return resp, envInfo, nil
}

func (j *DeployJob) MergeArgs(args *commonmodels.Job) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ type ZadigScanningJobSpec struct {

type ZadigDeployJobPreviewSpec struct {
Env string `bson:"env" json:"env"`
EnvAlias string `bson:"-" json:"env_alias"`
Production bool `bson:"-" json:"production"`
SkipCheckRunStatus bool `bson:"skip_check_run_status" json:"skip_check_run_status"`
ServiceAndImages []*ServiceAndImage `bson:"service_and_images" json:"service_and_images"`
Expand Down Expand Up @@ -1440,17 +1441,29 @@ func jobsToJobPreviews(jobs []*commonmodels.JobTask, context map[string]string,
resp := []*JobTaskPreview{}

envMap := make(map[string]*commonmodels.Product)
getEnvProduction := func(envName string) bool {
getEnv := func(envName string) *commonmodels.Product {
if env, ok := envMap[envName]; ok {
return env.Production
return env
}
envInfo, err := commonrepo.NewProductColl().Find(&commonrepo.ProductFindOptions{Name: projectName, EnvName: envName})
if err != nil {
log.Errorf("failed to get env production %s/%s, error : %v", projectName, envName, err)
return false
return nil
}
envMap[envName] = envInfo
return envInfo.Production
return envInfo
}
getEnvProduction := func(env *commonmodels.Product) bool {
if env == nil {
return false
}
return env.Production
}
getEnvAlias := func(env *commonmodels.Product) string {
if env == nil {
return ""
}
return env.Alias
}

for _, job := range jobs {
Expand Down Expand Up @@ -1716,7 +1729,8 @@ func jobsToJobPreviews(jobs []*commonmodels.JobTask, context map[string]string,
continue
}
spec.Env = taskJobSpec.Env
spec.Production = getEnvProduction(taskJobSpec.Env)
spec.Production = getEnvProduction(getEnv(taskJobSpec.Env))
spec.EnvAlias = getEnvAlias(getEnv(taskJobSpec.Env))
spec.VariableConfigs = taskJobSpec.VariableConfigs
spec.VariableKVs = taskJobSpec.VariableKVs
spec.YamlContent = taskJobSpec.YamlContent
Expand Down Expand Up @@ -1747,7 +1761,8 @@ func jobsToJobPreviews(jobs []*commonmodels.JobTask, context map[string]string,
continue
}
spec.Env = taskJobSpec.Env
spec.Production = getEnvProduction(taskJobSpec.Env)
spec.Production = getEnvProduction(getEnv(taskJobSpec.Env))
spec.EnvAlias = getEnvAlias(getEnv(taskJobSpec.Env))
spec.YamlContent = taskJobSpec.YamlContent
spec.UserSuppliedValue = taskJobSpec.UserSuppliedValue
spec.SkipCheckRunStatus = taskJobSpec.SkipCheckRunStatus
Expand All @@ -1768,6 +1783,8 @@ func jobsToJobPreviews(jobs []*commonmodels.JobTask, context map[string]string,
continue
}
spec.Env = taskJobSpec.Env
spec.Production = getEnvProduction(getEnv(taskJobSpec.Env))
spec.EnvAlias = getEnvAlias(getEnv(taskJobSpec.Env))
spec.SkipCheckRunStatus = taskJobSpec.SkipCheckRunStatus
spec.DeployHelmCharts = append(spec.DeployHelmCharts, taskJobSpec.DeployHelmChart)
jobPreview.Spec = spec
Expand All @@ -1792,6 +1809,8 @@ func jobsToJobPreviews(jobs []*commonmodels.JobTask, context map[string]string,
serviceName = arg.Value
}
}
spec.Production = getEnvProduction(getEnv(spec.Env))
spec.EnvAlias = getEnvAlias(getEnv(spec.Env))

serviceAndVMDeploy := []*commonmodels.ServiceAndVMDeploy{}
for _, step := range taskJobSpec.Steps {
Expand Down

0 comments on commit feabc99

Please sign in to comment.