Skip to content
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ spec:
This function can load templates from three sources: `Inline`, `FileSystem` and `Environment`.

Use the `Inline` source to specify a simple template inline in your Composition.
Multiple YAML manifests can be specified using the `---` document separator.
Multiple YAML manifests can be specified using the `templates` field, which may contain a slice of
strings, or by using the `---` document separator in the `template` field.

Use the `FileSystem` source to specify a directory of templates. The
`FileSystem` source treats all files under the specified directory as templates.
Expand Down
19 changes: 19 additions & 0 deletions example/inline-templates/composition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: example-inline
spec:
mode: Pipeline
compositeTypeRef:
apiVersion: example.crossplane.io/v1beta1
kind: XR
pipeline:
- step: render-templates
functionRef:
name: function-go-templating
input:
apiVersion: gotemplating.fn.crossplane.io/v1beta1
kind: GoTemplate
source: Inline
inline:
templates: [] # Kustomize will add the templates here
10 changes: 10 additions & 0 deletions example/inline-templates/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- composition.yaml
patches:
- path: templates/user.patch.yaml
target: { kind: Composition, name: example-inline }
- path: templates/access-key.patch.yaml
target: { kind: Composition, name: example-inline }
10 changes: 10 additions & 0 deletions example/inline-templates/templates/access-key.patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- op: add
path: /spec/pipeline/0/input/inline/templates/-
value: |
apiVersion: iam.aws.upbound.io/v1beta1
kind: AccessKey
metadata:
annotations:
gotemplating.fn.crossplane.io/composition-resource-name: access-key
spec:
forProvider: {}
10 changes: 10 additions & 0 deletions example/inline-templates/templates/user.patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- op: add
path: /spec/pipeline/0/input/inline/templates/-
value: |
apiVersion: iam.aws.upbound.io/v1beta1
kind: User
metadata:
annotations:
gotemplating.fn.crossplane.io/composition-resource-name: user
spec:
forProvider: {}
40 changes: 39 additions & 1 deletion fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestRunFunction(t *testing.T) {
Results: []*fnv1.Result{
{
Severity: fnv1.Severity_SEVERITY_FATAL,
Message: "invalid function input: inline.template should be provided",
Message: "invalid function input: inline.template or inline.templates should be provided",
Target: fnv1.Target_TARGET_COMPOSITE.Enum(),
},
},
Expand Down Expand Up @@ -359,6 +359,44 @@ func TestRunFunction(t *testing.T) {
},
},
},
"ResponseIsReturnedWithTemplatingUsingTheTemplatesField": {
reason: "The Function should return the desired composite resource and the templated composed resources.",
args: args{
req: &fnv1.RunFunctionRequest{
Meta: &fnv1.RequestMeta{Tag: "templates"},
Input: resource.MustStructObject(
&v1beta1.GoTemplate{
Source: v1beta1.InlineSource,
Inline: &v1beta1.TemplateSourceInline{Templates: []string{cdTmpl}},
}),
Observed: &fnv1.State{
Composite: &fnv1.Resource{
Resource: resource.MustStructJSON(xr),
},
},
Desired: &fnv1.State{
Composite: &fnv1.Resource{
Resource: resource.MustStructJSON(xr),
},
},
},
},
want: want{
rsp: &fnv1.RunFunctionResponse{
Meta: &fnv1.ResponseMeta{Tag: "templates", Ttl: durationpb.New(response.DefaultTTL)},
Desired: &fnv1.State{
Composite: &fnv1.Resource{
Resource: resource.MustStructJSON(xr),
},
Resources: map[string]*fnv1.Resource{
"cool-cd": {
Resource: resource.MustStructJSON(`{"apiVersion": "example.org/v1","kind":"CD","metadata":{"annotations":{},"name":"cool-cd","labels":{"belongsTo":"cool-xr"}}}`),
},
},
},
},
},
},
"UpdateDesiredCompositeStatus": {
reason: "The Function should update the desired composite resource status.",
args: args{
Expand Down
6 changes: 4 additions & 2 deletions input/v1beta1/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ const (
EnvironmentSource TemplateSource = "Environment"
)

// TemplateSourceInline defines the structure of the inline source.
// TemplateSourceInline defines the structure of the inline source. Allows specifying either a single inline template or multiple templates, but not both.
// +kubebuilder:validation:XValidation:rule="(has(self.template) ? 1 : 0) + (has(self.templates) ? 1 : 0) == 1",message="Exactly one of 'template' or 'templates' must be set"
type TemplateSourceInline struct {
Template string `json:"template,omitempty"`
Template string `json:"template,omitempty"`
Templates []string `json:"templates,omitempty"`
}

// TemplateSourceFileSystem defines the structure of the filesystem source.
Expand Down
7 changes: 6 additions & 1 deletion input/v1beta1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ spec:
properties:
template:
type: string
templates:
items:
type: string
type: array
type: object
x-kubernetes-validations:
- message: Exactly one of 'template' or 'templates' must be set
rule: '(has(self.template) ? 1 : 0) + (has(self.templates) ? 1 : 0)
== 1'
kind:
description: |-
Kind is a string value representing the REST resource this object represents.
Expand Down
13 changes: 10 additions & 3 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"io/fs"
"path/filepath"
"strings"

"github.com/crossplane-contrib/function-go-templating/input/v1beta1"
"google.golang.org/protobuf/types/known/structpb"
Expand Down Expand Up @@ -57,12 +58,18 @@ func (is *InlineSource) GetTemplates() string {
}

func newInlineSource(in *v1beta1.GoTemplate) (*InlineSource, error) {
if in.Inline == nil || in.Inline.Template == "" {
return nil, errors.New("inline.template should be provided")
if in.Inline == nil || (in.Inline.Template == "" && len(in.Inline.Templates) == 0) {
return nil, errors.New("inline.template or inline.templates should be provided")
}

template := strings.Join(in.Inline.Templates, "\n---\n")

if in.Inline.Template != "" {
template = in.Inline.Template
}

return &InlineSource{
Template: in.Inline.Template,
Template: template,
}, nil
}

Expand Down
Loading