@@ -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
2023 argocdEnvVarPrefix = "ARGOCD_ENV"
2124)
2225
26+ type ConfigFileSeq struct {
27+ Seq int
28+ Name string
29+ }
30+
2331type 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
127154func (renderer * Renderer ) getArgocdEnvList () []string {
0 commit comments