feat: sless_service CRD + ServiceReconciler, RBAC fix, split postgres/functions.tf, operator v0.1.41

This commit is contained in:
Naeel
2026-03-20 13:03:12 +03:00
parent dc65f7ab8f
commit 680beb675b
19 changed files with 2448 additions and 693 deletions
@@ -547,3 +547,170 @@ func (c *Client) WaitJobDone(ctx context.Context, ns, name string, timeout time.
}
return nil, fmt.Errorf("timeout waiting for job %s/%s to complete", ns, name)
}
// --- Service CRUD (sless_service — long-running Deployment + URL) ---
// ServiceRequest — тело POST/PUT /v1/namespaces/{ns}/services[/{name}]
type ServiceRequest struct {
Name string `json:"name"`
Runtime string `json:"runtime"`
Entrypoint string `json:"entrypoint,omitempty"`
MemoryMB int32 `json:"memory_mb,omitempty"`
TimeoutSec int32 `json:"timeout_sec,omitempty"`
Env map[string]string `json:"env_vars,omitempty"`
}
// ServiceResponse — ответ GET /v1/namespaces/{ns}/services/{name}
// URL — ключевое поле: заполняется оператором после деплоя Deployment+Ingress.
type ServiceResponse struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Runtime string `json:"runtime"`
Entrypoint string `json:"entrypoint"`
MemoryMB int32 `json:"memory_mb"`
TimeoutSec int32 `json:"timeout_sec"`
Env map[string]string `json:"env_vars"`
S3Bucket string `json:"s3_bucket"`
S3Key string `json:"s3_key"`
Phase string `json:"phase"`
ImageRef string `json:"image_ref"`
URL string `json:"url"`
Message string `json:"message"`
}
// CreateService — POST /v1/namespaces/{ns}/services → 201
func (c *Client) CreateService(ctx context.Context, ns string, req ServiceRequest) (*ServiceResponse, error) {
url := fmt.Sprintf("%s/v1/namespaces/%s/services", c.endpoint, ns)
resp, err := c.doJSON(ctx, http.MethodPost, url, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("create service: status %d: %s", resp.StatusCode, body)
}
var svc ServiceResponse
return &svc, json.NewDecoder(resp.Body).Decode(&svc)
}
// GetService — GET /v1/namespaces/{ns}/services/{name} → nil если 404
func (c *Client) GetService(ctx context.Context, ns, name string) (*ServiceResponse, error) {
url := fmt.Sprintf("%s/v1/namespaces/%s/services/%s", c.endpoint, ns, name)
resp, err := c.doJSON(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return nil, nil
}
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("get service: status %d: %s", resp.StatusCode, body)
}
var svc ServiceResponse
return &svc, json.NewDecoder(resp.Body).Decode(&svc)
}
// UpdateService — PUT /v1/namespaces/{ns}/services/{name} → 200
func (c *Client) UpdateService(ctx context.Context, ns, name string, req ServiceRequest) (*ServiceResponse, error) {
url := fmt.Sprintf("%s/v1/namespaces/%s/services/%s", c.endpoint, ns, name)
resp, err := c.doJSON(ctx, http.MethodPut, url, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("update service: status %d: %s", resp.StatusCode, body)
}
var svc ServiceResponse
return &svc, json.NewDecoder(resp.Body).Decode(&svc)
}
// DeleteService — DELETE /v1/namespaces/{ns}/services/{name} → 204
func (c *Client) DeleteService(ctx context.Context, ns, name string) error {
url := fmt.Sprintf("%s/v1/namespaces/%s/services/%s", c.endpoint, ns, name)
resp, err := c.doJSON(ctx, http.MethodDelete, url, nil)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusNotFound {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("delete service: status %d: %s", resp.StatusCode, body)
}
return nil
}
// UploadServiceCode — POST /v1/namespaces/{ns}/services/{name}/upload (multipart, field=code)
// После вызова оператор начинает kaniko-сборку и затем деплоит Deployment.
func (c *Client) UploadServiceCode(ctx context.Context, ns, name, zipPath string) error {
f, err := os.Open(zipPath)
if err != nil {
return fmt.Errorf("open zip %q: %w", zipPath, err)
}
defer f.Close()
return c.UploadServiceCodeReader(ctx, ns, name, filepath.Base(zipPath), f)
}
// UploadServiceCodeReader — загружает код сервиса из произвольного io.Reader.
func (c *Client) UploadServiceCodeReader(ctx context.Context, ns, name, filename string, r io.Reader) error {
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
fw, err := mw.CreateFormFile("code", filename)
if err != nil {
return fmt.Errorf("create form file: %w", err)
}
if _, err := io.Copy(fw, r); err != nil {
return fmt.Errorf("copy zip: %w", err)
}
mw.Close()
url := fmt.Sprintf("%s/v1/namespaces/%s/services/%s/upload", c.endpoint, ns, name)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, &buf)
if err != nil {
return err
}
req.Header.Set("Content-Type", mw.FormDataContentType())
req.Header.Set("Authorization", "Bearer "+c.token)
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("upload service code: status %d: %s", resp.StatusCode, body)
}
return nil
}
// WaitServiceReady опрашивает сервис каждые 5 секунд пока phase != Ready/Failed.
// Нужен после UploadServiceCode — kaniko-сборка + деплой занимают ~1-2 минуты.
func (c *Client) WaitServiceReady(ctx context.Context, ns, name string, timeout time.Duration) (*ServiceResponse, error) {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
svc, err := c.GetService(ctx, ns, name)
if err != nil {
return nil, err
}
if svc == nil {
return nil, fmt.Errorf("service %s/%s not found while waiting", ns, name)
}
switch svc.Phase {
case "Ready":
return svc, nil
case "Failed":
return nil, fmt.Errorf("service build failed: %s", svc.Message)
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(5 * time.Second):
}
}
return nil, fmt.Errorf("timeout waiting for service %s/%s to become Ready", ns, name)
}