Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikel-Landa authored Nov 28, 2023
2 parents 2bd5227 + d962203 commit b2bf95c
Show file tree
Hide file tree
Showing 20 changed files with 170 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ about `crossplane beta render`.

| Name | Description |
|------------------------------------------------------------------| --------------------------------------------------- |
| [`randomChoice`](example/functions/randomChoice) | Randomly selects one of a given strings |
| [`randomChoice`](example/inline) | Randomly selects one of a given strings |
| [`toYaml`](example/functions/toYaml) | Marshals any object into a YAML string |
| [`fromYaml`](example/functions/fromYaml) | Unmarshals a YAML string into an object |
| [`getResourceCondition`](example/functions/getResourceCondition) | Helper function to retreive conditions of resources |
| [`setResourceNameAnnotation`](example/functions/inline) | Returns the special resource-name annotation with given name |
| [`setResourceNameAnnotation`](example/inline) | Returns the special resource-name annotation with given name |
| [`include`](example/functions/include) | Outputs template as a string |

## Developing this function
Expand Down
2 changes: 1 addition & 1 deletion example/.dev/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ metadata:
render.crossplane.io/runtime: Development
render.crossplane.io/runtime-development-target: localhost:9443
spec:
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:latest # ignored
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.3.0
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 example/filesystem/claim.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ metadata:
namespace: crossplane-system
spec:
id: test-user
count: 2
count: 2
4 changes: 2 additions & 2 deletions example/filesystem/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ kind: Function
metadata:
name: function-auto-ready
spec:
package: xpkg.upbound.io/crossplane-contrib/function-auto-ready:v0.1.0
package: xpkg.upbound.io/crossplane-contrib/function-auto-ready:v0.2.1
---
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-20231102045703-5dbf5b2adbc4
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.3.0
runtimeConfigRef:
name: mount-templates
---
Expand Down
2 changes: 1 addition & 1 deletion example/filesystem/templates.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ data:
apiVersion: aws.platformref.upbound.io/v1alpha1
kind: XUser
status:
dummy: cool-status
dummy: cool-status
2 changes: 1 addition & 1 deletion example/functions/fromYaml/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ kind: Function
metadata:
name: function-go-templating
spec:
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:latest
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.3.0
2 changes: 1 addition & 1 deletion example/functions/fromYaml/xr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ spec:
yamlBlob: |
key1: value1
key2: value2
key3: value3
key3: value3
2 changes: 1 addition & 1 deletion example/functions/getResourceCondition/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ kind: Function
metadata:
name: function-go-templating
spec:
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:latest
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.3.0
2 changes: 1 addition & 1 deletion example/functions/getResourceCondition/observed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ status:
- type: Ready
status: "True"
reason: "foo"
lastTransitionTime: "2023-11-03T09:07:31Z"
lastTransitionTime: "2023-11-03T09:07:31Z"
2 changes: 1 addition & 1 deletion example/functions/toYaml/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ kind: Function
metadata:
name: function-go-templating
spec:
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:latest
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.3.0
2 changes: 1 addition & 1 deletion example/inline/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ kind: Function
metadata:
name: function-go-templating
spec:
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.0.0-20231101231317-cdb49945da4e
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.3.0
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 @@ -10,7 +10,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"
"github.com/crossplane/function-sdk-go/errors"
Expand All @@ -28,9 +28,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
12 changes: 11 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:base"
"config:recommended"
],
"crossplane": {
"fileMatch": ["(^|/)example/.*\\.ya?ml$"]
},
"packageRules": [
{
"matchManagers": ["crossplane"],
"matchFileNames": ["example/**"],
"groupName": "examples"
}
]
}

0 comments on commit b2bf95c

Please sign in to comment.