Skip to content

Commit

Permalink
feat: add Sprig to templates for additional templating capabilities
Browse files Browse the repository at this point in the history
Signed-off-by: Adrien Fillon <[email protected]>
  • Loading branch information
adrien-f authored and vsukhin committed Sep 12, 2023
1 parent 8846771 commit 4d20552
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 43 deletions.
6 changes: 3 additions & 3 deletions cmd/kubectl-testkube/commands/common/render/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"encoding/json"
"io"
"os"
"text/template"

"gopkg.in/yaml.v2"

"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/kubeshop/testkube/pkg/utils"
)

type OutputType string
Expand All @@ -32,7 +32,7 @@ func RenderYaml(obj interface{}, w io.Writer) error {
}

func RenderGoTemplate(item interface{}, w io.Writer, tpl string) error {
tmpl, err := template.New("result").Parse(tpl)
tmpl, err := utils.NewTemplate("result").Parse(tpl)
if err != nil {
return err
}
Expand All @@ -41,7 +41,7 @@ func RenderGoTemplate(item interface{}, w io.Writer, tpl string) error {
}

func RenderGoTemplateList(list []interface{}, w io.Writer, tpl string) error {
tmpl, err := template.New("result").Parse(tpl)
tmpl, err := utils.NewTemplate("result").Parse(tpl)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubectl-testkube/commands/github/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"bytes"
"errors"
"fmt"
"html/template"

prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared"
"github.com/skratchdot/open-golang/open"

"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/kubeshop/testkube/pkg/utils"
)

const (
Expand Down Expand Up @@ -76,7 +76,7 @@ func buildTicket(d testkube.DebugInfo) (string, string, error) {
if d.ClientVersion == "" || d.ClusterVersion == "" {
return "", "", errors.New("client version and cluster version must be populated to create debug message")
}
t, err := template.New("debug").Parse(Template)
t, err := utils.NewTemplate("debug").Parse(Template)
if err != nil {
return "", "", fmt.Errorf("cannot create template: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions contrib/executor/curl/pkg/runner/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package runner

import (
"strings"
"text/template"

"github.com/kubeshop/testkube/pkg/utils"
)

// ResolveTemplates fills the string array with the values if they are templated
Expand All @@ -21,7 +22,7 @@ func ResolveTemplates(stringsToResolve []string, params map[string]string) error
// ResolveTemplate fills a string with the values if they are templated
func ResolveTemplate(stringToResolve string, params map[string]string) (string, error) {

ut, err := template.New("cmd").Parse(stringToResolve)
ut, err := utils.NewTemplate("cmd").Parse(stringToResolve)

if err != nil {
return "", err
Expand Down
2 changes: 2 additions & 0 deletions docs/docs/articles/templates.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import TabItem from "@theme/TabItem";

Templates allow you to store templates for other resources used in Testkube. We support a list of templates job | container | cronjob | scraper | pvc | webhook. To define templates in Testkube, you'll need to provide a template body (in Golang template format) and a type of the template.

TestKube provides access to the [Sprig](http://masterminds.github.io/sprig/) functions library in templates.

## Creating a Template
The template can be created using the API, CLI, or a Custom Resource.

Expand Down
13 changes: 7 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/fasthttp/websocket v1.5.0
github.com/fluxcd/pkg/apis/event v0.2.0
github.com/gabriel-vasile/mimetype v1.4.1
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572
github.com/gofiber/adaptor/v2 v2.1.29
github.com/gofiber/fiber/v2 v2.39.0
github.com/gofiber/websocket/v2 v2.1.1
Expand Down Expand Up @@ -154,8 +155,8 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.9
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0
github.com/imdario/mergo v0.3.13 // indirect
github.com/google/uuid v1.3.1
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.15 // indirect
Expand Down Expand Up @@ -184,13 +185,13 @@ require (
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/oauth2 v0.1.0
golang.org/x/sync v0.3.0
golang.org/x/sys v0.9.0 // indirect
golang.org/x/term v0.9.0 // indirect
golang.org/x/text v0.10.0
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.12.0 // indirect
golang.org/x/text v0.13.0
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
26 changes: 14 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl
github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ=
github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/gofiber/adaptor/v2 v2.1.29 h1:JnYd6fbqVM9D4zPchk+kg89PfxyuKqZKhBWGQDHfKH4=
Expand Down Expand Up @@ -314,8 +316,9 @@ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm4
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQHCoQ=
Expand All @@ -339,8 +342,8 @@ github.com/henvic/httpretty v0.1.0/go.mod h1:ViEsly7wgdugYtymX54pYp6Vv2wqZmNHayJ
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/itchyny/gojq v0.12.9 h1:biKpbKwMxVYhCU1d6mR7qMr3f0Hn9F5k5YykCVb3gmM=
Expand Down Expand Up @@ -670,8 +673,8 @@ golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce/go.mod h1:IxCIyHEi3zRg3s0
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM=
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I=
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -841,16 +844,16 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.9.0 h1:GRRCnKYhdQrD8kfRAdQ6Zcw1P0OcELxGLKJvtjVMZ28=
golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo=
golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -862,8 +865,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down Expand Up @@ -1041,7 +1044,6 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
6 changes: 3 additions & 3 deletions pkg/cronjob/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"strings"
"text/template"

"go.uber.org/zap"
v1 "k8s.io/api/batch/v1"
Expand All @@ -18,6 +17,7 @@ import (

"github.com/kubeshop/testkube/pkg/k8sclient"
"github.com/kubeshop/testkube/pkg/log"
"github.com/kubeshop/testkube/pkg/utils"
)

// Client data struct for managing running cron jobs
Expand Down Expand Up @@ -172,7 +172,7 @@ func (c *Client) DeleteAll(resource, selector string) error {

// NewApplySpec is a method to return cron job apply spec
func NewApplySpec(log *zap.SugaredLogger, parameters templateParameters) (*batchv1.CronJobApplyConfiguration, error) {
tmpl, err := template.New("cronJob").Parse(parameters.CronJobTemplate)
tmpl, err := utils.NewTemplate("cronJob").Parse(parameters.CronJobTemplate)
if err != nil {
return nil, fmt.Errorf("creating cron job spec from options.CronJobTemplate error: %w", err)
}
Expand All @@ -186,7 +186,7 @@ func NewApplySpec(log *zap.SugaredLogger, parameters templateParameters) (*batch
var cronJob batchv1.CronJobApplyConfiguration
cronJobSpec := buffer.String()
if parameters.CronJobTemplateExtensions != "" {
tmplExt, err := template.New("cronJobExt").Parse(parameters.CronJobTemplateExtensions)
tmplExt, err := utils.NewTemplate("cronJobExt").Parse(parameters.CronJobTemplateExtensions)
if err != nil {
return nil, fmt.Errorf("creating cron job extensions spec from default template error: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/event/kind/webhook/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/kubeshop/testkube/pkg/event/kind/common"
thttp "github.com/kubeshop/testkube/pkg/http"
"github.com/kubeshop/testkube/pkg/log"
"github.com/kubeshop/testkube/pkg/utils"
)

var _ common.Listener = (*WebhookListener)(nil)
Expand Down Expand Up @@ -168,7 +169,7 @@ func (l *WebhookListener) processTemplate(field, body string, event testkube.Eve
log := l.Log.With(event.Log()...)

var tmpl *template.Template
tmpl, err := template.New(field).Parse(body)
tmpl, err := utils.NewTemplate(field).Parse(body)
if err != nil {
log.Errorw(fmt.Sprintf("creating webhook %s error", field), "error", err)
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/executor/client/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ func NewJobSpec(log *zap.SugaredLogger, options JobOptions) (*batchv1.Job, error
secretEnvVars := append(envManager.PrepareSecrets(options.SecretEnvs, options.Variables),
envManager.PrepareGitCredentials(options.UsernameSecret, options.TokenSecret)...)

tmpl, err := template.New("job").Funcs(template.FuncMap{"vartypeptrtostring": testkube.VariableTypeString}).
tmpl, err := utils.NewTemplate("job").Funcs(template.FuncMap{"vartypeptrtostring": testkube.VariableTypeString}).
Parse(options.JobTemplate)
if err != nil {
return nil, errors.Errorf("creating job spec from options.JobTemplate error: %v", err)
Expand All @@ -709,7 +709,7 @@ func NewJobSpec(log *zap.SugaredLogger, options JobOptions) (*batchv1.Job, error
var job batchv1.Job
jobSpec := buffer.String()
if options.JobTemplateExtensions != "" {
tmplExt, err := template.New("jobExt").Funcs(template.FuncMap{"vartypeptrtostring": testkube.VariableTypeString}).
tmplExt, err := utils.NewTemplate("jobExt").Funcs(template.FuncMap{"vartypeptrtostring": testkube.VariableTypeString}).
Parse(options.JobTemplateExtensions)
if err != nil {
return nil, errors.Errorf("creating job extensions spec from template error: %v", err)
Expand Down
16 changes: 7 additions & 9 deletions pkg/executor/containerexecutor/tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"path/filepath"
"strings"
"text/template"

_ "embed"

Expand All @@ -25,6 +24,7 @@ import (
"github.com/kubeshop/testkube/pkg/executor/env"
"github.com/kubeshop/testkube/pkg/secret"
"github.com/kubeshop/testkube/pkg/skopeo"
"github.com/kubeshop/testkube/pkg/utils"
)

const (
Expand All @@ -41,8 +41,7 @@ func NewExecutorJobSpec(log *zap.SugaredLogger, options *JobOptions) (*batchv1.J
secretEnvVars := append(envManager.PrepareSecrets(options.SecretEnvs, options.Variables),
envManager.PrepareGitCredentials(options.UsernameSecret, options.TokenSecret)...)

tmpl, err := template.New("job").Funcs(template.FuncMap{"vartypeptrtostring": testkube.VariableTypeString}).
Parse(options.JobTemplate)
tmpl, err := utils.NewTemplate("job").Parse(options.JobTemplate)
if err != nil {
return nil, fmt.Errorf("creating job spec from executor template error: %w", err)
}
Expand All @@ -68,8 +67,7 @@ func NewExecutorJobSpec(log *zap.SugaredLogger, options *JobOptions) (*batchv1.J
var job batchv1.Job
jobSpec := buffer.String()
if options.JobTemplateExtensions != "" {
tmplExt, err := template.New("jobExt").Funcs(template.FuncMap{"vartypeptrtostring": testkube.VariableTypeString}).
Parse(options.JobTemplateExtensions)
tmplExt, err := utils.NewTemplate("jobExt").Parse(options.JobTemplateExtensions)
if err != nil {
return nil, fmt.Errorf("creating job extensions spec from executor template error: %w", err)
}
Expand Down Expand Up @@ -139,7 +137,7 @@ func NewExecutorJobSpec(log *zap.SugaredLogger, options *JobOptions) (*batchv1.J

// NewScraperJobSpec is a method to create new scraper job spec
func NewScraperJobSpec(log *zap.SugaredLogger, options *JobOptions) (*batchv1.Job, error) {
tmpl, err := template.New("job").Parse(options.ScraperTemplate)
tmpl, err := utils.NewTemplate("job").Parse(options.ScraperTemplate)
if err != nil {
return nil, fmt.Errorf("creating job spec from scraper template error: %w", err)
}
Expand All @@ -153,7 +151,7 @@ func NewScraperJobSpec(log *zap.SugaredLogger, options *JobOptions) (*batchv1.Jo
var job batchv1.Job
jobSpec := buffer.String()
if options.ScraperTemplateExtensions != "" {
tmplExt, err := template.New("jobExt").Parse(options.ScraperTemplateExtensions)
tmplExt, err := utils.NewTemplate("jobExt").Parse(options.ScraperTemplateExtensions)
if err != nil {
return nil, fmt.Errorf("creating scraper extensions spec from executor template error: %w", err)
}
Expand Down Expand Up @@ -198,7 +196,7 @@ func NewScraperJobSpec(log *zap.SugaredLogger, options *JobOptions) (*batchv1.Jo

// NewPersistentVolumeClaimSpec is a method to create new persistent volume claim spec
func NewPersistentVolumeClaimSpec(log *zap.SugaredLogger, options *JobOptions) (*corev1.PersistentVolumeClaim, error) {
tmpl, err := template.New("volume-claim").Parse(options.PvcTemplate)
tmpl, err := utils.NewTemplate("volume-claim").Parse(options.PvcTemplate)
if err != nil {
return nil, fmt.Errorf("creating volume claim spec from pvc template error: %w", err)
}
Expand All @@ -211,7 +209,7 @@ func NewPersistentVolumeClaimSpec(log *zap.SugaredLogger, options *JobOptions) (
var pvc corev1.PersistentVolumeClaim
pvcSpec := buffer.String()
if options.PvcTemplateExtensions != "" {
tmplExt, err := template.New("jobExt").Parse(options.PvcTemplateExtensions)
tmplExt, err := utils.NewTemplate("jobExt").Parse(options.PvcTemplateExtensions)
if err != nil {
return nil, fmt.Errorf("creating pvc extensions spec from executor template error: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"bytes"
"encoding/json"
"os"
"text/template"

"github.com/slack-go/slack"

"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/log"
"github.com/kubeshop/testkube/pkg/utils"
)

type MessageArgs struct {
Expand Down Expand Up @@ -166,7 +166,7 @@ func (s *Notifier) composeMessage(event *testkube.Event) (view *slack.Message, n
}

func (s *Notifier) composeTestsuiteMessage(execution *testkube.TestSuiteExecution, eventType testkube.EventType) ([]byte, error) {
t, err := template.New("message").Parse(s.messageTemplate)
t, err := utils.NewTemplate("message").Parse(s.messageTemplate)
if err != nil {
log.DefaultLogger.Warnw("error while parsing slack template", "error", err.Error())
return nil, err
Expand Down Expand Up @@ -202,7 +202,7 @@ func (s *Notifier) composeTestsuiteMessage(execution *testkube.TestSuiteExecutio
}

func (s *Notifier) composeTestMessage(execution *testkube.Execution, eventType testkube.EventType) ([]byte, error) {
t, err := template.New("message").Parse(s.messageTemplate)
t, err := utils.NewTemplate("message").Parse(s.messageTemplate)
if err != nil {
log.DefaultLogger.Warnw("error while parsing slack template", "error", err.Error(), "template", s.messageTemplate)
return nil, err
Expand Down
Loading

0 comments on commit 4d20552

Please sign in to comment.