From 0571a07706922c2bfc05a9c9b1a0b97b4de7091f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Mon, 9 Mar 2026 17:46:52 +0400 Subject: [PATCH] =?UTF-8?q?chore:=20=D1=83=D0=B4=D0=B0=D0=BB=D1=91=D0=BD?= =?UTF-8?q?=20=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=20pg-query?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pg-query/code/handler.py | 39 --------------------------- pg-query/code/requirements.txt | 1 - pg-query/main.tf | 21 --------------- pg-query/pg-query.tf | 48 ---------------------------------- 4 files changed, 109 deletions(-) delete mode 100644 pg-query/code/handler.py delete mode 100644 pg-query/code/requirements.txt delete mode 100644 pg-query/main.tf delete mode 100644 pg-query/pg-query.tf diff --git a/pg-query/code/handler.py b/pg-query/code/handler.py deleted file mode 100644 index b0a5835..0000000 --- a/pg-query/code/handler.py +++ /dev/null @@ -1,39 +0,0 @@ -# 2026-03-07 -# handler.py — пример serverless функции, работающей с PostgreSQL. -# Подключается к postgres.sless.svc.cluster.local:5432 (внутри кластера). -# DSN берётся из env переменной PG_DSN (задаётся через env_vars ресурса sless_function). -# handle(event) — принимает JSON event, возвращает список записей из таблицы invocations. -import os -import json - -def handle(event): - # psycopg2 входит в python:3.11-slim через pip install ниже, - # либо нужно добавить в Dockerfile — для этого примера устанавливаем через requirements.txt. - try: - import psycopg2 - import psycopg2.extras - except ImportError: - return {"error": "psycopg2 not installed — add requirements.txt with psycopg2-binary"} - - dsn = os.environ.get("PG_DSN", "") - if not dsn: - return {"error": "PG_DSN env variable is not set"} - - limit = event.get("limit", 5) - - conn = psycopg2.connect(dsn) - try: - cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) - cur.execute("SELECT id, namespace, function_name, status, created_at FROM invocations ORDER BY created_at DESC LIMIT %s", (limit,)) - rows = cur.fetchall() - # RealDictCursor возвращает объекты, сериализуем вручную - result = [] - for row in rows: - r = dict(row) - # datetime → str - if r.get("created_at"): - r["created_at"] = str(r["created_at"]) - result.append(r) - return {"invocations": result, "count": len(result)} - finally: - conn.close() diff --git a/pg-query/code/requirements.txt b/pg-query/code/requirements.txt deleted file mode 100644 index 58ab769..0000000 --- a/pg-query/code/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -psycopg2-binary==2.9.9 diff --git a/pg-query/main.tf b/pg-query/main.tf deleted file mode 100644 index 55806cc..0000000 --- a/pg-query/main.tf +++ /dev/null @@ -1,21 +0,0 @@ -# 2026-03-09 -# main.tf — провайдеры для pg-query примера. -# Ресурсы — в pg-query.tf - -terraform { - required_providers { - sless = { - source = "terra.k8c.ru/naeel/sless" - version = "~> 0.1.9" - } - archive = { - source = "hashicorp/archive" - version = "~> 2.0" - } - } -} - -provider "sless" { - endpoint = "https://sless-api.kube5s.ru" - token = "dev-token-change-me" -} diff --git a/pg-query/pg-query.tf b/pg-query/pg-query.tf deleted file mode 100644 index e12048a..0000000 --- a/pg-query/pg-query.tf +++ /dev/null @@ -1,48 +0,0 @@ -# 2026-03-09 -# pg-query.tf — функция pg-query: читает записи из PostgreSQL. -# Код: code/handler.py + code/requirements.txt -# -# Использование: -# terraform apply -# curl -s -X POST $(terraform output -raw trigger_url) -d '{}' - -data "archive_file" "handler_pg_query" { - type = "zip" - source_dir = "${path.module}/code" - output_path = "${path.module}/dist/handler.zip" -} - -resource "sless_function" "pg_query" { - name = "pg-query" - runtime = "python3.11" - entrypoint = "handler.handle" - memory_mb = 128 - timeout_sec = 30 - - # DSN для подключения к postgres внутри кластера - # Хост: postgres.sless.svc.cluster.local (Service в namespace sless) - env_vars = { - PG_DSN = "postgres://sless:sless-pg-password@postgres.sless.svc.cluster.local:5432/sless?sslmode=disable" - } - - code_path = data.archive_file.handler_pg_query.output_path - code_hash = filesha256("${path.module}/code/handler.py") -} - -resource "sless_trigger" "pg_query_http" { - name = "pg-query-http" - type = "http" - function = sless_function.pg_query.name -} - -output "function_phase" { - value = sless_function.pg_query.phase -} - -output "function_image" { - value = sless_function.pg_query.image_ref -} - -output "trigger_url" { - value = sless_trigger.pg_query_http.url -}