Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 'Lookup' template function #4032

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading