refactor: SoC — EnsureNamespace в namespace.go, маршрут /ensure, client.EnsureNamespace, fix secrets в .gitignore

- handler.go: убраны бизнес-логика и k8s-типы (corev1/k8serrors/metav1)
  handler.go теперь только инфраструктура: Handler struct + helpers
- namespace.go: новый файл — EnsureNamespace хендлер живёт здесь
  SoC: создание namespace — отдельная ответственность, не смешивается с CRUD
- router.go: добавлен маршрут POST /v1/namespaces/{namespace}/ensure
- client.go: добавлен метод EnsureNamespace(ctx, ns) → POST /ensure
- provider.go: Configure() вызывает c.EnsureNamespace(ctx, namespace) после создания Client
  Namespace создаётся ОДИН РАЗ при инициализации провайдера
  Resource-хендлеры (Function, Trigger, Job) namespace не трогают
- .gitignore: добавлена директория secrets/ (токены, ключи)
- provider v0.1.13, operator v0.1.21

Operator: naeel/sless-operator:v0.1.21
Provider: terra.k8c.ru/naeel/sless v0.1.13
This commit is contained in:
“Naeel”
2026-03-11 08:37:33 +04:00
parent 5ae2ee7f85
commit a1774e178f
11 changed files with 114 additions and 60 deletions
@@ -496,6 +496,24 @@ func (c *Client) DeleteJob(ctx context.Context, ns, name string) error {
return nil
}
// EnsureNamespace — POST /v1/namespaces/{ns}/ensure
// Создаёт k8s namespace пользователя если не существует. Идемпотентен.
// Вызывается ОДИН РАЗ из provider.Configure() до любых ресурсных операций.
// 200 OK = namespace уже был, 201 Created = создан сейчас, остальное = ошибка.
func (c *Client) EnsureNamespace(ctx context.Context, ns string) error {
url := fmt.Sprintf("%s/v1/namespaces/%s/ensure", c.endpoint, ns)
resp, err := c.doJSON(ctx, http.MethodPost, url, nil)
if err != nil {
return fmt.Errorf("ensure namespace: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("ensure namespace: status %d: %s", resp.StatusCode, body)
}
return nil
}
// WaitJobDone опрашивает job каждые 5 секунд пока phase не Succeeded или Failed.
// Блокирует terraform apply до завершения джоба.
func (c *Client) WaitJobDone(ctx context.Context, ns, name string, timeout time.Duration) (*JobResponse, error) {