Skip to content

Commit 24e0de1

Browse files
committed
add localLookup/mustLocalLookup template functions in helm/kustomize generator
1 parent dec40a6 commit 24e0de1

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

pkg/manifests/helm.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,17 @@ type HelmGenerator struct {
4444
var _ Generator = &HelmGenerator{}
4545

4646
// Create a new HelmGenerator.
47-
// Deprecation warning: the parameters name, client and discoveryClient are ignored (can be passed as empty resp. nil) and will be removed in a future release;
47+
// Deprecation warning: the parameters name and discoveryClient are ignored (can be passed as empty resp. nil) and will be removed in a future release;
4848
// the according values will be retrieved from the context passed to Generate().
49+
// The parameter client should be a client for the local cluster (i.e. the cluster where the component object resides);
50+
// it is used by the localLookup and mustLocalLookup template functions.
4951
// If fsys is nil, the local operating system filesystem will be used, and chartPath can be an absolute or relative path (in the latter case it will be considered
5052
// relative to the current working directory). If fsys is non-nil, then chartPath should be a relative path; if an absolute path is supplied, it will be turned
5153
// An empty chartPath will be treated like ".".
5254
func NewHelmGenerator(name string, fsys fs.FS, chartPath string, client client.Client, discoveryClient discovery.DiscoveryInterface) (*HelmGenerator, error) {
53-
g := HelmGenerator{}
54-
g.data = make(map[string]any)
55+
g := HelmGenerator{
56+
data: make(map[string]any),
57+
}
5558

5659
if fsys == nil {
5760
fsys = os.DirFS("/")
@@ -157,6 +160,7 @@ func NewHelmGenerator(name string, fsys fs.FS, chartPath string, client client.C
157160
Funcs(sprig.TxtFuncMap()).
158161
Funcs(templatex.FuncMap()).
159162
Funcs(templatex.FuncMapForTemplate(t)).
163+
Funcs(templatex.FuncMapForLocalClient(client)).
160164
Funcs(templatex.FuncMapForClient(nil))
161165
} else {
162166
t = t.New(manifest)

pkg/manifests/kustomize.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ type KustomizeGenerator struct {
4141
var _ Generator = &KustomizeGenerator{}
4242

4343
// Create a new KustomizeGenerator.
44-
// Deprecation warning: the parameter client is ignored (can be passed as nil) and will be removed in a future release;
45-
// the according value will be retrieved from the context passed to Generate().
44+
// The parameter client should be a client for the local cluster (i.e. the cluster where the component object resides);
45+
// it is used by the localLookup and mustLocalLookup template functions.
4646
// 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
4747
// 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
4848
// An empty kustomizationPath will be treated like ".".
@@ -71,6 +71,11 @@ func NewKustomizeGenerator(fsys fs.FS, kustomizationPath string, templateSuffix
7171
g.kustomizer = krusty.MakeKustomizer(options)
7272

7373
var t *template.Template
74+
// TODO: we should consider the whole of fsys, not only the subtree rooted at kustomizationPath;
75+
// this would allow people to reference resources or patches or components located in parent directories
76+
// (which is probably a common usecase); however it has to be clarified how to handle template scopes;
77+
// for example it might be desired that subtrees with a kustomization.yaml file are processed in an own
78+
// template context
7479
files, err := find(fsys, kustomizationPath, "*", fileTypeRegular, 0)
7580
if err != nil {
7681
return nil, err
@@ -94,6 +99,7 @@ func NewKustomizeGenerator(fsys fs.FS, kustomizationPath string, templateSuffix
9499
Funcs(sprig.TxtFuncMap()).
95100
Funcs(templatex.FuncMap()).
96101
Funcs(templatex.FuncMapForTemplate(t)).
102+
Funcs(templatex.FuncMapForLocalClient(client)).
97103
Funcs(templatex.FuncMapForClient(nil)).
98104
Funcs(funcMapForGenerateContext(nil, "", ""))
99105
} else {

website/content/en/docs/generators/helm.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Here:
2525
- `name` is deprecated and will be removed in future releases; it can be passed as empty string.
2626
- `fsys` must be an implementation of `fs.FS`, such as `embed.FS`; or it can be passed as nil; then, all file operations will be executed on the current OS filesystem.
2727
- `chartPath` is the path containing the used Helm chart; if `fsys` was provided, this has to be a relative path; otherwise, it will be interpreted with respect to the OS filesystem (as an absolute path, or relative to the current working directory of the controller).
28-
- `client` and `discoveryClient` are deprecated and will be removed in future releases; they can be passed as nil.
28+
- `client` should be a client for the local cluster (i.e. the cluster where the component object exists).
29+
- `discoveryClient` is deprecated and will be removed in future releases; they can be passed as nil.
2930

3031
It should be noted that `HelmGenerator` does not use the Helm SDK; instead it tries to emulate the Helm behavior as good as possible.
3132
A few differences and restrictions arise from this:

website/content/en/docs/generators/kustomize.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ Here:
3232
- `fsys` must be an implementation of `fs.FS`, such as `embed.FS`; or it can be passed as nil; then, all file operations will be executed on the current OS filesystem.
3333
- `kustomizationPath` is the path containing the (potentially templatized) kustomatization; if `fsys` was provided, this has to be a relative path; otherwise, it will be interpreted with respect to the OS filesystem (as an absolute path, or relative to the current working directory of the controller).
3434
- `templateSuffx` is optional; if empty, all files under `kustomizationPath` will be subject to go templating; otherwise, only files matching the specified suffix will be considered as templates.
35-
- `client` is deprecated and will be removed in future releases; it can be passed as nil.
35+
- `client` should be a client for the local cluster (i.e. the cluster where the component object exists).

0 commit comments

Comments
 (0)