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

added ability to set custom template delims #18

Merged
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
69 changes: 69 additions & 0 deletions example/custom-delims/composition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: example-inline
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1beta1
kind: XR
mode: Pipeline
pipeline:
- step: render-templates
functionRef:
name: function-go-templating
input:
apiVersion: gotemplating.fn.crossplane.io/v1beta1
kind: GoTemplate
delims:
left: '[['
right: ']]'
source: Inline
inline:
template: |
[[- range $i := until ( .observed.composite.resource.spec.count | int ) ]]
---
apiVersion: iam.aws.upbound.io/v1beta1
kind: User
metadata:
annotations:
gotemplating.fn.crossplane.io/composition-resource-name: test-user-[[ $i ]]
labels:
testing.upbound.io/example-name: test-user-[[ $i ]]
[[ if eq $.observed.resources nil ]]
dummy: [[ randomChoice "foo" "bar" "baz" ]]
[[ else ]]
dummy: [[ ( index $.observed.resources ( print "test-user-" $i ) ).resource.metadata.labels.dummy ]]
[[ end ]]
spec:
forProvider: {}
---
apiVersion: iam.aws.upbound.io/v1beta1
kind: AccessKey
metadata:
annotations:
gotemplating.fn.crossplane.io/composition-resource-name: sample-access-key-[[ $i ]]
spec:
forProvider:
userSelector:
matchLabels:
testing.upbound.io/example-name: test-user-[[ $i ]]
writeConnectionSecretToRef:
name: sample-access-key-secret-[[ $i ]]
namespace: crossplane-system
[[- end ]]
---
apiVersion: meta.gotemplating.fn.crossplane.io/v1alpha1
kind: CompositeConnectionDetails
[[ if eq $.observed.resources nil ]]
data: {}
[[ else ]]
data:
username: [[ ( index $.observed.resources "sample-access-key-0" ).connectionDetails.username ]]
password: [[ ( index $.observed.resources "sample-access-key-0" ).connectionDetails.password ]]
url: [[ "http://www.example.com" | b64enc ]]
[[ end ]]
---
apiVersion: example.crossplane.io/v1beta1
kind: XR
status:
dummy: cool-status
6 changes: 6 additions & 0 deletions example/custom-delims/functions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-go-templating
spec:
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.0.0-20231101231317-cdb49945da4e
6 changes: 6 additions & 0 deletions example/custom-delims/xr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: example.crossplane.io/v1beta1
kind: XR
metadata:
name: example
spec:
count: 2
2 changes: 1 addition & 1 deletion fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
return rsp, nil
}

tmpl, err := GetNewTemplateWithFunctionMaps().Parse(tg.GetTemplates())
tmpl, err := GetNewTemplateWithFunctionMaps(in.Delims).Parse(tg.GetTemplates())
if err != nil {
response.Fatal(rsp, errors.Wrap(err, "invalid function input: cannot parse the provided templates"))
return rsp, nil
Expand Down
10 changes: 8 additions & 2 deletions function_maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"gopkg.in/yaml.v3"

sprig "github.com/Masterminds/sprig/v3"

"github.com/crossplane-contrib/function-go-templating/input/v1beta1"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
)
Expand Down Expand Up @@ -48,9 +48,15 @@ var funcMaps = []template.FuncMap{
},
}

func GetNewTemplateWithFunctionMaps() *template.Template {
func GetNewTemplateWithFunctionMaps(delims *v1beta1.Delims) *template.Template {
tpl := template.New("manifests")

if delims != nil {
if delims.Left != nil && delims.Right != nil {
tpl = tpl.Delims(*delims.Left, *delims.Right)
}
}

for _, f := range funcMaps {
tpl.Funcs(f)
}
Expand Down
15 changes: 14 additions & 1 deletion input/v1beta1/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
type GoTemplate struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

// Template delimiters
// +optional
Delims *Delims `json:"delims,omitempty"`
// Source specifies the different types of input sources that can be used with this function
Source TemplateSource `json:"source"`
// Inline is the inline form input of the templates
Expand All @@ -44,3 +46,14 @@ type TemplateSourceInline struct {
type TemplateSourceFileSystem struct {
DirPath string `json:"dirPath,omitempty"`
}

type Delims struct {
// Template start characters
// +kubebuilder:default:="{{"
// +optional
Left *string `json:"left,omitempty"`
// Template end characters
// +kubebuilder:default:="}}"
// +optional
Right *string `json:"right,omitempty"`
}
30 changes: 30 additions & 0 deletions input/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions package/input/gotemplating.fn.crossplane.io_gotemplates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ spec:
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
delims:
description: Template delimiters
properties:
left:
default: '{{'
description: Template start characters
type: string
right:
default: '}}'
description: Template end characters
type: string
type: object
fileSystem:
description: FileSystem is the folder path where the templates are located
properties:
Expand Down