Skip to content

Commit

Permalink
fundamentally change how workflow is organized
Browse files Browse the repository at this point in the history
Signed-off-by: Min Min <[email protected]>
  • Loading branch information
jamsman94 committed Apr 1, 2024
1 parent 287e436 commit fa90003
Show file tree
Hide file tree
Showing 38 changed files with 479 additions and 383 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ type JobTaskDeploySpec struct {
Production bool `bson:"production" json:"production" yaml:"production"`
DeployContents []config.DeployContent `bson:"deploy_contents" json:"deploy_contents" yaml:"deploy_contents"`
KeyVals []*ServiceKeyVal `bson:"key_vals" json:"key_vals" yaml:"key_vals"` // deprecated since 1.18.0
VariableConfigs []*DeplopyVariableConfig `bson:"variable_configs" json:"variable_configs" yaml:"variable_configs"` // new since 1.18.0, only used for k8s
VariableConfigs []*DeployVariableConfig `bson:"variable_configs" json:"variable_configs" yaml:"variable_configs"` // new since 1.18.0, only used for k8s
VariableKVs []*commontypes.RenderVariableKV `bson:"variable_kvs" json:"variable_kvs" yaml:"variable_kvs"` // new since 1.18.0, only used for k8s
UpdateConfig bool `bson:"update_config" json:"update_config" yaml:"update_config"`
YamlContent string `bson:"yaml_content" json:"yaml_content" yaml:"yaml_content"`
Expand Down
66 changes: 35 additions & 31 deletions pkg/microservice/aslan/core/common/repository/models/workflow_v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,16 @@ type WorkflowServiceModule struct {
}

type CustomDeployJobSpec struct {
Namespace string `bson:"namespace" json:"namespace" yaml:"namespace"`
ClusterID string `bson:"cluster_id" json:"cluster_id" yaml:"cluster_id"`
ClusterOptions []*ClusterBrief `bson:"-" json:"cluster_options" yaml:"cluster_options"`
DockerRegistryID string `bson:"docker_registry_id" json:"docker_registry_id" yaml:"docker_registry_id"`
SkipCheckRunStatus bool `bson:"skip_check_run_status" json:"skip_check_run_status" yaml:"skip_check_run_status"`
Namespace string `bson:"namespace" json:"namespace" yaml:"namespace"`
ClusterID string `bson:"cluster_id" json:"cluster_id" yaml:"cluster_id"`
DockerRegistryID string `bson:"docker_registry_id" json:"docker_registry_id" yaml:"docker_registry_id"`
SkipCheckRunStatus bool `bson:"skip_check_run_status" json:"skip_check_run_status" yaml:"skip_check_run_status"`
// support two sources, runtime/fixed.
Source string `bson:"source" json:"source" yaml:"source"`
// unit is minute.
Timeout int64 `bson:"timeout" json:"timeout" yaml:"timeout"`
Targets []*DeployTargets `bson:"targets" json:"targets" yaml:"targets"`
Timeout int64 `bson:"timeout" json:"timeout" yaml:"timeout"`
Targets []*DeployTargets `bson:"targets" json:"targets" yaml:"targets"`
TargetOptions []*DeployTargets `bson:"-" json:"target_options" yaml:"target_options"`
}

type DeployTargets struct {
Expand Down Expand Up @@ -272,20 +272,19 @@ type ServiceAndBuild struct {
}

type ZadigDeployJobSpec struct {
Env string `bson:"env" yaml:"env" json:"env"`
EnvOptions []string `bson:"-" yaml:"env_options" json:"env_options"`
Production bool `bson:"production" yaml:"production" json:"production"`
DeployType string `bson:"deploy_type" yaml:"deploy_type,omitempty" json:"deploy_type"`
SkipCheckRunStatus bool `bson:"skip_check_run_status" yaml:"skip_check_run_status" json:"skip_check_run_status"`
Env string `bson:"env" yaml:"env" json:"env"`
EnvOptions []*ZadigDeployEnvInformation `bson:"-" yaml:"env_options" json:"env_options"`
Production bool `bson:"production" yaml:"production" json:"production"`
DeployType string `bson:"deploy_type" yaml:"deploy_type,omitempty" json:"deploy_type"`
SkipCheckRunStatus bool `bson:"skip_check_run_status" yaml:"skip_check_run_status" json:"skip_check_run_status"`
// fromjob/runtime, runtime 表示运行时输入,fromjob 表示从上游构建任务中获取
Source config.DeploySourceType `bson:"source" yaml:"source" json:"source"`
DeployContents []config.DeployContent `bson:"deploy_contents" yaml:"deploy_contents" json:"deploy_contents"`
// 当 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"`
Services []*DeployServiceInfo `bson:"services" yaml:"services" json:"services"`
ServiceOptions []*DeployServiceInfo `bson:"-" yaml:"service_options" json:"service_options"`
OriginJobName string `bson:"origin_job_name" yaml:"origin_job_name" json:"origin_job_name"`
Services []*DeployServiceInfo `bson:"services" yaml:"services" json:"services"`
}

type ServiceAndVMDeploy struct {
Expand Down Expand Up @@ -331,7 +330,7 @@ type DeployHelmChart struct {
type DeployService struct {
ServiceName string `bson:"service_name" yaml:"service_name" json:"service_name"`
// VariableConfigs added since 1.18
VariableConfigs []*DeplopyVariableConfig `bson:"variable_configs" json:"variable_configs" yaml:"variable_configs"`
VariableConfigs []*DeployVariableConfig `bson:"variable_configs" json:"variable_configs" yaml:"variable_configs"`
// VariableKVs added since 1.18
VariableKVs []*commontypes.RenderVariableKV `bson:"variable_kvs" yaml:"variable_kvs" json:"variable_kvs"`
// LatestVariableKVs added since 1.18
Expand All @@ -347,13 +346,15 @@ type DeployService struct {
}

type DeployServiceInfo struct {
ServiceName string `bson:"service_name" yaml:"service_name" json:"service_name"`
VariableConfigs []*DeplopyVariableConfig `bson:"variable_configs" yaml:"variable_configs" json:"variable_configs"`
ServiceName string `bson:"service_name" yaml:"service_name" json:"service_name"`
// VariableConfigs used to determine if a variable is visible to the workflow user.
VariableConfigs []*DeployVariableConfig `bson:"variable_configs" yaml:"variable_configs,omitempty" json:"variable_configs,omitempty"`
VariableKVs []*commontypes.RenderVariableKV `bson:"variable_kvs" yaml:"variable_kvs" json:"variable_kvs"`
LatestVariableKVs []*commontypes.RenderVariableKV `bson:"latest_variable_kvs" yaml:"latest_variable_kvs" json:"latest_variable_kvs"`
VariableYaml string `bson:"variable_yaml" yaml:"variable_yaml" json:"variable_yaml"`
UpdateConfig bool `bson:"update_config" yaml:"update_config" json:"update_config"`
Updatable bool `bson:"updatable" yaml:"updatable" json:"updatable"`
Updatable bool `bson:"-" yaml:"updatable" json:"updatable"`
Deployed bool `bson:"-" yaml:"deployed" json:"deployed"`
Modules []*DeployModuleInfo `bson:"modules" yaml:"modules" json:"modules"`
// Deprecated since 1.18
KeyVals []*ServiceKeyVal `bson:"key_vals" yaml:"key_vals" json:"key_vals"`
Expand All @@ -366,7 +367,7 @@ type DeployModuleInfo struct {
ImageName string `bson:"image_name" yaml:"image_name" json:"image_name"`
}

type DeplopyVariableConfig struct {
type DeployVariableConfig struct {
VariableKey string `bson:"variable_key" json:"variable_key" yaml:"variable_key"`
UseGlobalVariable bool `bson:"use_global_variable" json:"use_global_variable" yaml:"use_global_variable"`
}
Expand Down Expand Up @@ -397,12 +398,11 @@ type ZadigDistributeImageJobSpec struct {
Targets []*DistributeTarget `bson:"targets" json:"targets" yaml:"targets"`
TargetOptions []*DistributeTarget `bson:"target_options" json:"target_options" yaml:"target_options"`
// unit is minute.
Timeout int64 `bson:"timeout" json:"timeout" yaml:"timeout"`
ClusterID string `bson:"cluster_id" json:"cluster_id" yaml:"cluster_id"`
ClusterOptions []*ClusterBrief `bson:"-" json:"cluster_options" yaml:"cluster_options"`
StrategyID string `bson:"strategy_id" json:"strategy_id" yaml:"strategy_id"`
EnableTargetImageTagRule bool `bson:"enable_target_image_tag_rule" json:"enable_target_image_tag_rule" yaml:"enable_target_image_tag_rule"`
TargetImageTagRule string `bson:"target_image_tag_rule" json:"target_image_tag_rule" yaml:"target_image_tag_rule"`
Timeout int64 `bson:"timeout" json:"timeout" yaml:"timeout"`
ClusterID string `bson:"cluster_id" json:"cluster_id" yaml:"cluster_id"`
StrategyID string `bson:"strategy_id" json:"strategy_id" yaml:"strategy_id"`
EnableTargetImageTagRule bool `bson:"enable_target_image_tag_rule" json:"enable_target_image_tag_rule" yaml:"enable_target_image_tag_rule"`
TargetImageTagRule string `bson:"target_image_tag_rule" json:"target_image_tag_rule" yaml:"target_image_tag_rule"`
}

type DistributeTarget struct {
Expand Down Expand Up @@ -520,10 +520,10 @@ type BlueGreenTarget struct {

type CanaryDeployJobSpec struct {
ClusterID string `bson:"cluster_id" json:"cluster_id" yaml:"cluster_id"`
ClusterOptions []*ClusterBrief `bson:"-" json:"cluster_options" yaml:"cluster_options"`
Namespace string `bson:"namespace" json:"namespace" yaml:"namespace"`
DockerRegistryID string `bson:"docker_registry_id" json:"docker_registry_id" yaml:"docker_registry_id"`
Targets []*CanaryTarget `bson:"targets" json:"targets" yaml:"targets"`
TargetOptions []*CanaryTarget `bson:"target_options" json:"target_options" yaml:"target_options"`
}

type CanaryReleaseJobSpec struct {
Expand Down Expand Up @@ -563,10 +563,14 @@ type GrayReleaseTarget struct {
}

type K8sPatchJobSpec struct {
ClusterID string `bson:"cluster_id" json:"cluster_id" yaml:"cluster_id"`
ClusterOptions []*ClusterBrief `bson:"-" json:"cluster_options" yaml:"cluster_options"`
Namespace string `bson:"namespace" json:"namespace" yaml:"namespace"`
PatchItems []*PatchItem `bson:"patch_items" json:"patch_items" yaml:"patch_items"`
ClusterID string `bson:"cluster_id" json:"cluster_id" yaml:"cluster_id"`
Namespace string `bson:"namespace" json:"namespace" yaml:"namespace"`
PatchItems []*PatchItem `bson:"patch_items" json:"patch_items" yaml:"patch_items"`
}

type ZadigDeployEnvInformation struct {
Env string `json:"env" yaml:"env"`
Services []*DeployServiceInfo `json:"services" yaml:"services"`
}

type ClusterBrief struct {
Expand Down
11 changes: 11 additions & 0 deletions pkg/microservice/aslan/core/workflow/service/workflow/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ var (

type JobCtl interface {
Instantiate() error
// SetPreset sets all the default values configured by user
SetPreset() error
// SetOptions sets all the possible options for the workflow
SetOptions() error
ToJobs(taskID int64) ([]*commonmodels.JobTask, error)
MergeArgs(args *commonmodels.Job) error
LintJob() error
Expand Down Expand Up @@ -154,6 +157,14 @@ func SetPreset(job *commonmodels.Job, workflow *commonmodels.WorkflowV4) error {
return jobCtl.SetPreset()
}

func SetOptions(job *commonmodels.Job, workflow *commonmodels.WorkflowV4) error {
jobCtl, err := InitJobCtl(job, workflow)
if err != nil {
return warpJobError(job.Name, err)
}
return jobCtl.SetOptions()
}

func JobPresetSkiped(job *commonmodels.Job) {
if job.RunPolicy == config.ForceRun {
job.Skipped = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func (j *ApolloJob) SetPreset() error {
return nil
}

func (j *ApolloJob) SetOptions() error {
return nil
}

func (j *ApolloJob) MergeArgs(args *commonmodels.Job) error {
j.spec = &commonmodels.ApolloJobSpec{}
if err := commonmodels.IToi(args.Spec, j.spec); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package job

import (
Expand Down Expand Up @@ -161,6 +162,10 @@ func (j *BlueGreenDeployV2Job) SetPreset() error {
return nil
}

func (j *BlueGreenDeployV2Job) SetOptions() error {
return nil
}

func (j *BlueGreenDeployV2Job) MergeArgs(args *commonmodels.Job) error {
if j.job.Name == args.Name && j.job.JobType == args.JobType {
j.spec = &commonmodels.BlueGreenDeployV2JobSpec{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (j *BlueGreenReleaseV2Job) SetPreset() error {
return nil
}

func (j *BlueGreenReleaseV2Job) SetOptions() error {
return nil
}

func (j *BlueGreenReleaseV2Job) MergeArgs(args *commonmodels.Job) error {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,25 @@ func (j *BuildJob) Instantiate() error {
return nil
}

// SetPreset will now update all the possible build service option into serviceOption instead of ServiceAndBuilds
// Updated @2023-03-30 before v1.17.0
// Updated @2023-04-07 after v1.17.0 revert to old version
// SetPreset will clear the selected field (ServiceAndBuilds
func (j *BuildJob) SetPreset() error {
j.spec = &commonmodels.ZadigBuildJobSpec{}
if err := commonmodels.IToi(j.job.Spec, j.spec); err != nil {
return err
}

chosenObject := make([]*commonmodels.ServiceAndBuild, 0)

j.spec.ServiceAndBuilds = chosenObject
j.job.Spec = j.spec
return nil
}

func (j *BuildJob) SetOptions() error {
j.spec = &commonmodels.ZadigBuildJobSpec{}
if err := commonmodels.IToi(j.job.Spec, j.spec); err != nil {
return err
}

servicesMap, err := repository.GetMaxRevisionsServicesMap(j.workflow.Project, false)
if err != nil {
Expand Down Expand Up @@ -128,7 +138,7 @@ func (j *BuildJob) SetPreset() error {

newBuilds = append(newBuilds, build)
}
j.spec.ServiceAndBuilds = newBuilds

j.spec.ServiceAndBuildsOptions = newBuilds
j.job.Spec = j.spec
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"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"
e "github.com/koderover/zadig/v2/pkg/tool/errors"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -56,34 +55,14 @@ func (j *CanaryDeployJob) SetPreset() error {
return err
}

clusters, err := commonrepo.NewK8SClusterColl().List(&commonrepo.ClusterListOpts{})
if err != nil {
return fmt.Errorf("failed to list clusters, error: %s", err)
}
options := make([]*commonmodels.ClusterBrief, 0)
for _, cluster := range clusters {
options = append(options, &commonmodels.ClusterBrief{
ClusterID: cluster.ID.Hex(),
ClusterName: cluster.Name,
})

strategies := make([]*commonmodels.ClusterStrategyBrief, 0)

if cluster.AdvancedConfig != nil {
for _, strategy := range cluster.AdvancedConfig.ScheduleStrategy {
strategies = append(strategies, &commonmodels.ClusterStrategyBrief{
StrategyID: strategy.StrategyID,
StrategyName: strategy.StrategyName,
})
}
}
}

j.spec.ClusterOptions = options
j.job.Spec = j.spec
return nil
}

func (j *CanaryDeployJob) SetOptions() error {
return nil
}

func (j *CanaryDeployJob) MergeArgs(args *commonmodels.Job) error {
if j.job.Name == args.Name && j.job.JobType == args.JobType {
j.spec = &commonmodels.CanaryDeployJobSpec{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (j *CanaryReleaseJob) SetPreset() error {
return nil
}

func (j *CanaryReleaseJob) SetOptions() error {
return nil
}

func (j *CanaryReleaseJob) MergeArgs(args *commonmodels.Job) error {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ limitations under the License.
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"
commonrepo "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/mongodb"
"github.com/koderover/zadig/v2/pkg/tool/log"
)

Expand All @@ -47,34 +45,15 @@ func (j *CustomDeployJob) SetPreset() error {
return err
}

clusters, err := commonrepo.NewK8SClusterColl().List(&commonrepo.ClusterListOpts{})
if err != nil {
return fmt.Errorf("failed to list cluster info for custom job preset options")
}
clusterList := make([]*commonmodels.ClusterBrief, 0)
for _, cluster := range clusters {
clusterList = append(clusterList, &commonmodels.ClusterBrief{
ClusterID: cluster.ID.Hex(),
ClusterName: cluster.Name,
})

strategies := make([]*commonmodels.ClusterStrategyBrief, 0)

if cluster.AdvancedConfig != nil {
for _, strategy := range cluster.AdvancedConfig.ScheduleStrategy {
strategies = append(strategies, &commonmodels.ClusterStrategyBrief{
StrategyID: strategy.StrategyID,
StrategyName: strategy.StrategyName,
})
}
}
}

j.spec.ClusterOptions = clusterList
j.spec.TargetOptions = j.spec.Targets
j.job.Spec = j.spec
return nil
}

func (j *CustomDeployJob) SetOptions() error {
return nil
}

func (j *CustomDeployJob) MergeArgs(args *commonmodels.Job) error {
if j.job.Name == args.Name && j.job.JobType == args.JobType {
j.spec = &commonmodels.CustomDeployJobSpec{}
Expand Down
Loading

0 comments on commit fa90003

Please sign in to comment.