Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cherrypick #3923 #3925

Merged
merged 6 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -318,26 +318,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 ""
Expand Down
2 changes: 1 addition & 1 deletion pkg/microservice/aslan/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ import (
client2 "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"
Expand Down
19 changes: 14 additions & 5 deletions pkg/microservice/aslan/core/workflow/service/workflow/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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, "<SERVICE>", serviceName)
env.Value = strings.ReplaceAll(env.Value, "<MODULE>", serviceModule)
env.Value = renderString(env.Value, setting.RenderValueTemplate, params)
}
}
return nil
return duplicatedEnvs, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,10 +558,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{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/util/deep_copy.go
Original file line number Diff line number Diff line change
@@ -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
}
161 changes: 0 additions & 161 deletions pkg/util/deepcopy/deepcopy.go

This file was deleted.

Loading