Skip to content

Commit

Permalink
fix pointer access to avoid unexpected array value error
Browse files Browse the repository at this point in the history
Signed-off-by: Min Min <[email protected]>
  • Loading branch information
jamsman94 committed Dec 30, 2024
1 parent 40c37c6 commit 5f39159
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
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 @@ -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"
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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
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
}

0 comments on commit 5f39159

Please sign in to comment.