Skip to content

Commit bef4c04

Browse files
authored
Merge pull request #91 from negz/hatchery
Update hatch, and use a more illustrative example
2 parents 34ed177 + cf90450 commit bef4c04

File tree

7 files changed

+57
-43
lines changed

7 files changed

+57
-43
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ on:
1414

1515
env:
1616
# Common versions
17-
PYTHON_VERSION: '3.11.5'
17+
PYTHON_VERSION: '3.11'
18+
HATCH_VERSION: '1.12.0'
1819
DOCKER_BUILDX_VERSION: 'v0.11.2'
1920

2021
# These environment variables are important to the Crossplane CLI install.sh
@@ -47,10 +48,10 @@ jobs:
4748
python-version: ${{ env.PYTHON_VERSION }}
4849

4950
- name: Setup Hatch
50-
run: pipx install hatch==1.7.0
51+
run: pipx install hatch==${{ env.HATCH_VERSION }}
5152

5253
- name: Lint
53-
run: hatch run lint:check
54+
run: hatch fmt
5455

5556
unit-test:
5657
runs-on: ubuntu-22.04
@@ -64,10 +65,10 @@ jobs:
6465
python-version: ${{ env.PYTHON_VERSION }}
6566

6667
- name: Setup Hatch
67-
run: pipx install hatch==1.7.0
68+
run: pipx install hatch==${{ env.HATCH_VERSION }}
6869

6970
- name: Run Unit Tests
70-
run: hatch run test:unit
71+
run: hatch test --all --randomize
7172

7273
# We want to build most packages for the amd64 and arm64 architectures. To
7374
# speed this up we build single-platform packages in parallel. We then upload

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ CLI][cli] to build functions.
2525
# Run the code in development mode, for crossplane beta render
2626
hatch run development
2727

28-
# Lint the code - see pyproject.toml
29-
hatch run lint:check
28+
# Lint and format the code - see pyproject.toml
29+
hatch fmt
3030

3131
# Run unit tests - see tests/test_fn.py
32-
hatch run test:unit
32+
hatch test
3333

3434
# Build the function's runtime image - see Dockerfile
3535
$ docker build . --tag=runtime

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

pyproject.toml

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,10 @@ dependencies = ["ipython==8.28.0"]
4848
[tool.hatch.envs.default.scripts]
4949
development = "python function/main.py --insecure --debug"
5050

51-
[tool.hatch.envs.lint]
52-
type = "virtual"
53-
detached = true
54-
path = ".venv-lint"
51+
# This special environment is used by hatch fmt.
52+
[tool.hatch.envs.hatch-static-analysis]
5553
dependencies = ["ruff==0.6.9"]
56-
57-
[tool.hatch.envs.lint.scripts]
58-
check = "ruff check function tests && ruff format --diff function tests"
59-
60-
[tool.hatch.envs.test]
61-
type = "virtual"
62-
path = ".venv-test"
63-
64-
[tool.hatch.envs.test.scripts]
65-
unit = "python -m unittest tests/*.py"
54+
config-path = "none" # Disable Hatch's default Ruff config.
6655

6756
[tool.ruff]
6857
target-version = "py311"

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)