Skip to content

Commit

Permalink
Merge pull request #16 from Avarei/add-minimal-example
Browse files Browse the repository at this point in the history
Add minimal example
  • Loading branch information
Avarei authored Jul 1, 2024
2 parents d9d6b3c + facdc4c commit 23f5405
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 22 deletions.
20 changes: 20 additions & 0 deletions example/minimal/composition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: function-pkl
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1
kind: XR
mode: Pipeline
pipeline:
- step: run-the-template
functionRef:
name: function-pkl
input:
apiVersion: template.fn.crossplane.io/v1beta1
kind: Pkl
spec:
type: uri
# This pkl file is at `pkl/crossplane-example/minimal.pkl` in this repo
uri: "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]#/minimal.pkl"
11 changes: 11 additions & 0 deletions example/minimal/functions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-pkl
annotations:
# This tells crossplane beta render to connect to the function locally.
render.crossplane.io/runtime: Development
spec:
# This is ignored when using the Development runtime.
package: function-pkl
6 changes: 6 additions & 0 deletions example/minimal/xr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Replace this with your XR!
apiVersion: example.crossplane.io/v1
kind: XR
metadata:
name: example-xr
spec: {}
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,6 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20240509183442-62759503f434 h1:
google.golang.org/genproto/googleapis/rpc v0.0.0-20240509183442-62759503f434/go.mod h1:I7Y+G38R2bu5j1aLzfFmQfTcU/WnFuqDwLZAbvKTKpM=
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand All @@ -282,8 +280,6 @@ k8s.io/api v0.30.0 h1:siWhRq7cNjy2iHssOB9SCGNCl2spiF1dO3dABqZ8niA=
k8s.io/api v0.30.0/go.mod h1:OPlaYhoHs8EQ1ql0R/TsUgaRPhpKNxIMrKQfWUp8QSE=
k8s.io/apiextensions-apiserver v0.30.0 h1:jcZFKMqnICJfRxTgnC4E+Hpcq8UEhT8B2lhBcQ+6uAs=
k8s.io/apiextensions-apiserver v0.30.0/go.mod h1:N9ogQFGcrbWqAY9p2mUAL5mGxsLqwgtUce127VtRX5Y=
k8s.io/apimachinery v0.30.0 h1:qxVPsyDM5XS96NIh9Oj6LavoVFYff/Pon9cZeDIkHHA=
k8s.io/apimachinery v0.30.0/go.mod h1:iexa2somDaxdnj7bha06bhb43Zpa6eWH8N8dbqVjTUc=
k8s.io/apimachinery v0.30.2 h1:fEMcnBj6qkzzPGSVsAZtQThU62SmQ4ZymlXRC5yFSCg=
k8s.io/apimachinery v0.30.2/go.mod h1:iexa2somDaxdnj7bha06bhb43Zpa6eWH8N8dbqVjTUc=
k8s.io/client-go v0.30.0 h1:sB1AGGlhY/o7KCyCEQ0bPWzYDL0pwOZO4vAtTSh/gJQ=
Expand Down
40 changes: 24 additions & 16 deletions internal/function/fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
Ctx: ctx,
}))
}

if err != nil {
response.Fatal(rsp, errors.Wrap(err, "could not create Pkl Evaluator"))
return rsp, nil
Expand All @@ -104,34 +103,43 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
return rsp, nil
}

helperResponse := &helper.CompositionResponse{}
err = yaml.Unmarshal([]byte(renderedManifest), helperResponse)
rsp, err = toResponse(renderedManifest, req.GetMeta().GetTag())
if err != nil {
return nil, errors.Wrapf(err, "rendered Pkl file was not in expected format. did you amend @crossplane/CompositionResponse.pkl?")
response.Fatal(rsp, errors.Wrap(err, "error while converting Pkl file"))
return rsp, nil
}

fixedRequirements := &fnv1beta1.Requirements{
ExtraResources: convertExtraResources(helperResponse.Requirements.ExtraResources),
return rsp, nil
}

func toResponse(renderedManifest, tag string) (*fnv1beta1.RunFunctionResponse, error) {
rspHelper := &helper.CompositionResponse{}
err := yaml.Unmarshal([]byte(renderedManifest), rspHelper)
if err != nil {
return nil, errors.Wrapf(err, "rendered Pkl file was not in expected format. did you amend @crossplane/CompositionResponse.pkl?")
}

responseMeta := &fnv1beta1.ResponseMeta{
Tag: req.GetMeta().GetTag(),
Tag: tag,
Ttl: durationpb.New(response.DefaultTTL),
}
if ttl := rspHelper.GetMeta().GetTtl(); ttl != nil {
responseMeta.Ttl = ttl
}

// Note: consider not overwriting rsp and whether it makes a difference.
rsp = &fnv1beta1.RunFunctionResponse{
Meta: responseMeta,
Desired: helperResponse.Desired,
Results: helperResponse.Results,
Context: helperResponse.Context,
Requirements: fixedRequirements,
rsp := &fnv1beta1.RunFunctionResponse{
Meta: responseMeta,
Desired: rspHelper.Desired,
Results: rspHelper.Results,
Context: rspHelper.Context,
}

if ttl := helperResponse.GetMeta().GetTtl(); ttl != nil {
rsp.Meta.Ttl = ttl
if rspHelper.Requirements != nil && rspHelper.Requirements.ExtraResources != nil {
rsp.Requirements = &fnv1beta1.Requirements{
ExtraResources: convertExtraResources(rspHelper.Requirements.ExtraResources),
}
}

return rsp, nil
}

Expand Down
66 changes: 66 additions & 0 deletions internal/function/fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/logging"
fnv1beta1 "github.com/crossplane/function-sdk-go/proto/v1beta1"
"github.com/crossplane/function-sdk-go/resource"
"github.com/crossplane/function-sdk-go/response"

"github.com/crossplane-contrib/function-pkl/input/v1beta1"
)
Expand All @@ -54,6 +55,71 @@ func TestRunFunction(t *testing.T) {
args args
want want
}{
"Minimal": {
reason: "The Function should create a single resource",
args: args{
ctx: context.TODO(),
req: &fnv1beta1.RunFunctionRequest{
Input: resource.MustStructObject(&v1beta1.Pkl{
Spec: v1beta1.PklSpec{
Type: "local",
Local: &v1beta1.Local{
ProjectDir: pklPackage,
File: pklPackage + "/minimal.pkl",
},
},
}),
Observed: &fnv1beta1.State{
Composite: &fnv1beta1.Resource{
Resource: resource.MustStructJSON(`{
"apiVersion": "example.crossplane.io/v1",
"kind": "XR",
"metadata": {
"name": "example-xr"
},
"spec": {}
}`),
},
},
},
},
want: want{
rsp: &fnv1beta1.RunFunctionResponse{
Meta: &fnv1beta1.ResponseMeta{
Ttl: durationpb.New(response.DefaultTTL),
},
Desired: &fnv1beta1.State{
Composite: &fnv1beta1.Resource{},
Resources: map[string]*fnv1beta1.Resource{
"cm-minimal": {
Resource: resource.MustStructJSON(`{
"apiVersion": "kubernetes.crossplane.io/v1alpha2",
"kind": "Object",
"metadata": {
"name": "cm-one"
},
"spec": {
"forProvider": {
"manifest": {
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "cm-minimal",
"namespace": "crossplane-system"
},
"data": {
"foo": "bar"
}
}
}
}
}`),
},
},
},
},
},
},
"Full": {
reason: "The Function should create a full functionResult",
args: args{
Expand Down
3 changes: 1 addition & 2 deletions pkl/crossplane.contrib.example/full.pkl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
amends "@crossplane.contrib/CompositionResponse.pkl"
import "@crossplane.contrib/Resource.pkl"
import "@crossplane.contrib/crossplane.pkl"

import "crds/XR.pkl"
Expand Down Expand Up @@ -42,7 +41,7 @@ desired {
}
}
resources {
["cm-one"] = new Resource {
["cm-one"] = new {
resource = new Object {
metadata {
name = "cm-one"
Expand Down
28 changes: 28 additions & 0 deletions pkl/crossplane.contrib.example/minimal.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
amends "@crossplane.contrib/CompositionResponse.pkl"
import "crds/Object.pkl"
import "@k8s/api/core/v1/ConfigMap.pkl"

desired {
resources {
["cm-minimal"] = new {
resource = new Object {
metadata {
name = "cm-one"
}
spec {
forProvider {
manifest = new ConfigMap {
metadata {
name = "cm-minimal"
namespace = "crossplane-system"
}
data {
["foo"] = "bar"
}
}
}
}
}
}
}
}

0 comments on commit 23f5405

Please sign in to comment.