@@ -21,6 +21,7 @@ import (
2121 apierrors "k8s.io/apimachinery/pkg/api/errors"
2222 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2323 apitypes "k8s.io/apimachinery/pkg/types"
24+ "k8s.io/client-go/tools/clientcmd"
2425 "sigs.k8s.io/controller-runtime/pkg/client"
2526 kyaml "sigs.k8s.io/yaml"
2627)
@@ -65,8 +66,10 @@ func FuncMapForTemplate(t *template.Template) template.FuncMap {
6566// template FuncMap generator for functions called in target Kubernetes context
6667func FuncMapForClient (c client.Client ) template.FuncMap {
6768 return template.FuncMap {
68- "lookup" : makeFuncLookup (c , true ),
69- "mustLookup" : makeFuncLookup (c , false ),
69+ "lookup" : makeFuncLookup (c , true ),
70+ "mustLookup" : makeFuncLookup (c , false ),
71+ "lookupWithKubeConfig" : makeFuncLookupWithKubeConfig (true ),
72+ "mustLookupWithKubeConfig" : makeFuncLookupWithKubeConfig (false ),
7073 }
7174}
7275
@@ -284,3 +287,27 @@ func makeFuncLookup(c client.Client, ignoreNotFound bool) func(string, string, s
284287 return object .UnstructuredContent (), nil
285288 }
286289}
290+
291+ func makeFuncLookupWithKubeConfig (ignoreNotFound bool ) func (string , string , string , string , string ) (map [string ]any , error ) {
292+ return func (apiVersion string , kind string , namespace string , name string , kubeConfig string ) (map [string ]any , error ) {
293+ cfg , err := clientcmd .RESTConfigFromKubeConfig ([]byte (kubeConfig ))
294+ if err != nil {
295+ return map [string ]any {}, err
296+ }
297+ c , err := client .New (cfg , client.Options {})
298+ if err != nil {
299+ return map [string ]any {}, err
300+ }
301+ object := & unstructured.Unstructured {}
302+ object .SetAPIVersion (apiVersion )
303+ object .SetKind (kind )
304+ if err := c .Get (context .Background (), apitypes.NamespacedName {Namespace : namespace , Name : name }, object ); err != nil {
305+ // TODO: should apimeta.IsNoMatchError be ignored as well?
306+ if apierrors .IsNotFound (err ) && ignoreNotFound {
307+ err = nil
308+ }
309+ return map [string ]any {}, err
310+ }
311+ return object .UnstructuredContent (), nil
312+ }
313+ }
0 commit comments