Skip to content

Commit

Permalink
improve dynamic workflow variable feature
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zhao <[email protected]>
  • Loading branch information
PetrusZ committed Nov 7, 2024
1 parent 3e3e888 commit 038b4a7
Show file tree
Hide file tree
Showing 24 changed files with 837 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -521,11 +521,25 @@ type ServiceAndTest struct {
TestModule `bson:",inline" yaml:",inline" json:",inline"`
}

func (s *ServiceAndTest) GetKey() string {
if s == nil {
return ""
}
return s.ServiceName + "-" + s.ServiceModule
}

type ServiceTestTarget struct {
ServiceName string `bson:"service_name" yaml:"service_name" json:"service_name"`
ServiceModule string `bson:"service_module" yaml:"service_module" json:"service_module"`
}

func (s *ServiceTestTarget) GetKey() string {
if s == nil {
return ""
}
return s.ServiceName + "-" + s.ServiceModule
}

type TestModule struct {
Name string `bson:"name" yaml:"name" json:"name"`
ProjectName string `bson:"project_name" yaml:"project_name" json:"project_name"`
Expand Down Expand Up @@ -554,6 +568,13 @@ type ServiceAndScannings struct {
ScanningModule `bson:",inline" yaml:",inline" json:",inline"`
}

func (s *ServiceAndScannings) GetKey() string {
if s == nil {
return ""
}
return s.ServiceName + "-" + s.ServiceModule
}

type ScanningModule struct {
Name string `bson:"name" yaml:"name" json:"name"`
ProjectName string `bson:"project_name" yaml:"project_name" json:"project_name"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/microservice/aslan/core/common/service/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func GetChartValues(projectName, envName, serviceName string, isHelmChartDeploy
serviceMap := prod.GetServiceMap()
prodSvc, ok := serviceMap[serviceName]
if !ok {
return nil, fmt.Errorf("failed to find sercice: %s in env: %s", serviceName, envName)
return nil, fmt.Errorf("failed to find service: %s in env: %s", serviceName, envName)
}

revisionSvc, err := repository.QueryTemplateService(&commonrepo.ServiceFindOption{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c *BlueGreenDeployJobCtl) run(ctx context.Context) error {
c.ack()
service.Spec.Selector[config.BlueGreenVersionLabelName] = config.OriginVersion
if err := updater.CreateOrPatchService(service, c.kubeClient); err != nil {
msg := fmt.Sprintf("add origin label selector to serivce: %s error: %v", c.jobTaskSpec.K8sServiceName, err)
msg := fmt.Sprintf("add origin label selector to service: %s error: %v", c.jobTaskSpec.K8sServiceName, err)
logError(c.job, msg, c.logger)
c.jobTaskSpec.Events.Error(msg)
return errors.New(msg)
Expand All @@ -146,7 +146,7 @@ func (c *BlueGreenDeployJobCtl) run(ctx context.Context) error {
if _, ok := service.Spec.Selector[config.BlueGreenVersionLabelName]; !ok {
service.Spec.Selector[config.BlueGreenVersionLabelName] = previousLabel
if err := updater.CreateOrPatchService(service, c.kubeClient); err != nil {
msg := fmt.Sprintf("add label selector to serivce: %s error: %v", c.jobTaskSpec.K8sServiceName, err)
msg := fmt.Sprintf("add label selector to service: %s error: %v", c.jobTaskSpec.K8sServiceName, err)
logError(c.job, msg, c.logger)
c.jobTaskSpec.Events.Error(msg)
return errors.New(msg)
Expand All @@ -170,7 +170,7 @@ func (c *BlueGreenDeployJobCtl) run(ctx context.Context) error {
blueService.ObjectMeta.ResourceVersion = ""

if err := updater.CreateOrPatchService(blueService, c.kubeClient); err != nil {
msg := fmt.Sprintf("create blue serivce: %s error: %v", c.jobTaskSpec.BlueK8sServiceName, err)
msg := fmt.Sprintf("create blue service: %s error: %v", c.jobTaskSpec.BlueK8sServiceName, err)
logError(c.job, msg, c.logger)
c.jobTaskSpec.Events.Error(msg)
return errors.New(msg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (c *BlueGreenDeployV2JobCtl) run(ctx context.Context) error {
// green service selector add original version label
greenService.Spec.Selector[config.BlueGreenVersionLabelName] = config.OriginVersion
if err := updater.CreateOrPatchService(greenService, c.kubeClient); err != nil {
msg := fmt.Sprintf("add origin label selector to green serivce: %s error: %v", greenService.Name, err)
msg := fmt.Sprintf("add origin label selector to green service: %s error: %v", greenService.Name, err)
logError(c.job, msg, c.logger)
c.jobTaskSpec.Events.Error(msg)
return errors.New(msg)
Expand All @@ -166,12 +166,12 @@ func (c *BlueGreenDeployV2JobCtl) run(ctx context.Context) error {
}
service.Namespace = c.namespace
if err := c.kubeClient.Create(ctx, service); err != nil {
msg := fmt.Sprintf("create blue serivce: %s error: %v", c.jobTaskSpec.Service.ServiceName, err)
msg := fmt.Sprintf("create blue service: %s error: %v", c.jobTaskSpec.Service.ServiceName, err)
logError(c.job, msg, c.logger)
c.jobTaskSpec.Events.Error(msg)
return errors.New(msg)
}
c.jobTaskSpec.Events.Info(fmt.Sprintf("create blue serivce: %s success", service.Name))
c.jobTaskSpec.Events.Info(fmt.Sprintf("create blue service: %s success", service.Name))

// create blue deployment
deployment := &v1.Deployment{}
Expand Down
6 changes: 3 additions & 3 deletions pkg/microservice/aslan/core/common/types/service_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,14 @@ func MergeServiceAndServiceTemplateVariableKVs(service []*ServiceVariableKV, ser
// merge render variables base on service template variables
// render variables has higher value priority, service template variables has higher type priority
// normally used to get latest variables for a service
func MergeRenderAndServiceTemplateVariableKVs(render []*RenderVariableKV, serivceTemplate []*ServiceVariableKV) (string, []*RenderVariableKV, error) {
func MergeRenderAndServiceTemplateVariableKVs(render []*RenderVariableKV, serviceTemplate []*ServiceVariableKV) (string, []*RenderVariableKV, error) {
renderMap := map[string]*RenderVariableKV{}
for _, kv := range render {
renderMap[kv.Key] = kv
}

ret := []*RenderVariableKV{}
for _, kv := range serivceTemplate {
for _, kv := range serviceTemplate {
if renderKV, ok := renderMap[kv.Key]; !ok {
ret = append(ret, &RenderVariableKV{
ServiceVariableKV: *kv,
Expand Down Expand Up @@ -546,7 +546,7 @@ func RemoveGlobalVariableRelatedService(globalVariableKVs []*GlobalVariableKV, s
return globalVariableKVs
}

// update global variable's related serivces and render variables value base on useGlobalVariable flag
// update global variable's related services and render variables value base on useGlobalVariable flag
func UpdateGlobalVariableKVs(serviceName string, globalVariables []*GlobalVariableKV, argVariables, currentVariables []*RenderVariableKV) ([]*GlobalVariableKV, []*RenderVariableKV, error) {
globalVariableMap := map[string]*GlobalVariableKV{}
for _, kv := range globalVariables {
Expand Down
2 changes: 1 addition & 1 deletion pkg/microservice/aslan/core/environment/handler/pm_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func ConnectSshPmExec(c *gin.Context) {
// @Param hostId query string true "host id"
// @Param commandType query service.VmServiceCommandType true "vm service command type"
// @Param name path string true "env name"
// @Param serviceName path string true "serivce name"
// @Param serviceName path string true "service name"
// @success 200 {object} service.ExecVmServiceCommandResponse
// @Router /api/aslan/environment/environments/{name}/services/{serviceName}/execmd [post]
func ExecVmServiceCommand(c *gin.Context) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/microservice/aslan/core/environment/service/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ func ListCustomWorkload(clusterID, namespace string, log *zap.SugaredLogger) ([]
return resp, nil
}

// list serivce and matched deployment containers for canary and blue-green deployment.
// list service and matched deployment containers for canary and blue-green deployment.
func ListCanaryDeploymentServiceInfo(clusterID, namespace string, log *zap.SugaredLogger) ([]*ServiceMatchedDeploymentContainers, error) {
resp := []*ServiceMatchedDeploymentContainers{}
kubeClient, err := kubeclient.GetKubeClient(config.HubServerAddress(), clusterID)
Expand Down
8 changes: 4 additions & 4 deletions pkg/microservice/aslan/core/environment/service/pm_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func ExecVmServiceCommand(projectName, envName, serviceName, hostId string, comm
host.Port = setting.PMHostDefaultPort
}

serivce, err := commonrepo.NewServiceColl().Find(&commonrepo.ServiceFindOption{ServiceName: serviceName, ProductName: projectName})
service, err := commonrepo.NewServiceColl().Find(&commonrepo.ServiceFindOption{ServiceName: serviceName, ProductName: projectName})
if err != nil {
err = fmt.Errorf("Service.Find service %s error: %s", serviceName, err)
return nil, e.ErrLoginPm.AddErr(err)
Expand All @@ -159,11 +159,11 @@ func ExecVmServiceCommand(projectName, envName, serviceName, hostId string, comm
cmd := ""
switch commandType {
case VmServiceCommandTypeStart:
cmd = serivce.StartCmd
cmd = service.StartCmd
case VmServiceCommandTypeStop:
cmd = serivce.StopCmd
cmd = service.StopCmd
case VmServiceCommandTypeRestart:
cmd = serivce.RestartCmd
cmd = service.RestartCmd
default:
err = fmt.Errorf("unknown command type %s", commandType)
log.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/microservice/aslan/core/environment/service/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func RollbackEnvServiceVersion(ctx *internalhandler.Context, projectName, envNam
// // in needToAddValues, but not in service's chart values, add it to mergedValues and set value from needToAddValues
// mergedValuesFlatMap[key] = value
// } else {
// // in needToAddValues, and in service's chart values, add it to mergedValues and set value from template serivce values
// // in needToAddValues, and in service's chart values, add it to mergedValues and set value from template service values
// mergedValuesFlatMap[key] = v
// }
// }
Expand Down
10 changes: 2 additions & 8 deletions pkg/microservice/aslan/core/workflow/handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ func (*Router) Inject(router *gin.RouterGroup) {
workflowV4.PUT("/:name", UpdateWorkflowV4)
workflowV4.DELETE("/:name", DeleteWorkflowV4)
workflowV4.GET("/preset/:name", GetWorkflowV4Preset)
workflowV4.POST("/dynamicVariable/available", GetWorkflowV4DynamicVariableAvailable)
workflowV4.POST("/dynamicVariable/render", RenderWorkflowV4DynamicVariables)
workflowV4.GET("/webhook/preset", GetWebhookForWorkflowV4Preset)
workflowV4.GET("/webhook", ListWebhookForWorkflowV4)
workflowV4.POST("/webhook/:workflowName", CreateWebhookForWorkflowV4)
Expand Down Expand Up @@ -267,14 +269,6 @@ func (*Router) Inject(router *gin.RouterGroup) {
plugin.GET("", ListUnofficalPluginRepositories)
plugin.DELETE("/:id", DeletePluginRepo)
}

// ---------------------------------------------------------------------------------------
// utility apis
// ---------------------------------------------------------------------------------------
util := router.Group("utils")
{
util.POST("/renderVariables", RenderWorkflowVariables)
}
}

type OpenAPIRouter struct{}
Expand Down
47 changes: 0 additions & 47 deletions pkg/microservice/aslan/core/workflow/handler/util.go

This file was deleted.

53 changes: 53 additions & 0 deletions pkg/microservice/aslan/core/workflow/handler/workflow_v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,59 @@ func FindWorkflowV4(c *gin.Context) {
c.YAML(200, resp)
}

// @Summary Render Workflow V4 Variables
// @Description Render Workflow V4 Variables
// @Tags workflow
// @Accept json
// @Produce json
// @Param jobName query string true "job name"
// @Param serviceName query string false "service name"
// @Param moduleName query string false "service module name"
// @Param key query string true "render variable key"
// @Param body body commonmodels.WorkflowV4 true "body"
// @Success 200 {array} string
// @Router /api/aslan/workflow/v4/dynamicVariable/render [post]
func RenderWorkflowV4DynamicVariables(c *gin.Context) {
ctx := internalhandler.NewContext(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()

args := new(commonmodels.WorkflowV4)
data := getBody(c)
if err := json.Unmarshal([]byte(data), args); err != nil {
err = fmt.Errorf("RenderWorkflowV4Variables json.Unmarshal err : %s", err)
ctx.Logger.Error(err)
ctx.RespErr = e.ErrInvalidParam.AddErr(err)
return
}

ctx.Resp, ctx.RespErr = workflow.RenderWorkflowV4Variables(ctx, args, c.Query("jobName"), c.Query("serviceName"), c.Query("moduleName"), c.Query("key"))
}

// @Summary Get Workflow V4 Dynamic Variable's Available Variables
// @Description Get Workflow V4 Dynamic Variable's Available Variables
// @Tags workflow
// @Accept json
// @Produce json
// @Param jobName query string true "job name"
// @Param body body commonmodels.WorkflowV4 true "body"
// @Success 200 {array} string
// @Router /api/aslan/workflow/v4/dynamicVariable/available [post]
func GetWorkflowV4DynamicVariableAvailable(c *gin.Context) {
ctx := internalhandler.NewContext(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()

args := new(commonmodels.WorkflowV4)
data := getBody(c)
if err := json.Unmarshal([]byte(data), args); err != nil {
err = fmt.Errorf("RenderWorkflowV4Variables json.Unmarshal err : %s", err)
ctx.Logger.Error(err)
ctx.RespErr = e.ErrInvalidParam.AddErr(err)
return
}

ctx.Resp, ctx.RespErr = workflow.GetWorkflowV4DynamicVariableAvailable(ctx, args, c.Query("jobName"))
}

func GetWorkflowV4Preset(c *gin.Context) {
ctx := internalhandler.NewContext(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()
Expand Down
Loading

0 comments on commit 038b4a7

Please sign in to comment.