diff --git a/README.md b/README.md index c4ff06e..5bf4e0e 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ terraform apply -auto-approve terraform output job_result # Проверить функцию: -curl -s $(terraform output -raw display_url) +curl -s https://sless-api.kube5s.ru/fn/default/simple-py-time-display ``` --- @@ -47,7 +47,7 @@ terraform init terraform apply -auto-approve terraform output job_result -curl -s $(terraform output -raw display_url) +curl -s https://sless-api.kube5s.ru/fn/default/simple-node-time-display ``` --- @@ -62,7 +62,7 @@ terraform init terraform apply -auto-approve # Проверить HTTP-функцию: -curl -s -X POST $(terraform output -raw trigger_url) \ +curl -s -X POST https://sless-api.kube5s.ru/fn/default/hello-http \ -H 'Content-Type: application/json' -d '{"name":"World"}' # Посмотреть результат джоба: @@ -99,16 +99,16 @@ terraform output notes_url # CRUD terraform output notes_list_url # список всех записей # Создать запись: -curl -s -X POST "$(terraform output -raw notes_url)/add?title=Hello&body=World" +curl -s -X POST "https://sless-api.kube5s.ru/fn/default/notes/add?title=Hello&body=World" # Список записей: -curl -s $(terraform output -raw notes_list_url) +curl -s https://sless-api.kube5s.ru/fn/default/notes-list # Обновить (id из предыдущего ответа): -curl -s -X POST "$(terraform output -raw notes_url)/update?id=1&title=Updated&body=New+body" +curl -s -X POST "https://sless-api.kube5s.ru/fn/default/notes/update?id=1&title=Updated&body=New+body" # Удалить: -curl -s -X POST "$(terraform output -raw notes_url)/delete?id=1" +curl -s -X POST "https://sless-api.kube5s.ru/fn/default/notes/delete?id=1" ``` --- diff --git a/hello-node/http.tf b/hello-node/http.tf index 76c92d1..beee105 100644 --- a/hello-node/http.tf +++ b/hello-node/http.tf @@ -1,17 +1,6 @@ -# 2026-03-08 +# 2026-03-08 / Изменено: 2026-03-09 # http.tf — HTTP-функция: принимает запросы, возвращает приветствие. # Код: code/handler-http.js -# -# Использование: -# terraform apply -# curl -s -X POST $(terraform output -raw trigger_url) \ -# -H 'Content-Type: application/json' -d '{"name":"Naeel"}' - -data "archive_file" "handler_http" { - type = "zip" - source_file = "${path.module}/code/handler-http.js" - output_path = "${path.module}/dist/handler-http.zip" -} resource "sless_function" "hello_http" { name = "hello-http" @@ -20,17 +9,13 @@ resource "sless_function" "hello_http" { memory_mb = 128 timeout_sec = 30 - code_path = data.archive_file.handler_http.output_path - # filesha256 исходного файла — надёжнее чем output_md5 zip: - # MD5 zip зависит от метаданных архива и может совпасть при разном содержимом - code_hash = filesha256("${path.module}/code/handler-http.js") + source_dir = "${path.module}/code" } resource "sless_trigger" "hello_http" { name = "hello-http-trigger" type = "http" function = sless_function.hello_http.name - # enabled = false — чтобы заморозить функцию (не удаляя ресурс), потом поставить зновь true enabled = true } diff --git a/hello-node/job.tf b/hello-node/job.tf index 0c8fc5a..843d02f 100644 --- a/hello-node/job.tf +++ b/hello-node/job.tf @@ -1,19 +1,6 @@ -# 2026-03-08 +# 2026-03-08 / Изменено: 2026-03-09 # job.tf — одноразовая функция: суммирует числа из переданного массива. # Код: code/handler-job.js -# -# Использование: -# terraform apply -# # apply блокируется до завершения джоба (~2 мин kaniko + выполнение) -# terraform output job_message -# -# Повторный запуск — изменить event_json и снова terraform apply. - -data "archive_file" "handler_job" { - type = "zip" - source_file = "${path.module}/code/handler-job.js" - output_path = "${path.module}/dist/handler-job.zip" -} resource "sless_function" "hello_job" { name = "hello-job" @@ -22,13 +9,10 @@ resource "sless_function" "hello_job" { memory_mb = 128 timeout_sec = 30 - code_path = data.archive_file.handler_job.output_path - # filesha256 исходного файла — надёжнее чем output_md5 zip - code_hash = filesha256("${path.module}/code/handler-job.js") + source_dir = "${path.module}/code" } -# Одноразовый запуск. Все поля immutable — изменение любого пересоздаёт джоб. -# run_id: 0 = не запускать, 1+ = запустить. Для повторного запуска увеличь run_id (1→2→3...). +# Одноразовый запуск. Для повторного запуска увеличь run_id (1→2→3...). resource "sless_job" "hello_run" { name = "hello-run" function = sless_function.hello_job.name diff --git a/hello-node/main.tf b/hello-node/main.tf index d5843b9..cfb00b4 100644 --- a/hello-node/main.tf +++ b/hello-node/main.tf @@ -8,11 +8,7 @@ terraform { required_providers { sless = { source = "terra.k8c.ru/naeel/sless" - version = "~> 0.1.9" - } - archive = { - source = "hashicorp/archive" - version = "~> 2.0" + version = "~> 0.1.10" } } } diff --git a/notes-python/main.tf b/notes-python/main.tf index 82d0415..505e104 100644 --- a/notes-python/main.tf +++ b/notes-python/main.tf @@ -14,12 +14,7 @@ terraform { # Провайдер для управления serverless функциями через sless API sless = { source = "terra.k8c.ru/naeel/sless" - version = "~> 0.1.9" - } - # hashicorp/archive — для упаковки исходников в zip перед загрузкой - archive = { - source = "hashicorp/archive" - version = "~> 2.0" + version = "~> 0.1.10" } } } diff --git a/notes-python/notes-list.tf b/notes-python/notes-list.tf index 1b76e13..ced284b 100644 --- a/notes-python/notes-list.tf +++ b/notes-python/notes-list.tf @@ -1,22 +1,6 @@ # 2026-03-09 -# notes-list.tf — функция для чтения всех заметок одним запросом. -# -# Отдельная от CRUD функция — «read-only» эндпоинт без роутинга. -# Принимает GET или POST, параметры игнорирует. -# Возвращает JSON-массив всех записей, сортировка: новые первые. -# -# Пример запроса: -# curl https://sless-api.kube5s.ru/fn/default/notes-list +# notes-list.tf — read-only эндпоинт: возвращает все заметки, сортировка новые первые. -# Упаковка исходников notes_list.py в zip. -data "archive_file" "notes_list_zip" { - type = "zip" - source_dir = "${path.module}/code/notes-list" - output_path = "${path.module}/dist/notes-list.zip" -} - -# Read-only функция в кластере. -# entrypoint = "notes_list.list_notes" → файл notes_list.py, функция list_notes(). resource "sless_function" "notes_list" { name = "notes-list" runtime = "python3.11" @@ -28,12 +12,9 @@ resource "sless_function" "notes_list" { PG_DSN = var.pg_dsn } - code_path = data.archive_file.notes_list_zip.output_path - code_hash = filesha256("${path.module}/code/notes-list/notes_list.py") + source_dir = "${path.module}/code/notes-list" } -# HTTP-триггер для read-only функции. -# Создаёт Ingress, URL доступен в outputs.tf. resource "sless_trigger" "notes_list_http" { name = "notes-list-http" type = "http" diff --git a/notes-python/notes.tf b/notes-python/notes.tf index d7f2b8e..7830dc3 100644 --- a/notes-python/notes.tf +++ b/notes-python/notes.tf @@ -1,24 +1,11 @@ # 2026-03-09 # notes.tf — CRUD функция для управления заметками (CREATE / UPDATE / DELETE). # -# Одна функция обрабатывает все операции — роутинг по sub-path URL. -# Sub-path и query string пробрасывает прокси (invoke.go) → runtime добавляет -# их в event как _path и _query. -# -# Маршруты (все методы принимаются, рекомендуется POST): -# /fn/default/notes/add?title=...&body=... → INSERT, возвращает запись -# /fn/default/notes/update?id=1&title=...&body=... → UPDATE, возвращает запись -# /fn/default/notes/delete?id=1 → DELETE, возвращает {deleted: id} +# Маршруты (рекомендуется POST): +# /fn/default/notes/add?title=...&body=... → INSERT +# /fn/default/notes/update?id=1&title=...&body=... → UPDATE +# /fn/default/notes/delete?id=1 → DELETE -# Упаковка исходников notes_crud.py в zip. -data "archive_file" "notes_crud_zip" { - type = "zip" - source_dir = "${path.module}/code/notes" - output_path = "${path.module}/dist/notes.zip" -} - -# CRUD функция в кластере. -# entrypoint = "notes_crud.crud" → файл notes_crud.py, функция crud(). resource "sless_function" "notes_crud" { name = "notes" runtime = "python3.11" @@ -30,13 +17,9 @@ resource "sless_function" "notes_crud" { PG_DSN = var.pg_dsn } - code_path = data.archive_file.notes_crud_zip.output_path - code_hash = filesha256("${path.module}/code/notes/notes_crud.py") + source_dir = "${path.module}/code/notes" } -# HTTP-триггер для CRUD функции. -# Создаёт Ingress в кластере, URL доступен в outputs.tf. -# Базовый URL: https://sless-api.kube5s.ru/fn/default/notes resource "sless_trigger" "notes_crud_http" { name = "notes-http" type = "http" diff --git a/notes-python/sql-runner.tf b/notes-python/sql-runner.tf index 92bc8ad..29be82d 100644 --- a/notes-python/sql-runner.tf +++ b/notes-python/sql-runner.tf @@ -5,17 +5,6 @@ # Это сделано намеренно: функция выполняет произвольный SQL, и открывать её # наружу через HTTP было бы небезопасно. -# Упаковка исходников в zip для загрузки в кластер. -# archive_file пересоздаёт zip при изменении любого файла в source_dir. -data "archive_file" "sql_runner_zip" { - type = "zip" - source_dir = "${path.module}/code/sql-runner" - output_path = "${path.module}/dist/sql-runner.zip" -} - -# Сама функция в кластере. -# entrypoint = "sql_runner.run_sql" → файл sql_runner.py, функция run_sql(). -# memory_mb=128 достаточно — DDL запросы не требуют памяти на вычисления. resource "sless_function" "sql_runner" { name = "sql-runner" runtime = "python3.11" @@ -27,7 +16,5 @@ resource "sless_function" "sql_runner" { PG_DSN = var.pg_dsn } - code_path = data.archive_file.sql_runner_zip.output_path - # code_hash — при изменении кода terraform пересобирает образ функции - code_hash = filesha256("${path.module}/code/sql-runner/sql_runner.py") + source_dir = "${path.module}/code/sql-runner" } diff --git a/simple-node/main.tf b/simple-node/main.tf index 8400e2c..9cb2843 100644 --- a/simple-node/main.tf +++ b/simple-node/main.tf @@ -19,11 +19,7 @@ terraform { required_providers { sless = { source = "terra.k8c.ru/naeel/sless" - version = "~> 0.1.9" - } - archive = { - source = "hashicorp/archive" - version = "~> 2.0" + version = "~> 0.1.10" } } } diff --git a/simple-node/time-display.tf b/simple-node/time-display.tf index 0779f74..7d29bfe 100644 --- a/simple-node/time-display.tf +++ b/simple-node/time-display.tf @@ -1,14 +1,7 @@ -# Создано: 2026-03-09 +# Создано: 2026-03-09 / Изменено: 2026-03-09 # time-display.tf — HTTP-функция, доступная по URL после apply. # Получает результат джоба (из time-getter.tf) через переменную окружения JOB_TIME. -# Упаковываем код функции в zip для загрузки -data "archive_file" "time_display_zip" { - type = "zip" - source_dir = "${path.module}/code/time_display" - output_path = "${path.module}/dist/time_display.zip" -} - # HTTP-функция — отвечает на запросы по URL из outputs.tf resource "sless_function" "time_display" { name = "simple-node-time-display" # уникальное имя в namespace @@ -22,8 +15,7 @@ resource "sless_function" "time_display" { JOB_TIME = sless_job.run_getter.message } - code_path = data.archive_file.time_display_zip.output_path - code_hash = filesha256("${path.module}/code/time_display/time_display.js") # для пересборки при изменении кода + source_dir = "${path.module}/code/time_display" depends_on = [sless_job.run_getter] # ждём завершения джоба перед деплоем функции } diff --git a/simple-node/time-getter.tf b/simple-node/time-getter.tf index b1212c2..426bbcc 100644 --- a/simple-node/time-getter.tf +++ b/simple-node/time-getter.tf @@ -1,15 +1,8 @@ -# Создано: 2026-03-09 +# Создано: 2026-03-09 / Изменено: 2026-03-09 # time-getter.tf — скрипт который запускается ОДИН РАЗ при terraform apply. # После запуска его результат доступен через: sless_job.run_getter.message # Смотри time-display.tf — там этот результат передаётся в функцию. -# Упаковываем код скрипта в zip для загрузки -data "archive_file" "time_getter_zip" { - type = "zip" - source_dir = "${path.module}/code/time_getter" - output_path = "${path.module}/dist/time_getter.zip" -} - # Функция для скрипта — без HTTP-триггера, вызывается только через джоб ниже resource "sless_function" "time_getter" { name = "simple-node-time-getter" # уникальное имя в namespace @@ -17,8 +10,7 @@ resource "sless_function" "time_getter" { entrypoint = "time_getter.getTime" # файл.функция в code/time_getter/ memory_mb = 64 - code_path = data.archive_file.time_getter_zip.output_path - code_hash = filesha256("${path.module}/code/time_getter/time_getter.js") # для пересборки при изменении кода + source_dir = "${path.module}/code/time_getter" } # Джоб — запускает функцию time_getter один раз прямо при apply. diff --git a/simple-python/main.tf b/simple-python/main.tf index 5aff48e..5032776 100644 --- a/simple-python/main.tf +++ b/simple-python/main.tf @@ -18,11 +18,7 @@ terraform { required_providers { sless = { source = "terra.k8c.ru/naeel/sless" - version = "~> 0.1.9" - } - archive = { - source = "hashicorp/archive" - version = "~> 2.0" + version = "~> 0.1.10" } } } diff --git a/simple-python/time-display.tf b/simple-python/time-display.tf index 236a0de..c5db376 100644 --- a/simple-python/time-display.tf +++ b/simple-python/time-display.tf @@ -1,14 +1,7 @@ -# Создано: 2026-03-09 +# Создано: 2026-03-09 / Изменено: 2026-03-09 # time-display.tf — HTTP-функция, доступная по URL после apply. # Получает результат джоба (из time-getter.tf) через переменную окружения JOB_TIME. -# Упаковываем код функции в zip для загрузки -data "archive_file" "time_display_zip" { - type = "zip" - source_dir = "${path.module}/code/time_display" - output_path = "${path.module}/dist/time_display.zip" -} - # HTTP-функция — отвечает на запросы по URL из outputs.tf resource "sless_function" "time_display" { name = "simple-py-time-display" # уникальное имя в namespace @@ -22,8 +15,7 @@ resource "sless_function" "time_display" { JOB_TIME = sless_job.run_getter.message } - code_path = data.archive_file.time_display_zip.output_path - code_hash = filesha256("${path.module}/code/time_display/time_display.py") # для пересборки при изменении кода + source_dir = "${path.module}/code/time_display" depends_on = [sless_job.run_getter] # ждём завершения джоба перед деплоем функции } diff --git a/simple-python/time-getter.tf b/simple-python/time-getter.tf index 3295ef0..fd57615 100644 --- a/simple-python/time-getter.tf +++ b/simple-python/time-getter.tf @@ -1,15 +1,8 @@ -# Создано: 2026-03-09 +# Создано: 2026-03-09 / Изменено: 2026-03-09 # time-getter.tf — скрипт который запускается ОДИН РАЗ при terraform apply. # После запуска его результат доступен через: sless_job.run_getter.message # Смотри time-display.tf — там этот результат передаётся в функцию. -# Упаковываем код скрипта в zip для загрузки -data "archive_file" "time_getter_zip" { - type = "zip" - source_dir = "${path.module}/code/time_getter" - output_path = "${path.module}/dist/time_getter.zip" -} - # Функция для скрипта — без HTTP-триггера, вызывается только через джоб ниже resource "sless_function" "time_getter" { name = "simple-py-time-getter" # уникальное имя в namespace @@ -17,8 +10,7 @@ resource "sless_function" "time_getter" { entrypoint = "time_getter.get_time" # файл.функция в code/time_getter/ memory_mb = 64 - code_path = data.archive_file.time_getter_zip.output_path - code_hash = filesha256("${path.module}/code/time_getter/time_getter.py") # для пересборки при изменении кода + source_dir = "${path.module}/code/time_getter" } # Джоб — запускает функцию time_getter один раз прямо при apply.