Skip to content

Commit dbb9e41

Browse files
Merge branch 'devops/fix-helm-config-sequence' into 'master'
Devops/fix helm config sequence See merge request technology/utils/cicd/argocd-helm-envsubst-plugin!17
2 parents 39878bc + 1dcab1b commit dbb9e41

5 files changed

Lines changed: 97 additions & 76 deletions

File tree

‎.gitlab-ci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
include:
22
- project: technology/utils/cicd/gitlab-ci-pipeline-templates
3-
ref: v2.10.12
3+
ref: v2.16.0
44
file:
55
- workflow.gitlab-ci.yml
66
- docker.buildx.gitlab-ci.yml

‎Makefile‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ helm-build:
2222
go run main.go build --path asset-master-service --repository-path repositories.yaml
2323

2424
helm-render:
25-
go run main.go render --path asset-master-service
25+
go run main.go render --path asset-master-service
26+
27+
test:
28+
go test ./... -v

‎internal/build_test.go‎

Lines changed: 0 additions & 63 deletions
This file was deleted.

‎internal/render.go‎

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import (
88
"os"
99
"os/exec"
1010
"path/filepath"
11+
"sort"
1112
"strings"
1213
"time"
1314

15+
"regexp"
16+
1417
"gopkg.in/yaml.v2"
1518
)
1619

@@ -20,6 +23,11 @@ var (
2023
argocdEnvVarPrefix = "ARGOCD_ENV"
2124
)
2225

26+
type ConfigFileSeq struct {
27+
Seq int
28+
Name string
29+
}
30+
2331
type Renderer struct {
2432
debugLogFilePath string
2533
}
@@ -47,7 +55,7 @@ func (renderer *Renderer) RenderTemplate(helmChartPath string, debugLogFilePath
4755

4856
useExternalHelmChartPathIfSet()
4957

50-
configFileNames := renderer.findHelmConfigs()
58+
configFileNames := renderer.FindHelmConfigs()
5159
if len(configFileNames) > 0 {
5260
for _, name := range configFileNames {
5361
args = append(args, "-f")
@@ -97,31 +105,50 @@ func (renderer *Renderer) RenderTemplate(helmChartPath string, debugLogFilePath
97105
fmt.Println(out.String())
98106
}
99107

100-
func (renderer *Renderer) findHelmConfigs() []string {
108+
func (renderer *Renderer) FindHelmConfigs() []string {
101109
// Default to values.yaml
102-
files := []string{"values.yaml"}
110+
files := []ConfigFileSeq{
111+
{
112+
Seq: 0,
113+
Name: "values.yaml",
114+
},
115+
}
103116
root := "config/"
104117
environment := os.Getenv("ARGOCD_ENV_ENVIRONMENT")
105118
cluster := os.Getenv("ARGOCD_ENV_CLUSTER")
119+
helmConfigFilePatterns := []string{
120+
"values.yaml",
121+
root + environment + ".yaml",
122+
root + cluster + "_" + environment + ".yaml",
123+
}
106124
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
107125
if err != nil {
108126
// log.Fatalf("Config folder not found: %v", err)
109127
return nil
110128
}
111-
// e.g. config/dev.yaml
112-
if !info.IsDir() && path == root+environment+".yaml" {
113-
files = append(files, path)
114-
}
115-
// e.g. config/operator_dev.yaml
116-
if !info.IsDir() && path == root+cluster+"_"+environment+".yaml" {
117-
files = append(files, path)
129+
for seq, pattern := range helmConfigFilePatterns {
130+
if match, _ := regexp.MatchString(pattern, path); match {
131+
files = append(files, ConfigFileSeq{Seq: seq, Name: path})
132+
}
118133
}
119134
return nil
120135
})
121136
if err != nil {
122137
log.Fatalf("Find config file in dir error: %v", err)
123138
}
124-
return files
139+
140+
// sort
141+
sort.Slice(files, func(i, j int) bool {
142+
return files[i].Seq < files[j].Seq
143+
})
144+
145+
// convert array of struct to array of string
146+
sortedFiles := []string{}
147+
for _, file := range files {
148+
sortedFiles = append(sortedFiles, file.Name)
149+
}
150+
151+
return sortedFiles
125152
}
126153

127154
func (renderer *Renderer) getArgocdEnvList() []string {

‎internal/render_test.go‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package internal_test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"reflect"
7+
"testing"
8+
9+
"gitlab.int.hextech.io/technology/utils/cicd/argocd-helm-envsubst-plugin/internal"
10+
)
11+
12+
const (
13+
TEST_ARGOCD_ENV_ENVIRONMENT = "prod"
14+
TEST_ARGOCD_ENV_CLUSTER = "blockchain"
15+
)
16+
17+
func setup(t *testing.T) {
18+
if err := os.Mkdir("config", os.ModePerm); err != nil {
19+
t.Fatal(err)
20+
}
21+
if _, err := os.Create(fmt.Sprintf("config/%s_%s.yaml", TEST_ARGOCD_ENV_CLUSTER, TEST_ARGOCD_ENV_ENVIRONMENT)); err != nil {
22+
t.Fatal(err)
23+
}
24+
if _, err := os.Create(fmt.Sprintf("config/%s.yaml", TEST_ARGOCD_ENV_ENVIRONMENT)); err != nil {
25+
t.Fatal(err)
26+
}
27+
28+
os.Setenv("ARGOCD_ENV_ENVIRONMENT", TEST_ARGOCD_ENV_ENVIRONMENT)
29+
os.Setenv("ARGOCD_ENV_CLUSTER", TEST_ARGOCD_ENV_CLUSTER)
30+
}
31+
32+
func teardown(t *testing.T) {
33+
if err := os.RemoveAll("config"); err != nil {
34+
t.Fatal(err)
35+
}
36+
}
37+
38+
func TestFindHelmConfigs(t *testing.T) {
39+
setup(t)
40+
defer teardown(t)
41+
42+
r := internal.NewRenderer()
43+
44+
expect := []string{
45+
"values.yaml",
46+
fmt.Sprintf("config/%s.yaml", TEST_ARGOCD_ENV_ENVIRONMENT),
47+
fmt.Sprintf("config/%s_%s.yaml", TEST_ARGOCD_ENV_CLUSTER, TEST_ARGOCD_ENV_ENVIRONMENT),
48+
}
49+
actual := r.FindHelmConfigs()
50+
51+
if !reflect.DeepEqual(actual, expect) {
52+
t.Errorf("Expected %s do not match actual %s", expect, actual)
53+
}
54+
}

0 commit comments

Comments
 (0)