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

feat(deep-links): sprig support (cherry-pick #14660) #14676

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion server/deeplinks/deeplinks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"text/template"

"github.com/Masterminds/sprig/v3"
"github.com/antonmedv/expr"
"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
Expand All @@ -14,6 +15,15 @@ import (
"k8s.io/utils/pointer"
)

var sprigFuncMap = sprig.GenericFuncMap() // a singleton for better performance

func init() {
// Avoid allowing the user to learn things about the environment.
delete(sprigFuncMap, "env")
delete(sprigFuncMap, "expandenv")
delete(sprigFuncMap, "getHostByName")
}

const (
ResourceDeepLinkKey = "resource"
AppDeepLinkKey = "application"
Expand Down Expand Up @@ -71,7 +81,7 @@ func EvaluateDeepLinksResponse(obj map[string]interface{}, name string, links []
finalLinks := []*application.LinkInfo{}
errors := []string{}
for _, link := range links {
t, err := template.New("deep-link").Parse(link.URL)
t, err := template.New("deep-link").Funcs(sprigFuncMap).Parse(link.URL)
if err != nil {
errors = append(errors, fmt.Sprintf("failed to parse link template '%v', error=%v", link.URL, err.Error()))
continue
Expand Down
16 changes: 16 additions & 0 deletions server/deeplinks/deeplinks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ func TestDeepLinks(t *testing.T) {
}},
error: []string{"link condition '1 + 1' evaluated to non-boolean value for resource test"},
},
{
appObj: appObj,
resourceObj: resourceObj,
projectObj: projectObj,
clusterObj: clusterObj,
inputLinks: []settings.DeepLink{{
Title: "link",
URL: "http://example.com/{{ .cluster.name | replace \"-\" \"_\" }}&{{ first .project.spec.sourceRepos }}",
Condition: pointer.String(`application.metadata.name == "test" && project.metadata.name == "test-project"`),
}},
outputLinks: []*application.LinkInfo{{
Title: pointer.String("link"),
Url: pointer.String("http://example.com/test_cluster&test-repo.git"),
}},
error: []string{},
},
}

for _, tc := range testTable {
Expand Down