feature(pkg/engine): introduce RenderWithClientProvider

Signed-off-by: Marcin Owsiany <porridge@redhat.com>
pull/12617/head
Marcin Owsiany 1 year ago
parent e6edb15067
commit bfec4ec926

@ -40,16 +40,17 @@ type Engine struct {
Strict bool Strict bool
// In LintMode, some 'required' template values may be missing, so don't fail // In LintMode, some 'required' template values may be missing, so don't fail
LintMode bool LintMode bool
// the rest config to connect to the kubernetes api // optional provider of clients to talk to the Kubernetes API
config *rest.Config clientProvider *ClientProvider
// EnableDNS tells the engine to allow DNS lookups when rendering templates // EnableDNS tells the engine to allow DNS lookups when rendering templates
EnableDNS bool EnableDNS bool
} }
// New creates a new instance of Engine using the passed in rest config. // New creates a new instance of Engine using the passed in rest config.
func New(config *rest.Config) Engine { func New(config *rest.Config) Engine {
var clientProvider ClientProvider = clientProviderFromConfig{config}
return Engine{ return Engine{
config: config, clientProvider: &clientProvider,
} }
} }
@ -85,10 +86,21 @@ func Render(chrt *chart.Chart, values chartutil.Values) (map[string]string, erro
// RenderWithClient takes a chart, optional values, and value overrides, and attempts to // 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 // 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) { func RenderWithClient(chrt *chart.Chart, values chartutil.Values, config *rest.Config) (map[string]string, error) {
var clientProvider ClientProvider = clientProviderFromConfig{config}
return Engine{ return Engine{
config: config, clientProvider: &clientProvider,
}.Render(chrt, values)
}
// RenderWithClientProvider 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.
// This function differs from RenderWithClient in that it lets you customize the way a dynamic client is constructed.
func RenderWithClientProvider(chrt *chart.Chart, values chartutil.Values, clientProvider ClientProvider) (map[string]string, error) {
return Engine{
clientProvider: &clientProvider,
}.Render(chrt, values) }.Render(chrt, values)
} }
@ -194,8 +206,8 @@ func (e Engine) initFunMap(t *template.Template, referenceTpls map[string]render
// If we are not linting and have a cluster connection, provide a Kubernetes-backed // If we are not linting and have a cluster connection, provide a Kubernetes-backed
// implementation. // implementation.
if !e.LintMode && e.config != nil { if !e.LintMode && e.clientProvider != nil {
funcMap["lookup"] = NewLookupFunction(e.config) funcMap["lookup"] = newLookupFunction(*e.clientProvider)
} }
// When DNS lookups are not enabled override the sprig function and return // When DNS lookups are not enabled override the sprig function and return

@ -39,9 +39,28 @@ 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 // This function is considered deprecated, and will be renamed in Helm 4. It will no
// longer be a public function. // longer be a public function.
func NewLookupFunction(config *rest.Config) lookupFunc { func NewLookupFunction(config *rest.Config) lookupFunc {
return func(apiversion string, resource string, namespace string, name string) (map[string]interface{}, error) { return newLookupFunction(clientProviderFromConfig{config: config})
}
type ClientProvider interface {
// GetClientFor returns a dynamic.NamespaceableResourceInterface suitable for interacting with resources
// corresponding to the provided apiVersion and kind, as well as a boolean indicating whether the resources
// are namespaced.
GetClientFor(apiVersion, kind string) (dynamic.NamespaceableResourceInterface, bool, error)
}
type clientProviderFromConfig struct {
config *rest.Config
}
func (c clientProviderFromConfig) GetClientFor(apiVersion, kind string) (dynamic.NamespaceableResourceInterface, bool, error) {
return getDynamicClientOnKind(apiVersion, kind, c.config)
}
func newLookupFunction(clientProvider ClientProvider) lookupFunc {
return func(apiversion string, kind string, namespace string, name string) (map[string]interface{}, error) {
var client dynamic.ResourceInterface var client dynamic.ResourceInterface
c, namespaced, err := getDynamicClientOnKind(apiversion, resource, config) c, namespaced, err := clientProvider.GetClientFor(apiversion, kind)
if err != nil { if err != nil {
return map[string]interface{}{}, err return map[string]interface{}{}, err
} }

Loading…
Cancel
Save