Skip to content

Commit 289f54b

Browse files
committed
fix helm import values were merged
Signed-off-by: Patrick Zhao <[email protected]>
1 parent fe5bba4 commit 289f54b

File tree

2 files changed

+45
-70
lines changed

2 files changed

+45
-70
lines changed

pkg/microservice/aslan/core/environment/handler/renderset.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package handler
1818

1919
import (
2020
"fmt"
21-
"strings"
2221

2322
"github.com/koderover/zadig/v2/pkg/types"
2423

@@ -255,13 +254,12 @@ func GetYamlContent(c *gin.Context) {
255254
return
256255
}
257256

258-
if len(arg.ValuesPaths) == 0 {
259-
ctx.RespErr = e.ErrInvalidParam.AddDesc("paths can't be empty")
257+
if len(arg.ValuesPath) == 0 {
258+
ctx.RespErr = e.ErrInvalidParam.AddDesc("values path can't be empty")
260259
return
261260
}
262261

263-
pathArr := strings.Split(arg.ValuesPaths, ",")
264-
ctx.Resp, ctx.RespErr = service.GetMergedYamlContent(arg, pathArr)
262+
ctx.Resp, ctx.RespErr = service.GetMergedYamlContent(arg)
265263
}
266264

267265
type getGlobalVariablesRespone struct {

pkg/microservice/aslan/core/environment/service/renderset.go

Lines changed: 42 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,24 @@ import (
2020
"fmt"
2121
"os"
2222
"path"
23-
"sync"
2423

25-
"github.com/hashicorp/go-multierror"
2624
"github.com/pkg/errors"
2725
"go.mongodb.org/mongo-driver/mongo"
2826
"go.uber.org/zap"
27+
"sigs.k8s.io/yaml"
2928

3029
"github.com/koderover/zadig/v2/pkg/microservice/aslan/config"
3130
templatemodels "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models/template"
3231
commonrepo "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/mongodb"
3332
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/service"
3433
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/service/command"
3534
fsservice "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/service/fs"
35+
helmservice "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/service/helm"
3636
commontypes "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/types"
3737
"github.com/koderover/zadig/v2/pkg/setting"
3838
"github.com/koderover/zadig/v2/pkg/shared/client/systemconfig"
3939
e "github.com/koderover/zadig/v2/pkg/tool/errors"
4040
"github.com/koderover/zadig/v2/pkg/tool/log"
41-
yamlutil "github.com/koderover/zadig/v2/pkg/util/yaml"
4241
)
4342

4443
type DefaultValuesResp struct {
@@ -47,13 +46,13 @@ type DefaultValuesResp struct {
4746
}
4847

4948
type YamlContentRequestArg struct {
50-
CodehostID int `json:"codehostID" form:"codehostID"`
51-
Owner string `json:"owner" form:"owner"`
52-
Repo string `json:"repo" form:"repo"`
53-
Namespace string `json:"namespace" form:"namespace"`
54-
Branch string `json:"branch" form:"branch"`
55-
RepoLink string `json:"repoLink" form:"repoLink"`
56-
ValuesPaths string `json:"valuesPaths" form:"valuesPaths"`
49+
CodehostID int `json:"codehostID" form:"codehostID"`
50+
Owner string `json:"owner" form:"owner"`
51+
Repo string `json:"repo" form:"repo"`
52+
Namespace string `json:"namespace" form:"namespace"`
53+
Branch string `json:"branch" form:"branch"`
54+
RepoLink string `json:"repoLink" form:"repoLink"`
55+
ValuesPath string `json:"valuesPath" form:"valuesPath"`
5756
}
5857

5958
func GetDefaultValues(productName, envName string, production bool, log *zap.SugaredLogger) (*DefaultValuesResp, error) {
@@ -83,12 +82,9 @@ func GetDefaultValues(productName, envName string, production bool, log *zap.Sug
8382
return ret, nil
8483
}
8584

86-
func GetMergedYamlContent(arg *YamlContentRequestArg, paths []string) (string, error) {
85+
func GetMergedYamlContent(arg *YamlContentRequestArg) (string, error) {
8786
var (
88-
fileContentMap sync.Map
89-
wg sync.WaitGroup
90-
err error
91-
errLock sync.Mutex
87+
err error
9288
)
9389
detail, err := systemconfig.New().GetCodeHost(arg.CodehostID)
9490
if err != nil {
@@ -103,63 +99,44 @@ func GetMergedYamlContent(arg *YamlContentRequestArg, paths []string) (string, e
10399
}
104100
}
105101

106-
errorList := &multierror.Error{}
107-
108-
for i, filePath := range paths {
109-
wg.Add(1)
110-
go func(index int, filePath string, isOtherTypeRepo bool) {
111-
defer wg.Done()
112-
if !isOtherTypeRepo {
113-
fileContent, errDownload := fsservice.DownloadFileFromSource(
114-
&fsservice.DownloadFromSourceArgs{
115-
CodehostID: arg.CodehostID,
116-
Owner: arg.Owner,
117-
Namespace: arg.Namespace,
118-
Repo: arg.Repo,
119-
Path: filePath,
120-
Branch: arg.Branch,
121-
RepoLink: arg.RepoLink,
122-
})
123-
if errDownload != nil {
124-
errLock.Lock()
125-
errorList = multierror.Append(errorList, errors.Wrapf(errDownload, fmt.Sprintf("failed to download file from git, path %s", filePath)))
126-
errLock.Unlock()
127-
return
128-
}
129-
fileContentMap.Store(index, fileContent)
130-
} else {
131-
base := path.Join(config.S3StoragePath(), arg.Repo)
132-
relativePath := path.Join(base, filePath)
133-
fileContent, errReadFile := os.ReadFile(relativePath)
134-
if errReadFile != nil {
135-
errLock.Lock()
136-
errorList = multierror.Append(errorList, errors.Wrapf(errReadFile, fmt.Sprintf("failed to read file from git repo, relative path %s", relativePath)))
137-
errLock.Unlock()
138-
return
139-
}
140-
fileContentMap.Store(index, fileContent)
141-
}
142-
}(i, filePath, detail.Type == setting.SourceFromOther)
102+
var fileContent []byte
103+
isOtherTypeRepo := detail.Type == setting.SourceFromOther
104+
if !isOtherTypeRepo {
105+
fileContent, err = fsservice.DownloadFileFromSource(
106+
&fsservice.DownloadFromSourceArgs{
107+
CodehostID: arg.CodehostID,
108+
Owner: arg.Owner,
109+
Namespace: arg.Namespace,
110+
Repo: arg.Repo,
111+
Path: arg.ValuesPath,
112+
Branch: arg.Branch,
113+
RepoLink: arg.RepoLink,
114+
})
115+
if err != nil {
116+
err = errors.Wrapf(err, fmt.Sprintf("failed to download file from git, path %s", arg.ValuesPath))
117+
return "", err
118+
}
119+
} else {
120+
base := path.Join(config.S3StoragePath(), arg.Repo)
121+
relativePath := path.Join(base, arg.ValuesPath)
122+
fileContent, err = os.ReadFile(relativePath)
123+
if err != nil {
124+
err = errors.Wrapf(err, fmt.Sprintf("failed to read file from git repo, relative path %s", relativePath))
125+
return "", err
126+
}
143127
}
144-
wg.Wait()
145128

146-
err = errorList.ErrorOrNil()
129+
// check if the file content is a valid yaml
130+
yamlMap, err := helmservice.GetValuesMapFromString(string(fileContent))
147131
if err != nil {
148-
return "", err
132+
return "", fmt.Errorf("failed to generate yaml map from file content, err: %s", err)
149133
}
150134

151-
contentArr := make([][]byte, 0, len(paths))
152-
for i := 0; i < len(paths); i++ {
153-
contentObj, _ := fileContentMap.Load(i)
154-
if contentObj != nil {
155-
contentArr = append(contentArr, contentObj.([]byte))
156-
}
157-
}
158-
ret, err := yamlutil.Merge(contentArr)
135+
yamlContent, err := yaml.Marshal(yamlMap)
159136
if err != nil {
160-
return "", errors.Wrapf(err, "failed to merge files")
137+
return "", fmt.Errorf("failed to marshal yaml map to string, err: %s", err)
161138
}
162-
return string(ret), nil
139+
return string(yamlContent), nil
163140
}
164141

165142
func GetGlobalVariables(productName, envName string, production bool, log *zap.SugaredLogger) ([]*commontypes.GlobalVariableKV, int64, error) {

0 commit comments

Comments
 (0)