Skip to content

Commit

Permalink
add DisplayName in jobTask, improve generate jobName
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zhao <[email protected]>
  • Loading branch information
PetrusZ committed Nov 13, 2024
1 parent db9c98d commit cd6d3d8
Show file tree
Hide file tree
Showing 40 changed files with 186 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ type StageTask struct {
type JobTask struct {
ProjectKey string `bson:"project_key" json:"project_key"`
WorkflowKey string `bson:"workflow_key" json:"workflow_key"`
// job key, used by global variable in context
Key string `bson:"key" json:"key"`
// job name, and also the job id, unique in a workflow
Name string `bson:"name" json:"name"`
// jobTask unique id, unique in the workflow
Key string `bson:"key" json:"key"`
K8sJobName string `bson:"k8s_job_name" json:"k8s_job_name"`
DisplayName string `bson:"display_name" json:"display_name"`
K8sJobName string `bson:"k8s_job_name" json:"k8s_job_name"`
// JobInfo contains the fields that make up the job task name, for frontend display
JobInfo interface{} `bson:"job_info" json:"job_info"`
JobType string `bson:"type" json:"type"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/microservice/aslan/core/common/util/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ func CalcWorkflowTaskRunningTime(task *commonmodels.WorkflowTask) int64 {
}
}
return runningTime
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,12 @@ func (j *ApolloJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error) {
return nil, errors.New("apollo issue list is empty")
}
jobTask := &commonmodels.JobTask{
Name: j.job.Name,
Name: genJobName(j.workflow, j.job.Name, 0),
Key: genJobKey(j.job.Name),
DisplayName: genJobDisplayName(j.job.Name),
JobInfo: map[string]string{
JobNameKey: j.job.Name,
},
Key: j.job.Name,
JobType: string(config.JobApollo),
Spec: &commonmodels.JobTaskApolloSpec{
ApolloID: j.spec.ApolloID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,12 @@ func (j *ApprovalJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error) {
WorkWXApproval: j.spec.WorkWXApproval,
}
jobTask := &commonmodels.JobTask{
Name: j.job.Name,
Name: genJobName(j.workflow, j.job.Name, 0),
Key: genJobKey(j.job.Name),
DisplayName: genJobDisplayName(j.job.Name),
JobInfo: map[string]string{
JobNameKey: j.job.Name,
},
Key: j.job.Name,
JobType: string(config.JobApproval),
Spec: jobSpec,
Timeout: j.spec.Timeout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"regexp"
"strings"
"time"

"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -100,7 +99,7 @@ func (j *BlueGreenDeployJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, erro
return resp, err
}

for _, target := range j.spec.Targets {
for jobSubTaskID, target := range j.spec.Targets {
service, exist, err := getter.GetService(j.spec.Namespace, target.K8sServiceName, kubeClient)
if err != nil || !exist {
msg := fmt.Sprintf("Failed to get service, err: %v", err)
Expand Down Expand Up @@ -134,8 +133,9 @@ func (j *BlueGreenDeployJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, erro
target.BlueK8sServiceName = target.K8sServiceName + config.BlueServiceNameSuffix
target.BlueWorkloadName = getBlueWorkloadName(deployment.Name, version)
task := &commonmodels.JobTask{
Name: jobNameFormat(j.job.Name + "-" + target.K8sServiceName),
Key: strings.Join([]string{j.job.Name, target.K8sServiceName}, "."),
Name: genJobName(j.workflow, j.job.Name, jobSubTaskID),
Key: genJobKey(j.job.Name, target.K8sServiceName),
DisplayName: genJobDisplayName(j.job.Name, target.K8sServiceName),
JobInfo: map[string]string{
JobNameKey: j.job.Name,
"k8s_service_name": target.K8sServiceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func (j *BlueGreenDeployV2Job) ToJobs(taskID int64) ([]*commonmodels.JobTask, er
return resp, errors.Errorf("target services is empty")
}

for _, target := range j.spec.Services {
for jobSubTaskID, target := range j.spec.Services {
var (
deployment *v1.Deployment
deploymentYaml string
Expand Down Expand Up @@ -536,8 +536,9 @@ func (j *BlueGreenDeployV2Job) ToJobs(taskID int64) ([]*commonmodels.JobTask, er
target.GreenDeploymentName = greenDeploymentName

task := &commonmodels.JobTask{
Name: jobNameFormat(j.job.Name + "-" + target.ServiceName),
Key: strings.Join([]string{j.job.Name, target.ServiceName}, "."),
Name: genJobName(j.workflow, j.job.Name, jobSubTaskID),
Key: genJobKey(j.job.Name),
DisplayName: genJobDisplayName(j.job.Name),
JobInfo: map[string]string{
JobNameKey: j.job.Name,
"service_name": target.ServiceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package job

import (
"fmt"
"strings"

"github.com/koderover/zadig/v2/pkg/microservice/aslan/config"
commonmodels "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
Expand Down Expand Up @@ -83,13 +82,14 @@ func (j *BlueGreenReleaseJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, err
if !found {
return resp, fmt.Errorf("no blue-green release job: %s found, please check workflow configuration", j.spec.FromJob)
}
for _, target := range deployJobSpec.Targets {
for jobSubTaskID, target := range deployJobSpec.Targets {
if target.WorkloadName == "" {
continue
}
task := &commonmodels.JobTask{
Name: jobNameFormat(j.job.Name + "-" + target.K8sServiceName),
Key: strings.Join([]string{j.job.Name, target.K8sServiceName}, "."),
Name: genJobName(j.workflow, j.job.Name, jobSubTaskID),
Key: genJobKey(j.job.Name, target.K8sServiceName),
DisplayName: genJobDisplayName(j.job.Name, target.K8sServiceName),
JobInfo: map[string]string{
JobNameKey: j.job.Name,
"k8s_service_name": target.K8sServiceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package job

import (
"fmt"
"strings"

"github.com/koderover/zadig/v2/pkg/microservice/aslan/config"
commonmodels "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
Expand Down Expand Up @@ -103,10 +102,11 @@ func (j *BlueGreenReleaseV2Job) ToJobs(taskID int64) ([]*commonmodels.JobTask, e
}
timeout := templateProduct.Timeout * 60

for _, target := range deployJobSpec.Services {
for jobSubTaskID, target := range deployJobSpec.Services {
task := &commonmodels.JobTask{
Name: jobNameFormat(j.job.Name + "-" + target.ServiceName),
Key: strings.Join([]string{j.job.Name, target.ServiceName}, "."),
Name: genJobName(j.workflow, j.job.Name, jobSubTaskID),
Key: genJobKey(j.job.Name, target.ServiceName),
DisplayName: genJobDisplayName(j.job.Name, target.ServiceName),
JobInfo: map[string]string{
JobNameKey: j.job.Name,
"service_name": target.ServiceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,13 @@ func (j *BlueKingJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error) {

resp := make([]*commonmodels.JobTask, 0)
resp = append(resp, &commonmodels.JobTask{
Name: j.job.Name,
Name: genJobName(j.workflow, j.job.Name, 0),
Key: genJobKey(j.job.Name, strconv.FormatInt(j.spec.ExecutionPlanID, 10)),
DisplayName: genJobDisplayName(j.job.Name),
JobInfo: map[string]string{
JobNameKey: j.job.Name,
"blueking_job_id": strconv.FormatInt(j.spec.ExecutionPlanID, 10),
},
Key: j.job.Name + "." + strconv.FormatInt(j.spec.ExecutionPlanID, 10),
JobType: string(config.JobBlueKing),
Spec: &commonmodels.JobTaskBlueKingSpec{
ToolID: j.spec.ToolID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func (j *BuildJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error) {
}

buildSvc := commonservice.NewBuildService()
for _, build := range j.spec.ServiceAndBuilds {
for i, build := range j.spec.ServiceAndBuilds {
imageTag := commonservice.ReleaseCandidate(build.Repos, taskID, j.workflow.Project, build.ServiceModule, "", build.ImageName, "image")

image := fmt.Sprintf("%s/%s", registry.RegAddr, imageTag)
Expand Down Expand Up @@ -428,13 +428,14 @@ func (j *BuildJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error) {
outputs := ensureBuildInOutputs(buildInfo.Outputs)
jobTaskSpec := &commonmodels.JobTaskFreestyleSpec{}
jobTask := &commonmodels.JobTask{
Name: jobNameFormat(build.ServiceName + "-" + build.ServiceModule + "-" + j.job.Name),
JobInfo: map[string]string{
"service_name": build.ServiceName,
"service_module": build.ServiceModule,
JobNameKey: j.job.Name,
},
Key: strings.Join([]string{j.job.Name, build.ServiceName, build.ServiceModule}, "."),
Key: genJobKey(j.job.Name, build.ServiceName, build.ServiceModule),
Name: genJobName(j.workflow, j.job.Name, i),
DisplayName: genJobDisplayName(j.job.Name, build.ServiceName, build.ServiceModule),
JobType: string(config.JobZadigBuild),
Spec: jobTaskSpec,
Timeout: int64(buildInfo.Timeout),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"math"
"strings"

commonrepo "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/mongodb"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/util"
Expand Down Expand Up @@ -214,7 +213,7 @@ func (j *CanaryDeployJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error)
return resp, fmt.Errorf("failed to get kube client: %s, err: %v", j.spec.ClusterID, err)
}

for _, target := range j.spec.Targets {
for jobSubTaskID, target := range j.spec.Targets {
service, exist, err := getter.GetService(j.spec.Namespace, target.K8sServiceName, kubeClient)
if err != nil || !exist {
msg := fmt.Sprintf("Failed to get service, err: %v", err)
Expand Down Expand Up @@ -248,8 +247,9 @@ func (j *CanaryDeployJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error)
target.WorkloadType = setting.Deployment
canaryReplica := math.Ceil(float64(*deployment.Spec.Replicas) * (float64(target.CanaryPercentage) / 100))
task := &commonmodels.JobTask{
Name: jobNameFormat(j.job.Name + "-" + target.K8sServiceName),
Key: strings.Join([]string{j.job.Name, target.K8sServiceName}, "."),
Name: genJobName(j.workflow, j.job.Name, jobSubTaskID),
Key: genJobKey(j.job.Name, target.K8sServiceName),
DisplayName: genJobDisplayName(j.job.Name, target.K8sServiceName),
JobInfo: map[string]string{
JobNameKey: j.job.Name,
"k8s_service_name": target.K8sServiceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package job

import (
"fmt"
"strings"

"github.com/koderover/zadig/v2/pkg/microservice/aslan/config"
commonmodels "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
Expand Down Expand Up @@ -95,13 +94,14 @@ func (j *CanaryReleaseJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error)
if !found {
return resp, fmt.Errorf("no canary release job: %s found, please check workflow configuration", j.spec.FromJob)
}
for _, target := range deployJobSpec.Targets {
for jobSubTaskID, target := range deployJobSpec.Targets {
if target.WorkloadName == "" {
continue
}
task := &commonmodels.JobTask{
Name: jobNameFormat(j.job.Name + "-" + target.K8sServiceName),
Key: strings.Join([]string{j.job.Name, target.K8sServiceName}, "."),
Name: genJobName(j.workflow, j.job.Name, jobSubTaskID),
Key: genJobKey(j.job.Name, target.K8sServiceName),
DisplayName: genJobDisplayName(j.job.Name, target.K8sServiceName),
JobInfo: map[string]string{
JobNameKey: j.job.Name,
"k8s_service_name": target.K8sServiceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (j *CustomDeployJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error)
if err := commonmodels.IToi(j.job.Spec, j.spec); err != nil {
return resp, err
}
for _, target := range j.spec.Targets {
for jobSubTaskID, target := range j.spec.Targets {
t := strings.Split(target.Target, "/")
if len(t) != 3 {
log.Errorf("target string: %s wrong format", target.Target)
Expand All @@ -205,8 +205,9 @@ func (j *CustomDeployJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error)
SkipCheckRunStatus: j.spec.SkipCheckRunStatus,
}
jobTask := &commonmodels.JobTask{
Name: jobNameFormat(j.job.Name + "-" + workloadType + "-" + workloadName + "-" + containerName),
Key: strings.Join([]string{j.job.Name, workloadType, workloadName, containerName}, "."),
Name: genJobName(j.workflow, j.job.Name, jobSubTaskID),
Key: genJobKey(j.job.Name, workloadType, workloadName, containerName),
DisplayName: genJobDisplayName(j.job.Name, workloadType, workloadName, containerName),
JobInfo: map[string]string{
JobNameKey: j.job.Name,
"workload_type": workloadType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ func (j *DeployJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error) {
timeout := templateProduct.Timeout * 60

if j.spec.DeployType == setting.K8SDeployType {
for _, svc := range j.spec.Services {
for jobSubTaskID, svc := range j.spec.Services {
serviceName := svc.ServiceName
jobTaskSpec := &commonmodels.JobTaskDeploySpec{
Env: envName,
Expand Down Expand Up @@ -921,8 +921,9 @@ func (j *DeployJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error) {
}
}
jobTask := &commonmodels.JobTask{
Name: jobNameFormat(serviceName + "-" + j.job.Name),
Key: strings.Join([]string{j.job.Name, serviceName}, "."),
Key: genJobKey(j.job.Name, serviceName),
Name: genJobName(j.workflow, j.job.Name, jobSubTaskID),
DisplayName: genJobDisplayName(j.job.Name, serviceName),
JobInfo: map[string]string{
JobNameKey: j.job.Name,
"service_name": serviceName,
Expand Down Expand Up @@ -992,7 +993,7 @@ func (j *DeployJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error) {
}

if j.spec.DeployType == setting.HelmDeployType {
for _, svc := range j.spec.Services {
for jobSubTaskID, svc := range j.spec.Services {
var serviceRevision int64
if pSvc, ok := productServiceMap[svc.ServiceName]; ok {
serviceRevision = pSvc.Revision
Expand Down Expand Up @@ -1035,8 +1036,9 @@ func (j *DeployJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error) {
})
}
jobTask := &commonmodels.JobTask{
Name: jobNameFormat(svc.ServiceName + "-" + j.job.Name),
Key: strings.Join([]string{j.job.Name, svc.ServiceName}, "."),
Key: genJobKey(j.job.Name, svc.ServiceName),
Name: genJobName(j.workflow, j.job.Name, jobSubTaskID),
DisplayName: genJobDisplayName(j.job.Name, svc.ServiceName),
JobInfo: map[string]string{
JobNameKey: j.job.Name,
"service_name": svc.ServiceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,9 @@ func (j *ImageDistributeJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, erro
},
}
jobTask := &commonmodels.JobTask{
Name: j.job.Name,
Key: j.job.Name,
Name: genJobName(j.workflow, j.job.Name, 0),
Key: genJobKey(j.job.Name),
DisplayName: genJobDisplayName(j.job.Name),
JobInfo: map[string]string{
JobNameKey: j.job.Name,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ func (j *FreeStyleJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error) {
j.spec.Services = targets
}

for _, service := range j.spec.Services {
task, err := j.toJob(taskID, registries, service, logger)
for i, service := range j.spec.Services {
task, err := j.toJob(taskID, i, registries, service, logger)
if err != nil {
return nil, err
}
Expand All @@ -329,34 +329,36 @@ func (j *FreeStyleJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error) {
return tasks, nil
} else {
// save user defined variables.
jobTask, err := j.toJob(taskID, registries, nil, logger)
jobTask, err := j.toJob(taskID, 0, registries, nil, logger)
if err != nil {
return nil, err
}
return []*commonmodels.JobTask{jobTask}, nil
}
}

func (j *FreeStyleJob) toJob(taskID int64, registries []*commonmodels.RegistryNamespace, service *commonmodels.FreeStyleServiceInfo, logger *zap.SugaredLogger) (*commonmodels.JobTask, error) {
func (j *FreeStyleJob) toJob(taskID int64, jobSubTaskID int, registries []*commonmodels.RegistryNamespace, service *commonmodels.FreeStyleServiceInfo, logger *zap.SugaredLogger) (*commonmodels.JobTask, error) {
jobTaskSpec := &commonmodels.JobTaskFreestyleSpec{
Properties: *j.spec.Properties,
Steps: j.stepsToStepTasks(j.spec.Steps, service, registries),
}

jobName := j.job.Name
jobKey := j.job.Name
jobDisplayName := genJobDisplayName(j.job.Name)
jobKey := genJobKey(j.job.Name)
jobName := genJobName(j.workflow, j.job.Name, jobSubTaskID)
jobInfo := map[string]string{
JobNameKey: j.job.Name,
}
if service != nil {
jobName = jobNameFormat(service.ServiceName + "-" + service.ServiceModule + "-" + j.job.Name)
jobKey = strings.Join([]string{j.job.Name, service.ServiceName, service.ServiceModule}, ".")
jobDisplayName = genJobDisplayName(j.job.Name, service.ServiceName, service.ServiceModule)
jobKey = genJobKey(j.job.Name, service.ServiceName, service.ServiceModule)
jobInfo["service_name"] = service.ServiceName
jobInfo["service_module"] = service.ServiceModule
}
jobTask := &commonmodels.JobTask{
Name: jobName,
Key: jobKey,
Name: jobName,
DisplayName: jobDisplayName,
JobInfo: jobInfo,
JobType: string(config.JobFreestyle),
Spec: jobTaskSpec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,12 @@ func (j *GrafanaJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error) {
}

jobTask := &commonmodels.JobTask{
Name: j.job.Name,
Name: genJobName(j.workflow, j.job.Name, 0),
Key: genJobKey(j.job.Name),
DisplayName: genJobDisplayName(j.job.Name),
JobInfo: map[string]string{
JobNameKey: j.job.Name,
},
Key: j.job.Name,
JobType: string(config.JobGrafana),
Spec: &commonmodels.JobTaskGrafanaSpec{
ID: j.spec.ID,
Expand Down
Loading

0 comments on commit cd6d3d8

Please sign in to comment.