Skip to content

Commit e35b078

Browse files
authored
add new template functions to list objects (#343)
* add new template functions to list objects * update docs
1 parent c69ea9f commit e35b078

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

internal/templatex/functions.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import (
1616
"text/template"
1717

1818
"github.com/pkg/errors"
19+
"github.com/sap/go-generics/slices"
1920
"github.com/spf13/cast"
2021

2122
apierrors "k8s.io/apimachinery/pkg/api/errors"
2223
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24+
"k8s.io/apimachinery/pkg/labels"
2325
apitypes "k8s.io/apimachinery/pkg/types"
2426
"k8s.io/client-go/tools/clientcmd"
2527
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -55,6 +57,7 @@ func FuncMap() template.FuncMap {
5557
"formatIPv4Address": formatIPv4Address,
5658
"lookupWithKubeConfig": makeFuncLookupWithKubeConfig(true),
5759
"mustLookupWithKubeConfig": makeFuncLookupWithKubeConfig(false),
60+
"lookupListWithKubeConfig": makeFuncLookupListWithKubeConfig(),
5861
}
5962
}
6063

@@ -71,6 +74,7 @@ func FuncMapForClient(c client.Client) template.FuncMap {
7174
return template.FuncMap{
7275
"lookup": makeFuncLookup(c, true),
7376
"mustLookup": makeFuncLookup(c, false),
77+
"lookupList": makeFuncLookupList(c),
7478
}
7579
}
7680

@@ -79,6 +83,7 @@ func FuncMapForLocalClient(c client.Client) template.FuncMap {
7983
return template.FuncMap{
8084
"localLookup": makeFuncLookup(c, true),
8185
"mustLocalLookup": makeFuncLookup(c, false),
86+
"localLookupList": makeFuncLookupList(c),
8287
}
8388
}
8489

@@ -312,3 +317,43 @@ func makeFuncLookupWithKubeConfig(ignoreNotFound bool) func(string, string, stri
312317
return object.UnstructuredContent(), nil
313318
}
314319
}
320+
321+
func makeFuncLookupList(c client.Client) func(string, string, string, string) ([]map[string]any, error) {
322+
return func(apiVersion string, kind string, namespace string, labelSelector string) ([]map[string]any, error) {
323+
objectList := &unstructured.UnstructuredList{}
324+
objectList.SetAPIVersion(apiVersion)
325+
objectList.SetKind(kind)
326+
selector, err := labels.Parse(labelSelector)
327+
if err != nil {
328+
return nil, err
329+
}
330+
if err := c.List(context.Background(), objectList, client.InNamespace(namespace), client.MatchingLabelsSelector{Selector: selector}); err != nil {
331+
return nil, err
332+
}
333+
return slices.Collect(objectList.Items, func(object unstructured.Unstructured) map[string]any { return object.UnstructuredContent() }), nil
334+
}
335+
}
336+
337+
func makeFuncLookupListWithKubeConfig() func(string, string, string, string, string) ([]map[string]any, error) {
338+
return func(apiVersion string, kind string, namespace string, labelSelector string, kubeConfig string) ([]map[string]any, error) {
339+
cfg, err := clientcmd.RESTConfigFromKubeConfig([]byte(kubeConfig))
340+
if err != nil {
341+
return nil, err
342+
}
343+
c, err := client.New(cfg, client.Options{})
344+
if err != nil {
345+
return nil, err
346+
}
347+
objectList := &unstructured.UnstructuredList{}
348+
objectList.SetAPIVersion(apiVersion)
349+
objectList.SetKind(kind)
350+
selector, err := labels.Parse(labelSelector)
351+
if err != nil {
352+
return nil, err
353+
}
354+
if err := c.List(context.Background(), objectList, client.InNamespace(namespace), client.MatchingLabelsSelector{Selector: selector}); err != nil {
355+
return nil, err
356+
}
357+
return slices.Collect(objectList.Items, func(object unstructured.Unstructured) map[string]any { return object.UnstructuredContent() }), nil
358+
}
359+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ and the well-known template function set that is available in Helm can be used;
4747
| `mustLocalLookup <apiVersion string, kind string, namespace string, name string>` | Lookup a Kubernetes resource with the local client; fail in case of 404 (not found) errors. |
4848
| `lookupWithKubeConfig <apiVersion string, kind string, namespace string, name string kubeConfig string> ` | Lookup a Kubernetes resource with the given kubeconfig; return nil in case of 404 (not found) errors. |
4949
| `mustLookupWithKubeConfig <apiVersion string, kind string, namespace string, name string, kubeConfig string>` | Lookup a Kubernetes resource with the given kubeconfig; fail in case of 404 (not found) errors. |
50+
| `lookupList <apiVersion string, kind string, namespace string, labelSelector string>` | Lookup (list) Kubernetes resources with the target client. |
51+
| `localLookupList <apiVersion string, kind string, namespace string, labelSelector string>` | Lookup (list) Kubernetes resources with the local client. |
52+
| `lookupListWithKubeConfig <apiVersion string, kind string, namespace string, labelSelector string kubeConfig string> ` | Lookup (list) Kubernetes resources with the given kubeconfig. |
5053
| `listFiles <pattern string>` | List files relative to the provided kustomization directory, matching the given [pattern](https://pkg.go.dev/github.com/gobwas/glob). |
5154
| `existsFile <path string>` | Check if the given file path exists, relative to the provided kustomization directory. |
5255
| `readFile <path string>` | Read the given file, relative to the provided kustomization directory. |

0 commit comments

Comments
 (0)