Skip to content

Commit 5c2d50d

Browse files
authored
[INCOMPATIBLE CHANGE] refactor kustomize-generator options (#135)
options for the kustomize-generator are now passed via a new struct KustomizeGeneratorOptions, which is supplied to the New*() constructors; besides providing a suffix for templating-subjected files, it is now possible to use non-standard template delimiters (i.e. something different from {{ and }}); in addition, options supplied through the constructors can be overridden in the source, if there exists a file .component-config.yaml at the kustomization's root directory; finally, implicitly generated kustomizations now ignore hidden files, i.e. files starting with a dot
1 parent b270d2a commit 5c2d50d

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

pkg/manifests/kustomize/generator.go

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package kustomize
88
import (
99
"bytes"
1010
"context"
11+
"errors"
1112
"io"
1213
"io/fs"
1314
"os"
@@ -37,6 +38,20 @@ import (
3738

3839
// TODO: carve out logic into an internal Kustomization type (similar to the helm Chart case)
3940

41+
const (
42+
componentConfigFilename = ".component-config.yaml"
43+
)
44+
45+
// KustomizeGeneratorOptions allows to tweak the behavior of the kustomize generator.
46+
type KustomizeGeneratorOptions struct {
47+
// If defined, only files with that suffix will be subject to templating.
48+
TemplateSuffix *string
49+
// If defined, the given left delimiter will be used to parse go templates; otherwise, defaults to '{{'
50+
LeftTemplateDelimiter *string
51+
// If defined, the given right delimiter will be used to parse go templates; otherwise, defaults to '}}'
52+
RightTemplateDelimiter *string
53+
}
54+
4055
// KustomizeGenerator is a Generator implementation that basically renders a given Kustomization.
4156
type KustomizeGenerator struct {
4257
kustomizer *krusty.Kustomizer
@@ -54,7 +69,17 @@ var _ manifests.Generator = &KustomizeGenerator{}
5469
// If fsys is nil, the local operating system filesystem will be used, and kustomizationPath can be an absolute or relative path (in the latter case it will be considered
5570
// relative to the current working directory). If fsys is non-nil, then kustomizationPath should be a relative path; if an absolute path is supplied, it will be turned
5671
// An empty kustomizationPath will be treated like ".".
57-
func NewKustomizeGenerator(fsys fs.FS, kustomizationPath string, templateSuffix string, clnt client.Client) (*KustomizeGenerator, error) {
72+
func NewKustomizeGenerator(fsys fs.FS, kustomizationPath string, clnt client.Client, options KustomizeGeneratorOptions) (*KustomizeGenerator, error) {
73+
if options.TemplateSuffix == nil {
74+
options.TemplateSuffix = ref("")
75+
}
76+
if options.LeftTemplateDelimiter == nil {
77+
options.LeftTemplateDelimiter = ref("")
78+
}
79+
if options.RightTemplateDelimiter == nil {
80+
options.RightTemplateDelimiter = ref("")
81+
}
82+
5883
g := KustomizeGenerator{
5984
files: make(map[string][]byte),
6085
templates: make(map[string]*template.Template),
@@ -72,11 +97,15 @@ func NewKustomizeGenerator(fsys fs.FS, kustomizationPath string, templateSuffix
7297
}
7398
kustomizationPath = filepath.Clean(kustomizationPath)
7499

75-
options := &krusty.Options{
100+
kustomizerOptions := &krusty.Options{
76101
LoadRestrictions: kustypes.LoadRestrictionsNone,
77102
PluginConfig: kustypes.DisabledPluginConfig(),
78103
}
79-
g.kustomizer = krusty.MakeKustomizer(options)
104+
g.kustomizer = krusty.MakeKustomizer(kustomizerOptions)
105+
106+
if err := readOptions(fsys, filepath.Clean(kustomizationPath+"/"+componentConfigFilename), &options); err != nil {
107+
return nil, err
108+
}
80109

81110
var t *template.Template
82111
// TODO: we should consider the whole of fsys, not only the subtree rooted at kustomizationPath;
@@ -100,9 +129,10 @@ func NewKustomizeGenerator(fsys fs.FS, kustomizationPath string, templateSuffix
100129
// TODO: is it ok to panic here in case of error ?
101130
panic("this cannot happen")
102131
}
103-
if strings.HasSuffix(name, templateSuffix) {
132+
if filepath.Base(name) != componentConfigFilename && strings.HasSuffix(name, *options.TemplateSuffix) {
104133
if t == nil {
105134
t = template.New(name)
135+
t.Delims(*options.LeftTemplateDelimiter, *options.RightTemplateDelimiter)
106136
t.Option("missingkey=zero").
107137
Funcs(sprig.TxtFuncMap()).
108138
Funcs(templatex.FuncMap()).
@@ -116,7 +146,7 @@ func NewKustomizeGenerator(fsys fs.FS, kustomizationPath string, templateSuffix
116146
if _, err := t.Parse(string(raw)); err != nil {
117147
return nil, err
118148
}
119-
g.templates[strings.TrimSuffix(name, templateSuffix)] = t
149+
g.templates[strings.TrimSuffix(name, *options.TemplateSuffix)] = t
120150
} else {
121151
g.files[name] = raw
122152
}
@@ -128,26 +158,26 @@ func NewKustomizeGenerator(fsys fs.FS, kustomizationPath string, templateSuffix
128158
}
129159

130160
// Create a new KustomizeGenerator as TransformableGenerator.
131-
func NewTransformableKustomizeGenerator(fsys fs.FS, kustomizationPath string, templateSuffix string, clnt client.Client) (manifests.TransformableGenerator, error) {
132-
g, err := NewKustomizeGenerator(fsys, kustomizationPath, templateSuffix, clnt)
161+
func NewTransformableKustomizeGenerator(fsys fs.FS, kustomizationPath string, clnt client.Client, options KustomizeGeneratorOptions) (manifests.TransformableGenerator, error) {
162+
g, err := NewKustomizeGenerator(fsys, kustomizationPath, clnt, options)
133163
if err != nil {
134164
return nil, err
135165
}
136166
return manifests.NewGenerator(g), nil
137167
}
138168

139169
// Create a new KustomizeGenerator with a ParameterTransformer attached (further transformers can be attached to the returned generator object).
140-
func NewKustomizeGeneratorWithParameterTransformer(fsys fs.FS, kustomizationPath string, templateSuffix string, clnt client.Client, transformer manifests.ParameterTransformer) (manifests.TransformableGenerator, error) {
141-
g, err := NewTransformableKustomizeGenerator(fsys, kustomizationPath, templateSuffix, clnt)
170+
func NewKustomizeGeneratorWithParameterTransformer(fsys fs.FS, kustomizationPath string, clnt client.Client, options KustomizeGeneratorOptions, transformer manifests.ParameterTransformer) (manifests.TransformableGenerator, error) {
171+
g, err := NewTransformableKustomizeGenerator(fsys, kustomizationPath, clnt, options)
142172
if err != nil {
143173
return nil, err
144174
}
145175
return g.WithParameterTransformer(transformer), nil
146176
}
147177

148178
// Create a new KustomizeGenerator with an ObjectTransformer attached (further transformers can be attached to the returned generator object).
149-
func NewKustomizeGeneratorWithObjectTransformer(fsys fs.FS, kustomizationPath string, templateSuffix string, clnt client.Client, transformer manifests.ObjectTransformer) (manifests.TransformableGenerator, error) {
150-
g, err := NewTransformableKustomizeGenerator(fsys, kustomizationPath, templateSuffix, clnt)
179+
func NewKustomizeGeneratorWithObjectTransformer(fsys fs.FS, kustomizationPath string, clnt client.Client, options KustomizeGeneratorOptions, transformer manifests.ObjectTransformer) (manifests.TransformableGenerator, error) {
180+
g, err := NewTransformableKustomizeGenerator(fsys, kustomizationPath, clnt, options)
151181
if err != nil {
152182
return nil, err
153183
}
@@ -279,7 +309,7 @@ func generateKustomization(fsys kustfsys.FileSystem) ([]byte, error) {
279309
if err != nil {
280310
return err
281311
}
282-
if !info.IsDir() && (strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml")) {
312+
if !info.IsDir() && !strings.HasPrefix(filepath.Base(path), ".") && (strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml")) {
283313
resources = append(resources, path)
284314
}
285315
return nil
@@ -304,3 +334,19 @@ func generateKustomization(fsys kustfsys.FileSystem) ([]byte, error) {
304334

305335
return rawKustomization, nil
306336
}
337+
338+
func readOptions(fsys fs.FS, path string, options *KustomizeGeneratorOptions) error {
339+
rawOptions, err := fs.ReadFile(fsys, path)
340+
if err != nil {
341+
if errors.Is(err, fs.ErrNotExist) {
342+
return nil
343+
}
344+
return err
345+
}
346+
347+
if err := kyaml.Unmarshal(rawOptions, options); err != nil {
348+
return err
349+
}
350+
351+
return nil
352+
}

pkg/manifests/kustomize/util.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and component-operator-runtime contributors
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package kustomize
7+
8+
// TODO: consolidate all the util files into an internal reuse package
9+
10+
func ref[T any](x T) *T {
11+
return &x
12+
}

0 commit comments

Comments
 (0)