Skip to content

Commit

Permalink
mr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Chubatiuk committed Nov 3, 2023
1 parent f582994 commit 223dfec
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 7 deletions.
68 changes: 68 additions & 0 deletions example/custom-delims/composition.yaml
Original file line number Diff line number Diff line change
@@ -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
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
15 changes: 10 additions & 5 deletions function_maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions input/v1beta1/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 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.

2 changes: 2 additions & 0 deletions package/input/gotemplating.fn.crossplane.io_gotemplates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 223dfec

Please sign in to comment.