11package api
22
33import (
4- "github.com/rebuy-de/kubernetes-deployment/pkg/gh"
54 "regexp"
65 "strings"
76
7+ jsonnet "github.com/google/go-jsonnet"
88 "github.com/pkg/errors"
9- "github.com/rebuy-de/kubernetes-deployment/pkg/templates"
109 log "github.com/sirupsen/logrus"
1110 "k8s.io/apimachinery/pkg/runtime"
1211 "k8s.io/client-go/kubernetes/scheme"
12+
13+ "github.com/rebuy-de/kubernetes-deployment/pkg/gh"
14+ "github.com/rebuy-de/kubernetes-deployment/pkg/templates"
1315)
1416
1517func (app * App ) Render (fetched * FetchResult ) ([]runtime.Object , error ) {
@@ -24,48 +26,108 @@ func (app *App) Render(fetched *FetchResult) ([]runtime.Object, error) {
2426 "Values" : fetched .Service .Variables ,
2527 }).Debug ("collected template values" )
2628
27- rendered , err := templates .RenderAll (fetched .Templates , fetched .Service .Variables )
28- if err != nil {
29- return nil , errors .WithStack (err )
29+ return app .decode (fetched .Templates , fetched .Service .Variables )
30+ }
31+
32+ func (app * App ) decode (files []gh.File , vars templates.Variables ) ([]runtime.Object , error ) {
33+ var objects []runtime.Object
34+
35+ for _ , file := range files {
36+ switch {
37+ case strings .HasSuffix (file .Name (), ".yml" ):
38+ fallthrough
39+
40+ case strings .HasSuffix (file .Name (), ".yaml" ):
41+ objs , err := app .decodeYAML (file , vars )
42+ if err != nil {
43+ return nil , errors .WithStack (err )
44+ }
45+ objects = append (objects , objs ... )
46+
47+ case strings .HasSuffix (file .Name (), ".jsonnet" ):
48+ objs , err := app .decodeJsonnet (file , vars , files )
49+ if err != nil {
50+ return nil , errors .WithStack (err )
51+ }
52+ objects = append (objects , objs ... )
53+
54+ default :
55+ log .WithFields (log.Fields {
56+ "Name" : file .Name (),
57+ }).Debug ("Ignoring file with wrong extension." )
58+ }
3059 }
3160
32- return app . decode ( rendered )
61+ return objects , nil
3362}
3463
35- func (app * App ) decode (files []gh.File ) ([]runtime.Object , error ) {
64+ func (app * App ) decodeYAML (file gh.File , vars templates.Variables ) ([]runtime.Object , error ) {
65+ rendered , err := templates .Render (file .Content , vars )
66+ if err != nil {
67+ return nil , errors .WithStack (err )
68+ }
69+
3670 var objects []runtime.Object
71+ splitted := regexp .MustCompile ("[\n \r ]---" ).Split (rendered , - 1 )
3772 decode := scheme .Codecs .UniversalDeserializer ().Decode
3873
39- for _ , file := range files {
40- if ! strings .HasSuffix ( file . Name (), ".yaml" ) && ! strings . HasSuffix ( file . Name (), ".yml" ) {
74+ for _ , part := range splitted {
75+ if strings .TrimSpace ( part ) == "" {
4176 log .WithFields (log.Fields {
4277 "Name" : file .Name (),
43- }).Debug ("Ignoring file with wrong extension ." )
78+ }).Debug ("Ignoring empty file ." )
4479 continue
4580 }
4681
47- splitted := regexp .MustCompile ("[\n \r ]---" ).Split (file .Content , - 1 )
82+ obj , _ , err := decode ([]byte (part ), nil , nil )
83+ if err != nil {
84+ return nil , errors .Wrapf (err , "unable to decode file '%s'" , file .Name ())
85+ }
4886
49- for _ , part := range splitted {
50- if strings .TrimSpace (part ) == "" {
51- log .WithFields (log.Fields {
52- "Name" : file .Name (),
53- }).Debug ("Ignoring empty file." )
54- continue
55- }
87+ obj , err = app .Interceptors .PostManifestRender (obj )
88+ if err != nil {
89+ return nil , errors .WithStack (err )
90+ }
5691
57- obj , _ , err := decode ([]byte (part ), nil , nil )
58- if err != nil {
59- return nil , errors .Wrapf (err , "unable to decode file '%s'" , file .Name ())
60- }
92+ objects = append (objects , obj )
93+ }
6194
62- obj , err = app .Interceptors .PostManifestRender (obj )
63- if err != nil {
64- return nil , errors .WithStack (err )
65- }
95+ return objects , nil
96+ }
6697
67- objects = append (objects , obj )
98+ func (app * App ) decodeJsonnet (file gh.File , vars templates.Variables , all []gh.File ) ([]runtime.Object , error ) {
99+ var objects []runtime.Object
100+ decode := scheme .Codecs .UniversalDeserializer ().Decode
101+
102+ importer := new (jsonnet.MemoryImporter )
103+ importer .Data = make (map [string ]string )
104+ for _ , f := range all {
105+ importer .Data [f .Name ()] = f .Content
106+ }
107+
108+ vm := jsonnet .MakeVM ()
109+ vm .Importer (importer )
110+ for k , v := range vars {
111+ vm .ExtVar (k , v )
112+ }
113+
114+ docs , err := vm .EvaluateSnippetStream (file .Name (), file .Content )
115+ if err != nil {
116+ return nil , errors .WithStack (err )
117+ }
118+
119+ for _ , doc := range docs {
120+ obj , _ , err := decode ([]byte (doc ), nil , nil )
121+ if err != nil {
122+ return nil , errors .Wrapf (err , "unable to decode file '%s'" , file .Name ())
68123 }
124+
125+ obj , err = app .Interceptors .PostManifestRender (obj )
126+ if err != nil {
127+ return nil , errors .WithStack (err )
128+ }
129+
130+ objects = append (objects , obj )
69131 }
70132
71133 return objects , nil
0 commit comments