Skip to content

Commit

Permalink
add template functions (must)lookupWithKubeConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
cbarbian-sap committed Dec 8, 2024
1 parent 9a01b9e commit f4d8702
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions internal/templatex/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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
}
}

0 comments on commit f4d8702

Please sign in to comment.