added rest client passed with action configuration

Signed-off-by: raffaelespazzoli <raffaele.spazzoli@gmail.com>
pull/6752/head
raffaelespazzoli 5 years ago
parent b3495c7353
commit e98cd621f0

@ -436,7 +436,12 @@ func (c *Configuration) renderResources(ch *chart.Chart, values chartutil.Values
} }
} }
files, err := engine.Render(ch, values) rest, err := c.RESTClientGetter.ToRESTConfig()
if err != nil {
return hs, b, "", err
}
files, err := engine.RenderWithClient(ch, values, rest)
if err != nil { if err != nil {
return hs, b, "", err return hs, b, "", err
} }

@ -27,6 +27,7 @@ import (
"text/template" "text/template"
"github.com/pkg/errors" "github.com/pkg/errors"
"k8s.io/client-go/rest"
"helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/chartutil"
@ -39,6 +40,8 @@ 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 te kubernetes api
config *rest.Config
} }
// Render takes a chart, optional values, and value overrides, and attempts to render the Go templates. // Render takes a chart, optional values, and value overrides, and attempts to render the Go templates.
@ -71,6 +74,15 @@ func Render(chrt *chart.Chart, values chartutil.Values) (map[string]string, erro
return new(Engine).Render(chrt, values) return new(Engine).Render(chrt, values)
} }
// 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
func RenderWithClient(chrt *chart.Chart, values chartutil.Values, config *rest.Config) (map[string]string, error) {
return Engine{
config: config,
}.Render(chrt, values)
}
// renderable is an object that can be rendered. // renderable is an object that can be rendered.
type renderable struct { type renderable struct {
// tpl is the current template. // tpl is the current template.
@ -157,6 +169,9 @@ func (e Engine) initFunMap(t *template.Template, referenceTpls map[string]render
} }
return val, nil return val, nil
} }
if e.config != nil {
funcMap["lookup"] = NewLookupFunction(e.config)
}
t.Funcs(funcMap) t.Funcs(funcMap)
} }

@ -53,7 +53,6 @@ func funcMap() template.FuncMap {
"fromYaml": fromYAML, "fromYaml": fromYAML,
"toJson": toJSON, "toJson": toJSON,
"fromJson": fromJSON, "fromJson": fromJSON,
"lookup": lookup,
// This is a placeholder for the "include" function, which is // This is a placeholder for the "include" function, which is
// late-bound to a template. By declaring it here, we preserve the // late-bound to a template. By declaring it here, we preserve the

@ -17,10 +17,7 @@ limitations under the License.
package engine package engine
import ( import (
"flag"
"log" "log"
"os"
"path/filepath"
"strings" "strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -28,67 +25,50 @@ import (
"k8s.io/client-go/discovery" "k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic" "k8s.io/client-go/dynamic"
"k8s.io/client-go/rest" "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
) )
var config *rest.Config type lookupFunc = func(apiversion string, resource string, namespace string, name string) (map[string]interface{}, error)
func init() { func NewLookupFunction(config *rest.Config) lookupFunc {
// try the out-cluster config, this will default to the in-cluster config is not successful return func(apiversion string, resource string, namespace string, name string) (map[string]interface{}, error) {
var kubeconfig *string var client dynamic.ResourceInterface
if home := homeDir(); home != "" { c, namespaced, err := getDynamicClientOnKind(apiversion, resource, config)
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") if err != nil {
} else { return map[string]interface{}{}, err
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file") }
} if namespaced && namespace != "" {
flag.Parse() client = c.Namespace(namespace)
} else {
// use the current context in kubeconfig client = c
config1, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) }
if err == nil { if name != "" {
config = config1 //this will return a single object
} obj, err := client.Get(name, metav1.GetOptions{})
} if err != nil {
return map[string]interface{}{}, err
func homeDir() string { }
if h := os.Getenv("HOME"); h != "" { return obj.Object, nil
return h }
} //this will return a list
return os.Getenv("USERPROFILE") // windows obj, err := client.List(metav1.ListOptions{})
}
func lookup(apiversion string, resource string, namespace string, name string) (map[string]interface{}, error) {
var client dynamic.ResourceInterface
c, namespaced, err := getDynamicClientOnKind(apiversion, resource)
if err != nil {
return map[string]interface{}{}, err
}
if namespaced && namespace != "" {
client = c.Namespace(namespace)
} else {
client = c
}
if name != "" {
//this will return a single object
obj, err := client.Get(name, metav1.GetOptions{})
if err != nil { if err != nil {
return map[string]interface{}{}, err return map[string]interface{}{}, err
} }
return obj.Object, nil return obj.Object, nil
} }
//this will return a list
obj, err := client.List(metav1.ListOptions{})
if err != nil {
return map[string]interface{}{}, err
}
return obj.Object, nil
} }
// func homeDir() string {
// if h := os.Getenv("HOME"); h != "" {
// return h
// }
// return os.Getenv("USERPROFILE") // windows
// }
// GetDynamicClientOnUnstructured returns a dynamic client on an Unstructured type. This client can be further namespaced. // GetDynamicClientOnUnstructured returns a dynamic client on an Unstructured type. This client can be further namespaced.
func getDynamicClientOnKind(apiversion string, kind string) (dynamic.NamespaceableResourceInterface, bool, error) { func getDynamicClientOnKind(apiversion string, kind string, config *rest.Config) (dynamic.NamespaceableResourceInterface, bool, error) {
gvk := schema.FromAPIVersionAndKind(apiversion, kind) gvk := schema.FromAPIVersionAndKind(apiversion, kind)
apiRes, err := getAPIReourceForGVK(gvk) apiRes, err := getAPIReourceForGVK(gvk, config)
if err != nil { if err != nil {
log.Printf("[ERROR] unable to get apiresource from unstructured: %s , error %s", gvk.String(), err) log.Printf("[ERROR] unable to get apiresource from unstructured: %s , error %s", gvk.String(), err)
return nil, false, err return nil, false, err
@ -107,7 +87,7 @@ func getDynamicClientOnKind(apiversion string, kind string) (dynamic.Namespaceab
return res, apiRes.Namespaced, nil return res, apiRes.Namespaced, nil
} }
func getAPIReourceForGVK(gvk schema.GroupVersionKind) (metav1.APIResource, error) { func getAPIReourceForGVK(gvk schema.GroupVersionKind, config *rest.Config) (metav1.APIResource, error) {
res := metav1.APIResource{} res := metav1.APIResource{}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config) discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil { if err != nil {

Loading…
Cancel
Save