Skip to content

Commit 2886e90

Browse files
committed
✨ Added kustomization generator
1 parent 31a2a21 commit 2886e90

15 files changed

+184
-21
lines changed

.github/workflows/release-test.yml

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
args: --snapshot --skip-publish --rm-dist
3939
env:
4040
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41+
SCOOP_BUCKET_GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
4142
# - name: Build APK repo
4243
# uses: ./.github/actions/make-apkindex
4344
# with:

.github/workflows/release.yml

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
args: release --rm-dist
3737
env:
3838
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39+
SCOOP_BUCKET_GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
3940
- name: Trigger APK repo build
4041
uses: peter-evans/repository-dispatch@v2
4142
with:

.goreleaser.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ builds:
2121
archives:
2222
- format: binary
2323
name_template: '{{ .Binary }}_v{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ with .Arm }}v{{ . }}{{ end }}{{ with .Mips }}_{{ . }}{{ end }}{{ if not (eq .Amd64 "v1") }}{{ .Amd64 }}{{ end }}'
24+
format_overrides:
25+
- goos: windows
26+
format: zip
2427
checksum:
2528
name_template: "SHA256SUMS"
2629
snapshot:
@@ -73,3 +76,16 @@ dockers:
7376
- --label=org.opencontainers.image.created={{ time "2006-01-02T15:04:05Z07:00" }}
7477
- --label=org.opencontainers.image.revision={{ .FullCommit }}
7578
- --label=org.opencontainers.image.licenses=Apache-2.0
79+
scoop:
80+
bucket:
81+
owner: kaweezle
82+
name: scoop-bucket
83+
branch: main
84+
token: "{{ .Env.SCOOP_BUCKET_GITHUB_TOKEN }}"
85+
commit_author:
86+
name: Antoine Martin
87+
88+
commit_msg_template: "Scoop update for {{ .ProjectName }} version {{ .Tag }}"
89+
homepage: https://github.com/kaweezle/krmfnbuiltin
90+
description: "Kustomize SOPS KRM function"
91+
license: Apache-2.0

README.md

+62-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ transformation in your kustomize projects.
2323
<li><a href="#configmap-generator-with-git-properties">ConfigMap generator with git properties</a></li>
2424
<li><a href="#heredoc-generator">Heredoc generator</a></li>
2525
<li><a href="#extended-replacement-in-structured-content">Extended replacement in structured content</a></li>
26+
<li><a href="#kustomization-generator">Kustomization generator</a></li>
2627
</ul>
2728
</li>
2829
<li><a href="#installation">Installation</a></li>
@@ -351,6 +352,24 @@ metadata:
351352
With these annotations, the generated config map will be saved in the
352353
`local-config.yaml` file in the configuration directory.
353354

355+
If the file name is empty, i.e. the annotation is:
356+
357+
```yaml
358+
config.kaweezle.com/path: ""
359+
```
360+
361+
The generated resources will be saved each in its own file with the pattern:
362+
363+
```text
364+
<namespace>/<kind>_<name>.yaml
365+
```
366+
367+
For instance:
368+
369+
```text
370+
kube-flannel/daemonset_kube-flannel-ds.yaml
371+
```
372+
354373
## Extensions
355374

356375
### Remove Transformer
@@ -735,7 +754,7 @@ metadata:
735754
config.kaweezle.com/local-config: "true"
736755
config.kubernetes.io/function: |
737756
exec:
738-
path: ../../krmfnbuiltin
757+
path: krmfnbuiltin
739758
data:
740759
sish:
741760
# New properties
@@ -751,7 +770,7 @@ metadata:
751770
config.kaweezle.com/prune-local: "true"
752771
config.kubernetes.io/function: |
753772
exec:
754-
path: ../../krmfnbuiltin
773+
path: krmfnbuiltin
755774
replacements:
756775
- source:
757776
kind: LocalConfiguration
@@ -810,6 +829,47 @@ will become
810829
HostName target.link
811830
```
812831

832+
### Kustomization generator
833+
834+
`KustomizationGenerator` is the kustomize equivalent to
835+
`HelmChartInflationGenerator`. It allows generating resources from a
836+
kustomization.
837+
838+
Example:
839+
840+
```yaml
841+
apiVersion: builtin
842+
kind: KustomizationGenerator
843+
metadata:
844+
name: kustomization-generator
845+
annotations:
846+
config.kaweezle.com/path: "uninode.yaml"
847+
config.kubernetes.io/function: |
848+
exec:
849+
path: krmfnbuiltin
850+
kustomizeDirectory: https://github.com/antoinemartin/autocloud.git//packages/uninode?ref=deploy/citest
851+
```
852+
853+
If this function is run with the following command:
854+
855+
```console
856+
> kustomize fn run --enable-exec --fn-path functions applications
857+
```
858+
859+
It will generate a file named `uninode.yaml` in the `applications` directory.
860+
With:
861+
862+
```yaml
863+
config.kaweezle.com/path: ""
864+
```
865+
866+
One file will be created per resource (see
867+
[Keeping or deleting generated resources](#keeping-or-deleting-generated-resources)).
868+
869+
**IMPORTANT** `krmfnbuiltin` runs from the directory where the
870+
`kustomize run fn` command has been launched, and **not from the function
871+
configuration folder**. Any relative path should take this into consideration.
872+
813873
## Installation
814874

815875
With each [Release](https://github.com/kaweezle/krmfnbuiltin/releases), we

go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/stretchr/testify v1.8.1
99
golang.org/x/tools v0.5.0
1010
sigs.k8s.io/kustomize/api v0.12.1
11-
sigs.k8s.io/kustomize/kyaml v0.13.10
11+
sigs.k8s.io/kustomize/kyaml v0.13.9
1212
sigs.k8s.io/yaml v1.2.0
1313

1414
)
@@ -33,6 +33,7 @@ require (
3333
github.com/go-openapi/swag v0.22.3 // indirect
3434
github.com/golang/protobuf v1.5.2 // indirect
3535
github.com/google/gnostic v0.5.7-v3refs // indirect
36+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
3637
github.com/imdario/mergo v0.3.13 // indirect
3738
github.com/inconshreveable/mousetrap v1.0.0 // indirect
3839
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
@@ -51,6 +52,7 @@ require (
5152
github.com/spf13/pflag v1.0.5 // indirect
5253
github.com/xanzy/ssh-agent v0.3.3 // indirect
5354
github.com/xlab/treeprint v1.1.0 // indirect
55+
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
5456
golang.org/x/crypto v0.3.0 // indirect
5557
golang.org/x/mod v0.7.0 // indirect
5658
golang.org/x/net v0.5.0 // indirect

go.sum

+8-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a h1:idn718Q4
1919
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
2020
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
2121
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
22+
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
23+
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
24+
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
2225
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
2326
github.com/cloudflare/circl v1.1.0 h1:bZgT/A+cikZnKIwn7xL2OBj012Bmvho/o6RpRvv3GKY=
2427
github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I=
@@ -89,6 +92,7 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
8992
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
9093
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
9194
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
95+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
9296
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
9397
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
9498
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
@@ -173,6 +177,7 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
173177
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
174178
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
175179
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 h1:+FNtrFTmVw0YZGpBGX56XDee331t6JAXeK2bcyhLOOc=
180+
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5/go.mod h1:nmDLcffg48OtT/PSW0Hg7FvpRQsQh5OSqIylirxKC7o=
176181
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
177182
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
178183
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
@@ -217,6 +222,7 @@ golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
217222
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
218223
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
219224
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
225+
golang.org/x/sys v0.0.0-20191002063906-3421d5a6bb1c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
220226
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
221227
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
222228
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -319,8 +325,8 @@ k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 h1:HNSDgDCrr/6Ly3WEGKZftiE7IY19V
319325
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
320326
sigs.k8s.io/kustomize/api v0.12.1 h1:7YM7gW3kYBwtKvoY216ZzY+8hM+lV53LUayghNRJ0vM=
321327
sigs.k8s.io/kustomize/api v0.12.1/go.mod h1:y3JUhimkZkR6sbLNwfJHxvo1TCLwuwm14sCYnkH6S1s=
322-
sigs.k8s.io/kustomize/kyaml v0.13.10 h1:htPMvrk7ZDfTDyrgXIm/2mfmcYJHEmRb6s+yCLgtNms=
323-
sigs.k8s.io/kustomize/kyaml v0.13.10/go.mod h1:PzDV8gSaY8mwdd7nR9zg7Pw5yh9fu8G+ElAXoQVzBq8=
328+
sigs.k8s.io/kustomize/kyaml v0.13.9 h1:Qz53EAaFFANyNgyOEJbT/yoIHygK40/ZcvU3rgry2Tk=
329+
sigs.k8s.io/kustomize/kyaml v0.13.9/go.mod h1:QsRbD0/KcU+wdk0/L0fIp2KLnohkVzs6fQ85/nOXac4=
324330
sigs.k8s.io/structured-merge-diff/v4 v4.0.2/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
325331
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
326332
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func main() {
111111

112112
cmd := command.Build(processor, command.StandaloneDisabled, false)
113113
command.AddGenerateDockerfile(cmd)
114-
cmd.Version = "v0.3.0" // <---VERSION--->
114+
cmd.Version = "v0.3.1" // <---VERSION--->
115115

116116
if err := cmd.Execute(); err != nil {
117117
os.Exit(1)

pkg/extras/KustomizationGenerator.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package extras
2+
3+
import (
4+
"sigs.k8s.io/kustomize/api/krusty"
5+
"sigs.k8s.io/kustomize/api/resmap"
6+
"sigs.k8s.io/kustomize/api/types"
7+
"sigs.k8s.io/kustomize/kyaml/filesys"
8+
"sigs.k8s.io/yaml"
9+
)
10+
11+
// KustomizationGeneratorPlugin configures the KustomizationGenerator.
12+
type KustomizationGeneratorPlugin struct {
13+
Directory string `json:"kustomizeDirectory,omitempty" yaml:"kustomizeDirectory,omitempty"`
14+
}
15+
16+
// enablePlugins adds to opts the options to run exec functions
17+
func enablePlugins(opts *krusty.Options) *krusty.Options {
18+
opts.PluginConfig = types.EnabledPluginConfig(types.BploUseStaticallyLinked) // cSpell: disable-line
19+
opts.PluginConfig.FnpLoadingOptions.EnableExec = true
20+
opts.PluginConfig.FnpLoadingOptions.AsCurrentUser = true
21+
opts.PluginConfig.HelmConfig.Command = "helm"
22+
opts.LoadRestrictions = types.LoadRestrictionsNone
23+
return opts
24+
}
25+
26+
// runKustomizations runs the kustomization in dirname (URL compatible) with
27+
// the filesystem fs.
28+
func runKustomizations(fs filesys.FileSystem, dirname string) (resources resmap.ResMap, err error) {
29+
30+
opts := enablePlugins(krusty.MakeDefaultOptions())
31+
k := krusty.MakeKustomizer(opts)
32+
resources, err = k.Run(fs, dirname)
33+
return
34+
}
35+
36+
// Config reads the function configuration, i.e. the kustomizeDirectory
37+
func (p *KustomizationGeneratorPlugin) Config(
38+
h *resmap.PluginHelpers, c []byte) (err error) {
39+
err = yaml.Unmarshal(c, p)
40+
if err != nil {
41+
return err
42+
}
43+
return err
44+
}
45+
46+
// Generate generates the resources of the directory
47+
func (p *KustomizationGeneratorPlugin) Generate() (resmap.ResMap, error) {
48+
return runKustomizations(filesys.MakeFsOnDisk(), p.Directory)
49+
}
50+
51+
// NewKustomizationGeneratorPlugin returns a newly Created KustomizationGenerator
52+
func NewKustomizationGeneratorPlugin() resmap.GeneratorPlugin {
53+
return &KustomizationGeneratorPlugin{}
54+
}

pkg/plugins/builtinplugintype_string.go

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/plugins/factories.go

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const (
3939
ReplacementTransformer
4040
GitConfigMapGenerator
4141
RemoveTransformer
42+
KustomizationGenerator
4243
)
4344

4445
var stringToBuiltinPluginTypeMap map[string]BuiltinPluginType
@@ -122,6 +123,7 @@ var GeneratorFactories = map[BuiltinPluginType]func() resmap.GeneratorPlugin{
122123
SecretGenerator: builtins.NewSecretGeneratorPlugin,
123124
HelmChartInflationGenerator: builtins.NewHelmChartInflationGeneratorPlugin,
124125
GitConfigMapGenerator: extras.NewGitConfigMapGeneratorPlugin,
126+
KustomizationGenerator: extras.NewKustomizationGeneratorPlugin,
125127
}
126128

127129
func MakeBuiltinPlugin(r resid.Gvk) (resmap.Configurable, error) {

pkg/utils/utils.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ func TransferAnnotations(list []*yaml.RNode, config *yaml.RNode) (err error) {
6161
if local {
6262
annotations[FunctionAnnotationLocalConfig] = "true"
6363
}
64-
annotations[kioutil.LegacyPathAnnotation] = path
65-
annotations[kioutil.PathAnnotation] = path
64+
if path != "" {
65+
annotations[kioutil.LegacyPathAnnotation] = path
66+
annotations[kioutil.PathAnnotation] = path
6667

67-
curIndex := strconv.Itoa(startIndex + index)
68-
annotations[kioutil.LegacyIndexAnnotation] = curIndex
69-
annotations[kioutil.IndexAnnotation] = curIndex
68+
curIndex := strconv.Itoa(startIndex + index)
69+
annotations[kioutil.LegacyIndexAnnotation] = curIndex
70+
annotations[kioutil.IndexAnnotation] = curIndex
71+
}
7072
delete(annotations, FunctionAnnotationInjectLocal)
7173
delete(annotations, FunctionAnnotationFunction)
7274
r.SetAnnotations(annotations)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: builtin
2+
kind: KustomizationGenerator
3+
metadata:
4+
name: kustomization-generator
5+
annotations:
6+
config.kaweezle.com/path: ""
7+
config.kubernetes.io/function: |
8+
exec:
9+
path: ../../krmfnbuiltin
10+
kustomizeDirectory: https://github.com/antoinemartin/autocloud.git//packages/uninode?ref=deploy/citest

tests/kustomization/original/.gitkeep

Whitespace-only changes.

tests/test_krmfnbuiltin.sh

+8-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ for d in $(ls -d */); do
1616
cp -r original applications
1717
echo " > Performing kustomizations..."
1818
kustomize fn run --enable-exec --fn-path functions applications
19-
for f in $(ls -1 applications); do
20-
echo " > Checking $f..."
21-
diff <(yq eval -P expected/$f) <(yq eval -P applications/$f)
22-
done
19+
if [ -d expected ]; then
20+
for f in $(ls -1 applications); do
21+
echo " > Checking $f..."
22+
diff <(yq eval -P expected/$f) <(yq eval -P applications/$f)
23+
done
24+
else
25+
echo " > No expected result. Skipping check"
26+
fi
2327
cd ..
2428
done
2529
echo "Done ok 🎉"

tests/test_krmfnbuiltin_kpt.sh

+8-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ for d in $(ls -d */); do
2323
mv $temp_file_2 $temp_file
2424
done
2525
cat $temp_file | kpt fn sink applications
26-
for f in $(ls -1 expected); do
27-
echo " > Checking $f..."
28-
diff <(yq eval -P expected/$f) <(yq eval -P applications/$f)
29-
done
26+
if [ -d expected ]; then
27+
for f in $(ls -1 applications); do
28+
echo " > Checking $f..."
29+
diff <(yq eval -P expected/$f) <(yq eval -P applications/$f)
30+
done
31+
else
32+
echo " > No expected result. Skipping check"
33+
fi
3034
cd ..
3135
done
3236
echo "Done ok 🎉"

0 commit comments

Comments
 (0)