From 7e3f45176bc367fdc64f3b8f30d6d802499f1909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Mon, 9 Mar 2026 09:11:31 +0400 Subject: [PATCH] =?UTF-8?q?refactor:=20pg-query=20=E2=80=94=20=D1=80=D0=B5?= =?UTF-8?q?=D1=81=D1=83=D1=80=D1=81=D1=8B=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=B2=20pg-query.tf,=20main.tf=20=D1=82?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=B9=D0=B4=D0=B5=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pg-query/main.tf | 59 ++------------------------------------------ pg-query/pg-query.tf | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 57 deletions(-) create mode 100644 pg-query/pg-query.tf diff --git a/pg-query/main.tf b/pg-query/main.tf index 28e85a4..42f18db 100644 --- a/pg-query/main.tf +++ b/pg-query/main.tf @@ -1,18 +1,6 @@ # 2026-03-09 -# main.tf — e2e тест: создать serverless функцию, которая читает из PostgreSQL. -# -# Структура: -# code/ — исходники (handler.py, requirements.txt) -# dist/ — zip-архив (генерируется archive_file datasource) -# -# Использование: -# terraform init && terraform apply -# После apply (ждёт ~2 мин пока kaniko соберёт образ): -# curl -s -X POST -d '{}' -# -# Оператор запущен в кластере: https://sless-api.kube5s.ru -# Функция подключается к postgres.sless.svc.cluster.local:5432 изнутри кластера. -# PG_DSN задаётся как env_var ресурса sless_function. +# main.tf — провайдеры для pg-query примера. +# Ресурсы — в pg-query.tf terraform { required_providers { @@ -31,46 +19,3 @@ provider "sless" { endpoint = "https://sless-api.kube5s.ru" token = "dev-token-change-me" } - -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" { - namespace = "default" - 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" { - namespace = "default" - 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 -} diff --git a/pg-query/pg-query.tf b/pg-query/pg-query.tf new file mode 100644 index 0000000..f3b876b --- /dev/null +++ b/pg-query/pg-query.tf @@ -0,0 +1,50 @@ +# 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" { + namespace = "default" + 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" { + namespace = "default" + 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 +}