From f4d870267a352fcef8932892be3bfbe60b49fd46 Mon Sep 17 00:00:00 2001 From: Christoph Barbian Date: Sun, 8 Dec 2024 11:17:01 +0100 Subject: [PATCH] add template functions (must)lookupWithKubeConfig --- internal/templatex/functions.go | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/internal/templatex/functions.go b/internal/templatex/functions.go index 33392c9..036ced1 100644 --- a/internal/templatex/functions.go +++ b/internal/templatex/functions.go @@ -21,6 +21,7 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" apitypes "k8s.io/apimachinery/pkg/types" + "k8s.io/client-go/tools/clientcmd" "sigs.k8s.io/controller-runtime/pkg/client" kyaml "sigs.k8s.io/yaml" ) @@ -65,8 +66,10 @@ func FuncMapForTemplate(t *template.Template) template.FuncMap { // template FuncMap generator for functions called in target Kubernetes context func FuncMapForClient(c client.Client) template.FuncMap { return template.FuncMap{ - "lookup": makeFuncLookup(c, true), - "mustLookup": makeFuncLookup(c, false), + "lookup": makeFuncLookup(c, true), + "mustLookup": makeFuncLookup(c, false), + "lookupWithKubeConfig": makeFuncLookupWithKubeConfig(true), + "mustLookupWithKubeConfig": makeFuncLookupWithKubeConfig(false), } } @@ -284,3 +287,27 @@ func makeFuncLookup(c client.Client, ignoreNotFound bool) func(string, string, s return object.UnstructuredContent(), nil } } + +func makeFuncLookupWithKubeConfig(ignoreNotFound bool) func(string, string, string, string, []byte) (map[string]any, error) { + return func(apiVersion string, kind string, namespace string, name string, kubeConfig []byte) (map[string]any, error) { + cfg, err := clientcmd.RESTConfigFromKubeConfig(kubeConfig) + if err != nil { + return map[string]any{}, err + } + c, err := client.New(cfg, client.Options{}) + if err != nil { + return map[string]any{}, err + } + object := &unstructured.Unstructured{} + object.SetAPIVersion(apiVersion) + object.SetKind(kind) + if err := c.Get(context.Background(), apitypes.NamespacedName{Namespace: namespace, Name: name}, object); err != nil { + // TODO: should apimeta.IsNoMatchError be ignored as well? + if apierrors.IsNotFound(err) && ignoreNotFound { + err = nil + } + return map[string]any{}, err + } + return object.UnstructuredContent(), nil + } +}