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 <nicc@rk0n.org>
This commit is contained in:
parent
3268b9c0dc
commit
cf90450981
|
@ -14,4 +14,4 @@ spec:
|
||||||
input:
|
input:
|
||||||
apiVersion: template.fn.crossplane.io/v1beta1
|
apiVersion: template.fn.crossplane.io/v1beta1
|
||||||
kind: Input
|
kind: Input
|
||||||
example: "Hello world"
|
version: v1beta2
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""A Crossplane composition function."""
|
"""A Crossplane composition function."""
|
||||||
|
|
||||||
import grpc
|
import grpc
|
||||||
from crossplane.function import logging, response
|
from crossplane.function import logging, resource, response
|
||||||
from crossplane.function.proto.v1 import run_function_pb2 as fnv1
|
from crossplane.function.proto.v1 import run_function_pb2 as fnv1
|
||||||
from crossplane.function.proto.v1 import run_function_pb2_grpc as grpcv1
|
from crossplane.function.proto.v1 import run_function_pb2_grpc as grpcv1
|
||||||
|
|
||||||
|
@ -22,12 +22,18 @@ class FunctionRunner(grpcv1.FunctionRunnerService):
|
||||||
|
|
||||||
rsp = response.to(req)
|
rsp = response.to(req)
|
||||||
|
|
||||||
example = ""
|
version = req.input["version"]
|
||||||
if "example" in req.input:
|
region = req.observed.composite.resource["spec"]["region"]
|
||||||
example = req.input["example"]
|
|
||||||
|
|
||||||
# TODO: Add your function logic here!
|
resource.update(
|
||||||
response.normal(rsp, f"I was run with input {example}!")
|
rsp.desired.resources["bucket"],
|
||||||
log.info("I was run!", input=example)
|
{
|
||||||
|
"apiVersion": f"s3.aws.upbound.io/{version}",
|
||||||
|
"kind": "Bucket",
|
||||||
|
"spec": {
|
||||||
|
"forProvider": {"region": region},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
return rsp
|
return rsp
|
||||||
|
|
|
@ -24,9 +24,8 @@ spec:
|
||||||
of an object. Servers should convert recognized schemas to the latest
|
of an object. Servers should convert recognized schemas to the latest
|
||||||
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
|
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
|
||||||
type: string
|
type: string
|
||||||
example:
|
version:
|
||||||
description: Example is an example field. Replace it with whatever input
|
description: The bucket version to compose (e.g. v1beta2).
|
||||||
you need. :)
|
|
||||||
type: string
|
type: string
|
||||||
kind:
|
kind:
|
||||||
description: 'Kind is a string value representing the REST resource this
|
description: 'Kind is a string value representing the REST resource this
|
||||||
|
@ -36,7 +35,7 @@ spec:
|
||||||
metadata:
|
metadata:
|
||||||
type: object
|
type: object
|
||||||
required:
|
required:
|
||||||
- example
|
- version
|
||||||
type: object
|
type: object
|
||||||
served: true
|
served: true
|
||||||
storage: true
|
storage: true
|
||||||
|
|
|
@ -28,17 +28,36 @@ class TestFunctionRunner(unittest.IsolatedAsyncioTestCase):
|
||||||
TestCase(
|
TestCase(
|
||||||
reason="The function should return the input as a result.",
|
reason="The function should return the input as a result.",
|
||||||
req=fnv1.RunFunctionRequest(
|
req=fnv1.RunFunctionRequest(
|
||||||
input=resource.dict_to_struct({"example": "Hello, world"})
|
input=resource.dict_to_struct({"version": "v1beta2"}),
|
||||||
|
observed=fnv1.State(
|
||||||
|
composite=fnv1.Resource(
|
||||||
|
resource=resource.dict_to_struct(
|
||||||
|
{
|
||||||
|
"apiVersion": "example.crossplane.io/v1",
|
||||||
|
"kind": "XR",
|
||||||
|
"spec": {"region": "us-west-2"},
|
||||||
|
}
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
want=fnv1.RunFunctionResponse(
|
want=fnv1.RunFunctionResponse(
|
||||||
meta=fnv1.ResponseMeta(ttl=durationpb.Duration(seconds=60)),
|
meta=fnv1.ResponseMeta(ttl=durationpb.Duration(seconds=60)),
|
||||||
desired=fnv1.State(),
|
desired=fnv1.State(
|
||||||
results=[
|
resources={
|
||||||
fnv1.Result(
|
"bucket": fnv1.Resource(
|
||||||
severity=fnv1.SEVERITY_NORMAL,
|
resource=resource.dict_to_struct(
|
||||||
message="I was run with input Hello, world!",
|
{
|
||||||
)
|
"apiVersion": "s3.aws.upbound.io/v1beta2",
|
||||||
],
|
"kind": "Bucket",
|
||||||
|
"spec": {
|
||||||
|
"forProvider": {"region": "us-west-2"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
context=structpb.Struct(),
|
context=structpb.Struct(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in New Issue