From b378b15a8b9808f978b32e67442c41b36945592b Mon Sep 17 00:00:00 2001 From: Christoph Barbian Date: Mon, 3 Nov 2025 18:56:51 +0100 Subject: [PATCH 1/2] add new template functions to list objects --- internal/templatex/functions.go | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/internal/templatex/functions.go b/internal/templatex/functions.go index 7199b9b9..020f5da9 100644 --- a/internal/templatex/functions.go +++ b/internal/templatex/functions.go @@ -16,10 +16,12 @@ import ( "text/template" "github.com/pkg/errors" + "github.com/sap/go-generics/slices" "github.com/spf13/cast" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/labels" apitypes "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/clientcmd" "sigs.k8s.io/controller-runtime/pkg/client" @@ -55,6 +57,7 @@ func FuncMap() template.FuncMap { "formatIPv4Address": formatIPv4Address, "lookupWithKubeConfig": makeFuncLookupWithKubeConfig(true), "mustLookupWithKubeConfig": makeFuncLookupWithKubeConfig(false), + "lookupListWithKubeConfig": makeFuncLookupListWithKubeConfig(), } } @@ -71,6 +74,7 @@ func FuncMapForClient(c client.Client) template.FuncMap { return template.FuncMap{ "lookup": makeFuncLookup(c, true), "mustLookup": makeFuncLookup(c, false), + "lookupList": makeFuncLookupList(c), } } @@ -79,6 +83,7 @@ func FuncMapForLocalClient(c client.Client) template.FuncMap { return template.FuncMap{ "localLookup": makeFuncLookup(c, true), "mustLocalLookup": makeFuncLookup(c, false), + "localLookupList": makeFuncLookupList(c), } } @@ -312,3 +317,43 @@ func makeFuncLookupWithKubeConfig(ignoreNotFound bool) func(string, string, stri return object.UnstructuredContent(), nil } } + +func makeFuncLookupList(c client.Client) func(string, string, string, string) ([]map[string]any, error) { + return func(apiVersion string, kind string, namespace string, labelSelector string) ([]map[string]any, error) { + objectList := &unstructured.UnstructuredList{} + objectList.SetAPIVersion(apiVersion) + objectList.SetKind(kind) + selector, err := labels.Parse(labelSelector) + if err != nil { + return nil, err + } + if err := c.List(context.Background(), objectList, client.InNamespace(namespace), client.MatchingLabelsSelector{Selector: selector}); err != nil { + return nil, err + } + return slices.Collect(objectList.Items, func(object unstructured.Unstructured) map[string]any { return object.UnstructuredContent() }), nil + } +} + +func makeFuncLookupListWithKubeConfig() func(string, string, string, string, string) ([]map[string]any, error) { + return func(apiVersion string, kind string, namespace string, labelSelector string, kubeConfig string) ([]map[string]any, error) { + cfg, err := clientcmd.RESTConfigFromKubeConfig([]byte(kubeConfig)) + if err != nil { + return nil, err + } + c, err := client.New(cfg, client.Options{}) + if err != nil { + return nil, err + } + objectList := &unstructured.UnstructuredList{} + objectList.SetAPIVersion(apiVersion) + objectList.SetKind(kind) + selector, err := labels.Parse(labelSelector) + if err != nil { + return nil, err + } + if err := c.List(context.Background(), objectList, client.InNamespace(namespace), client.MatchingLabelsSelector{Selector: selector}); err != nil { + return nil, err + } + return slices.Collect(objectList.Items, func(object unstructured.Unstructured) map[string]any { return object.UnstructuredContent() }), nil + } +} From ac99c871348c20d000199f10b471e3993cdff098 Mon Sep 17 00:00:00 2001 From: Christoph Barbian Date: Mon, 3 Nov 2025 19:10:39 +0100 Subject: [PATCH 2/2] update docs --- website/content/en/docs/generators/kustomize.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/website/content/en/docs/generators/kustomize.md b/website/content/en/docs/generators/kustomize.md index cbfacaed..63dda488 100644 --- a/website/content/en/docs/generators/kustomize.md +++ b/website/content/en/docs/generators/kustomize.md @@ -47,6 +47,9 @@ and the well-known template function set that is available in Helm can be used; | `mustLocalLookup ` | Lookup a Kubernetes resource with the local client; fail in case of 404 (not found) errors. | | `lookupWithKubeConfig ` | Lookup a Kubernetes resource with the given kubeconfig; return nil in case of 404 (not found) errors. | | `mustLookupWithKubeConfig ` | Lookup a Kubernetes resource with the given kubeconfig; fail in case of 404 (not found) errors. | +| `lookupList ` | Lookup (list) Kubernetes resources with the target client. | +| `localLookupList ` | Lookup (list) Kubernetes resources with the local client. | +| `lookupListWithKubeConfig ` | Lookup (list) Kubernetes resources with the given kubeconfig. | | `listFiles ` | List files relative to the provided kustomization directory, matching the given [pattern](https://pkg.go.dev/github.com/gobwas/glob). | | `existsFile ` | Check if the given file path exists, relative to the provided kustomization directory. | | `readFile ` | Read the given file, relative to the provided kustomization directory. |