Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit 0c7127c

Browse files
templating support (#36)
1 parent a415f60 commit 0c7127c

File tree

10 files changed

+302
-40
lines changed

10 files changed

+302
-40
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
settings:
2+
cluster:
3+
name: "cluster-01"
4+
Context: {{- toYaml $ | nindent 4 }}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
settings:
2+
cluster:
3+
name: "cluster-01"
4+
resources:
5+
- metdata:
6+
name: "test"
7+
- apiVersion: v1
8+
kind: Pod
9+
metadata:
10+
name: nginx
11+
spec:
12+
containers:
13+
- name: nginx
14+
image: nginx:1.14.2
15+
ports:
16+
- containerPort: 80
17+
Context: {{- toYaml $ | nindent 4 }}

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ require (
2525
)
2626

2727
require (
28+
github.com/BurntSushi/toml v1.2.1 // indirect
2829
github.com/Knetic/govaluate v3.0.0+incompatible // indirect
30+
github.com/Masterminds/goutils v1.1.1 // indirect
31+
github.com/Masterminds/semver/v3 v3.2.0 // indirect
32+
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
2933
github.com/aws/aws-sdk-go v1.44.168 // indirect
3034
github.com/cloudfoundry-community/vaultkv v0.5.0 // indirect
3135
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
@@ -46,15 +50,19 @@ require (
4650
github.com/google/go-cmp v0.5.9 // indirect
4751
github.com/google/gofuzz v1.2.0 // indirect
4852
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
53+
github.com/google/uuid v1.1.2 // indirect
4954
github.com/hashicorp/hcl v1.0.0 // indirect
55+
github.com/huandu/xstrings v1.3.3 // indirect
5056
github.com/imdario/mergo v0.3.13 // indirect
5157
github.com/inconshreveable/mousetrap v1.1.0 // indirect
5258
github.com/jmespath/go-jmespath v0.4.0 // indirect
5359
github.com/josharian/intern v1.0.0 // indirect
5460
github.com/magiconair/properties v1.8.7 // indirect
5561
github.com/mailru/easyjson v0.7.7 // indirect
5662
github.com/mattn/go-isatty v0.0.16 // indirect
63+
github.com/mitchellh/copystructure v1.0.0 // indirect
5764
github.com/mitchellh/mapstructure v1.5.0 // indirect
65+
github.com/mitchellh/reflectwalk v1.0.0 // indirect
5866
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
5967
github.com/modern-go/reflect2 v1.0.2 // indirect
6068
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
@@ -66,6 +74,7 @@ require (
6674
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
6775
github.com/pkg/errors v0.9.1 // indirect
6876
github.com/russross/blackfriday/v2 v2.1.0 // indirect
77+
github.com/shopspring/decimal v1.2.0 // indirect
6978
github.com/spf13/afero v1.9.3 // indirect
7079
github.com/spf13/cast v1.5.0 // indirect
7180
github.com/spf13/jwalterweatherman v1.1.0 // indirect

go.sum

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,19 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
3737
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
3838
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
3939
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
40+
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
41+
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
4042
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
4143
github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg=
4244
github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
4345
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
4446
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
47+
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
48+
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
49+
github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7YgDP83g=
50+
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
51+
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
52+
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
4553
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
4654
github.com/Shopify/ejson v1.3.3 h1:dPzgmvFhUPTJIzwdF5DaqbwW1dWaoR8ADKRdSTy6Mss=
4755
github.com/Shopify/ejson v1.3.3/go.mod h1:VZMUtDzvBW/PAXRUF5fzp1ffb1ucT8MztrZXXLYZurw=
@@ -178,6 +186,8 @@ github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLe
178186
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
179187
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
180188
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
189+
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
190+
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
181191
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
182192
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
183193
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
@@ -189,8 +199,11 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
189199
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
190200
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
191201
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
202+
github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4=
203+
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
192204
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
193205
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
206+
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
194207
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
195208
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
196209
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
@@ -225,8 +238,12 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0
225238
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
226239
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
227240
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
241+
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
242+
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
228243
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
229244
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
245+
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
246+
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
230247
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
231248
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
232249
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -268,13 +285,16 @@ github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBO
268285
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
269286
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
270287
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
288+
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
289+
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
271290
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
272291
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
273292
github.com/smartystreets/assertions v1.2.1 h1:bKNHfEv7tSIjZ8JbKaFjzFINljxG4lzZvmHUnElzOIg=
274293
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
275294
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
276295
github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk=
277296
github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
297+
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
278298
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
279299
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
280300
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
@@ -332,6 +352,7 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
332352
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
333353
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
334354
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
355+
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
335356
golang.org/x/crypto v0.4.0 h1:UVQgzMY87xqpKNgb+kDsll2Igd33HszWHFLmpaRMq/8=
336357
golang.org/x/crypto v0.4.0/go.mod h1:3quD/ATkf6oY+rnes5c3ExXTbLc8mueNue5/DoinL80=
337358
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@@ -405,6 +426,7 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b
405426
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
406427
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
407428
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
429+
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
408430
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
409431
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
410432
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
@@ -479,12 +501,14 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
479501
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
480502
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
481503
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
504+
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
482505
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
483506
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
484507
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
485508
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
486509
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
487510
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
511+
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
488512
golang.org/x/term v0.3.0 h1:qoo4akIqOcDME5bhc/NgxUdovd6BSS2uMsVjB56q1xI=
489513
golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA=
490514
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

internal/kustomize/kustomize.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ func NewKustomize(root string) (k *Kustomize, err error) {
3535
if err != nil {
3636
return nil, err
3737
}
38+
err = k.addPath(root)
39+
if err != nil {
40+
return nil, err
41+
}
3842

3943
return k, nil
4044
}

internal/utils/file.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package utils
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"io/ioutil"
7+
"text/template"
68

79
"gopkg.in/yaml.v2"
810
)
@@ -24,6 +26,22 @@ func NewFile(path string) (*File, error) {
2426
return result, err
2527
}
2628

29+
func (f *File) Template(funcmap template.FuncMap, values map[interface{}]interface{}) (err error) {
30+
tmpl, err := template.New("fileTemplate").Funcs(funcmap).Parse(string(f.Byte()))
31+
if err != nil {
32+
return err
33+
}
34+
35+
var buf bytes.Buffer
36+
err = tmpl.Execute(&buf, ToMap(values))
37+
if err != nil {
38+
return err
39+
}
40+
41+
f.data = buf.Bytes()
42+
return nil
43+
}
44+
2745
func (f *File) load() ([]byte, error) {
2846
var data []byte
2947
data, err := ioutil.ReadFile(f.Path)

internal/utils/template.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Original source:
2+
// https://raw.githubusercontent.com/helm/helm/952708b436bb2c8d5ef0e2e9ef1d8aabe64aeae9/pkg/engine/funcs.go
3+
package utils
4+
5+
import (
6+
"bytes"
7+
"encoding/json"
8+
"strings"
9+
"text/template"
10+
11+
"github.com/BurntSushi/toml"
12+
"github.com/Masterminds/sprig/v3"
13+
"sigs.k8s.io/yaml"
14+
)
15+
16+
// funcMap returns a mapping of all of the functions that Engine has.
17+
//
18+
// Because some functions are late-bound (e.g. contain context-sensitive
19+
// data), the functions may not all perform identically outside of an Engine
20+
// as they will inside of an Engine.
21+
//
22+
// Known late-bound functions:
23+
//
24+
// - "include"
25+
// - "tpl"
26+
//
27+
// These are late-bound in Engine.Render(). The
28+
// version included in the FuncMap is a placeholder.
29+
func SprigFuncMap() template.FuncMap {
30+
f := sprig.TxtFuncMap()
31+
delete(f, "env")
32+
delete(f, "expandenv")
33+
34+
// Add some extra functionality
35+
extra := template.FuncMap{
36+
"toToml": toTOML,
37+
"toYaml": toYAML,
38+
"fromYaml": fromYAML,
39+
"fromYamlArray": fromYAMLArray,
40+
"toJson": toJSON,
41+
"fromJson": fromJSON,
42+
"fromJsonArray": fromJSONArray,
43+
}
44+
45+
for k, v := range extra {
46+
f[k] = v
47+
}
48+
49+
return f
50+
}
51+
52+
// toYAML takes an interface, marshals it to yaml, and returns a string. It will
53+
// always return a string, even on marshal error (empty string).
54+
//
55+
// This is designed to be called from a template.
56+
func toYAML(v interface{}) string {
57+
data, err := yaml.Marshal(v)
58+
if err != nil {
59+
// Swallow errors inside of a template.
60+
return ""
61+
}
62+
return strings.TrimSuffix(string(data), "\n")
63+
}
64+
65+
// fromYAML converts a YAML document into a map[string]interface{}.
66+
//
67+
// This is not a general-purpose YAML parser, and will not parse all valid
68+
// YAML documents. Additionally, because its intended use is within templates
69+
// it tolerates errors. It will insert the returned error message string into
70+
// m["Error"] in the returned map.
71+
func fromYAML(str string) map[string]interface{} {
72+
m := map[string]interface{}{}
73+
74+
if err := yaml.Unmarshal([]byte(str), &m); err != nil {
75+
m["Error"] = err.Error()
76+
}
77+
return m
78+
}
79+
80+
// fromYAMLArray converts a YAML array into a []interface{}.
81+
//
82+
// This is not a general-purpose YAML parser, and will not parse all valid
83+
// YAML documents. Additionally, because its intended use is within templates
84+
// it tolerates errors. It will insert the returned error message string as
85+
// the first and only item in the returned array.
86+
func fromYAMLArray(str string) []interface{} {
87+
a := []interface{}{}
88+
89+
if err := yaml.Unmarshal([]byte(str), &a); err != nil {
90+
a = []interface{}{err.Error()}
91+
}
92+
return a
93+
}
94+
95+
// toTOML takes an interface, marshals it to toml, and returns a string. It will
96+
// always return a string, even on marshal error (empty string).
97+
//
98+
// This is designed to be called from a template.
99+
func toTOML(v interface{}) string {
100+
b := bytes.NewBuffer(nil)
101+
e := toml.NewEncoder(b)
102+
err := e.Encode(v)
103+
if err != nil {
104+
return err.Error()
105+
}
106+
return b.String()
107+
}
108+
109+
// toJSON takes an interface, marshals it to json, and returns a string. It will
110+
// always return a string, even on marshal error (empty string).
111+
//
112+
// This is designed to be called from a template.
113+
func toJSON(v interface{}) string {
114+
data, err := json.Marshal(v)
115+
if err != nil {
116+
// Swallow errors inside of a template.
117+
return ""
118+
}
119+
return string(data)
120+
}
121+
122+
// fromJSON converts a JSON document into a map[string]interface{}.
123+
//
124+
// This is not a general-purpose JSON parser, and will not parse all valid
125+
// JSON documents. Additionally, because its intended use is within templates
126+
// it tolerates errors. It will insert the returned error message string into
127+
// m["Error"] in the returned map.
128+
func fromJSON(str string) map[string]interface{} {
129+
m := make(map[string]interface{})
130+
131+
if err := json.Unmarshal([]byte(str), &m); err != nil {
132+
m["Error"] = err.Error()
133+
}
134+
return m
135+
}
136+
137+
// fromJSONArray converts a JSON array into a []interface{}.
138+
//
139+
// This is not a general-purpose JSON parser, and will not parse all valid
140+
// JSON documents. Additionally, because its intended use is within templates
141+
// it tolerates errors. It will insert the returned error message string as
142+
// the first and only item in the returned array.
143+
func fromJSONArray(str string) []interface{} {
144+
a := []interface{}{}
145+
146+
if err := json.Unmarshal([]byte(str), &a); err != nil {
147+
a = []interface{}{err.Error()}
148+
}
149+
return a
150+
}

pkg/subst/build.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func New(config config.Configuration) (build *Build, err error) {
4242
SubstFileRegex: init.cfg.FileRegex,
4343
}
4444

45-
s, err := NewSubstitutions(SubstitutionsConfig, init.decryptors)
45+
s, err := NewSubstitutions(SubstitutionsConfig, init.decryptors, k.Build)
4646
if err != nil {
4747
return nil, err
4848
}
@@ -52,15 +52,14 @@ func New(config config.Configuration) (build *Build, err error) {
5252
if err != nil {
5353
return nil, err
5454
}
55-
5655
return init, err
5756
}
5857

5958
func (b *Build) Build() (err error) {
6059

6160
// Run Build
6261
logrus.Debug("substitute manifests")
63-
for _, manifest := range b.Kustomization.Build.Resources() {
62+
for _, manifest := range b.Substitutions.Resources.Resources() {
6463
var c map[interface{}]interface{}
6564

6665
mBytes, _ := manifest.MarshalJSON()

0 commit comments

Comments
 (0)