Skip to content

Commit

Permalink
feature(pkg/engine): introduce RenderWithDynamicClient
Browse files Browse the repository at this point in the history
Signed-off-by: Marcin Owsiany <porridge@redhat.com>
  • Loading branch information
porridge committed Dec 7, 2023
1 parent e6edb15 commit ed3ea03
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
28 changes: 22 additions & 6 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"text/template"

"github.com/pkg/errors"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"

"helm.sh/helm/v3/pkg/chart"
Expand All @@ -40,7 +41,9 @@ type Engine struct {
Strict bool
// In LintMode, some 'required' template values may be missing, so don't fail
LintMode bool
// the rest config to connect to the kubernetes api
// optional fake client to talk to the Kubernetes API
client *dynamic.Interface
// optional REST config to connect to the Kubernetes API
config *rest.Config
// EnableDNS tells the engine to allow DNS lookups when rendering templates
EnableDNS bool
Expand Down Expand Up @@ -85,13 +88,22 @@ func Render(chrt *chart.Chart, values chartutil.Values) (map[string]string, erro

// RenderWithClient takes a chart, optional values, and value overrides, and attempts to
// render the Go templates using the default options. This engine is client aware and so can have template
// functions that interact with the client
// functions that interact with the client.
func RenderWithClient(chrt *chart.Chart, values chartutil.Values, config *rest.Config) (map[string]string, error) {
return Engine{
config: config,
}.Render(chrt, values)
}

// RenderWithDynamicClient takes a chart, optional values, and value overrides, and attempts to
// render the Go templates using the default options. This engine is client aware and so can have template
// functions that interact with the client.
func RenderWithDynamicClient(chrt *chart.Chart, values chartutil.Values, client dynamic.Interface) (map[string]string, error) {
return Engine{
client: &client,
}.Render(chrt, values)
}

// renderable is an object that can be rendered.
type renderable struct {
// tpl is the current template.
Expand Down Expand Up @@ -192,10 +204,14 @@ func (e Engine) initFunMap(t *template.Template, referenceTpls map[string]render
return "", errors.New(warnWrap(msg))
}

// If we are not linting and have a cluster connection, provide a Kubernetes-backed
// implementation.
if !e.LintMode && e.config != nil {
funcMap["lookup"] = NewLookupFunction(e.config)
// If we are not linting and have a fake client or real cluster connection,
// provide a Kubernetes-backed implementation.
if !e.LintMode {
if e.client != nil {
funcMap["lookup"] = newLookupFunctionForClient(*e.client)
} else if e.config != nil {
funcMap["lookup"] = NewLookupFunction(e.config)
}
}

// When DNS lookups are not enabled override the sprig function and return
Expand Down
26 changes: 24 additions & 2 deletions pkg/engine/lookup_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
Expand All @@ -39,9 +40,30 @@ type lookupFunc = func(apiversion string, resource string, namespace string, nam
// This function is considered deprecated, and will be renamed in Helm 4. It will no
// longer be a public function.
func NewLookupFunction(config *rest.Config) lookupFunc {
return func(apiversion string, resource string, namespace string, name string) (map[string]interface{}, error) {
return newLookupFunctionForConfig(config)
}

func newLookupFunctionForClient(client dynamic.Interface) lookupFunc {
return newLookupFunction(func(apiVersion, kind string) (dynamic.NamespaceableResourceInterface, bool, error) {
// This is suboptimal but the best kind->resource translation we can do without a discovery client.
gvr, _ := meta.UnsafeGuessKindToResource(schema.FromAPIVersionAndKind(apiVersion, kind))
namespaced := true // we infer this from the namespace argument to the lookup function
return client.Resource(gvr), namespaced, nil
})
}

func newLookupFunctionForConfig(config *rest.Config) lookupFunc {
return newLookupFunction(func(apiVersion, kind string) (dynamic.NamespaceableResourceInterface, bool, error) {
return getDynamicClientOnKind(apiVersion, kind, config)
})
}

type clientProviderFunc func(apiVersion, kind string) (dynamic.NamespaceableResourceInterface, bool, error)

func newLookupFunction(getClient clientProviderFunc) lookupFunc {
return func(apiversion string, kind string, namespace string, name string) (map[string]interface{}, error) {
var client dynamic.ResourceInterface
c, namespaced, err := getDynamicClientOnKind(apiversion, resource, config)
c, namespaced, err := getClient(apiversion, kind)
if err != nil {
return map[string]interface{}{}, err
}
Expand Down

0 comments on commit ed3ea03

Please sign in to comment.