Compare commits
14
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef79bf9a80 | ||
|
|
db8f103fa5 | ||
|
|
234d2a2c02 | ||
|
|
d16597ca0b | ||
|
|
509cb39521 | ||
|
|
03f3239cd0 | ||
|
|
d5c7440655 | ||
|
|
bff46fefe7 | ||
|
|
c93bfca6a8 | ||
|
|
44ec3502dc | ||
|
|
6608f1a8d5 | ||
|
|
c6cd41fd4b | ||
|
|
b0f608f145 | ||
|
|
cc9a32f53a |
@@ -0,0 +1,4 @@
|
|||||||
|
(apply,CacheStats{hitCount=337, missCount=199, loadSuccessCount=199, loadExceptionCount=0, totalLoadTime=581291927, evictionCount=0})
|
||||||
|
(tree,CacheStats{hitCount=986, missCount=352, loadSuccessCount=299, loadExceptionCount=0, totalLoadTime=821650758, evictionCount=0})
|
||||||
|
(commit,CacheStats{hitCount=108, missCount=107, loadSuccessCount=107, loadExceptionCount=0, totalLoadTime=78983052, evictionCount=0})
|
||||||
|
(tag,CacheStats{hitCount=0, missCount=2, loadSuccessCount=2, loadExceptionCount=0, totalLoadTime=319542, evictionCount=0})
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
e71e5b78236a67327c678490cb50b46981f19de0 bbcbb68b91e786eb71bbb0a4443d7b8a26140e1b .sops.yaml
|
||||||
|
4189696f5581ac0ffdc125c3bf9b9f664b3ddfb0 7cd3f1ee4865c563d141464f6fc185436993b84b .sops.yaml
|
||||||
|
635630e73152a5f22e6cbd42322ec55d79f8d9c0 297e94a89d73d18c4f47013bb0e8303f123715f3 configmap.yaml
|
||||||
|
29e515e7b46742fab8c3fcc2189af7010a6ccc62 6869fa11f96e03f7ec76a0ea14a4ddaf604004a4 gateway-config-secret.enc.yaml
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
0a95af80c0051bacbeb8483c1632e47acd3db5be 40207e487cfb63409a976fb2a0b9e1e62c8b1513
|
||||||
|
27428d910111299d0699f429190284a9ca6e50b7 3318daf758349402aef43b095482743ab96b37f9
|
||||||
|
329a495af4c935529fdae17229314101c0c77876 67f24ea76359c8dba4b56267790aad76bbc58464
|
||||||
|
4c8bc6c920b6b75399555827022f69ef0c4f7d15 1fa839b41975fa3f0ac9052355ffb625f5a8f324
|
||||||
|
528545f414c83217408edfea234dcd1f3edee0c2 b8f95506ca1545b876b5531cd385172e9ca5b4b0
|
||||||
|
81038e1cf7567a9133d7c233a97b1e2f19fa1c82 4a00312906ba725f3968187656fde2663b1763ab
|
||||||
|
a5b3b5c44a406896bcb414df6c6426c277715706 2ab47a9dbe5ba36dfa0e275991ef7b7656908410
|
||||||
|
ce27643667a0399115cd1f2b6d38123fdcf2b4f1 6ff0a50de8efbad105fa588245f22fdb26afddc4
|
||||||
|
d49756886a46542b38533b913a1f776b5145f5ec d82cc5a6970a1fb32e21dda9a737b987a8668111
|
||||||
|
db3a30fbcf1f139c667fb68a91762582c49b8cee 04619a269fed9eeea53ab4d4d73131e3713f40a0
|
||||||
|
eb54715e4dec0fb35402576fcc224a09808b00c1 d53b7632cf9646dda1c978a5f94615dc9eaed5e8
|
||||||
|
ef72b5bbccf2df89aa1c86dee29311c63f33bf62 ba55d184fefef1a73a50409ca4fb1f7b27f5b075
|
||||||
+50
-107
@@ -32,14 +32,13 @@ jobs:
|
|||||||
run: go vet ./...
|
run: go vet ./...
|
||||||
|
|
||||||
- name: Go test
|
- name: Go test
|
||||||
run: go test -short ./...
|
run: go test ./...
|
||||||
|
|
||||||
- name: Get short SHA
|
- name: Get short SHA
|
||||||
id: sha
|
id: sha
|
||||||
run: echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
|
run: echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
- name: Registry login
|
- name: Registry login
|
||||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
||||||
run: |
|
run: |
|
||||||
echo "${REGISTRY_TOKEN}" | docker login "${REGISTRY}" \
|
echo "${REGISTRY_TOKEN}" | docker login "${REGISTRY}" \
|
||||||
--username "${REGISTRY_USER}" --password-stdin
|
--username "${REGISTRY_USER}" --password-stdin
|
||||||
@@ -48,143 +47,87 @@ jobs:
|
|||||||
REGISTRY_TOKEN: ${{ secrets.FORGEJO_REGISTRY_TOKEN }}
|
REGISTRY_TOKEN: ${{ secrets.FORGEJO_REGISTRY_TOKEN }}
|
||||||
|
|
||||||
- name: Build Docker image
|
- name: Build Docker image
|
||||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
||||||
run: |
|
run: |
|
||||||
docker build --no-cache \
|
docker build --no-cache \
|
||||||
-t "${IMAGE}:${{ steps.sha.outputs.short_sha }}" \
|
-t "${IMAGE}:${{ steps.sha.outputs.short_sha }}" \
|
||||||
-f Dockerfile .
|
-f Dockerfile .
|
||||||
|
|
||||||
- name: Push image (SHA tag)
|
- name: Push image (SHA tag)
|
||||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
||||||
run: docker push "${IMAGE}:${{ steps.sha.outputs.short_sha }}"
|
run: docker push "${IMAGE}:${{ steps.sha.outputs.short_sha }}"
|
||||||
|
|
||||||
# ── Deploy canary pod + integration test ────────────────
|
# ── Tekton integration tests ─────────────────────────────
|
||||||
# Canary pod runs in-cluster with real secrets and real upstreams
|
|
||||||
# (Temporal, Gotify). Tests run against its pod IP directly.
|
|
||||||
# Zero downtime: production pods untouched until tests pass.
|
|
||||||
- name: Setup kubeconfig
|
- name: Setup kubeconfig
|
||||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
||||||
run: |
|
run: |
|
||||||
mkdir -p ~/.kube
|
mkdir -p ~/.kube
|
||||||
echo "${KUBECONFIG_B64}" | base64 -d > ~/.kube/config
|
echo "${KUBECONFIG_B64}" | base64 -d > ~/.kube/config
|
||||||
kubectl get nodes --no-headers | head -1
|
kubectl get pipelineruns -n api --no-headers | head -1 || echo 'No PipelineRuns yet'
|
||||||
echo '✓ kubeconfig works'
|
echo '✓ kubeconfig works'
|
||||||
env:
|
env:
|
||||||
KUBECONFIG_B64: ${{ secrets.KUBECONFIG_B64 }}
|
KUBECONFIG_B64: ${{ secrets.KUBECONFIG_B64 }}
|
||||||
|
|
||||||
- name: Deploy canary pod
|
- name: Trigger Tekton PipelineRun
|
||||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
id: tekton
|
||||||
id: canary
|
|
||||||
run: |
|
run: |
|
||||||
SHA="${{ steps.sha.outputs.short_sha }}"
|
SHA="${{ steps.sha.outputs.short_sha }}"
|
||||||
POD_NAME="api-gateway-canary-${SHA}"
|
RUN_NAME="integration-test-${SHA}"
|
||||||
|
|
||||||
# Remove any stale canary
|
# Clean up any previous run with the same name
|
||||||
kubectl delete pod "${POD_NAME}" -n api --ignore-not-found
|
kubectl delete taskrun "${RUN_NAME}" -n api --ignore-not-found
|
||||||
|
|
||||||
# Create canary pod with real secrets + config
|
# Create TaskRun — spins up gateway sidecar + curl tests
|
||||||
kubectl run "${POD_NAME}" \
|
cat <<YAML | kubectl create -f -
|
||||||
--image="${IMAGE}:${SHA}" \
|
apiVersion: tekton.dev/v1
|
||||||
--restart=Never \
|
kind: TaskRun
|
||||||
--namespace=api \
|
metadata:
|
||||||
--labels="app=api-gateway-canary,sha=${SHA}" \
|
name: ${RUN_NAME}
|
||||||
--overrides="$(cat <<JSON
|
namespace: api
|
||||||
{
|
labels:
|
||||||
"spec": {
|
commit-sha: "${SHA}"
|
||||||
"imagePullSecrets": [{"name": "forgejo-registry"}],
|
spec:
|
||||||
"volumes": [
|
taskRef:
|
||||||
{"name": "config", "secret": {"secretName": "api-gateway-config"}}
|
name: integration-test
|
||||||
],
|
params:
|
||||||
"containers": [{
|
- name: image
|
||||||
"name": "${POD_NAME}",
|
value: "${IMAGE}:${SHA}"
|
||||||
"image": "${IMAGE}:${SHA}",
|
YAML
|
||||||
"imagePullPolicy": "Always",
|
|
||||||
"env": [
|
|
||||||
{"name": "LISTEN_ADDR", "value": "0.0.0.0:8080"},
|
|
||||||
{"name": "CONFIG_PATH", "value": "/etc/gateway/config.yaml"},
|
|
||||||
{"name": "LOG_LEVEL", "value": "info"},
|
|
||||||
{"name": "GOTIFY_URL", "valueFrom": {"secretKeyRef": {"name": "gotify-webhook-secret", "key": "gotify-url", "optional": true}}},
|
|
||||||
{"name": "GOTIFY_APP_TOKEN", "valueFrom": {"secretKeyRef": {"name": "gotify-webhook-secret", "key": "gotify-app-token", "optional": true}}},
|
|
||||||
{"name": "FORGEJO_WEBHOOK_SECRET","valueFrom": {"secretKeyRef": {"name": "gotify-webhook-secret", "key": "forgejo-webhook-secret", "optional": true}}},
|
|
||||||
{"name": "AUTH_CLIENT_SECRET", "valueFrom": {"secretKeyRef": {"name": "api-gw-client-secret", "key": "client-secret", "optional": true}}}
|
|
||||||
],
|
|
||||||
"volumeMounts": [
|
|
||||||
{"name": "config", "mountPath": "/etc/gateway", "readOnly": true}
|
|
||||||
],
|
|
||||||
"readinessProbe": {
|
|
||||||
"httpGet": {"path": "/healthz", "port": 8080},
|
|
||||||
"initialDelaySeconds": 3,
|
|
||||||
"periodSeconds": 2
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
JSON
|
|
||||||
)"
|
|
||||||
|
|
||||||
echo "✓ Canary pod created: ${POD_NAME}"
|
echo "✓ TaskRun created: ${RUN_NAME}"
|
||||||
|
|
||||||
# Wait for pod to be ready (real secrets, real upstreams)
|
# Wait for completion (Succeeded or Failed)
|
||||||
kubectl wait pod/"${POD_NAME}" -n api \
|
echo "Waiting for tests (timeout 5m)..."
|
||||||
--for=condition=Ready --timeout=90s
|
if kubectl wait taskrun/"${RUN_NAME}" -n api \
|
||||||
|
--for=condition=Succeeded --timeout=5m 2>/dev/null; then
|
||||||
|
echo "result=pass" >> $GITHUB_OUTPUT
|
||||||
|
else
|
||||||
|
echo "result=fail" >> $GITHUB_OUTPUT
|
||||||
|
fi
|
||||||
|
|
||||||
# Capture pod IP for test runner
|
# Print logs + results
|
||||||
POD_IP=$(kubectl get pod "${POD_NAME}" -n api \
|
echo ""
|
||||||
-o jsonpath='{.status.podIP}')
|
echo "=== Test Logs ==="
|
||||||
echo "pod_name=${POD_NAME}" >> $GITHUB_OUTPUT
|
POD=$(kubectl get pod -n api -l tekton.dev/taskRun=${RUN_NAME} -o name | head -1)
|
||||||
echo "pod_ip=${POD_IP}" >> $GITHUB_OUTPUT
|
kubectl logs -n api "${POD}" -c step-run-tests 2>/dev/null || true
|
||||||
echo "✓ Canary ready at ${POD_IP}:8080"
|
echo ""
|
||||||
|
REASON=$(kubectl get taskrun "${RUN_NAME}" -n api \
|
||||||
- name: Run integration tests against canary
|
-o jsonpath='{.status.conditions[0].reason}')
|
||||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
SUMMARY=$(kubectl get taskrun "${RUN_NAME}" -n api \
|
||||||
id: tests
|
-o jsonpath='{.status.results[?(@.name=="summary")].value}')
|
||||||
run: |
|
echo "Status: ${REASON}"
|
||||||
POD_IP="${{ steps.canary.outputs.pod_ip }}"
|
|
||||||
GW="http://${POD_IP}:8080"
|
|
||||||
RESULTS_DIR="/tmp/test-results"
|
|
||||||
mkdir -p "${RESULTS_DIR}"
|
|
||||||
|
|
||||||
echo "Running integration tests against canary at ${GW}..."
|
|
||||||
sh k8s/tekton/scripts/integration-test.sh
|
|
||||||
|
|
||||||
RESULT=$(cat "${RESULTS_DIR}/result" 2>/dev/null || echo "fail")
|
|
||||||
SUMMARY=$(cat "${RESULTS_DIR}/summary" 2>/dev/null || echo "unknown")
|
|
||||||
echo "Result: ${RESULT}"
|
|
||||||
echo "Summary: ${SUMMARY}"
|
echo "Summary: ${SUMMARY}"
|
||||||
echo "result=${RESULT}" >> $GITHUB_OUTPUT
|
|
||||||
|
|
||||||
- name: Cleanup canary pod
|
|
||||||
if: always() && github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
||||||
run: |
|
|
||||||
POD_NAME="${{ steps.canary.outputs.pod_name }}"
|
|
||||||
kubectl delete pod "${POD_NAME}" -n api --ignore-not-found || true
|
|
||||||
echo "✓ Canary pod removed"
|
|
||||||
|
|
||||||
- name: Gate on test result
|
- name: Gate on test result
|
||||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main' && steps.tests.outputs.result != 'pass'
|
if: steps.tekton.outputs.result != 'pass'
|
||||||
run: |
|
run: |
|
||||||
echo "✗ Integration tests FAILED — production deployment unchanged"
|
echo "✗ Integration tests FAILED — image NOT promoted"
|
||||||
exit 1
|
exit 1
|
||||||
|
|
||||||
# ── Zero-downtime promotion ──────────────────────────────
|
# ── Promote only after tests pass ────────────────────────
|
||||||
# kubectl set image triggers a rolling update: new pods come up
|
- name: Promote image to latest
|
||||||
# before old pods are terminated. Production traffic uninterrupted.
|
|
||||||
- name: Promote — rolling update
|
|
||||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
||||||
run: |
|
|
||||||
SHA="${{ steps.sha.outputs.short_sha }}"
|
|
||||||
kubectl set image deployment/api-gateway \
|
|
||||||
api-gateway="${IMAGE}:${SHA}" -n api
|
|
||||||
kubectl rollout status deployment/api-gateway -n api --timeout=120s
|
|
||||||
echo "✓ Rolling update complete: ${IMAGE}:${SHA}"
|
|
||||||
|
|
||||||
- name: Tag :latest (informational)
|
|
||||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
||||||
run: |
|
run: |
|
||||||
docker tag "${IMAGE}:${{ steps.sha.outputs.short_sha }}" "${IMAGE}:latest"
|
docker tag "${IMAGE}:${{ steps.sha.outputs.short_sha }}" "${IMAGE}:latest"
|
||||||
docker push "${IMAGE}:latest"
|
docker push "${IMAGE}:latest"
|
||||||
echo "✓ Tagged :latest"
|
echo "✓ Promoted to latest"
|
||||||
|
|
||||||
- name: Cleanup docker images
|
- name: Cleanup
|
||||||
if: always()
|
if: always()
|
||||||
run: docker image prune -af 2>&1 | tail -3 || true
|
run: docker image prune -af 2>&1 | tail -3 || true
|
||||||
|
|||||||
+1
-9
@@ -108,16 +108,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
_ = registry.Add(notifAdapter)
|
_ = registry.Add(notifAdapter)
|
||||||
|
|
||||||
// Add Poimen orchestrator adapter (typed REST for AI orchestrator frontend)
|
// Add other adapters from config
|
||||||
poimenAdapter := serviceadapter.NewPoimenAdapter(temporalHandler)
|
|
||||||
_ = registry.Add(poimenAdapter)
|
|
||||||
|
|
||||||
// Add other adapters from config (skip if already registered in code)
|
|
||||||
for _, a := range cfg.Adapters {
|
for _, a := range cfg.Adapters {
|
||||||
if existing := registry.Get(a.ServiceName); existing != nil {
|
|
||||||
log.Printf("skip config adapter '%s': already registered with internal handler", a.ServiceName)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
_ = registry.Add(a)
|
_ = registry.Add(a)
|
||||||
}
|
}
|
||||||
log.Printf("%d service adapters loaded", registry.Count())
|
log.Printf("%d service adapters loaded", registry.Count())
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"forgejo.riotpiao.com/rock/homelab-frontend/internal/serviceadapter"
|
"forgejo.riotpiao.com/rock/homelab-frontend/internal/serviceadapter"
|
||||||
"forgejo.riotpiao.com/rock/homelab-frontend/internal/webhook"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Router implements an HTTP handler that routes health endpoints,
|
// Router implements an HTTP handler that routes health endpoints,
|
||||||
@@ -15,7 +14,6 @@ type Router struct {
|
|||||||
dispatcher *serviceadapter.Dispatcher
|
dispatcher *serviceadapter.Dispatcher
|
||||||
temporalHandler http.Handler
|
temporalHandler http.Handler
|
||||||
upstreamHandler http.Handler
|
upstreamHandler http.Handler
|
||||||
forgejoWebhook *webhook.ForgejoHandler
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRouter creates a new router with health endpoints.
|
// NewRouter creates a new router with health endpoints.
|
||||||
@@ -29,7 +27,6 @@ func NewRouter(healthChecker *HealthChecker, dispatcher *serviceadapter.Dispatch
|
|||||||
dispatcher: dispatcher,
|
dispatcher: dispatcher,
|
||||||
temporalHandler: temporalHandler,
|
temporalHandler: temporalHandler,
|
||||||
upstreamHandler: upstreamHandler,
|
upstreamHandler: upstreamHandler,
|
||||||
forgejoWebhook: webhook.NewForgejoHandler(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,12 +57,6 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Forgejo webhook receiver — no auth, HMAC-verified by handler
|
|
||||||
if req.URL.Path == "/v1/webhooks/forgejo" {
|
|
||||||
r.forgejoWebhook.ServeHTTP(w, req)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Workflow endpoints
|
// Workflow endpoints
|
||||||
// DEPRECATED: Path-based /workflow routing is legacy.
|
// DEPRECATED: Path-based /workflow routing is legacy.
|
||||||
// New clients should use X-Service: workflow header instead for consistent auth.
|
// New clients should use X-Service: workflow header instead for consistent auth.
|
||||||
|
|||||||
@@ -1,97 +0,0 @@
|
|||||||
package serviceadapter
|
|
||||||
|
|
||||||
import (
|
|
||||||
"forgejo.riotpiao.com/rock/homelab-frontend/internal/temporal"
|
|
||||||
)
|
|
||||||
|
|
||||||
// NewPoimenAdapter creates a ServiceAdapter for the Poimen orchestrator service.
|
|
||||||
func NewPoimenAdapter(h *temporal.Handler) *ServiceAdapter {
|
|
||||||
return &ServiceAdapter{
|
|
||||||
Namespace: "poimen",
|
|
||||||
ServiceName: "poimen",
|
|
||||||
Handler: temporal.NewPoimenHandler(h),
|
|
||||||
Spec: *GetPoimenSpec(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetPoimenSpec returns the ServiceAdapter spec for the Poimen service.
|
|
||||||
func GetPoimenSpec() *Spec {
|
|
||||||
return &Spec{
|
|
||||||
ServiceName: "poimen",
|
|
||||||
Upstream: Upstream{
|
|
||||||
URL: "internal://poimen",
|
|
||||||
TimeoutSeconds: 60,
|
|
||||||
},
|
|
||||||
Auth: Auth{
|
|
||||||
Required: true,
|
|
||||||
Capability: "workflow:read",
|
|
||||||
},
|
|
||||||
Resources: []Resource{
|
|
||||||
{
|
|
||||||
Name: "plan",
|
|
||||||
Methods: []Method{{
|
|
||||||
Verb: "POST",
|
|
||||||
UpstreamPath: "/v1/poimen/plan",
|
|
||||||
Auth: &Auth{Required: true, Capability: "workflow:execute"},
|
|
||||||
}},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "execute",
|
|
||||||
Methods: []Method{{
|
|
||||||
Verb: "POST",
|
|
||||||
UpstreamPath: "/v1/poimen/execute",
|
|
||||||
Auth: &Auth{Required: true, Capability: "workflow:execute"},
|
|
||||||
}},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "executions",
|
|
||||||
Methods: []Method{
|
|
||||||
{
|
|
||||||
Verb: "GET",
|
|
||||||
UpstreamPath: "/v1/poimen/executions",
|
|
||||||
Auth: &Auth{Required: true, Capability: "workflow:read"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "execution-detail",
|
|
||||||
Methods: []Method{
|
|
||||||
{
|
|
||||||
Verb: "GET",
|
|
||||||
UpstreamPath: "/v1/poimen/executions/{id}",
|
|
||||||
Auth: &Auth{Required: true, Capability: "workflow:read"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Verb: "DELETE",
|
|
||||||
UpstreamPath: "/v1/poimen/executions/{id}",
|
|
||||||
Auth: &Auth{Required: true, Capability: "workflow:execute"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "execution-history",
|
|
||||||
Methods: []Method{{
|
|
||||||
Verb: "GET",
|
|
||||||
UpstreamPath: "/v1/poimen/executions/{id}/history",
|
|
||||||
Auth: &Auth{Required: true, Capability: "workflow:read"},
|
|
||||||
}},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "execution-stream",
|
|
||||||
Methods: []Method{{
|
|
||||||
Verb: "GET",
|
|
||||||
UpstreamPath: "/v1/poimen/executions/{id}/stream",
|
|
||||||
Auth: &Auth{Required: true, Capability: "workflow:read"},
|
|
||||||
}},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "execution-signal",
|
|
||||||
Methods: []Method{{
|
|
||||||
Verb: "POST",
|
|
||||||
UpstreamPath: "/v1/poimen/executions/{id}/signal",
|
|
||||||
Auth: &Auth{Required: true, Capability: "workflow:execute"},
|
|
||||||
}},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -24,66 +24,6 @@ func NewWorkflowAdapter(handler *temporal.Handler) *WorkflowAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleStart handles workflow start requests.
|
|
||||||
// Expects payload: { "namespace": "default", "workflow_id": "...", "workflow_type": "...", "task_queue": "...", "input": {...} }
|
|
||||||
func (wa *WorkflowAdapter) HandleStart(w http.ResponseWriter, r *http.Request) {
|
|
||||||
wa.forwardToTemporal(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleDescribe handles workflow describe requests.
|
|
||||||
// Expects payload: { "namespace": "default", "workflow_id": "..." }
|
|
||||||
func (wa *WorkflowAdapter) HandleDescribe(w http.ResponseWriter, r *http.Request) {
|
|
||||||
wa.forwardToTemporal(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleList handles workflow list requests.
|
|
||||||
// Expects payload: { "namespace": "default", "query": "..." (optional) }
|
|
||||||
func (wa *WorkflowAdapter) HandleList(w http.ResponseWriter, r *http.Request) {
|
|
||||||
wa.forwardToTemporal(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleHistory handles workflow history requests.
|
|
||||||
// Expects payload: { "namespace": "default", "workflow_id": "..." }
|
|
||||||
func (wa *WorkflowAdapter) HandleHistory(w http.ResponseWriter, r *http.Request) {
|
|
||||||
wa.forwardToTemporal(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleTerminate handles workflow termination.
|
|
||||||
// Expects payload: { "namespace": "default", "workflow_id": "...", "reason": "..." }
|
|
||||||
func (wa *WorkflowAdapter) HandleTerminate(w http.ResponseWriter, r *http.Request) {
|
|
||||||
wa.forwardToTemporal(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleCancel handles workflow cancellation.
|
|
||||||
// Expects payload: { "namespace": "default", "workflow_id": "..." }
|
|
||||||
func (wa *WorkflowAdapter) HandleCancel(w http.ResponseWriter, r *http.Request) {
|
|
||||||
wa.forwardToTemporal(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleSignal handles workflow signal.
|
|
||||||
// Expects payload: { "namespace": "default", "workflow_id": "...", "signal_name": "...", "signal_data": {...} }
|
|
||||||
func (wa *WorkflowAdapter) HandleSignal(w http.ResponseWriter, r *http.Request) {
|
|
||||||
wa.forwardToTemporal(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleQuery handles workflow query.
|
|
||||||
// Expects payload: { "namespace": "default", "workflow_id": "...", "query_type": "...", "query_data": {...} }
|
|
||||||
func (wa *WorkflowAdapter) HandleQuery(w http.ResponseWriter, r *http.Request) {
|
|
||||||
wa.forwardToTemporal(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleReset handles workflow reset.
|
|
||||||
// Expects payload: { "namespace": "default", "workflow_id": "...", "reset_type": "..." }
|
|
||||||
func (wa *WorkflowAdapter) HandleReset(w http.ResponseWriter, r *http.Request) {
|
|
||||||
wa.forwardToTemporal(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleUpdate handles workflow update.
|
|
||||||
// Expects payload: { "namespace": "default", "workflow_id": "...", "update_data": {...} }
|
|
||||||
func (wa *WorkflowAdapter) HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
|
||||||
wa.forwardToTemporal(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// resourceToAction maps X-Resource names to Temporal action names.
|
// resourceToAction maps X-Resource names to Temporal action names.
|
||||||
var resourceToAction = map[string]string{
|
var resourceToAction = map[string]string{
|
||||||
"execute": "START_WORKFLOW",
|
"execute": "START_WORKFLOW",
|
||||||
@@ -134,13 +74,8 @@ func (wa *WorkflowAdapter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// Inject action into body for temporal handler
|
// Inject action into body for temporal handler
|
||||||
payload["action"] = action
|
payload["action"] = action
|
||||||
|
if _, ok := payload["namespace"]; !ok {
|
||||||
// namespace is required for all workflow operations
|
payload["namespace"] = "default"
|
||||||
if ns, ok := payload["namespace"].(string); !ok || ns == "" {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
fmt.Fprintf(w, `{"error":"namespace is required"}`)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
newBody, _ := json.Marshal(payload)
|
newBody, _ := json.Marshal(payload)
|
||||||
@@ -151,51 +86,17 @@ func (wa *WorkflowAdapter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
wa.temporalHandler.ServeHTTP(w, r)
|
wa.temporalHandler.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// forwardToTemporal reads the request body, ensures namespace is specified,
|
// GetWorkflowSpec returns the ServiceAdapter spec for workflow service.
|
||||||
// and forwards to the temporal handler.
|
|
||||||
func (wa *WorkflowAdapter) forwardToTemporal(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// Read request body
|
|
||||||
body, err := io.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, fmt.Sprintf("failed to read request body: %v", err), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
// Parse JSON to check for namespace
|
|
||||||
var payload map[string]interface{}
|
|
||||||
if err := json.Unmarshal(body, &payload); err != nil {
|
|
||||||
http.Error(w, fmt.Sprintf("invalid JSON payload: %v", err), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure namespace is specified (required for Temporal routing)
|
|
||||||
namespace, ok := payload["namespace"].(string)
|
|
||||||
if !ok || namespace == "" {
|
|
||||||
http.Error(w, `"namespace" field required in payload`, http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Forward to temporal handler by calling it with the request
|
|
||||||
// Restore body for temporal handler
|
|
||||||
r.Body = io.NopCloser(bytes.NewReader(body))
|
|
||||||
r.ContentLength = int64(len(body))
|
|
||||||
|
|
||||||
// Call temporal handler
|
|
||||||
wa.temporalHandler.ServeHTTP(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetSpec returns the ServiceAdapter spec for workflow service.
|
|
||||||
// This defines the available resources and methods.
|
// This defines the available resources and methods.
|
||||||
func GetWorkflowSpec() *Spec {
|
func GetWorkflowSpec() *Spec {
|
||||||
return &Spec{
|
return &Spec{
|
||||||
ServiceName: "workflow",
|
ServiceName: "workflow",
|
||||||
Upstream: Upstream{
|
Upstream: Upstream{
|
||||||
URL: "grpc://temporal:7233", // gRPC endpoint
|
URL: "grpc://temporal:7233",
|
||||||
TimeoutSeconds: 30,
|
TimeoutSeconds: 30,
|
||||||
},
|
},
|
||||||
Auth: Auth{
|
Auth: Auth{
|
||||||
Required: false,
|
Required: true,
|
||||||
Capability: "workflow:execute",
|
Capability: "workflow:execute",
|
||||||
},
|
},
|
||||||
Retryable: true,
|
Retryable: true,
|
||||||
@@ -359,19 +260,6 @@ func GetWorkflowSpec() *Spec {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
Name: "stream",
|
|
||||||
Methods: []Method{
|
|
||||||
{
|
|
||||||
Verb: "GET",
|
|
||||||
UpstreamPath: "/workflow/stream",
|
|
||||||
Auth: &Auth{
|
|
||||||
Required: true,
|
|
||||||
Capability: "workflow:read",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
package serviceadapter_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"forgejo.riotpiao.com/rock/homelab-frontend/internal/serviceadapter"
|
|
||||||
"forgejo.riotpiao.com/rock/homelab-frontend/internal/temporal"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestWorkflowListRequiresNamespace(t *testing.T) {
|
|
||||||
th := temporal.NewHandler("localhost:7233")
|
|
||||||
wfAdapter := serviceadapter.NewWorkflowAdapter(th)
|
|
||||||
wfSpec := serviceadapter.GetWorkflowSpec()
|
|
||||||
adapter := &serviceadapter.ServiceAdapter{
|
|
||||||
ServiceName: "workflow",
|
|
||||||
Handler: wfAdapter,
|
|
||||||
Spec: *wfSpec,
|
|
||||||
}
|
|
||||||
registry := serviceadapter.NewRegistry(nil)
|
|
||||||
registry.Add(adapter)
|
|
||||||
dispatcher := serviceadapter.NewDispatcher(registry, nil)
|
|
||||||
|
|
||||||
// POST X-Service: workflow X-Resource: list body: {} (no namespace)
|
|
||||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{}`))
|
|
||||||
req.Header.Set("X-Service", "workflow")
|
|
||||||
req.Header.Set("X-Resource", "list")
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
dispatcher.Dispatch(w, req)
|
|
||||||
|
|
||||||
t.Logf("Status: %d Body: %s", w.Code, w.Body.String())
|
|
||||||
if w.Code != http.StatusBadRequest {
|
|
||||||
t.Errorf("expected 400, got %d", w.Code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+404
-498
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,95 @@
|
|||||||
|
// Package temporal provides operation wrappers for Temporal operations
|
||||||
|
package temporal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// OperationHandler handles Temporal operations
|
||||||
|
type OperationHandler struct {
|
||||||
|
grpc *GRPCClient
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewOperationHandler creates a new operation handler
|
||||||
|
func NewOperationHandler(grpcClient *GRPCClient) *OperationHandler {
|
||||||
|
return &OperationHandler{
|
||||||
|
grpc: grpcClient,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartWorkflowExecution starts a new workflow execution
|
||||||
|
func (oh *OperationHandler) StartWorkflowExecution(ctx context.Context, namespace, workflowID, workflowType, taskQueue string, input map[string]interface{}) (map[string]interface{}, error) {
|
||||||
|
// TODO: Implement gRPC call to Temporal
|
||||||
|
// This is a placeholder for actual implementation
|
||||||
|
if oh.grpc == nil {
|
||||||
|
return nil, fmt.Errorf("gRPC client not initialized")
|
||||||
|
}
|
||||||
|
|
||||||
|
return map[string]interface{}{
|
||||||
|
"workflow_id": workflowID,
|
||||||
|
"run_id": fmt.Sprintf("run_%d", time.Now().UnixNano()),
|
||||||
|
"start_time": time.Now(),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DescribeWorkflowExecution gets workflow details
|
||||||
|
func (oh *OperationHandler) DescribeWorkflowExecution(ctx context.Context, namespace, workflowID, runID string) (map[string]interface{}, error) {
|
||||||
|
// TODO: Implement gRPC call to Temporal
|
||||||
|
return map[string]interface{}{
|
||||||
|
"workflow_id": workflowID,
|
||||||
|
"run_id": runID,
|
||||||
|
"status": "RUNNING",
|
||||||
|
"start_time": time.Now(),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TerminateWorkflowExecution terminates a workflow
|
||||||
|
func (oh *OperationHandler) TerminateWorkflowExecution(ctx context.Context, namespace, workflowID, runID, reason string) (map[string]interface{}, error) {
|
||||||
|
// TODO: Implement gRPC call to Temporal
|
||||||
|
return map[string]interface{}{
|
||||||
|
"workflow_id": workflowID,
|
||||||
|
"run_id": runID,
|
||||||
|
"terminated_at": time.Now(),
|
||||||
|
"reason": reason,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CancelWorkflowExecution cancels a workflow
|
||||||
|
func (oh *OperationHandler) CancelWorkflowExecution(ctx context.Context, namespace, workflowID, runID string) (map[string]interface{}, error) {
|
||||||
|
// TODO: Implement gRPC call to Temporal
|
||||||
|
return map[string]interface{}{
|
||||||
|
"workflow_id": workflowID,
|
||||||
|
"status": "canceling",
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SignalWorkflowExecution sends a signal to a workflow
|
||||||
|
func (oh *OperationHandler) SignalWorkflowExecution(ctx context.Context, namespace, workflowID, runID, signalName string, input map[string]interface{}) (map[string]interface{}, error) {
|
||||||
|
// TODO: Implement gRPC call to Temporal
|
||||||
|
return map[string]interface{}{
|
||||||
|
"workflow_id": workflowID,
|
||||||
|
"signal_name": signalName,
|
||||||
|
"signaled_at": time.Now(),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryWorkflowExecution queries a workflow
|
||||||
|
func (oh *OperationHandler) QueryWorkflowExecution(ctx context.Context, namespace, workflowID, runID, queryType string) (map[string]interface{}, error) {
|
||||||
|
// TODO: Implement gRPC call to Temporal
|
||||||
|
return map[string]interface{}{
|
||||||
|
"workflow_id": workflowID,
|
||||||
|
"query_type": queryType,
|
||||||
|
"query_result": map[string]interface{}{},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListWorkflowExecutions lists workflows
|
||||||
|
func (oh *OperationHandler) ListWorkflowExecutions(ctx context.Context, namespace string, pageSize int32) (map[string]interface{}, error) {
|
||||||
|
// TODO: Implement gRPC call to Temporal
|
||||||
|
return map[string]interface{}{
|
||||||
|
"executions": []interface{}{},
|
||||||
|
"next_page_token": "",
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
@@ -1,351 +0,0 @@
|
|||||||
package temporal
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// PoimenHandler provides typed REST endpoints for Poimen workflows.
|
|
||||||
// Accessed via X-Service: poimen header through the ServiceAdapter dispatcher.
|
|
||||||
type PoimenHandler struct {
|
|
||||||
handler *Handler
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewPoimenHandler creates a new Poimen handler wrapping the Temporal handler.
|
|
||||||
func NewPoimenHandler(h *Handler) *PoimenHandler {
|
|
||||||
return &PoimenHandler{handler: h}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ServeHTTP routes based on X-Upstream-Path set by the dispatcher.
|
|
||||||
func (p *PoimenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
path := r.Header.Get("X-Upstream-Path")
|
|
||||||
if path == "" {
|
|
||||||
path = r.URL.Path
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case path == "/v1/poimen/plan" && r.Method == http.MethodPost:
|
|
||||||
p.handlePlan(w, r)
|
|
||||||
case path == "/v1/poimen/execute" && r.Method == http.MethodPost:
|
|
||||||
p.handleExecute(w, r)
|
|
||||||
case path == "/v1/poimen/executions" && r.Method == http.MethodGet:
|
|
||||||
p.handleListExecutions(w, r)
|
|
||||||
case strings.HasPrefix(path, "/v1/poimen/executions/") && strings.HasSuffix(path, "/stream") && r.Method == http.MethodGet:
|
|
||||||
p.handleStream(w, r)
|
|
||||||
case strings.HasPrefix(path, "/v1/poimen/executions/") && strings.HasSuffix(path, "/signal") && r.Method == http.MethodPost:
|
|
||||||
p.handleSignal(w, r)
|
|
||||||
case strings.HasPrefix(path, "/v1/poimen/executions/") && strings.HasSuffix(path, "/history") && r.Method == http.MethodGet:
|
|
||||||
p.handleHistory(w, r)
|
|
||||||
case strings.HasPrefix(path, "/v1/poimen/executions/") && r.Method == http.MethodDelete:
|
|
||||||
p.handleTerminate(w, r)
|
|
||||||
case strings.HasPrefix(path, "/v1/poimen/executions/") && r.Method == http.MethodGet:
|
|
||||||
p.handleDescribe(w, r)
|
|
||||||
default:
|
|
||||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "endpoint not found"})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// extractWorkflowID extracts the workflow ID from paths like /v1/poimen/executions/{id}/...
|
|
||||||
func extractWorkflowID(path string) string {
|
|
||||||
parts := strings.Split(strings.TrimPrefix(path, "/v1/poimen/executions/"), "/")
|
|
||||||
if len(parts) > 0 && parts[0] != "" {
|
|
||||||
return parts[0]
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// PoimenWorkflowTypes maps Poimen workflow names to Temporal workflow types and task queues.
|
|
||||||
var PoimenWorkflowTypes = map[string]struct {
|
|
||||||
WorkflowType string
|
|
||||||
TaskQueue string
|
|
||||||
}{
|
|
||||||
"dag": {WorkflowType: "DAGWorkflow", TaskQueue: "poimen-worker"},
|
|
||||||
"orchestrator": {WorkflowType: "OrchestratorWorkflow", TaskQueue: "poimen-worker"},
|
|
||||||
"routing": {WorkflowType: "RoutingWorkflow", TaskQueue: "poimen-worker"},
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Plan ---
|
|
||||||
|
|
||||||
type PlanRequest struct {
|
|
||||||
Message string `json:"message"`
|
|
||||||
Context map[string]interface{} `json:"context,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PoimenHandler) handlePlan(w http.ResponseWriter, r *http.Request) {
|
|
||||||
var req PlanRequest
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
||||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if req.Message == "" {
|
|
||||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "message is required"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Call PlannerActivity directly (not via Temporal) for low latency.
|
|
||||||
// For now, return a placeholder indicating the plan endpoint is wired.
|
|
||||||
writeJSON(w, http.StatusNotImplemented, map[string]interface{}{
|
|
||||||
"error": "PlannerActivity not yet available",
|
|
||||||
"message": req.Message,
|
|
||||||
"context": req.Context,
|
|
||||||
"hint": "Depends on poimen-workflows#23 (PlannerActivity)",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Execute ---
|
|
||||||
|
|
||||||
type ExecuteRequest struct {
|
|
||||||
WorkflowType string `json:"workflow_type"` // "dag", "orchestrator", "routing"
|
|
||||||
Namespace string `json:"namespace,omitempty"`
|
|
||||||
Input map[string]interface{} `json:"input"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PoimenHandler) handleExecute(w http.ResponseWriter, r *http.Request) {
|
|
||||||
var req ExecuteRequest
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
||||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.Namespace == "" {
|
|
||||||
req.Namespace = "default"
|
|
||||||
}
|
|
||||||
|
|
||||||
wfType, ok := PoimenWorkflowTypes[req.WorkflowType]
|
|
||||||
if !ok {
|
|
||||||
writeJSON(w, http.StatusBadRequest, map[string]interface{}{
|
|
||||||
"error": fmt.Sprintf("unknown workflow_type: %s", req.WorkflowType),
|
|
||||||
"valid_types": []string{"dag", "orchestrator", "routing"},
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if p.handler.ops == nil {
|
|
||||||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "Temporal not available"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
workflowID := fmt.Sprintf("poimen-%s-%d", req.WorkflowType, time.Now().UnixMilli())
|
|
||||||
|
|
||||||
result, err := p.handler.ops.StartWorkflowExecution(
|
|
||||||
r.Context(),
|
|
||||||
req.Namespace,
|
|
||||||
workflowID,
|
|
||||||
wfType.WorkflowType,
|
|
||||||
wfType.TaskQueue,
|
|
||||||
req.Input,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": fmt.Sprintf("failed to start workflow: %v", err)})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
writeJSON(w, http.StatusOK, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- List Executions ---
|
|
||||||
|
|
||||||
func (p *PoimenHandler) handleListExecutions(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if p.handler.ops == nil {
|
|
||||||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "Temporal not available"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace := r.URL.Query().Get("namespace")
|
|
||||||
if namespace == "" {
|
|
||||||
namespace = "default"
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := p.handler.ops.ListWorkflowExecutions(r.Context(), namespace, 100)
|
|
||||||
if err != nil {
|
|
||||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": fmt.Sprintf("failed to list: %v", err)})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
writeJSON(w, http.StatusOK, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Describe ---
|
|
||||||
|
|
||||||
func (p *PoimenHandler) handleDescribe(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if p.handler.ops == nil {
|
|
||||||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "Temporal not available"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
path := r.Header.Get("X-Upstream-Path")
|
|
||||||
if path == "" {
|
|
||||||
path = r.URL.Path
|
|
||||||
}
|
|
||||||
workflowID := extractWorkflowID(path)
|
|
||||||
if workflowID == "" {
|
|
||||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "workflow_id required in path"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace := r.URL.Query().Get("namespace")
|
|
||||||
if namespace == "" {
|
|
||||||
namespace = "default"
|
|
||||||
}
|
|
||||||
runID := r.URL.Query().Get("run_id")
|
|
||||||
|
|
||||||
result, err := p.handler.ops.DescribeWorkflowExecution(r.Context(), namespace, workflowID, runID)
|
|
||||||
if err != nil {
|
|
||||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": fmt.Sprintf("workflow not found: %v", err)})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
writeJSON(w, http.StatusOK, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- History ---
|
|
||||||
|
|
||||||
func (p *PoimenHandler) handleHistory(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if p.handler.ops == nil {
|
|
||||||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "Temporal not available"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
path := r.Header.Get("X-Upstream-Path")
|
|
||||||
if path == "" {
|
|
||||||
path = r.URL.Path
|
|
||||||
}
|
|
||||||
workflowID := extractWorkflowID(path)
|
|
||||||
if workflowID == "" {
|
|
||||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "workflow_id required in path"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace := r.URL.Query().Get("namespace")
|
|
||||||
if namespace == "" {
|
|
||||||
namespace = "default"
|
|
||||||
}
|
|
||||||
runID := r.URL.Query().Get("run_id")
|
|
||||||
|
|
||||||
result, err := p.handler.ops.GetWorkflowExecutionHistory(r.Context(), namespace, workflowID, runID)
|
|
||||||
if err != nil {
|
|
||||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": fmt.Sprintf("failed to get history: %v", err)})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
writeJSON(w, http.StatusOK, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Signal ---
|
|
||||||
|
|
||||||
type SignalRequest struct {
|
|
||||||
SignalName string `json:"signal_name"`
|
|
||||||
Data map[string]interface{} `json:"data,omitempty"`
|
|
||||||
Namespace string `json:"namespace,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PoimenHandler) handleSignal(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if p.handler.ops == nil {
|
|
||||||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "Temporal not available"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
path := r.Header.Get("X-Upstream-Path")
|
|
||||||
if path == "" {
|
|
||||||
path = r.URL.Path
|
|
||||||
}
|
|
||||||
// Remove /signal suffix to get workflow ID
|
|
||||||
cleanPath := strings.TrimSuffix(path, "/signal")
|
|
||||||
workflowID := extractWorkflowID(cleanPath)
|
|
||||||
if workflowID == "" {
|
|
||||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "workflow_id required in path"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var req SignalRequest
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
||||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if req.SignalName == "" {
|
|
||||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "signal_name is required"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if req.Namespace == "" {
|
|
||||||
req.Namespace = "default"
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := p.handler.ops.SignalWorkflowExecution(r.Context(), req.Namespace, workflowID, "", req.SignalName, req.Data)
|
|
||||||
if err != nil {
|
|
||||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": fmt.Sprintf("failed to signal: %v", err)})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
writeJSON(w, http.StatusOK, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Terminate ---
|
|
||||||
|
|
||||||
func (p *PoimenHandler) handleTerminate(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if p.handler.ops == nil {
|
|
||||||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "Temporal not available"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
path := r.Header.Get("X-Upstream-Path")
|
|
||||||
if path == "" {
|
|
||||||
path = r.URL.Path
|
|
||||||
}
|
|
||||||
workflowID := extractWorkflowID(path)
|
|
||||||
if workflowID == "" {
|
|
||||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "workflow_id required in path"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace := r.URL.Query().Get("namespace")
|
|
||||||
if namespace == "" {
|
|
||||||
namespace = "default"
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := p.handler.ops.TerminateWorkflowExecution(r.Context(), namespace, workflowID, "", "terminated via API")
|
|
||||||
if err != nil {
|
|
||||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": fmt.Sprintf("failed to terminate: %v", err)})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
writeJSON(w, http.StatusOK, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Stream (delegate to existing SSE handler) ---
|
|
||||||
|
|
||||||
func (p *PoimenHandler) handleStream(w http.ResponseWriter, r *http.Request) {
|
|
||||||
path := r.Header.Get("X-Upstream-Path")
|
|
||||||
if path == "" {
|
|
||||||
path = r.URL.Path
|
|
||||||
}
|
|
||||||
// Remove /stream suffix
|
|
||||||
cleanPath := strings.TrimSuffix(path, "/stream")
|
|
||||||
workflowID := extractWorkflowID(cleanPath)
|
|
||||||
if workflowID == "" {
|
|
||||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "workflow_id required in path"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set query params for the SSE handler
|
|
||||||
q := r.URL.Query()
|
|
||||||
q.Set("workflow_id", workflowID)
|
|
||||||
if q.Get("namespace") == "" {
|
|
||||||
q.Set("namespace", "default")
|
|
||||||
}
|
|
||||||
r.URL.RawQuery = q.Encode()
|
|
||||||
|
|
||||||
p.handler.StreamWorkflowEvents(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Helpers ---
|
|
||||||
|
|
||||||
func writeJSON(w http.ResponseWriter, status int, data interface{}) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(status)
|
|
||||||
json.NewEncoder(w).Encode(data) //nolint:errcheck
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,224 +0,0 @@
|
|||||||
package temporal
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// SSEEvent represents a Server-Sent Event
|
|
||||||
type SSEEvent struct {
|
|
||||||
Event string `json:"event"`
|
|
||||||
Data interface{} `json:"data"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// StreamWorkflowEvents streams workflow execution history as SSE events.
|
|
||||||
// Polls Temporal GetWorkflowExecutionHistory every 2s and sends new events.
|
|
||||||
// Sends heartbeat every 15s. Closes on workflow completion or client disconnect.
|
|
||||||
func (h *Handler) StreamWorkflowEvents(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// Only GET allowed
|
|
||||||
if r.Method != http.MethodGet {
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract workflow_id from query params
|
|
||||||
workflowID := r.URL.Query().Get("workflow_id")
|
|
||||||
if workflowID == "" {
|
|
||||||
http.Error(w, `{"error":"workflow_id query parameter required"}`, http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
runID := r.URL.Query().Get("run_id")
|
|
||||||
namespace := r.URL.Query().Get("namespace")
|
|
||||||
if namespace == "" {
|
|
||||||
namespace = "default"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check gRPC client
|
|
||||||
if h.grpcClient == nil {
|
|
||||||
http.Error(w, `{"error":"Temporal server not available"}`, http.StatusServiceUnavailable)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set SSE headers
|
|
||||||
flusher, ok := w.(http.Flusher)
|
|
||||||
if !ok {
|
|
||||||
http.Error(w, "Streaming not supported", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/event-stream")
|
|
||||||
w.Header().Set("Cache-Control", "no-cache")
|
|
||||||
w.Header().Set("Connection", "keep-alive")
|
|
||||||
w.Header().Set("X-Accel-Buffering", "no") // disable nginx buffering
|
|
||||||
|
|
||||||
// Use request context for cancellation (client disconnect)
|
|
||||||
ctx := r.Context()
|
|
||||||
|
|
||||||
var lastEventID int64
|
|
||||||
pollTicker := time.NewTicker(2 * time.Second)
|
|
||||||
heartbeatTicker := time.NewTicker(15 * time.Second)
|
|
||||||
defer pollTicker.Stop()
|
|
||||||
defer heartbeatTicker.Stop()
|
|
||||||
|
|
||||||
// Send initial connected event
|
|
||||||
writeSSE(w, flusher, "connected", map[string]interface{}{
|
|
||||||
"workflow_id": workflowID,
|
|
||||||
"run_id": runID,
|
|
||||||
"namespace": namespace,
|
|
||||||
"timestamp": time.Now().Format(time.RFC3339),
|
|
||||||
})
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
// Client disconnected
|
|
||||||
return
|
|
||||||
|
|
||||||
case <-heartbeatTicker.C:
|
|
||||||
writeSSE(w, flusher, "heartbeat", map[string]string{
|
|
||||||
"timestamp": time.Now().Format(time.RFC3339),
|
|
||||||
})
|
|
||||||
|
|
||||||
case <-pollTicker.C:
|
|
||||||
events, workflowDone, err := h.pollNewEvents(ctx, namespace, workflowID, runID, lastEventID)
|
|
||||||
if err != nil {
|
|
||||||
writeSSE(w, flusher, "error", map[string]string{
|
|
||||||
"error": err.Error(),
|
|
||||||
"timestamp": time.Now().Format(time.RFC3339),
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, evt := range events {
|
|
||||||
writeSSE(w, flusher, evt.Event, evt.Data)
|
|
||||||
if evtData, ok := evt.Data.(map[string]interface{}); ok {
|
|
||||||
if id, ok := evtData["event_id"]; ok {
|
|
||||||
if idInt, ok := id.(int64); ok {
|
|
||||||
if idInt > lastEventID {
|
|
||||||
lastEventID = idInt
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if workflowDone {
|
|
||||||
writeSSE(w, flusher, "workflow_completed", map[string]string{
|
|
||||||
"workflow_id": workflowID,
|
|
||||||
"timestamp": time.Now().Format(time.RFC3339),
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// pollNewEvents fetches workflow history from Temporal and returns new events since lastEventID.
|
|
||||||
// Returns (events, workflowDone, error).
|
|
||||||
func (h *Handler) pollNewEvents(ctx context.Context, namespace, workflowID, runID string, lastEventID int64) ([]SSEEvent, bool, error) {
|
|
||||||
pollCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
grpcImpl := NewWorkflowGRPCImpl(h.grpcClient)
|
|
||||||
result, err := grpcImpl.GetWorkflowExecutionHistory(pollCtx, namespace, workflowID, runID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, false, fmt.Errorf("failed to get history: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
eventsRaw, ok := result["events"]
|
|
||||||
if !ok {
|
|
||||||
return nil, false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
events, ok := eventsRaw.([]map[string]interface{})
|
|
||||||
if !ok {
|
|
||||||
return nil, false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var sseEvents []SSEEvent
|
|
||||||
workflowDone := false
|
|
||||||
|
|
||||||
for _, evt := range events {
|
|
||||||
eventID, _ := evt["event_id"].(int64)
|
|
||||||
if eventID <= lastEventID {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// operations_grpc.go stores event type under "type" key
|
|
||||||
eventType, _ := evt["type"].(string)
|
|
||||||
sseEvent := mapTemporalEvent(eventType, evt)
|
|
||||||
if sseEvent != nil {
|
|
||||||
sseEvents = append(sseEvents, *sseEvent)
|
|
||||||
}
|
|
||||||
|
|
||||||
if isTerminalEvent(eventType) {
|
|
||||||
workflowDone = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sseEvents, workflowDone, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// mapTemporalEvent converts a Temporal history event to an SSE event.
|
|
||||||
func mapTemporalEvent(eventType string, evt map[string]interface{}) *SSEEvent {
|
|
||||||
data := map[string]interface{}{
|
|
||||||
"event_id": evt["event_id"],
|
|
||||||
"event_type": eventType,
|
|
||||||
"timestamp": evt["timestamp"],
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case strings.Contains(eventType, "EVENT_TYPE_ACTIVITY_TASK_SCHEDULED"):
|
|
||||||
return &SSEEvent{Event: "activity_scheduled", Data: data}
|
|
||||||
case strings.Contains(eventType, "EVENT_TYPE_ACTIVITY_TASK_STARTED"):
|
|
||||||
return &SSEEvent{Event: "activity_started", Data: data}
|
|
||||||
case strings.Contains(eventType, "EVENT_TYPE_ACTIVITY_TASK_COMPLETED"):
|
|
||||||
return &SSEEvent{Event: "activity_completed", Data: data}
|
|
||||||
case strings.Contains(eventType, "EVENT_TYPE_ACTIVITY_TASK_FAILED"):
|
|
||||||
return &SSEEvent{Event: "activity_failed", Data: data}
|
|
||||||
case strings.Contains(eventType, "EVENT_TYPE_ACTIVITY_TASK_TIMED_OUT"):
|
|
||||||
return &SSEEvent{Event: "activity_timed_out", Data: data}
|
|
||||||
case strings.Contains(eventType, "EVENT_TYPE_WORKFLOW_EXECUTION_STARTED"):
|
|
||||||
return &SSEEvent{Event: "workflow_started", Data: data}
|
|
||||||
case strings.Contains(eventType, "EVENT_TYPE_WORKFLOW_EXECUTION_COMPLETED"):
|
|
||||||
return &SSEEvent{Event: "workflow_completed", Data: data}
|
|
||||||
case strings.Contains(eventType, "EVENT_TYPE_WORKFLOW_EXECUTION_FAILED"):
|
|
||||||
return &SSEEvent{Event: "workflow_failed", Data: data}
|
|
||||||
case strings.Contains(eventType, "EVENT_TYPE_WORKFLOW_EXECUTION_TIMED_OUT"):
|
|
||||||
return &SSEEvent{Event: "workflow_timed_out", Data: data}
|
|
||||||
case strings.Contains(eventType, "EVENT_TYPE_WORKFLOW_EXECUTION_CANCELED"):
|
|
||||||
return &SSEEvent{Event: "workflow_canceled", Data: data}
|
|
||||||
case strings.Contains(eventType, "EVENT_TYPE_WORKFLOW_EXECUTION_TERMINATED"):
|
|
||||||
return &SSEEvent{Event: "workflow_terminated", Data: data}
|
|
||||||
case strings.Contains(eventType, "EVENT_TYPE_CHILD_WORKFLOW"):
|
|
||||||
return &SSEEvent{Event: "child_workflow", Data: data}
|
|
||||||
case strings.Contains(eventType, "EVENT_TYPE_WORKFLOW_EXECUTION_SIGNALED"):
|
|
||||||
return &SSEEvent{Event: "workflow_signaled", Data: data}
|
|
||||||
default:
|
|
||||||
// Skip internal/uninteresting events (timer, decision task, etc.)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// isTerminalEvent returns true if the event type indicates workflow completion.
|
|
||||||
func isTerminalEvent(eventType string) bool {
|
|
||||||
return strings.Contains(eventType, "COMPLETED") ||
|
|
||||||
strings.Contains(eventType, "FAILED") ||
|
|
||||||
strings.Contains(eventType, "TIMED_OUT") ||
|
|
||||||
strings.Contains(eventType, "CANCELED") ||
|
|
||||||
strings.Contains(eventType, "TERMINATED")
|
|
||||||
}
|
|
||||||
|
|
||||||
// writeSSE writes a single SSE event to the response writer.
|
|
||||||
func writeSSE(w http.ResponseWriter, flusher http.Flusher, event string, data interface{}) {
|
|
||||||
jsonData, err := json.Marshal(data)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Fprintf(w, "event: %s\ndata: %s\n\n", event, string(jsonData))
|
|
||||||
flusher.Flush()
|
|
||||||
}
|
|
||||||
@@ -1,218 +0,0 @@
|
|||||||
package webhook
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/hmac"
|
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"forgejo.riotpiao.com/rock/homelab-frontend/internal/notification"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ForgejoHandler receives Forgejo webhook payloads and forwards them to Gotify.
|
|
||||||
// Forgejo sends a Gitea-compatible JSON payload with X-Gitea-Signature-256 header.
|
|
||||||
type ForgejoHandler struct {
|
|
||||||
secret string
|
|
||||||
gotify *notification.GotifyClient
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewForgejoHandler creates a handler from environment variables.
|
|
||||||
// Required: GOTIFY_URL, GOTIFY_APP_TOKEN
|
|
||||||
// Optional: FORGEJO_WEBHOOK_SECRET (if empty, HMAC verification is skipped)
|
|
||||||
func NewForgejoHandler() *ForgejoHandler {
|
|
||||||
gotifyURL := os.Getenv("GOTIFY_URL")
|
|
||||||
appToken := os.Getenv("GOTIFY_APP_TOKEN")
|
|
||||||
|
|
||||||
var client *notification.GotifyClient
|
|
||||||
if gotifyURL != "" && appToken != "" {
|
|
||||||
client = notification.NewGotifyClient(gotifyURL, appToken, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
return &ForgejoHandler{
|
|
||||||
secret: os.Getenv("FORGEJO_WEBHOOK_SECRET"),
|
|
||||||
gotify: client,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ServeHTTP handles POST /v1/webhooks/forgejo
|
|
||||||
func (h *ForgejoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
body, err := io.ReadAll(io.LimitReader(r.Body, 1<<20)) // 1MB limit
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "failed to read body", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify HMAC if secret is set
|
|
||||||
if h.secret != "" {
|
|
||||||
sig := r.Header.Get("X-Gitea-Signature-256")
|
|
||||||
if sig == "" {
|
|
||||||
sig = r.Header.Get("X-Hub-Signature-256")
|
|
||||||
}
|
|
||||||
if !h.verifySignature(body, sig) {
|
|
||||||
http.Error(w, "invalid signature", http.StatusForbidden)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if h.gotify == nil {
|
|
||||||
log.Printf("forgejo webhook received but Gotify not configured (GOTIFY_URL/GOTIFY_APP_TOKEN missing)")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
event := r.Header.Get("X-Gitea-Event")
|
|
||||||
if event == "" {
|
|
||||||
event = r.Header.Get("X-GitHub-Event")
|
|
||||||
}
|
|
||||||
|
|
||||||
title, message, priority := h.formatMessage(event, body)
|
|
||||||
if title == "" {
|
|
||||||
// Unhandled event type — ack and ignore
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := notification.GotifyMessage{
|
|
||||||
Title: title,
|
|
||||||
Message: message,
|
|
||||||
Priority: priority,
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := h.gotify.SendMessage(msg); err != nil {
|
|
||||||
log.Printf("forgejo webhook: failed to send gotify message: %v", err)
|
|
||||||
http.Error(w, "failed to send notification", http.StatusBadGateway)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("forgejo webhook: sent gotify notification event=%s title=%q", event, title)
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
// verifySignature checks X-Gitea-Signature-256: sha256=<hex>
|
|
||||||
func (h *ForgejoHandler) verifySignature(body []byte, sig string) bool {
|
|
||||||
sig = strings.TrimPrefix(sig, "sha256=")
|
|
||||||
if sig == "" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
mac := hmac.New(sha256.New, []byte(h.secret))
|
|
||||||
mac.Write(body)
|
|
||||||
expected := hex.EncodeToString(mac.Sum(nil))
|
|
||||||
return hmac.Equal([]byte(sig), []byte(expected))
|
|
||||||
}
|
|
||||||
|
|
||||||
// formatMessage converts a Forgejo event payload into a Gotify title + message.
|
|
||||||
// Returns empty title if the event should be ignored.
|
|
||||||
func (h *ForgejoHandler) formatMessage(event string, body []byte) (title, message string, priority int) {
|
|
||||||
var payload map[string]interface{}
|
|
||||||
if err := json.Unmarshal(body, &payload); err != nil {
|
|
||||||
return "", "", 0
|
|
||||||
}
|
|
||||||
|
|
||||||
repo := jsonStr(payload, "repository", "full_name")
|
|
||||||
sender := jsonStr(payload, "sender", "login")
|
|
||||||
|
|
||||||
switch event {
|
|
||||||
case "push":
|
|
||||||
ref := strings.TrimPrefix(fmt.Sprintf("%v", payload["ref"]), "refs/heads/")
|
|
||||||
commits, _ := payload["commits"].([]interface{})
|
|
||||||
count := len(commits)
|
|
||||||
commitMsg := ""
|
|
||||||
if count > 0 {
|
|
||||||
if c, ok := commits[0].(map[string]interface{}); ok {
|
|
||||||
commitMsg = fmt.Sprintf("%v", c["message"])
|
|
||||||
// truncate long commit messages
|
|
||||||
if len(commitMsg) > 80 {
|
|
||||||
commitMsg = commitMsg[:80] + "…"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("📦 %s", repo),
|
|
||||||
fmt.Sprintf("%s pushed %d commit(s) to %s\n%s", sender, count, ref, commitMsg),
|
|
||||||
5
|
|
||||||
|
|
||||||
case "pull_request":
|
|
||||||
action := fmt.Sprintf("%v", payload["action"])
|
|
||||||
if action != "opened" && action != "closed" && action != "reopened" && action != "merged" {
|
|
||||||
return "", "", 0 // ignore noise (labeled, assigned, etc.)
|
|
||||||
}
|
|
||||||
pr, _ := payload["pull_request"].(map[string]interface{})
|
|
||||||
number := fmt.Sprintf("%v", pr["number"])
|
|
||||||
prTitle := fmt.Sprintf("%v", pr["title"])
|
|
||||||
merged, _ := pr["merged"].(bool)
|
|
||||||
if action == "closed" && merged {
|
|
||||||
action = "merged"
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("🔀 PR #%s %s — %s", number, action, repo),
|
|
||||||
fmt.Sprintf("%s: %s\nby %s", action, prTitle, sender),
|
|
||||||
5
|
|
||||||
|
|
||||||
case "issues":
|
|
||||||
action := fmt.Sprintf("%v", payload["action"])
|
|
||||||
if action != "opened" && action != "closed" && action != "reopened" {
|
|
||||||
return "", "", 0
|
|
||||||
}
|
|
||||||
issue, _ := payload["issue"].(map[string]interface{})
|
|
||||||
number := fmt.Sprintf("%v", issue["number"])
|
|
||||||
issueTitle := fmt.Sprintf("%v", issue["title"])
|
|
||||||
return fmt.Sprintf("🐛 Issue #%s %s — %s", number, action, repo),
|
|
||||||
fmt.Sprintf("%s: %s\nby %s", action, issueTitle, sender),
|
|
||||||
4
|
|
||||||
|
|
||||||
case "issue_comment", "pull_request_review_comment":
|
|
||||||
issue, _ := payload["issue"].(map[string]interface{})
|
|
||||||
comment, _ := payload["comment"].(map[string]interface{})
|
|
||||||
number := fmt.Sprintf("%v", issue["number"])
|
|
||||||
body := fmt.Sprintf("%v", comment["body"])
|
|
||||||
if len(body) > 100 {
|
|
||||||
body = body[:100] + "…"
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("💬 Comment on #%s — %s", number, repo),
|
|
||||||
fmt.Sprintf("%s: %s", sender, body),
|
|
||||||
3
|
|
||||||
|
|
||||||
case "release":
|
|
||||||
action := fmt.Sprintf("%v", payload["action"])
|
|
||||||
if action != "published" {
|
|
||||||
return "", "", 0
|
|
||||||
}
|
|
||||||
release, _ := payload["release"].(map[string]interface{})
|
|
||||||
tag := fmt.Sprintf("%v", release["tag_name"])
|
|
||||||
name := fmt.Sprintf("%v", release["name"])
|
|
||||||
return fmt.Sprintf("🚀 Release %s — %s", tag, repo),
|
|
||||||
fmt.Sprintf("%s published by %s", name, sender),
|
|
||||||
7
|
|
||||||
|
|
||||||
default:
|
|
||||||
return "", "", 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// jsonStr safely traverses nested map keys.
|
|
||||||
func jsonStr(m map[string]interface{}, keys ...string) string {
|
|
||||||
cur := m
|
|
||||||
for i, k := range keys {
|
|
||||||
v, ok := cur[k]
|
|
||||||
if !ok {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
if i == len(keys)-1 {
|
|
||||||
return fmt.Sprintf("%v", v)
|
|
||||||
}
|
|
||||||
cur, ok = v.(map[string]interface{})
|
|
||||||
if !ok {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
@@ -76,25 +76,6 @@ spec:
|
|||||||
value: "1.0.0"
|
value: "1.0.0"
|
||||||
- name: OTEL_ENVIRONMENT
|
- name: OTEL_ENVIRONMENT
|
||||||
value: "production"
|
value: "production"
|
||||||
# Gotify integration — Forgejo webhook → push notifications
|
|
||||||
- name: GOTIFY_URL
|
|
||||||
valueFrom:
|
|
||||||
secretKeyRef:
|
|
||||||
name: gotify-webhook-secret
|
|
||||||
key: gotify-url
|
|
||||||
optional: true
|
|
||||||
- name: GOTIFY_APP_TOKEN
|
|
||||||
valueFrom:
|
|
||||||
secretKeyRef:
|
|
||||||
name: gotify-webhook-secret
|
|
||||||
key: gotify-app-token
|
|
||||||
optional: true
|
|
||||||
- name: FORGEJO_WEBHOOK_SECRET
|
|
||||||
valueFrom:
|
|
||||||
secretKeyRef:
|
|
||||||
name: gotify-webhook-secret
|
|
||||||
key: forgejo-webhook-secret
|
|
||||||
optional: true
|
|
||||||
volumeMounts:
|
volumeMounts:
|
||||||
- name: config
|
- name: config
|
||||||
mountPath: /etc/gateway
|
mountPath: /etc/gateway
|
||||||
|
|||||||
@@ -2,12 +2,11 @@
|
|||||||
set -e
|
set -e
|
||||||
|
|
||||||
# Integration test runner for API gateway.
|
# Integration test runner for API gateway.
|
||||||
# Runs against a real canary pod in-cluster (has real secrets + upstreams).
|
# Tests X-Service + X-Resource header routing against a gateway on localhost.
|
||||||
# GW env var must be set: e.g. http://10.244.1.5:8080
|
|
||||||
#
|
#
|
||||||
# Required env:
|
# Required env:
|
||||||
# GW — gateway base URL
|
# GW — gateway base URL (e.g. http://localhost:8080)
|
||||||
# RESULTS_DIR — directory to write result/summary files
|
# RESULTS_DIR — directory to write Tekton results
|
||||||
|
|
||||||
PASS=0; FAIL=0; TOTAL=0
|
PASS=0; FAIL=0; TOTAL=0
|
||||||
|
|
||||||
@@ -26,23 +25,28 @@ assert() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# ── Wait for gateway ──────────────────────────────────────────────────────────
|
# ── Wait for sidecar gateway ──
|
||||||
echo "⏳ Waiting for gateway at ${GW}..."
|
echo "⏳ Waiting for gateway sidecar..."
|
||||||
READY=false
|
READY=false
|
||||||
for i in $(seq 1 30); do
|
for i in $(seq 1 60); do
|
||||||
CODE=$(curl -s -o /dev/null -w '%{http_code}' "${GW}/healthz" 2>/dev/null || echo "000")
|
CODE=$(curl -s -o /dev/null -w '%{http_code}' "${GW}/healthz" 2>/dev/null || echo "000")
|
||||||
if [ "$CODE" = "200" ]; then
|
if [ "$CODE" = "200" ]; then
|
||||||
|
sleep 1
|
||||||
|
C2=$(curl -s -o /dev/null -w '%{http_code}' "${GW}/healthz" 2>/dev/null || echo "000")
|
||||||
|
C3=$(curl -s -o /dev/null -w '%{http_code}' "${GW}/healthz" 2>/dev/null || echo "000")
|
||||||
|
if [ "$C2" = "200" ] && [ "$C3" = "200" ]; then
|
||||||
READY=true
|
READY=true
|
||||||
echo "✓ Gateway ready"
|
echo "✓ Gateway ready"
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
sleep 2
|
sleep 2
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ "$READY" = "false" ]; then
|
if [ "$READY" = "false" ]; then
|
||||||
echo "✗ Gateway never became ready"
|
echo "✗ Gateway never became ready"
|
||||||
echo "fail" > "${RESULTS_DIR}/result"
|
echo "fail" > "${RESULTS_DIR}/result"
|
||||||
echo "0/0 timeout" > "${RESULTS_DIR}/summary"
|
echo "0/0 gateway timeout" > "${RESULTS_DIR}/summary"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -50,69 +54,78 @@ echo ""
|
|||||||
echo "═══ Integration Tests ═══"
|
echo "═══ Integration Tests ═══"
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
# ── Health ────────────────────────────────────────────────────────────────────
|
# ── Health ──
|
||||||
echo "▸ Health"
|
echo "▸ Health"
|
||||||
assert "GET /healthz" 200 -X GET "${GW}/healthz"
|
assert "GET /healthz" 200 -X GET "${GW}/healthz"
|
||||||
assert "GET /readyz" 200 -X GET "${GW}/readyz"
|
assert "GET /readyz" 200 -X GET "${GW}/readyz"
|
||||||
|
|
||||||
# ── Header validation ─────────────────────────────────────────────────────────
|
# ── Header validation ──
|
||||||
echo "▸ Header validation"
|
echo "▸ Header validation"
|
||||||
assert "X-Service without X-Resource → 400" 400 \
|
assert "X-Service without X-Resource → 400" 400 \
|
||||||
-X GET -H "X-Service: memory" "${GW}/"
|
-X GET -H "X-Service: memory" "${GW}/"
|
||||||
assert "unknown service → 404" 404 \
|
assert "unknown service → 404" 404 \
|
||||||
-X GET -H "X-Service: nonexistent" -H "X-Resource: foo" "${GW}/"
|
-X GET -H "X-Service: nonexistent" -H "X-Resource: foo" "${GW}/"
|
||||||
|
|
||||||
# ── S3 (no auth, MinIO rejects → 403) ────────────────────────────────────────
|
# ── S3 (no auth, MinIO rejects → 403) ──
|
||||||
echo "▸ S3 service"
|
echo "▸ S3 service"
|
||||||
assert "s3/list-objects → 403" 403 \
|
assert "s3/list-objects" 403 \
|
||||||
-X GET -H "X-Service: s3" -H "X-Resource: list-objects" "${GW}/"
|
-X GET -H "X-Service: s3" -H "X-Resource: list-objects" "${GW}/"
|
||||||
|
|
||||||
# ── SQS (auth required → 401) ─────────────────────────────────────────────────
|
# ── SQS (auth required → 401) ──
|
||||||
echo "▸ SQS service"
|
echo "▸ SQS service"
|
||||||
assert "sqs/list-queues → 401" 401 \
|
assert "sqs/list-queues" 401 \
|
||||||
-X GET -H "X-Service: sqs" -H "X-Resource: list-queues" "${GW}/"
|
-X GET -H "X-Service: sqs" -H "X-Resource: list-queues" "${GW}/"
|
||||||
|
|
||||||
# ── Workflow ──────────────────────────────────────────────────────────────────
|
# ── Workflow visibility (namespace pass-down) ──
|
||||||
echo "▸ Workflow service"
|
echo "▸ Workflow service"
|
||||||
|
|
||||||
# List with namespace — real Temporal call.
|
# Test 1: List workflows in poimen-harness namespace (should see 4 terminated workflows)
|
||||||
# 200 = Temporal reachable, 503 = Temporal down but gateway routed correctly.
|
echo " Testing workflow visibility in poimen-harness namespace..."
|
||||||
echo " Testing workflow list (poimen-harness namespace)..."
|
WF_LIST=$(curl -s -X POST \
|
||||||
WF_LIST=$(curl -s \
|
|
||||||
-H "X-Service: workflow" \
|
-H "X-Service: workflow" \
|
||||||
-H "X-Resource: list" \
|
-H "X-Resource: list" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d '{"namespace": "poimen-harness"}' \
|
-d '{"namespace": "poimen-harness"}' \
|
||||||
"${GW}/" 2>/dev/null || echo '{}')
|
"${GW}/" 2>/dev/null || echo '{}')
|
||||||
|
|
||||||
TOTAL=$((TOTAL + 1))
|
# Check if response contains workflows
|
||||||
if echo "$WF_LIST" | grep -qE '"executions"|"TEMPORAL_UNAVAILABLE"'; then
|
if echo "$WF_LIST" | grep -q '"executions"'; then
|
||||||
echo " ✓ workflow/list responded correctly"
|
echo " ✓ Workflow list returned (poimen-harness namespace)"
|
||||||
PASS=$((PASS + 1))
|
PASS=$((PASS + 1))
|
||||||
else
|
else
|
||||||
echo " ✗ workflow/list unexpected: $WF_LIST"
|
echo " ✗ Workflow list failed to return executions"
|
||||||
FAIL=$((FAIL + 1))
|
FAIL=$((FAIL + 1))
|
||||||
fi
|
fi
|
||||||
|
TOTAL=$((TOTAL + 1))
|
||||||
|
|
||||||
# Namespace is required — canary pod has real WorkflowAdapter → must return 400
|
# Test 2: Verify we can query terminated workflows
|
||||||
assert "workflow/list without namespace → 400" 400 \
|
echo " Testing terminated workflow visibility..."
|
||||||
-X POST \
|
if echo "$WF_LIST" | grep -q '"Completed\|"status"'; then
|
||||||
|
echo " ✓ Found completed/terminated workflows in response"
|
||||||
|
PASS=$((PASS + 1))
|
||||||
|
else
|
||||||
|
echo " ⚠ No terminated workflows found in response (may be empty namespace)"
|
||||||
|
# Don't fail if namespace is empty - just note it
|
||||||
|
fi
|
||||||
|
TOTAL=$((TOTAL + 1))
|
||||||
|
|
||||||
|
# Test 3: Verify namespace is required (missing namespace → 400)
|
||||||
|
echo " Testing namespace validation..."
|
||||||
|
NO_NS=$(curl -s -w '%{http_code}' -X POST \
|
||||||
-H "X-Service: workflow" \
|
-H "X-Service: workflow" \
|
||||||
-H "X-Resource: list" \
|
-H "X-Resource: list" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d '{}' \
|
-d '{}' \
|
||||||
"${GW}/"
|
"${GW}/" 2>/dev/null || echo "000")
|
||||||
|
|
||||||
# ── Forgejo webhook ───────────────────────────────────────────────────────────
|
if [ "$NO_NS" = "400" ]; then
|
||||||
echo "▸ Forgejo webhook"
|
echo " ✓ Correctly rejected list without namespace (400)"
|
||||||
# Canary pod has real handler wired. No HMAC secret set (optional) → handler
|
PASS=$((PASS + 1))
|
||||||
# skips verification and forwards to Gotify (or logs if Gotify unavailable).
|
else
|
||||||
assert "POST /v1/webhooks/forgejo → 200" 200 \
|
echo " ✗ Expected 400 for missing namespace, got $NO_NS"
|
||||||
-X POST \
|
FAIL=$((FAIL + 1))
|
||||||
-H "Content-Type: application/json" \
|
fi
|
||||||
-H "X-Gitea-Event: push" \
|
TOTAL=$((TOTAL + 1))
|
||||||
-d '{"ref":"refs/heads/main","commits":[{"message":"test"}],"repository":{"full_name":"test/repo"},"sender":{"login":"ci"}}' \
|
|
||||||
"${GW}/v1/webhooks/forgejo"
|
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
echo "═══ Results: ${PASS}/${TOTAL} passed, ${FAIL} failed ═══"
|
echo "═══ Results: ${PASS}/${TOTAL} passed, ${FAIL} failed ═══"
|
||||||
|
|||||||
Reference in New Issue
Block a user