Skip to content

Commit

Permalink
Add support for 'Lookup' template function (#4032)
Browse files Browse the repository at this point in the history
* Add support for Lookup template function
  • Loading branch information
sgalsaleh authored Sep 19, 2023
1 parent 5de9edb commit 0a3a330
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/template/static_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/replicatedhq/kots/pkg/util"
analyze "github.com/replicatedhq/troubleshoot/pkg/analyze"
"gopkg.in/yaml.v3"
helmengine "helm.sh/helm/v3/pkg/engine"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sversion "k8s.io/apimachinery/pkg/version"
Expand Down Expand Up @@ -107,6 +108,8 @@ func (ctx StaticCtx) FuncMap() template.FuncMap {
funcMap["KubernetesMajorVersion"] = ctx.kubernetesMajorVersion
funcMap["KubernetesMinorVersion"] = ctx.kubernetesMinorVersion

funcMap["Lookup"] = ctx.lookup

return funcMap
}

Expand Down Expand Up @@ -657,3 +660,19 @@ func indent(spaces int, v string) string {
pad := strings.Repeat(" ", spaces)
return pad + strings.Replace(v, "\n", "\n"+pad, -1)
}

// use the lookup function from helm to mimic the behavior of the lookup function in helm.
func (ctx StaticCtx) lookup(apiversion string, resource string, namespace string, name string) map[string]interface{} {
config, err := k8sutil.GetClusterConfig()
if err != nil {
fmt.Printf("Failed to get cluster config: %v\n", err)
return map[string]interface{}{}
}
lookupFunc := helmengine.NewLookupFunction(config)
obj, err := lookupFunc(apiversion, resource, namespace, name)
if err != nil {
fmt.Printf("Failed to lookup %s/%s/%s: %v\n", apiversion, resource, name, err)
return map[string]interface{}{}
}
return obj
}

0 comments on commit 0a3a330

Please sign in to comment.