Skip to content

Commit 38fe415

Browse files
committed
Make example work correctly
Signed-off-by: Nic Cope <[email protected]>
1 parent c17c0e7 commit 38fe415

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

example/operation/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Operations Example
2+
3+
This example demonstrates using function-python with Crossplane Operations to
4+
check SSL certificate expiry for websites referenced in Kubernetes Ingress
5+
resources.
6+
7+
## Files
8+
9+
- `operation.yaml` - The Operation that checks certificate expiry
10+
- `functions.yaml` - Function definition for local development
11+
- `ingress.yaml` - Sample Ingress resource to check
12+
- `rbac.yaml` - RBAC permissions for Operations to access Ingress resources
13+
- `README.md` - This file
14+
15+
## Testing
16+
17+
Since Operations are runtime-only (they can't be statically rendered), you can
18+
test this example locally using the new `crossplane alpha render op` command.
19+
20+
### Prerequisites
21+
22+
1. Run the function in development mode:
23+
```bash
24+
hatch run development
25+
```
26+
27+
2. In another terminal, render the operation:
28+
```bash
29+
crossplane alpha render op operation.yaml functions.yaml --required-resources . -r
30+
```
31+
32+
The `-r` flag includes function results in the output, and
33+
`--required-resources .` tells the command to use the ingress.yaml file in this
34+
directory as the required resource.
35+
36+
## What it does
37+
38+
The Operation:
39+
40+
1. **Reads the Ingress** resource specified in `requirements.requiredResources`
41+
2. **Extracts the hostname** from the Ingress rules (`google.com` in this
42+
example)
43+
3. **Fetches the SSL certificate** for that hostname
44+
4. **Calculates expiry information** (days until expiration)
45+
5. **Annotates the Ingress** with certificate monitoring annotations
46+
6. **Returns status information** in the Operation's output field
47+
48+
This pattern is useful for:
49+
- Certificate monitoring and alerting
50+
- Compliance checking
51+
- Automated certificate renewal workflows
52+
- Integration with monitoring tools that read annotations
53+
54+
## Function Details
55+
56+
The operation function (`operate()`) demonstrates key Operations patterns:
57+
58+
- **Required Resources**: Accessing pre-populated resources via
59+
`request.get_required_resources(req, "ingress")`
60+
- **Resource Updates**: Using `rsp.desired.resources` to update existing
61+
resources
62+
- **Operation Output**: Using `rsp.output.update()` for monitoring data
63+
- **Server-side Apply**: Crossplane applies the changes with force ownership

example/operation/operation.yaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ spec:
2323
import socket
2424
from datetime import datetime
2525
26+
from crossplane.function import request, response
27+
2628
def operate(req, rsp):
2729
# Get the Ingress resource
28-
ingress = req.required_resources["ingress"].resource
30+
ingress = request.get_required_resource(req, "ingress")
31+
if not ingress:
32+
response.set_output(rsp, {"error": "No ingress resource found"})
33+
return
2934
3035
# Extract hostname from Ingress rules
3136
hostname = ingress["spec"]["rules"][0]["host"]
@@ -41,6 +46,10 @@ spec:
4146
expiry_date = datetime.strptime(cert['notAfter'], '%b %d %H:%M:%S %Y %Z')
4247
days_until_expiry = (expiry_date - datetime.now()).days
4348
49+
# Add warning if certificate expires soon
50+
if days_until_expiry < 30:
51+
response.warning(rsp, f"Certificate for {hostname} expires in {days_until_expiry} days")
52+
4453
# Annotate the Ingress with certificate expiry info
4554
rsp.desired.resources["ingress"].resource.update({
4655
"apiVersion": "networking.k8s.io/v1",
@@ -57,7 +66,7 @@ spec:
5766
})
5867
5968
# Return results in operation output for monitoring
60-
rsp.output.update({
69+
response.set_output(rsp, {
6170
"hostname": hostname,
6271
"certificateExpires": cert['notAfter'],
6372
"daysUntilExpiry": days_until_expiry,

0 commit comments

Comments
 (0)