Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions internal/templatex/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -55,6 +57,7 @@ func FuncMap() template.FuncMap {
"formatIPv4Address": formatIPv4Address,
"lookupWithKubeConfig": makeFuncLookupWithKubeConfig(true),
"mustLookupWithKubeConfig": makeFuncLookupWithKubeConfig(false),
"lookupListWithKubeConfig": makeFuncLookupListWithKubeConfig(),
}
}

Expand All @@ -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),
}
}

Expand All @@ -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),
}
}

Expand Down Expand Up @@ -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
}
}
3 changes: 3 additions & 0 deletions website/content/en/docs/generators/kustomize.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ and the well-known template function set that is available in Helm can be used;
| `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. |
| `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. |
| `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. |
| `lookupList <apiVersion string, kind string, namespace string, labelSelector string>` | Lookup (list) Kubernetes resources with the target client. |
| `localLookupList <apiVersion string, kind string, namespace string, labelSelector string>` | Lookup (list) Kubernetes resources with the local client. |
| `lookupListWithKubeConfig <apiVersion string, kind string, namespace string, labelSelector string kubeConfig string> ` | Lookup (list) Kubernetes resources with the given kubeconfig. |
| `listFiles <pattern string>` | List files relative to the provided kustomization directory, matching the given [pattern](https://pkg.go.dev/github.com/gobwas/glob). |
| `existsFile <path string>` | Check if the given file path exists, relative to the provided kustomization directory. |
| `readFile <path string>` | Read the given file, relative to the provided kustomization directory. |
Expand Down
Loading