diff --git a/pkg/microservice/aslan/core/common/repository/models/workflow_v4.go b/pkg/microservice/aslan/core/common/repository/models/workflow_v4.go index cb46440513..030fab1024 100644 --- a/pkg/microservice/aslan/core/common/repository/models/workflow_v4.go +++ b/pkg/microservice/aslan/core/common/repository/models/workflow_v4.go @@ -319,26 +319,6 @@ type FreeStyleServiceInfo struct { KeyVals []*KeyVal `bson:"key_vals" yaml:"key_vals" json:"key_vals"` } -func (j *FreeStyleServiceInfo) DeepCopyKeyVals() []*KeyVal { - envs := make([]*KeyVal, 0) - - for _, env := range j.KeyVals { - choiceOption := make([]string, 0) - for _, choice := range env.ChoiceOption { - choiceOption = append(choiceOption, choice) - } - envs = append(envs, &KeyVal{ - Key: env.Key, - Value: env.Value, - Type: env.Type, - RegistryID: env.RegistryID, - ChoiceOption: choiceOption, - IsCredential: env.IsCredential, - }) - } - return envs -} - func (i *FreeStyleServiceInfo) GetKey() string { if i == nil { return "" diff --git a/pkg/microservice/aslan/core/service.go b/pkg/microservice/aslan/core/service.go index adba2876b7..7f881797ee 100644 --- a/pkg/microservice/aslan/core/service.go +++ b/pkg/microservice/aslan/core/service.go @@ -34,12 +34,12 @@ import ( controllerRuntimeClient "sigs.k8s.io/controller-runtime/pkg/client" commonconfig "github.com/koderover/zadig/v2/pkg/config" - commonutil "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/util" configbase "github.com/koderover/zadig/v2/pkg/config" "github.com/koderover/zadig/v2/pkg/microservice/aslan/config" "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/service/kube" "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/service/webhook" "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/service/workflowcontroller" + commonutil "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/util" environmentservice "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/environment/service" multiclusterservice "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/multicluster/service" releaseplanservice "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/release_plan/service" diff --git a/pkg/microservice/aslan/core/workflow/service/workflow/job/job.go b/pkg/microservice/aslan/core/workflow/service/workflow/job/job.go index a36a7371cb..307dc063d3 100644 --- a/pkg/microservice/aslan/core/workflow/service/workflow/job/job.go +++ b/pkg/microservice/aslan/core/workflow/service/workflow/job/job.go @@ -25,6 +25,7 @@ import ( "strings" "time" + "github.com/koderover/zadig/v2/pkg/util" "github.com/mozillazg/go-pinyin" "github.com/pkg/errors" "go.uber.org/zap" @@ -922,23 +923,31 @@ func findMatchedRepoFromParams(params []*commonmodels.Param, paramName string) ( return nil, fmt.Errorf("not found repo from params") } -func renderServiceVariables(workflow *commonmodels.WorkflowV4, envs []*commonmodels.KeyVal, serviceName string, serviceModule string) error { +func renderServiceVariables(workflow *commonmodels.WorkflowV4, envs []*commonmodels.KeyVal, serviceName string, serviceModule string) ([]*commonmodels.KeyVal, error) { + duplicatedEnvs := make([]*commonmodels.KeyVal, 0) + + err := util.DeepCopy(duplicatedEnvs, envs) + if err != nil { + return nil, err + } + if serviceName == "" || serviceModule == "" { - return nil + return duplicatedEnvs, nil } params, err := getWorkflowStageParams(workflow) if err != nil { err = fmt.Errorf("failed to get workflow stage parameters, error: %s", err) - return err + return nil, err } - for _, env := range envs { + + for _, env := range duplicatedEnvs { if strings.HasPrefix(env.Value, "{{.") && strings.HasSuffix(env.Value, "}}") { env.Value = strings.ReplaceAll(env.Value, "", serviceName) env.Value = strings.ReplaceAll(env.Value, "", serviceModule) env.Value = renderString(env.Value, setting.RenderValueTemplate, params) } } - return nil + return duplicatedEnvs, nil } diff --git a/pkg/microservice/aslan/core/workflow/service/workflow/job/job_freestyle.go b/pkg/microservice/aslan/core/workflow/service/workflow/job/job_freestyle.go index da7bd5c68c..9a518f91a9 100644 --- a/pkg/microservice/aslan/core/workflow/service/workflow/job/job_freestyle.go +++ b/pkg/microservice/aslan/core/workflow/service/workflow/job/job_freestyle.go @@ -429,10 +429,12 @@ func (j *FreeStyleJob) toJob(taskID int64, jobSubTaskID int, registries []*commo } if service != nil { - err = renderServiceVariables(j.workflow, jobTaskSpec.Properties.Envs, service.ServiceName, service.ServiceModule) + renderedEnvs, err := renderServiceVariables(j.workflow, jobTaskSpec.Properties.Envs, service.ServiceName, service.ServiceModule) if err != nil { return nil, fmt.Errorf("failed to render service variables, error: %v", err) } + + jobTaskSpec.Properties.Envs = renderedEnvs } jobTaskSpec.Properties.CustomEnvs = jobTaskSpec.Properties.Envs diff --git a/pkg/microservice/aslan/core/workflow/service/workflow/job/job_scanning.go b/pkg/microservice/aslan/core/workflow/service/workflow/job/job_scanning.go index 17611127b2..4058e0aacd 100644 --- a/pkg/microservice/aslan/core/workflow/service/workflow/job/job_scanning.go +++ b/pkg/microservice/aslan/core/workflow/service/workflow/job/job_scanning.go @@ -560,10 +560,12 @@ func (j *ScanningJob) toJobTask(jobSubTaskID int, scanning *commonmodels.Scannin } if scanningType == string(config.ServiceScanningType) { - err = renderServiceVariables(j.workflow, jobTaskSpec.Properties.Envs, serviceName, serviceModule) + renderedEnv, err := renderServiceVariables(j.workflow, jobTaskSpec.Properties.Envs, serviceName, serviceModule) if err != nil { return nil, fmt.Errorf("failed to render service variables, error: %v", err) } + + jobTaskSpec.Properties.Envs = renderedEnv } cacheS3 := &commonmodels.S3Storage{} diff --git a/pkg/microservice/aslan/core/workflow/service/workflow/job/job_testing.go b/pkg/microservice/aslan/core/workflow/service/workflow/job/job_testing.go index 19a68ce037..fb5e5ec95b 100644 --- a/pkg/microservice/aslan/core/workflow/service/workflow/job/job_testing.go +++ b/pkg/microservice/aslan/core/workflow/service/workflow/job/job_testing.go @@ -450,7 +450,7 @@ func (j *TestingJob) toJobtask(jobSubTaskID int, testing *commonmodels.TestModul "service_module": serviceModule, } - err = renderServiceVariables(j.workflow, customEnvs, serviceName, serviceModule) + customEnvs, err = renderServiceVariables(j.workflow, customEnvs, serviceName, serviceModule) if err != nil { return nil, fmt.Errorf("failed to render service variables, error: %v", err) } diff --git a/pkg/util/deep_copy.go b/pkg/util/deep_copy.go new file mode 100644 index 0000000000..39e6c8b3c7 --- /dev/null +++ b/pkg/util/deep_copy.go @@ -0,0 +1,40 @@ +/* +Copyright 2024 The KodeRover Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package util + +import ( + "encoding/json" + "fmt" +) + +func DeepCopy(dst interface{}, src interface{}) error { + if dst == nil { + return fmt.Errorf("dst cannot be nil") + } + if src == nil { + return fmt.Errorf("src cannot be nil") + } + bytes, err := json.Marshal(src) + if err != nil { + return fmt.Errorf("unable to marshal src: %w", err) + } + err = json.Unmarshal(bytes, dst) + if err != nil { + return fmt.Errorf("unable to unmarshal into dst: %w", err) + } + return nil +}