Skip to content

Commit

Permalink
Merge pull request #196 from adamwg/awg/base-image
Browse files Browse the repository at this point in the history
Allow a default source to be configured via environment variable
  • Loading branch information
Peefy authored Nov 20, 2024
2 parents 84a7901 + f2e56ff commit 39a276e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
7 changes: 7 additions & 0 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"os"

"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/logging"
Expand All @@ -21,6 +22,8 @@ import (
"sigs.k8s.io/yaml"
)

var defaultSource = os.Getenv("FUNCTION_KCL_DEFAULT_SOURCE")

// Function returns whatever response you ask it to.
type Function struct {
fnv1.UnimplementedFunctionRunnerServiceServer
Expand All @@ -39,6 +42,10 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
response.Fatal(rsp, errors.Wrapf(err, "cannot get Function input from %T", req))
return rsp, nil
}
// Set default source
if in.Spec.Source == "" {
in.Spec.Source = defaultSource
}
// Set default target
if in.Spec.Target == "" {
in.Spec.Target = pkgresource.Default
Expand Down
64 changes: 61 additions & 3 deletions fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ func TestRunFunctionSimple(t *testing.T) {
)

cases := map[string]struct {
reason string
args args
want want
reason string
defaultSource string
args args
want want
}{
"ResponseIsReturned": {
reason: "The Function should return a fatal result if no input was specified",
Expand Down Expand Up @@ -387,6 +388,60 @@ func TestRunFunctionSimple(t *testing.T) {
},
},
},
"EmptyInputWithDefaultSource": {
reason: "The function should use the default source when input is not provided and default source is set",
defaultSource: "{\n apiVersion: \"example.org/v1\"\n kind: \"Generated\"\n}",
args: args{
req: &fnv1.RunFunctionRequest{
Meta: &fnv1.RequestMeta{Tag: "empty-input"},
Input: nil,
Observed: &fnv1.State{
Composite: &fnv1.Resource{
Resource: resource.MustStructJSON(`{"apiVersion":"example.org/v1","kind":"XR"}`),
},
},
},
},
want: want{
rsp: &fnv1.RunFunctionResponse{
Meta: &fnv1.ResponseMeta{Tag: "empty-input", Ttl: durationpb.New(response.DefaultTTL)},
Desired: &fnv1.State{
Composite: &fnv1.Resource{
Resource: resource.MustStructJSON(`{"apiVersion":"example.org/v1","kind":"XR"}`),
},
Resources: map[string]*fnv1.Resource{
"": {
Resource: resource.MustStructJSON(`{"apiVersion":"example.org/v1","kind":"Generated"}`),
},
},
},
},
},
},
"EmptyInputWithoutDefaultSource": {
reason: "The function should fail when input is not provided and default source is not set",
args: args{
req: &fnv1.RunFunctionRequest{
Meta: &fnv1.RequestMeta{Tag: "empty-input"},
Input: nil,
Observed: &fnv1.State{
Composite: &fnv1.Resource{
Resource: resource.MustStructJSON(`{"apiVersion":"example.org/v1","kind":"XR"}`),
},
},
},
},
want: want{
rsp: &fnv1.RunFunctionResponse{
Meta: &fnv1.ResponseMeta{Tag: "empty-input", Ttl: durationpb.New(response.DefaultTTL)},
Results: []*fnv1.Result{{
Target: fnv1.Target_TARGET_COMPOSITE.Enum(),
Severity: fnv1.Severity_SEVERITY_FATAL,
Message: "invalid function input: spec.source: Required value: kcl source cannot be empty",
}},
},
},
},
// TODO: disable the resource check, and fix the kcl dup resource evaluation issues.
// "MultipleResourceError": {
// reason: "The Function should return a fatal result if input resources have duplicate names",
Expand Down Expand Up @@ -425,6 +480,9 @@ func TestRunFunctionSimple(t *testing.T) {

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
// NOTE: This means we can't run tests in parallel.
defaultSource = tc.defaultSource

// f := &Function{log: logging.NewNopLogger()}
f := &Function{log: logging.NewLogrLogger(testr.New(t))}
rsp, err := f.RunFunction(tc.args.ctx, tc.args.req)
Expand Down

0 comments on commit 39a276e

Please sign in to comment.