96 lines
2.7 KiB
YAML
96 lines
2.7 KiB
YAML
# The simulator: a stub of the API the DAG reads, faithful to the wire (its field
|
|
# names are the API's, not the app's). Same shape as the starter's example-mock.
|
|
apiVersion: v1
|
|
kind: ConfigMap
|
|
metadata:
|
|
name: items-api-stub
|
|
data:
|
|
routes.json: |
|
|
{
|
|
"/health": {"status": 200, "body": {"status": "ok"}},
|
|
"/v1/items": {"status": 200, "body": {"items": [
|
|
{"id": "a-100", "name": "anvil", "price": {"amount_cents": 1999, "currency": "USD"}},
|
|
{"id": "b-200", "name": "bucket", "price": {"amount_cents": 450, "currency": "USD"}},
|
|
{"id": "c-300", "name": "crate", "price": {"amount_cents": 1200, "currency": "USD"}}
|
|
]}}
|
|
}
|
|
serve.py: |
|
|
import json, os
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
|
|
ROUTES = json.load(open("/etc/stub/routes.json"))
|
|
NAME = os.environ.get("STUB_NAME", "stub")
|
|
|
|
class H(BaseHTTPRequestHandler):
|
|
def do_GET(self):
|
|
r = ROUTES.get(self.path)
|
|
if r is None:
|
|
self.send_response(404)
|
|
self.end_headers()
|
|
self.wfile.write(json.dumps(
|
|
{"error": "no canned route", "stub": NAME, "path": self.path}
|
|
).encode())
|
|
return
|
|
body = json.dumps(r["body"]).encode()
|
|
self.send_response(r["status"])
|
|
self.send_header("Content-Type", "application/json")
|
|
self.send_header("X-Mocked-By", NAME)
|
|
self.end_headers()
|
|
self.wfile.write(body)
|
|
|
|
def log_message(self, fmt, *args):
|
|
print("%s %s" % (NAME, fmt % args), flush=True)
|
|
|
|
HTTPServer(("0.0.0.0", 8080), H).serve_forever()
|
|
---
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: items-api
|
|
labels:
|
|
app: items-api
|
|
rig.component/impl: mock
|
|
spec:
|
|
replicas: 1
|
|
selector:
|
|
matchLabels:
|
|
app: items-api
|
|
template:
|
|
metadata:
|
|
labels:
|
|
app: items-api
|
|
spec:
|
|
containers:
|
|
- name: stub
|
|
image: python:3.12-slim
|
|
command: ["python3", "/etc/stub/serve.py"]
|
|
env:
|
|
- name: STUB_NAME
|
|
value: items-api
|
|
ports:
|
|
- containerPort: 8080
|
|
volumeMounts:
|
|
- name: stub
|
|
mountPath: /etc/stub
|
|
readinessProbe:
|
|
httpGet: { path: /health, port: 8080 }
|
|
initialDelaySeconds: 2
|
|
resources:
|
|
requests: { memory: 32Mi, cpu: 10m }
|
|
limits: { memory: 64Mi }
|
|
volumes:
|
|
- name: stub
|
|
configMap:
|
|
name: items-api-stub
|
|
---
|
|
apiVersion: v1
|
|
kind: Service
|
|
metadata:
|
|
name: items-api
|
|
spec:
|
|
selector:
|
|
app: items-api
|
|
ports:
|
|
- port: 80
|
|
targetPort: 8080
|