Skip to content

Commit 39a276e

Browse files
authored
Merge pull request #196 from adamwg/awg/base-image
Allow a default source to be configured via environment variable
2 parents 84a7901 + f2e56ff commit 39a276e

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

fn.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"os"
78

89
"github.com/crossplane/crossplane-runtime/pkg/errors"
910
"github.com/crossplane/crossplane-runtime/pkg/logging"
@@ -21,6 +22,8 @@ import (
2122
"sigs.k8s.io/yaml"
2223
)
2324

25+
var defaultSource = os.Getenv("FUNCTION_KCL_DEFAULT_SOURCE")
26+
2427
// Function returns whatever response you ask it to.
2528
type Function struct {
2629
fnv1.UnimplementedFunctionRunnerServiceServer
@@ -39,6 +42,10 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
3942
response.Fatal(rsp, errors.Wrapf(err, "cannot get Function input from %T", req))
4043
return rsp, nil
4144
}
45+
// Set default source
46+
if in.Spec.Source == "" {
47+
in.Spec.Source = defaultSource
48+
}
4249
// Set default target
4350
if in.Spec.Target == "" {
4451
in.Spec.Target = pkgresource.Default

fn_test.go

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ func TestRunFunctionSimple(t *testing.T) {
3838
)
3939

4040
cases := map[string]struct {
41-
reason string
42-
args args
43-
want want
41+
reason string
42+
defaultSource string
43+
args args
44+
want want
4445
}{
4546
"ResponseIsReturned": {
4647
reason: "The Function should return a fatal result if no input was specified",
@@ -387,6 +388,60 @@ func TestRunFunctionSimple(t *testing.T) {
387388
},
388389
},
389390
},
391+
"EmptyInputWithDefaultSource": {
392+
reason: "The function should use the default source when input is not provided and default source is set",
393+
defaultSource: "{\n apiVersion: \"example.org/v1\"\n kind: \"Generated\"\n}",
394+
args: args{
395+
req: &fnv1.RunFunctionRequest{
396+
Meta: &fnv1.RequestMeta{Tag: "empty-input"},
397+
Input: nil,
398+
Observed: &fnv1.State{
399+
Composite: &fnv1.Resource{
400+
Resource: resource.MustStructJSON(`{"apiVersion":"example.org/v1","kind":"XR"}`),
401+
},
402+
},
403+
},
404+
},
405+
want: want{
406+
rsp: &fnv1.RunFunctionResponse{
407+
Meta: &fnv1.ResponseMeta{Tag: "empty-input", Ttl: durationpb.New(response.DefaultTTL)},
408+
Desired: &fnv1.State{
409+
Composite: &fnv1.Resource{
410+
Resource: resource.MustStructJSON(`{"apiVersion":"example.org/v1","kind":"XR"}`),
411+
},
412+
Resources: map[string]*fnv1.Resource{
413+
"": {
414+
Resource: resource.MustStructJSON(`{"apiVersion":"example.org/v1","kind":"Generated"}`),
415+
},
416+
},
417+
},
418+
},
419+
},
420+
},
421+
"EmptyInputWithoutDefaultSource": {
422+
reason: "The function should fail when input is not provided and default source is not set",
423+
args: args{
424+
req: &fnv1.RunFunctionRequest{
425+
Meta: &fnv1.RequestMeta{Tag: "empty-input"},
426+
Input: nil,
427+
Observed: &fnv1.State{
428+
Composite: &fnv1.Resource{
429+
Resource: resource.MustStructJSON(`{"apiVersion":"example.org/v1","kind":"XR"}`),
430+
},
431+
},
432+
},
433+
},
434+
want: want{
435+
rsp: &fnv1.RunFunctionResponse{
436+
Meta: &fnv1.ResponseMeta{Tag: "empty-input", Ttl: durationpb.New(response.DefaultTTL)},
437+
Results: []*fnv1.Result{{
438+
Target: fnv1.Target_TARGET_COMPOSITE.Enum(),
439+
Severity: fnv1.Severity_SEVERITY_FATAL,
440+
Message: "invalid function input: spec.source: Required value: kcl source cannot be empty",
441+
}},
442+
},
443+
},
444+
},
390445
// TODO: disable the resource check, and fix the kcl dup resource evaluation issues.
391446
// "MultipleResourceError": {
392447
// reason: "The Function should return a fatal result if input resources have duplicate names",
@@ -425,6 +480,9 @@ func TestRunFunctionSimple(t *testing.T) {
425480

426481
for name, tc := range cases {
427482
t.Run(name, func(t *testing.T) {
483+
// NOTE: This means we can't run tests in parallel.
484+
defaultSource = tc.defaultSource
485+
428486
// f := &Function{log: logging.NewNopLogger()}
429487
f := &Function{log: logging.NewLogrLogger(testr.New(t))}
430488
rsp, err := f.RunFunction(tc.args.ctx, tc.args.req)

0 commit comments

Comments
 (0)