@@ -3,6 +3,7 @@ package argocd
33import (
44 "context"
55 "encoding/json"
6+ "fmt"
67 "goff/util"
78 "io/fs"
89 "os"
@@ -16,9 +17,15 @@ import (
1617 "github.com/ghodss/yaml"
1718)
1819
19- func Render (dir , repoServerUrl , outputDir string ) {
20- conn := apiclient .NewRepoServerClientset (repoServerUrl , 300 , apiclient.TLSConfiguration {StrictValidation : false })
21- r , b , err := conn .NewRepoServerClient ()
20+ type RepoCredentails struct {
21+ Username string
22+ Password string
23+ KeyFile string
24+ }
25+
26+ func Render (dir , repoServerUrl , outputDir string , creds RepoCredentails ) {
27+ conn := apiclient .NewRepoServerClientset (repoServerUrl , 600 , apiclient.TLSConfiguration {StrictValidation : false })
28+ r , client , err := conn .NewRepoServerClient ()
2229 defer r .Close ()
2330
2431 if err != nil {
@@ -32,93 +39,124 @@ func Render(dir, repoServerUrl, outputDir string) {
3239 }
3340
3441 for _ , file := range files {
35- renderFile (file , repoServerUrl , outputDir , b )
42+ err = renderFile (file , repoServerUrl , outputDir , client , creds )
43+ if err != nil {
44+ fmt .Println (err )
45+ }
46+ //TODO log errors
3647 }
3748
3849}
3950
40- func renderFile (file , repoServerUrl , outputDir string , client apiclient.RepoServerServiceClient ) {
51+ func renderFile (file , repoServerUrl , outputDir string , client apiclient.RepoServerServiceClient , creds RepoCredentails ) error {
4152
4253 data , err := os .ReadFile (file )
4354 if err != nil {
44- panic ( err )
55+ return err
4556 }
4657
4758 data , err = yaml .YAMLToJSON (data )
4859 if err != nil {
49- panic ( err )
60+ return err
5061 }
5162
5263 app := & v1alpha1.Application {}
5364
5465 err = json .Unmarshal (data , app )
5566 if err != nil {
56- panic ( err )
67+ return err
5768 }
5869
5970 repoDB := & dbmocks.ArgoDB {}
71+ source := v1alpha1.ApplicationSource {}
72+
73+ var privateKey string
74+ if creds .KeyFile != "" {
75+ data , err := os .ReadFile (creds .KeyFile )
76+ if err != nil {
77+ return err
78+ }
79+ privateKey = string (data )
80+ }
6081
6182 if app .Spec .Source != nil {
6283 repoDB .On ("GetRepository" , context .Background (), app .Spec .Source .RepoURL ).Return (& v1alpha1.Repository {
63- Repo : app .Spec .Source .RepoURL ,
84+ Repo : app .Spec .Source .RepoURL ,
85+ SSHPrivateKey : privateKey ,
86+ Username : creds .Username ,
87+ Password : creds .Password ,
88+ ForceHttpBasicAuth : true ,
6489 }, nil )
90+ source = * app .Spec .Source
6591 }
6692
6793 if app .Spec .Sources != nil {
6894 for i := range app .Spec .Sources {
95+ source = app .Spec .Sources [i ]
6996 repo := app .Spec .Sources [i ].RepoURL
7097 if repo != "" {
7198 repoDB .On ("GetRepository" , context .Background (), repo ).Return (& v1alpha1.Repository {
72- Repo : repo ,
99+ Repo : repo ,
100+ SSHPrivateKey : privateKey ,
101+ Username : creds .Username ,
102+ Password : creds .Password ,
103+ ForceHttpBasicAuth : true ,
73104 }, nil )
74105 }
75106 }
76107 }
77108
78109 refSources , err := argo .GetRefSources (context .Background (), app .Spec , repoDB )
79110 req := & apiclient.ManifestRequest {
80- ApplicationSource : & app . Spec . Sources [ 0 ] ,
111+ ApplicationSource : & source ,
81112 AppName : "goff-test" ,
82113 NoCache : true ,
83114 RefSources : refSources ,
84115 HasMultipleSources : true ,
85- Revision : app .Spec .Sources [0 ].TargetRevision ,
116+ Revision : source .TargetRevision ,
117+ KustomizeOptions : & v1alpha1.KustomizeOptions {
118+ BuildOptions : "--enable-helm" ,
119+ },
86120 Repo : & v1alpha1.Repository {
87- Repo : app .Spec .Sources [0 ].RepoURL ,
121+ Repo : source .RepoURL ,
122+ SSHPrivateKey : privateKey ,
123+ Username : creds .Username ,
124+ Password : creds .Password ,
125+ ForceHttpBasicAuth : true ,
88126 },
89127 }
90128
91129 resp , err := client .GenerateManifest (context .Background (), req )
92130 if err != nil {
93- panic ( err )
131+ return fmt . Errorf ( "could not process application '%s': %w" , app . Name , err )
94132 }
95133
96134 err = os .MkdirAll (outputDir , 0777 )
97135 if err != nil {
98- panic ( err )
136+ return err
99137 }
100138
101139 for _ , manifest := range resp .Manifests {
102140
103141 fileName , err := util .FileNameFromManifest (manifest )
104142 if err != nil {
105- panic ( err )
143+ return err
106144 }
107145
108146 outputFile := filepath .Join (outputDir , fileName )
109147
110148 yamlManifest , err := yaml .JSONToYAML ([]byte (manifest ))
111149 if err != nil {
112- panic ( err )
150+ return err
113151 }
114152
115153 err = os .WriteFile (outputFile , yamlManifest , 0777 )
116154 if err != nil {
117- panic ( err )
155+ return err
118156 }
119157
120158 }
121-
159+ return nil
122160}
123161
124162func findArgoApps (rootDir string ) ([]string , error ) {
0 commit comments