Skip to content

Commit cf90450

Browse files
committed
Use a more illustrative default example
Emitting a hello world result isn't very useful. Instead, show how to compose an MR with some fields derived from the XR and some from the input. Signed-off-by: Nic Cope <[email protected]>
1 parent 3268b9c commit cf90450

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

example/composition.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ spec:
1414
input:
1515
apiVersion: template.fn.crossplane.io/v1beta1
1616
kind: Input
17-
example: "Hello world"
17+
version: v1beta2

function/fn.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""A Crossplane composition function."""
22

33
import grpc
4-
from crossplane.function import logging, response
4+
from crossplane.function import logging, resource, response
55
from crossplane.function.proto.v1 import run_function_pb2 as fnv1
66
from crossplane.function.proto.v1 import run_function_pb2_grpc as grpcv1
77

@@ -22,12 +22,18 @@ async def RunFunction(
2222

2323
rsp = response.to(req)
2424

25-
example = ""
26-
if "example" in req.input:
27-
example = req.input["example"]
28-
29-
# TODO: Add your function logic here!
30-
response.normal(rsp, f"I was run with input {example}!")
31-
log.info("I was run!", input=example)
25+
version = req.input["version"]
26+
region = req.observed.composite.resource["spec"]["region"]
27+
28+
resource.update(
29+
rsp.desired.resources["bucket"],
30+
{
31+
"apiVersion": f"s3.aws.upbound.io/{version}",
32+
"kind": "Bucket",
33+
"spec": {
34+
"forProvider": {"region": region},
35+
},
36+
},
37+
)
3238

3339
return rsp

package/input/template.fn.crossplane.io_inputs.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ spec:
2424
of an object. Servers should convert recognized schemas to the latest
2525
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
2626
type: string
27-
example:
28-
description: Example is an example field. Replace it with whatever input
29-
you need. :)
27+
version:
28+
description: The bucket version to compose (e.g. v1beta2).
3029
type: string
3130
kind:
3231
description: 'Kind is a string value representing the REST resource this
@@ -36,7 +35,7 @@ spec:
3635
metadata:
3736
type: object
3837
required:
39-
- example
38+
- version
4039
type: object
4140
served: true
4241
storage: true

tests/test_fn.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,36 @@ class TestCase:
2828
TestCase(
2929
reason="The function should return the input as a result.",
3030
req=fnv1.RunFunctionRequest(
31-
input=resource.dict_to_struct({"example": "Hello, world"})
31+
input=resource.dict_to_struct({"version": "v1beta2"}),
32+
observed=fnv1.State(
33+
composite=fnv1.Resource(
34+
resource=resource.dict_to_struct(
35+
{
36+
"apiVersion": "example.crossplane.io/v1",
37+
"kind": "XR",
38+
"spec": {"region": "us-west-2"},
39+
}
40+
),
41+
),
42+
),
3243
),
3344
want=fnv1.RunFunctionResponse(
3445
meta=fnv1.ResponseMeta(ttl=durationpb.Duration(seconds=60)),
35-
desired=fnv1.State(),
36-
results=[
37-
fnv1.Result(
38-
severity=fnv1.SEVERITY_NORMAL,
39-
message="I was run with input Hello, world!",
40-
)
41-
],
46+
desired=fnv1.State(
47+
resources={
48+
"bucket": fnv1.Resource(
49+
resource=resource.dict_to_struct(
50+
{
51+
"apiVersion": "s3.aws.upbound.io/v1beta2",
52+
"kind": "Bucket",
53+
"spec": {
54+
"forProvider": {"region": "us-west-2"},
55+
},
56+
}
57+
),
58+
),
59+
},
60+
),
4261
context=structpb.Struct(),
4362
),
4463
),

0 commit comments

Comments
 (0)