Skip to content

Commit

Permalink
feat(deep-links): sprig support (#14660) (#14676)
Browse files Browse the repository at this point in the history
Signed-off-by: daftping <21245083+daftping@users.noreply.github.com>
Co-authored-by: daftping <21245083+daftping@users.noreply.github.com>
  • Loading branch information
gcp-cherry-pick-bot[bot] and daftping authored Jul 24, 2023
1 parent dd61b74 commit d9975b2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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

0 comments on commit d9975b2

Please sign in to comment.