diff --git a/example/custom-delims/composition.yaml b/example/custom-delims/composition.yaml new file mode 100644 index 0000000..76b4fad --- /dev/null +++ b/example/custom-delims/composition.yaml @@ -0,0 +1,68 @@ +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 + leftDelims: '[[' + rightDelims: ']]' + 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 diff --git a/example/custom-delims/functions.yaml b/example/custom-delims/functions.yaml new file mode 100644 index 0000000..4247bb8 --- /dev/null +++ b/example/custom-delims/functions.yaml @@ -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 \ No newline at end of file diff --git a/example/custom-delims/xr.yaml b/example/custom-delims/xr.yaml new file mode 100644 index 0000000..27cae0a --- /dev/null +++ b/example/custom-delims/xr.yaml @@ -0,0 +1,6 @@ +apiVersion: example.crossplane.io/v1beta1 +kind: XR +metadata: + name: example +spec: + count: 2 diff --git a/function_maps.go b/function_maps.go index f5f1a6c..67ab840 100644 --- a/function_maps.go +++ b/function_maps.go @@ -9,6 +9,11 @@ import ( "github.com/crossplane-contrib/function-go-templating/input/v1beta1" ) +var ( + defaultLeftDelims = "{{" + defaultRightDelims = "}}" +) + var funcMaps = []template.FuncMap{ { "randomChoice": func(choices ...string) string { @@ -20,13 +25,13 @@ var funcMaps = []template.FuncMap{ } func GetNewTemplateWithFunctionMaps(in *v1beta1.GoTemplate) *template.Template { - if in.LeftDelims == "" { - in.LeftDelims = "{{" + if in.LeftDelims == nil { + in.LeftDelims = &defaultLeftDelims } - if in.RightDelims == "" { - in.RightDelims = "}}" + if in.RightDelims == nil { + in.RightDelims = &defaultRightDelims } - tpl := template.New("manifests").Delims(in.LeftDelims, in.RightDelims) + tpl := template.New("manifests").Delims(*in.LeftDelims, *in.RightDelims) for _, f := range funcMaps { tpl.Funcs(f) diff --git a/input/v1beta1/input.go b/input/v1beta1/input.go index d335fed..310db95 100644 --- a/input/v1beta1/input.go +++ b/input/v1beta1/input.go @@ -20,9 +20,13 @@ type GoTemplate struct { metav1.ObjectMeta `json:"metadata,omitempty"` // Template start characters - LeftDelims string `json:"leftDelims,omitempty"` + // +kubebuilder:default:="{{" + // +optional + LeftDelims *string `json:"leftDelims,omitempty"` // Template end characters - RightDelims string `json:"rightDelims,omitempty"` + // +kubebuilder:default:="}}" + // +optional + RightDelims *string `json:"rightDelims,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 diff --git a/input/v1beta1/zz_generated.deepcopy.go b/input/v1beta1/zz_generated.deepcopy.go index 40947fc..098984a 100644 --- a/input/v1beta1/zz_generated.deepcopy.go +++ b/input/v1beta1/zz_generated.deepcopy.go @@ -13,6 +13,16 @@ func (in *GoTemplate) DeepCopyInto(out *GoTemplate) { *out = *in out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + if in.LeftDelims != nil { + in, out := &in.LeftDelims, &out.LeftDelims + *out = new(string) + **out = **in + } + if in.RightDelims != nil { + in, out := &in.RightDelims, &out.RightDelims + *out = new(string) + **out = **in + } if in.Inline != nil { in, out := &in.Inline, &out.Inline *out = new(TemplateSourceInline) diff --git a/package/input/gotemplating.fn.crossplane.io_gotemplates.yaml b/package/input/gotemplating.fn.crossplane.io_gotemplates.yaml index 215395f..4f20615 100644 --- a/package/input/gotemplating.fn.crossplane.io_gotemplates.yaml +++ b/package/input/gotemplating.fn.crossplane.io_gotemplates.yaml @@ -44,11 +44,13 @@ spec: submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' type: string leftDelims: + default: '{{' description: Template start characters type: string metadata: type: object rightDelims: + default: '}}' description: Template end characters type: string source: